logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

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. #include <stdbool.h>
  21. #include "aw9523b.h"
  22. #include "wait.h"
  23. #include "i2c_master.h"
  24. #define AW9523B_P0_INPUT 0x00
  25. #define AW9523B_P1_INPUT 0x01
  26. #define AW9523B_P0_OUTPUT 0x02
  27. #define AW9523B_P1_OUTPUT 0x03
  28. #define AW9523B_P0_CONF 0x04
  29. #define AW9523B_P1_CONF 0x05
  30. #define AW9523B_P0_INT 0x06
  31. #define AW9523B_P1_INT 0x07
  32. #define AW9523B_ID 0x10
  33. #define AW9523B_CTL 0x11
  34. #define AW9523B_P0_LED 0x12
  35. #define AW9523B_P1_LED 0x13
  36. #define AW9523B_RESET 0x7F
  37. #define TIMEOUT 100
  38. #define PWM2BUF(x) ((x) - AW9523B_PWM_BASE)
  39. static uint8_t aw9523b_pwm_buf[AW9523B_PWM_SIZE];
  40. static bool aw9523b_pwm_dirty = false;
  41. void aw9523b_init(uint8_t addr)
  42. {
  43. i2c_init();
  44. // reset chip
  45. uint8_t data = 0;
  46. i2c_write_register(addr, AW9523B_RESET, &data, 1, TIMEOUT);
  47. wait_ms(1);
  48. // set max led current
  49. data = 0x03; // 37mA/4
  50. i2c_write_register(addr, AW9523B_CTL, &data, 1, TIMEOUT);
  51. // set port to led mode
  52. data = 0;
  53. i2c_write_register(addr, AW9523B_P0_LED, &data, 1, TIMEOUT);
  54. i2c_write_register(addr, AW9523B_P1_LED, &data, 1, TIMEOUT);
  55. // clear pwm buff
  56. for (uint8_t i = 0; i < 16; i++) {
  57. aw9523b_pwm_buf[i] = 0;
  58. }
  59. aw9523b_pwm_dirty = false;
  60. }
  61. void aw9523b_set_color(int index, uint8_t red, uint8_t green, uint8_t blue)
  62. {
  63. if (index >= AW9523B_RGB_NUM) return;
  64. aw9523b_led led = g_aw9523b_leds[index];
  65. aw9523b_pwm_buf[PWM2BUF(led.r)] = red;
  66. aw9523b_pwm_buf[PWM2BUF(led.g)] = green;
  67. aw9523b_pwm_buf[PWM2BUF(led.b)] = blue;
  68. aw9523b_pwm_dirty = true;
  69. }
  70. void aw9523b_set_color_all(uint8_t red, uint8_t green, uint8_t blue)
  71. {
  72. for (uint8_t i = 0; i < AW9523B_RGB_NUM; i++) {
  73. aw9523b_set_color(i, red, green, blue);
  74. }
  75. aw9523b_pwm_dirty = true;
  76. }
  77. void aw9523b_update_pwm_buffers(uint8_t addr)
  78. {
  79. if (aw9523b_pwm_dirty) {
  80. for (uint8_t i = 0; i < AW9523B_RGB_NUM; i++){
  81. aw9523b_led led = g_aw9523b_leds[i];
  82. i2c_write_register(addr, led.r, &aw9523b_pwm_buf[PWM2BUF(led.r)], 1, TIMEOUT);
  83. i2c_write_register(addr, led.g, &aw9523b_pwm_buf[PWM2BUF(led.g)], 1, TIMEOUT);
  84. i2c_write_register(addr, led.b, &aw9523b_pwm_buf[PWM2BUF(led.b)], 1, TIMEOUT);
  85. }
  86. aw9523b_pwm_dirty = false;
  87. }
  88. }