display.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #if MAX7219_ENABLED == 1
  2. // MAX 7219 -> 7seg
  3. #include "LedControl.h"
  4. // defines
  5. #define MAX7219_DIN A0
  6. #define MAX7219_CLK A2
  7. #define MAX7219_CS A1
  8. #define MAX7219_NUM_CHIPS 1
  9. #define MAX7219_CHIP_NUM 0
  10. #define MAX7219_MIN_BRIGHTNESS 0
  11. #define MAX7219_MAX_BRIGHTNESS 15
  12. #define DEFAULT_BRIGHTNESS 8
  13. // 7seg controller
  14. LedControl lc = LedControl(MAX7219_DIN,MAX7219_CLK,MAX7219_CS,MAX7219_NUM_CHIPS); // LedControl(DataIn, CLK, LOAD (CS), NumChips)
  15. #elif TM1618_ENABLED == 1
  16. #include "TM1618.h"
  17. #define TM1618_DIN A0
  18. #define TM1618_CLK A2
  19. #define TM1618_CS A1
  20. #define TM1618_NUM_DISPS 8
  21. #define TM1618_MIN_BRIGHTNESS 0
  22. #define TM1618_MAX_BRIGHTNESS 7
  23. #define DEFAULT_BRIGHTNESS 1
  24. TM1618 lc = TM1618(TM1618_DIN, TM1618_CLK, TM1618_CS, TM1618_NUM_DISPS); // TM16XX(byte dataPin, byte clockPin, byte strobePin, byte displays
  25. #endif // MAX7219_ENABLED vs TM1618_ENABLED
  26. /* specifies the value of the colon between the hours and minutes on a 7-segment display */
  27. bool bSeparatorColon;
  28. /* helper function declarations */
  29. void _sanitizeTwoDigitInput (int input, int *output);
  30. void _setDigit (int digit, byte value, boolean dp);
  31. void _setChar (int digit, char value, boolean dp);
  32. /*
  33. Setup Functions
  34. */
  35. // setup the 7-segment display driver
  36. void setupDisplay () {
  37. bSeparatorColon = true;
  38. #if MAX7219_ENABLED == 1
  39. // MAX7219 //
  40. //wake up from power-saving mode
  41. lc.shutdown(0,false);
  42. // Set the brightness to default
  43. lc.setIntensity(0, DEFAULT_BRIGHTNESS);
  44. // clear the display
  45. lc.clearDisplay(0);
  46. #elif TM1618_ENABLED == 1
  47. // TM1618 //
  48. // activate display and set brightness to default
  49. lc.setupDisplay(true, DEFAULT_BRIGHTNESS);
  50. #endif // MAX7219_ENABLED vs TM1618_ENABLED
  51. }
  52. /*
  53. Set Functions
  54. */
  55. void setDisplayBrightness(int brightness) {
  56. #if MAX7219_ENABLED == 1
  57. if (brightness >= MAX7219_MIN_BRIGHTNESS && brightness <= MAX7219_MAX_BRIGHTNESS) {
  58. lc.setIntensity(0,brightness);
  59. }
  60. #elif TM1618_ENABLED == 1
  61. if (brightness >= TM1618_MIN_BRIGHTNESS && brightness <= TM1618_MAX_BRIGHTNESS) {
  62. lc.setIntensity(brightness);
  63. }
  64. #endif // MAX7219_ENABLED vs TM1618_ENABLED
  65. }
  66. /*
  67. * Flip the value of hour-minute separator colon value on a 7-segment display
  68. * If the separator is on, turn it off,
  69. * If it is off, turn it on
  70. */
  71. void flipTimeDisplaySeparator() {
  72. bSeparatorColon = !bSeparatorColon;
  73. }
  74. void setError() {
  75. // handle the error case
  76. _setChar(3, 'o', false);
  77. _setChar(2, 'o', false);
  78. _setChar(1, 'b', false);
  79. _setChar(0, 'o', false);
  80. }
  81. /*
  82. * Display the time on the Onion 7-Segment display.
  83. * Params:
  84. * hours the hour value to be displayed - 24 hour time format (0-24)
  85. * minutes the minute value to be displayed (0-59)
  86. * b12hMode
  87. * false - display in 24hr mode
  88. * true - display in 12hr mode
  89. * bDayNightIndicator
  90. * false - disable the Onion day-night indicator
  91. * true - enable the Onion day-night indicator
  92. */
  93. void setTime(int hours, int minutes, boolean b12hMode, boolean bDayNightIndicator, boolean bDay) {
  94. int digits[4];
  95. bool bDayNight[2] = {false, false};
  96. // sort out 24hr vs 12hr format
  97. if (b12hMode) {
  98. // convert to 12hr mode
  99. if (hours > 12) {
  100. hours -= 12;
  101. }
  102. if (hours == 0) {
  103. hours += 12;
  104. }
  105. }
  106. // program the day-night indicator
  107. if (bDayNightIndicator) {
  108. if (bDay) {
  109. // day - both lights are on
  110. bDayNight[0] = true;
  111. bDayNight[1] = true;
  112. } else {
  113. bDayNight[0] = true;
  114. bDayNight[1] = false;
  115. }
  116. }
  117. // split the hours and minutes inputs into individial digits
  118. _sanitizeTwoDigitInput(minutes, &digits[0]);
  119. _sanitizeTwoDigitInput(hours, &digits[2]);
  120. // write to display
  121. _setDigit(3,digits[0], bDayNight[1]);
  122. _setDigit(2,digits[1], bDayNight[0]);
  123. _setDigit(1,digits[2], bSeparatorColon);
  124. // for 12hr time, do not show a leading zero
  125. if (!b12hMode || digits[3] > 0) {
  126. _setDigit(0,digits[3], bSeparatorColon);
  127. } else {
  128. _setChar(0, ' ', bSeparatorColon);
  129. }
  130. }
  131. // display helper Functions
  132. /* sanitize and split a two digit integer input into an array */
  133. void _sanitizeTwoDigitInput(int input, int *output) {
  134. if (input >= 10) {
  135. output[1] = (input/10) & 0x0f;
  136. output[0] = (input - (output[1]*10)) & 0x0f;
  137. } else {
  138. output[1] = 0;
  139. output[0] = (input & 0x0f);
  140. }
  141. }
  142. /* write a number to a 7segment digit */
  143. void _setDigit(int digit, byte value, boolean dp) {
  144. #if MAX7219_ENABLED == 1
  145. lc.setDigit(MAX7219_CHIP_NUM, digit, value, dp);
  146. #elif TM1618_ENABLED == 1
  147. lc.setDisplayDigit(value, digit, dp);
  148. #endif // MAX7219_ENABLED vs TM1618_ENABLED
  149. }
  150. /* write a character to a 7segment digit */
  151. void _setChar(int digit, char value, boolean dp) {
  152. #if MAX7219_ENABLED == 1
  153. lc.setChar(MAX7219_CHIP_NUM, digit, value, dp);
  154. #elif TM1618_ENABLED == 1
  155. lc.setDisplayDigit(((byte)value)-32, digit, dp, FONT_DEFAULT);
  156. #endif // MAX7219_ENABLED vs TM1618_ENABLED
  157. }