aw9523b.c (3094B)
1 /** 2 * @file aw9523b.c 3 * @brief driver implementation of aw9523b 4 * 5 * Copyright 2020 astro <yuleiz@gmail.com> 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #include <stdbool.h> 22 #include "aw9523b.h" 23 #include "wait.h" 24 #include "i2c_master.h" 25 26 #define AW9523B_P0_INPUT 0x00 27 #define AW9523B_P1_INPUT 0x01 28 #define AW9523B_P0_OUTPUT 0x02 29 #define AW9523B_P1_OUTPUT 0x03 30 #define AW9523B_P0_CONF 0x04 31 #define AW9523B_P1_CONF 0x05 32 #define AW9523B_P0_INT 0x06 33 #define AW9523B_P1_INT 0x07 34 35 #define AW9523B_ID 0x10 36 #define AW9523B_CTL 0x11 37 #define AW9523B_P0_LED 0x12 38 #define AW9523B_P1_LED 0x13 39 40 41 #define AW9523B_RESET 0x7F 42 43 #define TIMEOUT 100 44 45 #define PWM2BUF(x) ((x) - AW9523B_PWM_BASE) 46 static uint8_t aw9523b_pwm_buf[AW9523B_PWM_SIZE]; 47 static bool aw9523b_pwm_dirty = false; 48 49 void aw9523b_init(uint8_t addr) 50 { 51 i2c_init(); 52 // reset chip 53 uint8_t data = 0; 54 i2c_write_register(addr, AW9523B_RESET, &data, 1, TIMEOUT); 55 wait_ms(1); 56 // set max led current 57 data = 0x03; // 37mA/4 58 i2c_write_register(addr, AW9523B_CTL, &data, 1, TIMEOUT); 59 // set port to led mode 60 data = 0; 61 i2c_write_register(addr, AW9523B_P0_LED, &data, 1, TIMEOUT); 62 i2c_write_register(addr, AW9523B_P1_LED, &data, 1, TIMEOUT); 63 // clear pwm buff 64 for (uint8_t i = 0; i < 16; i++) { 65 aw9523b_pwm_buf[i] = 0; 66 } 67 aw9523b_pwm_dirty = false; 68 } 69 70 void aw9523b_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) 71 { 72 if (index >= AW9523B_RGB_NUM) return; 73 74 aw9523b_led led = g_aw9523b_leds[index]; 75 aw9523b_pwm_buf[PWM2BUF(led.r)] = red; 76 aw9523b_pwm_buf[PWM2BUF(led.g)] = green; 77 aw9523b_pwm_buf[PWM2BUF(led.b)] = blue; 78 aw9523b_pwm_dirty = true; 79 } 80 81 void aw9523b_set_color_all(uint8_t red, uint8_t green, uint8_t blue) 82 { 83 for (uint8_t i = 0; i < AW9523B_RGB_NUM; i++) { 84 aw9523b_set_color(i, red, green, blue); 85 } 86 aw9523b_pwm_dirty = true; 87 } 88 89 void aw9523b_update_pwm_buffers(uint8_t addr) 90 { 91 if (aw9523b_pwm_dirty) { 92 for (uint8_t i = 0; i < AW9523B_RGB_NUM; i++){ 93 aw9523b_led led = g_aw9523b_leds[i]; 94 i2c_write_register(addr, led.r, &aw9523b_pwm_buf[PWM2BUF(led.r)], 1, TIMEOUT); 95 i2c_write_register(addr, led.g, &aw9523b_pwm_buf[PWM2BUF(led.g)], 1, TIMEOUT); 96 i2c_write_register(addr, led.b, &aw9523b_pwm_buf[PWM2BUF(led.b)], 1, TIMEOUT); 97 } 98 aw9523b_pwm_dirty = false; 99 } 100 }