led_spi.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "led_spi.h"
  2. #include <HardwarePWM.h>
  3. /* Private defines */
  4. #define LED_NUM 4
  5. #define PinSet(pin) GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, ((uint16_t)1<<(pin)))
  6. #define PinRes(pin) GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, ((uint16_t)1<<(pin)))
  7. #define PIN_DOUT 14
  8. #define PIN_CLK 13
  9. #define PIN_LOAD 12
  10. #define PIN_OUTE 0
  11. /* Private typedefs */
  12. typedef enum t_led_pos {
  13. pos_led1 = 0x1,
  14. pos_led2 = 0x2,
  15. pos_led3 = 0x4,
  16. pos_led4 = 0x8
  17. } led_pos_t;
  18. typedef enum t_segment {
  19. seg_A = 0x01,
  20. seg_B = 0x02,
  21. seg_C = 0x04,
  22. seg_D = 0x08,
  23. seg_E = 0x10,
  24. seg_F = 0x20,
  25. seg_G = 0x40,
  26. seg_DP = 0x80
  27. } segment_t;
  28. /* LED Symbol map:
  29. A
  30. F B
  31. G
  32. E C
  33. D DP
  34. */
  35. typedef enum t_led_symb {
  36. sym_Blank = 0x0,
  37. sym_0 = (uint8_t)(seg_A | seg_B | seg_C | seg_D | seg_E | seg_F),
  38. sym_1 = (uint8_t)(seg_B | seg_C),
  39. sym_2 = (uint8_t)(seg_A | seg_B | seg_G | seg_E | seg_D),
  40. sym_3 = (uint8_t)(seg_A | seg_B | seg_G | seg_C | seg_D),
  41. sym_4 = (uint8_t)(seg_F | seg_G | seg_B | seg_C),
  42. sym_5 = (uint8_t)(seg_A | seg_F | seg_G | seg_E | seg_D),
  43. sym_6 = (uint8_t)(seg_A | seg_F | seg_G | seg_E | seg_D | seg_C),
  44. sym_7 = (uint8_t)(seg_A | seg_B | seg_C),
  45. sym_8 = (uint8_t)(seg_A | seg_B | seg_C | seg_D | seg_E | seg_F | seg_G),
  46. sym_9 = (uint8_t)(seg_A | seg_B | seg_C | seg_D | seg_G | seg_F),
  47. sym_DP = seg_DP
  48. } led_symb_t;
  49. /* Private variables */
  50. static const led_pos_t led_pos[LED_NUM] = {
  51. pos_led1, pos_led2, pos_led3, pos_led4
  52. };
  53. static const led_symb_t led_dig[10] = {
  54. sym_0, sym_1, sym_2, sym_3, sym_4, sym_5, sym_6, sym_7, sym_8, sym_9
  55. };
  56. static led_symb_t led_bufer[LED_NUM] = {sym_0, sym_1, sym_2, sym_3};
  57. static Timer ledTimer;
  58. static uint8_t hw_pwm_pins[1] = {PIN_OUTE};
  59. static HardwarePWM OE_pwm(hw_pwm_pins, 1);
  60. static uint32 MaxDuty;
  61. /* Private functions */
  62. void LED_writeData (led_pos_t pos, led_symb_t data) {
  63. PinRes(PIN_LOAD); // down latch
  64. // software spi
  65. uint8_t i;
  66. uint16_t sdata = (pos << 8) | data;
  67. for (i = 16; i != 0; i--) {
  68. PinRes(PIN_CLK); // prepare CLK
  69. if (sdata >= 0x8000) {
  70. // if msb bit 1
  71. PinSet(PIN_DOUT); // MOSI = 1
  72. } else {
  73. // if msb bit 0
  74. PinRes(PIN_DOUT); // MOSI = 0
  75. }
  76. PinSet(PIN_CLK); // lock CLK
  77. sdata <<= 1;
  78. }
  79. PinSet(PIN_LOAD); // up latch
  80. }
  81. void LED_ShowLed(void) {
  82. static uint8_t cnt = 0;
  83. LED_writeData(led_pos[cnt], led_bufer[cnt]);
  84. cnt ++;
  85. if (cnt >= LED_NUM) {
  86. cnt = 0;
  87. }
  88. }
  89. /* Exported functions */
  90. void LED_Init (void) {
  91. Serial.println("BIG LED SPI init!");
  92. /* in future, replace by pwm */
  93. pinMode(PIN_OUTE, OUTPUT);
  94. PinRes(PIN_OUTE); // output enable
  95. /*
  96. MaxDuty = OE_pwm.getMaxDuty();
  97. LED_SetBright(50);
  98. /*
  99. /* prepare soft spi */
  100. pinMode(PIN_DOUT, OUTPUT);
  101. pinMode(PIN_CLK, OUTPUT);
  102. pinMode(PIN_LOAD, OUTPUT);
  103. PinSet(PIN_CLK);
  104. PinSet(PIN_LOAD);
  105. /* Start timer for refresh leds */
  106. ledTimer.initializeMs(5, LED_ShowLed).start();
  107. }
  108. /**
  109. * @brief Show two Bin number to LED
  110. *
  111. * @param hbin High/Left Bin value 0..99
  112. * @param lbin Low/Right Bin Value 0..99
  113. */
  114. void LED_ShowBin(uint8_t hbin, uint8_t lbin) {
  115. uint8_t a = hbin / 10;
  116. uint8_t b = hbin % 10;
  117. uint8_t c = lbin / 10;
  118. uint8_t d = lbin % 10;
  119. if (a > 9) { a = 9; }
  120. if (b > 9) { b = 9; }
  121. if (c > 9) { c = 9; }
  122. if (d > 9) { d = 9; }
  123. led_bufer[0] = led_dig[a];
  124. led_bufer[1] = led_dig[b];
  125. led_bufer[2] = led_dig[c];
  126. led_bufer[3] = led_dig[d];
  127. }
  128. /**
  129. * @brief Set value 0..9 in single led
  130. *
  131. * @param pos Led num 0..3
  132. * @param bin Bin value 0..9
  133. */
  134. void LED_ShowBinPos(uint8_t pos, uint8_t bin) {
  135. if (pos >= LED_NUM) { return; }
  136. if (bin > 9) { bin = 9; }
  137. led_bufer[pos] = led_dig[bin];
  138. }
  139. /**
  140. * @brief On semicolon
  141. */
  142. void LED_SemicolonOn(void) {
  143. led_bufer[1] = led_symb_t((uint8_t)led_bufer[1] | (uint8_t)sym_DP);
  144. led_bufer[2] = led_symb_t((uint8_t)led_bufer[2] | (uint8_t)sym_DP);
  145. }
  146. /**
  147. * @brief Off semicolon
  148. */
  149. void LED_SemicolonOFF(void) {
  150. led_bufer[1] = led_symb_t((uint8_t)led_bufer[1] & ~((uint8_t)sym_DP));
  151. led_bufer[2] = led_symb_t((uint8_t)led_bufer[2] & ~((uint8_t)sym_DP));
  152. }
  153. /**
  154. * @brief Set LED bright.
  155. *
  156. * @param bright Percent value 0..100
  157. */
  158. void LED_SetBright(uint8_t bright) {
  159. if (bright > 100) { bright = 100;}
  160. bright = 100 - bright;
  161. uint32 new_duty = MaxDuty * bright / 100;
  162. OE_pwm.setDutyChan(0, bright);
  163. }