led_spi.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_CLK 14
  8. #define PIN_DOUT 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. asm("nop;");
  77. PinSet(PIN_CLK); // lock CLK
  78. sdata <<= 1;
  79. }
  80. asm("nop;");
  81. PinSet(PIN_LOAD); // up latch
  82. asm("nop;");
  83. PinRes(PIN_CLK);
  84. PinRes(PIN_LOAD);
  85. }
  86. void LED_ShowLed(void) {
  87. static uint8_t cnt = 0;
  88. LED_writeData(led_pos[cnt], led_bufer[cnt]);
  89. cnt ++;
  90. if (cnt >= LED_NUM) {
  91. cnt = 0;
  92. }
  93. }
  94. /* Exported functions */
  95. void LED_Init (void) {
  96. Serial.println("BIG LED SPI init!");
  97. /* in future, replace by pwm */
  98. pinMode(PIN_OUTE, OUTPUT);
  99. PinRes(PIN_OUTE); // output enable
  100. /*
  101. MaxDuty = OE_pwm.getMaxDuty();
  102. LED_SetBright(50);
  103. /*
  104. /* prepare soft spi */
  105. pinMode(PIN_DOUT, OUTPUT);
  106. pinMode(PIN_CLK, OUTPUT);
  107. pinMode(PIN_LOAD, OUTPUT);
  108. PinRes(PIN_CLK);
  109. PinRes(PIN_LOAD);
  110. /* Start timer for refresh leds */
  111. ledTimer.initializeMs(5, LED_ShowLed).start();
  112. }
  113. /**
  114. * @brief Show two Bin number to LED
  115. *
  116. * @param hbin High/Left Bin value 0..99
  117. * @param lbin Low/Right Bin Value 0..99
  118. */
  119. void LED_ShowBin(uint8_t hbin, uint8_t lbin) {
  120. uint8_t a = hbin / 10;
  121. uint8_t b = hbin % 10;
  122. uint8_t c = lbin / 10;
  123. uint8_t d = lbin % 10;
  124. if (a > 9) { a = 9; }
  125. if (b > 9) { b = 9; }
  126. if (c > 9) { c = 9; }
  127. if (d > 9) { d = 9; }
  128. led_bufer[0] = led_dig[a];
  129. led_bufer[1] = led_dig[b];
  130. led_bufer[2] = led_dig[c];
  131. led_bufer[3] = led_dig[d];
  132. }
  133. /**
  134. * @brief Set value 0..9 in single led
  135. *
  136. * @param pos Led num 0..3
  137. * @param bin Bin value 0..9
  138. */
  139. void LED_ShowBinPos(uint8_t pos, uint8_t bin) {
  140. if (pos >= LED_NUM) { return; }
  141. if (bin > 9) { bin = 9; }
  142. led_bufer[pos] = led_dig[bin];
  143. }
  144. /**
  145. * @brief On semicolon
  146. */
  147. void LED_SemicolonOn(void) {
  148. led_bufer[1] = led_symb_t((uint8_t)led_bufer[1] | (uint8_t)sym_DP);
  149. led_bufer[2] = led_symb_t((uint8_t)led_bufer[2] | (uint8_t)sym_DP);
  150. }
  151. /**
  152. * @brief Off semicolon
  153. */
  154. void LED_SemicolonOFF(void) {
  155. led_bufer[1] = led_symb_t((uint8_t)led_bufer[1] & ~((uint8_t)sym_DP));
  156. led_bufer[2] = led_symb_t((uint8_t)led_bufer[2] & ~((uint8_t)sym_DP));
  157. }
  158. /**
  159. * @brief Set LED bright.
  160. *
  161. * @param bright Percent value 0..100
  162. */
  163. void LED_SetBright(uint8_t bright) {
  164. if (bright > 100) { bright = 100;}
  165. bright = 100 - bright;
  166. uint32 new_duty = MaxDuty * bright / 100;
  167. OE_pwm.setDutyChan(0, bright);
  168. }