is31fl3731.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2018 Jack Humbert
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "is31fl3731.h"
  18. #include <avr/interrupt.h>
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include <string.h>
  22. #include "i2c_master.h"
  23. #include "progmem.h"
  24. // This is a 7-bit address, that gets left-shifted and bit 0
  25. // set to 0 for write, 1 for read (as per I2C protocol)
  26. // The address will vary depending on your wiring:
  27. // 0b1110100 AD <-> GND
  28. // 0b1110111 AD <-> VCC
  29. // 0b1110101 AD <-> SCL
  30. // 0b1110110 AD <-> SDA
  31. #define ISSI_ADDR_DEFAULT 0x74
  32. #define ISSI_REG_CONFIG 0x00
  33. #define ISSI_REG_CONFIG_PICTUREMODE 0x00
  34. #define ISSI_REG_CONFIG_AUTOPLAYMODE 0x08
  35. #define ISSI_REG_CONFIG_AUDIOPLAYMODE 0x18
  36. #define ISSI_CONF_PICTUREMODE 0x00
  37. #define ISSI_CONF_AUTOFRAMEMODE 0x04
  38. #define ISSI_CONF_AUDIOMODE 0x08
  39. #define ISSI_REG_PICTUREFRAME 0x01
  40. #define ISSI_REG_SHUTDOWN 0x0A
  41. #define ISSI_REG_AUDIOSYNC 0x06
  42. #define ISSI_COMMANDREGISTER 0xFD
  43. #define ISSI_BANK_FUNCTIONREG 0x0B // helpfully called 'page nine'
  44. // Transfer buffer for TWITransmitData()
  45. uint8_t g_twi_transfer_buffer[20];
  46. // These buffers match the IS31FL3731 PWM registers 0x24-0xB3.
  47. // Storing them like this is optimal for I2C transfers to the registers.
  48. // We could optimize this and take out the unused registers from these
  49. // buffers and the transfers in IS31FL3731_write_pwm_buffer() but it's
  50. // probably not worth the extra complexity.
  51. uint8_t g_pwm_buffer[DRIVER_COUNT][144];
  52. bool g_pwm_buffer_update_required = false;
  53. uint8_t g_led_control_registers[DRIVER_COUNT][18] = { { 0 }, { 0 } };
  54. bool g_led_control_registers_update_required = false;
  55. // This is the bit pattern in the LED control registers
  56. // (for matrix A, add one to register for matrix B)
  57. //
  58. // reg - b7 b6 b5 b4 b3 b2 b1 b0
  59. // 0x00 - R08,R07,R06,R05,R04,R03,R02,R01
  60. // 0x02 - G08,G07,G06,G05,G04,G03,G02,R00
  61. // 0x04 - B08,B07,B06,B05,B04,B03,G01,G00
  62. // 0x06 - - , - , - , - , - ,B02,B01,B00
  63. // 0x08 - - , - , - , - , - , - , - , -
  64. // 0x0A - B17,B16,B15, - , - , - , - , -
  65. // 0x0C - G17,G16,B14,B13,B12,B11,B10,B09
  66. // 0x0E - R17,G15,G14,G13,G12,G11,G10,G09
  67. // 0x10 - R16,R15,R14,R13,R12,R11,R10,R09
  68. uint8_t IS31FL3731_write_register( uint8_t addr, uint8_t reg, uint8_t data )
  69. {
  70. g_twi_transfer_buffer[0] = reg;
  71. g_twi_transfer_buffer[1] = data;
  72. //Transmit data until succesful
  73. //while(i2c_transmit(addr << 1, g_twi_transfer_buffer,2) != 0);
  74. return i2c_transmit(addr << 1, g_twi_transfer_buffer,2);
  75. }
  76. uint8_t IS31FL3731_write_pwm_buffer( uint8_t addr, uint8_t *pwm_buffer )
  77. {
  78. uint8_t ret = 0;
  79. // assumes bank is already selected
  80. // transmit PWM registers in 9 transfers of 16 bytes
  81. // g_twi_transfer_buffer[] is 20 bytes
  82. // iterate over the pwm_buffer contents at 16 byte intervals
  83. for ( int i = 0; i < 144; i += 16 )
  84. {
  85. // set the first register, e.g. 0x24, 0x34, 0x44, etc.
  86. g_twi_transfer_buffer[0] = 0x24 + i;
  87. // copy the data from i to i+15
  88. // device will auto-increment register for data after the first byte
  89. // thus this sets registers 0x24-0x33, 0x34-0x43, etc. in one transfer
  90. for ( int j = 0; j < 16; j++ )
  91. {
  92. g_twi_transfer_buffer[1 + j] = pwm_buffer[i + j];
  93. }
  94. //Transmit buffer until succesful
  95. //while(i2c_transmit(addr << 1, g_twi_transfer_buffer,17) != 0);
  96. ret |= i2c_transmit(addr << 1, g_twi_transfer_buffer, 17);
  97. }
  98. return ret;
  99. }
  100. uint8_t IS31FL3731_init( uint8_t addr )
  101. {
  102. uint8_t ret = 0;
  103. // In order to avoid the LEDs being driven with garbage data
  104. // in the LED driver's PWM registers, first enable software shutdown,
  105. // then set up the mode and other settings, clear the PWM registers,
  106. // then disable software shutdown.
  107. // select "function register" bank
  108. ret |= IS31FL3731_write_register( addr, ISSI_COMMANDREGISTER, ISSI_BANK_FUNCTIONREG );
  109. // enable software shutdown
  110. ret |= IS31FL3731_write_register( addr, ISSI_REG_SHUTDOWN, 0x00 );
  111. // this delay was copied from other drivers, might not be needed
  112. _delay_ms( 10 );
  113. // picture mode
  114. ret |= IS31FL3731_write_register( addr, ISSI_REG_CONFIG, ISSI_REG_CONFIG_PICTUREMODE );
  115. // display frame 0
  116. ret |= IS31FL3731_write_register( addr, ISSI_REG_PICTUREFRAME, 0x00 );
  117. // audio sync off
  118. ret |= IS31FL3731_write_register( addr, ISSI_REG_AUDIOSYNC, 0x00 );
  119. // select bank 0
  120. ret |= IS31FL3731_write_register( addr, ISSI_COMMANDREGISTER, 0 );
  121. // turn off all LEDs in the LED control register
  122. for ( int i = 0x00; i <= 0x11; i++ )
  123. {
  124. ret |= IS31FL3731_write_register( addr, i, 0x00 );
  125. }
  126. // turn off all LEDs in the blink control register (not really needed)
  127. for ( int i = 0x12; i <= 0x23; i++ )
  128. {
  129. ret |= IS31FL3731_write_register( addr, i, 0x00 );
  130. }
  131. // set PWM on all LEDs to 0
  132. for ( int i = 0x24; i <= 0xB3; i++ )
  133. {
  134. ret |= IS31FL3731_write_register( addr, i, 0x00 );
  135. }
  136. // select "function register" bank
  137. ret |= IS31FL3731_write_register( addr, ISSI_COMMANDREGISTER, ISSI_BANK_FUNCTIONREG );
  138. // disable software shutdown
  139. ret |= IS31FL3731_write_register( addr, ISSI_REG_SHUTDOWN, 0x01 );
  140. // select bank 0 and leave it selected.
  141. // most usage after initialization is just writing PWM buffers in bank 0
  142. // as there's not much point in double-buffering
  143. ret |= IS31FL3731_write_register( addr, ISSI_COMMANDREGISTER, 0 );
  144. return ret;
  145. }
  146. void IS31FL3731_set_color( int index, uint8_t red, uint8_t green, uint8_t blue )
  147. {
  148. if ( index >= 0 && index < DRIVER_LED_TOTAL ) {
  149. is31_led led = g_is31_leds[index];
  150. // Subtract 0x24 to get the second index of g_pwm_buffer
  151. g_pwm_buffer[led.driver][led.r - 0x24] = red;
  152. g_pwm_buffer[led.driver][led.g - 0x24] = green;
  153. g_pwm_buffer[led.driver][led.b - 0x24] = blue;
  154. g_pwm_buffer_update_required = true;
  155. }
  156. }
  157. void IS31FL3731_set_color_all( uint8_t red, uint8_t green, uint8_t blue )
  158. {
  159. for ( int i = 0; i < DRIVER_LED_TOTAL; i++ )
  160. {
  161. IS31FL3731_set_color( i, red, green, blue );
  162. }
  163. }
  164. void IS31FL3731_set_led_control_register( uint8_t index, bool red, bool green, bool blue )
  165. {
  166. is31_led led = g_is31_leds[index];
  167. uint8_t control_register_r = (led.r - 0x24) / 8;
  168. uint8_t control_register_g = (led.g - 0x24) / 8;
  169. uint8_t control_register_b = (led.b - 0x24) / 8;
  170. uint8_t bit_r = (led.r - 0x24) % 8;
  171. uint8_t bit_g = (led.g - 0x24) % 8;
  172. uint8_t bit_b = (led.b - 0x24) % 8;
  173. if ( red ) {
  174. g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r);
  175. } else {
  176. g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r);
  177. }
  178. if ( green ) {
  179. g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g);
  180. } else {
  181. g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g);
  182. }
  183. if ( blue ) {
  184. g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b);
  185. } else {
  186. g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b);
  187. }
  188. g_led_control_registers_update_required = true;
  189. }
  190. uint8_t IS31FL3731_update_pwm_buffers( uint8_t addr1, uint8_t addr2 )
  191. {
  192. uint8_t ret = 0;
  193. if ( g_pwm_buffer_update_required )
  194. {
  195. ret |= IS31FL3731_write_pwm_buffer( addr1, g_pwm_buffer[0] );
  196. ret |= IS31FL3731_write_pwm_buffer( addr2, g_pwm_buffer[1] );
  197. }
  198. g_pwm_buffer_update_required = false;
  199. return ret;
  200. }
  201. uint8_t IS31FL3731_update_led_control_registers( uint8_t addr1, uint8_t addr2 )
  202. {
  203. uint8_t ret = 0;
  204. if ( g_led_control_registers_update_required )
  205. {
  206. for ( int i=0; i<18; i++ )
  207. {
  208. ret |= IS31FL3731_write_register(addr1, i, g_led_control_registers[0][i] );
  209. ret |= IS31FL3731_write_register(addr2, i, g_led_control_registers[1][i] );
  210. }
  211. }
  212. return ret;
  213. }