lp5562.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "lp5562.h"
  2. #include "lp5562_reg.h"
  3. #include <api-hal-i2c.h>
  4. #include <stdio.h>
  5. bool lp5562_write_reg(uint8_t address, uint8_t* data) {
  6. uint8_t buffer[2] = {address, *data};
  7. bool ret;
  8. with_api_hal_i2c(
  9. bool, &ret, () {
  10. return api_hal_i2c_tx(POWER_I2C, LP5562_ADDRESS, buffer, 2, LP5562_I2C_TIMEOUT);
  11. });
  12. return ret;
  13. }
  14. void lp5562_reset() {
  15. Reg0D_Reset reg = {.value = 0xFF};
  16. lp5562_write_reg(0x0D, (uint8_t*)&reg);
  17. }
  18. void lp5562_configure() {
  19. Reg08_Config config = {.INT_CLK_EN = true, .PS_EN = true, .PWM_HF = true};
  20. lp5562_write_reg(0x08, (uint8_t*)&config);
  21. Reg70_LedMap map = {
  22. .red = EngSelectI2C,
  23. .green = EngSelectI2C,
  24. .blue = EngSelectI2C,
  25. .white = EngSelectI2C,
  26. };
  27. lp5562_write_reg(0x70, (uint8_t*)&map);
  28. }
  29. void lp5562_enable() {
  30. Reg00_Enable reg = {.CHIP_EN = true, .LOG_EN = true};
  31. lp5562_write_reg(0x00, (uint8_t*)&reg);
  32. }
  33. void lp5562_set_channel_current(LP5562Channel channel, uint8_t value) {
  34. uint8_t reg_no;
  35. if(channel == LP5562ChannelRed) {
  36. reg_no = 0x07;
  37. } else if(channel == LP5562ChannelGreen) {
  38. reg_no = 0x06;
  39. } else if(channel == LP5562ChannelBlue) {
  40. reg_no = 0x05;
  41. } else if(channel == LP5562ChannelWhite) {
  42. reg_no = 0x0F;
  43. } else {
  44. return;
  45. }
  46. lp5562_write_reg(reg_no, &value);
  47. }
  48. void lp5562_set_channel_value(LP5562Channel channel, uint8_t value) {
  49. uint8_t reg_no;
  50. if(channel == LP5562ChannelRed) {
  51. reg_no = 0x04;
  52. } else if(channel == LP5562ChannelGreen) {
  53. reg_no = 0x03;
  54. } else if(channel == LP5562ChannelBlue) {
  55. reg_no = 0x02;
  56. } else if(channel == LP5562ChannelWhite) {
  57. reg_no = 0x0E;
  58. } else {
  59. return;
  60. }
  61. lp5562_write_reg(reg_no, &value);
  62. }