SELKIELogger  1.0.0
I2C-SN3218.c
1 /*
2  * Copyright (C) 2023 Swansea University
3  *
4  * This file is part of the SELKIELogger suite of tools.
5  *
6  * SELKIELogger is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * SELKIELogger is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this SELKIELogger product.
18  * If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "I2C-SN3218.h"
22 
23 #include <i2c/smbus.h>
24 #include <linux/i2c-dev.h>
25 #include <sys/ioctl.h>
26 
32 bool i2c_sn3218_reset(const int busHandle) {
33  if (ioctl(busHandle, I2C_SLAVE, SN3218_ADDR_DEFAULT) < 0) { return false; }
34 
35  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_RESET, 0xFF) < 0) { return false; }
36 
37  return true;
38 }
39 
50 bool i2c_sn3218_update(const int busHandle, const i2c_sn3218_state *state) {
51  if (ioctl(busHandle, I2C_SLAVE, SN3218_ADDR_DEFAULT) < 0) { return false; }
52 
53  if (!state->global_enable) {
54  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_ENABLE, 0x00) < 0) { return false; }
55  return true;
56  }
57 
58  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_ENABLE, 0xFF) < 0) { return false; }
59 
60  uint8_t ledcontrol1 = 0;
61  uint8_t ledcontrol2 = 0;
62  uint8_t ledcontrol3 = 0;
63 
64  for (int i = 0; i < 6; ++i) {
65  if (state->led[0 + i] > 0) { ledcontrol1 |= 1 << i; }
66  }
67 
68  for (int i = 0; i < 6; ++i) {
69  if (state->led[6 + i] > 0) { ledcontrol2 |= 1 << i; }
70  }
71 
72  for (int i = 0; i < 6; ++i) {
73  if (state->led[12 + i] > 0) { ledcontrol3 |= 1 << i; }
74  }
75 
76  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_LED_01, ledcontrol1) < 0) { return false; }
77 
78  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_LED_02, ledcontrol2) < 0) { return false; }
79 
80  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_LED_03, ledcontrol3) < 0) { return false; }
81 
82  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_01, state->led[0]) < 0) { return false; }
83 
84  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_02, state->led[1]) < 0) { return false; }
85 
86  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_03, state->led[2]) < 0) { return false; }
87 
88  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_04, state->led[3]) < 0) { return false; }
89 
90  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_05, state->led[4]) < 0) { return false; }
91 
92  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_06, state->led[5]) < 0) { return false; }
93 
94  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_07, state->led[6]) < 0) { return false; }
95 
96  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_08, state->led[7]) < 0) { return false; }
97 
98  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_09, state->led[8]) < 0) { return false; }
99 
100  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_10, state->led[9]) < 0) { return false; }
101 
102  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_11, state->led[10]) < 0) { return false; }
103 
104  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_12, state->led[11]) < 0) { return false; }
105 
106  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_13, state->led[12]) < 0) { return false; }
107 
108  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_14, state->led[13]) < 0) { return false; }
109 
110  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_15, state->led[14]) < 0) { return false; }
111 
112  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_16, state->led[15]) < 0) { return false; }
113 
114  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_17, state->led[16]) < 0) { return false; }
115 
116  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_PWM_18, state->led[17]) < 0) { return false; }
117 
118  if (i2c_smbus_write_byte_data(busHandle, SN3218_REG_UPDATE, 0xFF) < 0) { return false; }
119 
120  return true;
121 }
#define SN3218_ADDR_DEFAULT
SN3218 I2C Address.
Definition: I2C-SN3218.h:33
bool i2c_sn3218_reset(const int busHandle)
Reset device to defaults.
Definition: I2C-SN3218.c:32
bool i2c_sn3218_update(const int busHandle, const i2c_sn3218_state *state)
Configure SN3218 chip to match state object.
Definition: I2C-SN3218.c:50
#define SN3218_REG_LED_03
LEDs 13-18 control register.
Definition: I2C-SN3218.h:129
#define SN3218_REG_LED_01
LEDs 1 - 6 control register.
Definition: I2C-SN3218.h:123
#define SN3218_REG_LED_02
LEDs 7-12 control register.
Definition: I2C-SN3218.h:126
#define SN3218_REG_PWM_16
LED 16 PWM Intensity (0-255)
Definition: I2C-SN3218.h:102
#define SN3218_REG_PWM_15
LED 15 PWM Intensity (0-255)
Definition: I2C-SN3218.h:99
#define SN3218_REG_PWM_06
LED 6 PWM Intensity (0-255)
Definition: I2C-SN3218.h:72
#define SN3218_REG_PWM_10
LED 10 PWM Intensity (0-255)
Definition: I2C-SN3218.h:84
#define SN3218_REG_PWM_13
LED 13 PWM Intensity (0-255)
Definition: I2C-SN3218.h:93
#define SN3218_REG_PWM_07
LED 7 PWM Intensity (0-255)
Definition: I2C-SN3218.h:75
#define SN3218_REG_PWM_05
LED 5 PWM Intensity (0-255)
Definition: I2C-SN3218.h:69
#define SN3218_REG_PWM_09
LED 9 PWM Intensity (0-255)
Definition: I2C-SN3218.h:81
#define SN3218_REG_PWM_17
LED 17 PWM Intensity (0-255)
Definition: I2C-SN3218.h:105
#define SN3218_REG_PWM_12
LED 12 PWM Intensity (0-255)
Definition: I2C-SN3218.h:90
#define SN3218_REG_PWM_18
LED 18 PWM Intensity (0-255)
Definition: I2C-SN3218.h:108
#define SN3218_REG_PWM_11
LED 11 PWM Intensity (0-255)
Definition: I2C-SN3218.h:87
#define SN3218_REG_PWM_01
LED 1 PWM Intensity (0-255)
Definition: I2C-SN3218.h:57
#define SN3218_REG_PWM_02
LED 2 PWM Intensity (0-255)
Definition: I2C-SN3218.h:60
#define SN3218_REG_PWM_14
LED 14 PWM Intensity (0-255)
Definition: I2C-SN3218.h:96
#define SN3218_REG_PWM_04
LED 4 PWM Intensity (0-255)
Definition: I2C-SN3218.h:66
#define SN3218_REG_PWM_08
LED 8 PWM Intensity (0-255)
Definition: I2C-SN3218.h:78
#define SN3218_REG_PWM_03
LED 3 PWM Intensity (0-255)
Definition: I2C-SN3218.h:63
#define SN3218_REG_RESET
SN3218 Reset register - set high to reset all control registers.
Definition: I2C-SN3218.h:136
#define SN3218_REG_UPDATE
SN3218 Update register - set high to apply changes.
Definition: I2C-SN3218.h:133
#define SN3218_REG_ENABLE
Global device enable register.
Definition: I2C-SN3218.h:43
SN3218 State representation.
Definition: I2C-SN3218.h:152
uint8_t led[18]
Individual LED brightnesses.
Definition: I2C-SN3218.h:153
bool global_enable
Global on/off switch.
Definition: I2C-SN3218.h:154