Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.7 KiB

  1. /*
  2. MY9291 LED Driver Arduino library 0.1.0
  3. Copyright (c) 2016 - 2026 MaiKe Labs
  4. Copyright (C) 2017 - Xose Pérez
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef _my9291_h
  17. #define _my9291_h
  18. #include <Arduino.h>
  19. typedef enum my9291_cmd_one_shot_t {
  20. MY9291_CMD_ONE_SHOT_DISABLE = 0X00,
  21. MY9291_CMD_ONE_SHOT_ENFORCE = 0X01,
  22. } my9291_cmd_one_shot_t;
  23. typedef enum my9291_cmd_reaction_t {
  24. MY9291_CMD_REACTION_FAST = 0X00,
  25. MY9291_CMD_REACTION_SLOW = 0X01,
  26. } my9291_cmd_reaction_t;
  27. typedef enum my9291_cmd_bit_width_t {
  28. MY9291_CMD_BIT_WIDTH_16 = 0X00,
  29. MY9291_CMD_BIT_WIDTH_14 = 0X01,
  30. MY9291_CMD_BIT_WIDTH_12 = 0X02,
  31. MY9291_CMD_BIT_WIDTH_8 = 0X03,
  32. } my9291_cmd_bit_width_t;
  33. typedef enum my9291_cmd_frequency_t {
  34. MY9291_CMD_FREQUENCY_DIVIDE_1 = 0X00,
  35. MY9291_CMD_FREQUENCY_DIVIDE_4 = 0X01,
  36. MY9291_CMD_FREQUENCY_DIVIDE_16 = 0X02,
  37. MY9291_CMD_FREQUENCY_DIVIDE_64 = 0X03,
  38. } my9291_cmd_frequency_t;
  39. typedef enum my9291_cmd_scatter_t {
  40. MY9291_CMD_SCATTER_APDM = 0X00,
  41. MY9291_CMD_SCATTER_PWM = 0X01,
  42. } my9291_cmd_scatter_t;
  43. typedef struct {
  44. my9291_cmd_scatter_t scatter:1;
  45. my9291_cmd_frequency_t frequency:2;
  46. my9291_cmd_bit_width_t bit_width:2;
  47. my9291_cmd_reaction_t reaction:1;
  48. my9291_cmd_one_shot_t one_shot:1;
  49. unsigned char resv:1;
  50. } __attribute__ ((aligned(1), packed)) my9291_cmd_t;
  51. #define MY9291_COMMAND_DEFAULT { \
  52. .scatter = MY9291_CMD_SCATTER_APDM, \
  53. .frequency = MY9291_CMD_FREQUENCY_DIVIDE_1, \
  54. .bit_width = MY9291_CMD_BIT_WIDTH_8, \
  55. .reaction = MY9291_CMD_REACTION_FAST, \
  56. .one_shot = MY9291_CMD_ONE_SHOT_DISABLE, \
  57. .resv = 0 \
  58. }
  59. class my9291 {
  60. public:
  61. my9291(unsigned char di, unsigned char dcki, my9291_cmd_t command);
  62. void send(unsigned int duty_r, unsigned int duty_g, unsigned int duty_b, unsigned int duty_w);
  63. private:
  64. void _di_pulse(unsigned int times);
  65. void _dcki_pulse(unsigned int times);
  66. void _set_cmd(my9291_cmd_t command);
  67. my9291_cmd_t _command;
  68. unsigned char _pin_di;
  69. unsigned char _pin_dcki;
  70. };
  71. #endif