SparkFun_Alphanumeric_Display.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /******************************************************************************
  2. SparkFun_Alphanumeric_Display.h
  3. SparkFun Alphanumeric Display Library Header File
  4. Priyanka Makin @ SparkFun Electronics
  5. Original Creation Date: July 25, 2019
  6. https://github.com/sparkfun/SparkFun_Alphanumeric_Display_Arduino_Library
  7. Updated April 30, 2020 by Gaston Williams to add defineChar function
  8. Pickup a board here: https://sparkle.sparkfun.com/sparkle/storefront_products/16391
  9. This file prototypes the HT16K33 class, implemented in SparkFun_Alphanumeric_Display.cpp.
  10. Development environment specifics:
  11. IDE: Arduino 1.8.9
  12. Hardware Platform: Arduino Uno
  13. Alphanumeric Display Breakout Version: 1.0.0
  14. This code is beerware; if you see me (or any other SparkFun employee) at the
  15. local, and you've found our code helpful, please buy us a round!
  16. Distributed as-is; no warranty is given.
  17. ******************************************************************************/
  18. #ifndef __SparkFun_Alphanumeric_Display_H__
  19. #define __SparkFun_Alphanumeric_Display_H__
  20. #include <SmingCore.h>
  21. #include <Wire.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #define DEFAULT_ADDRESS 0x70 // Default I2C address when A0, A1 are floating
  25. #define DEFAULT_NOTHING_ATTACHED 0xFF
  26. // Define constants for segment bits
  27. #define SEG_A 0x0001
  28. #define SEG_B 0x0002
  29. #define SEG_C 0x0004
  30. #define SEG_D 0x0008
  31. #define SEG_E 0x0010
  32. #define SEG_F 0x0020
  33. #define SEG_G 0x0040
  34. #define SEG_H 0x0080
  35. #define SEG_I 0x0100
  36. #define SEG_J 0x0200
  37. #define SEG_K 0x0400
  38. #define SEG_L 0x0800
  39. #define SEG_M 0x1000
  40. #define SEG_N 0x2000
  41. typedef enum
  42. {
  43. ALPHA_BLINK_RATE_NOBLINK = 0b00,
  44. ALPHA_BLINK_RATE_2HZ = 0b01,
  45. ALPHA_BLINK_RATE_1HZ = 0b10,
  46. ALPHA_BLINK_RATE_0_5HZ = 0b11,
  47. } alpha_blink_rate_t;
  48. typedef enum
  49. {
  50. ALPHA_DISPLAY_ON = 0b1,
  51. ALPHA_DISPLAY_OFF = 0b0,
  52. } alpha_display_t;
  53. typedef enum
  54. {
  55. ALPHA_DECIMAL_ON = 0b1,
  56. ALPHA_DECIMAL_OFF = 0b0,
  57. } alpha_decimal_t;
  58. typedef enum
  59. {
  60. ALPHA_COLON_ON = 0b1,
  61. ALPHA_COLON_OFF = 0b0,
  62. } alpha_colon_t;
  63. typedef enum
  64. {
  65. ALPHA_CMD_SYSTEM_SETUP = 0b00100000,
  66. ALPHA_CMD_DISPLAY_SETUP = 0b10000000,
  67. ALPHA_CMD_DIMMING_SETUP = 0b11100000,
  68. } alpha_command_t;
  69. // Structure for defining new character displays
  70. struct CharDef {
  71. uint8_t position;
  72. int16_t segments;
  73. struct CharDef * next;
  74. };
  75. // class HT16K33
  76. class HT16K33 : public Print
  77. {
  78. private:
  79. TwoWire *_i2cPort; // The generic connection to user's chosen I2C hardware
  80. uint8_t _deviceAddressDisplayOne; // Address of primary alphanumeric display
  81. uint8_t _deviceAddressDisplayTwo;
  82. uint8_t _deviceAddressDisplayThree;
  83. uint8_t _deviceAddressDisplayFour;
  84. uint8_t digitPosition = 0;
  85. uint8_t numberOfDisplays = 1;
  86. bool displayOnOff = 0; // Tracks display on/off bit of display setup register
  87. bool decimalOnOff = 0;
  88. bool colonOnOff = 0;
  89. uint8_t blinkRate = ALPHA_BLINK_RATE_NOBLINK; // Tracks blink bits in display setup register
  90. // Enough RAM for up to 4 displays on same I2C bus
  91. uint8_t displayRAM[16 * 4];
  92. char displayContent[4 * 4 + 1] = "";
  93. // Linked List of character definitions
  94. struct CharDef * pCharDefList = NULL;
  95. public:
  96. // Device status
  97. bool begin(uint8_t addressDisplayOne = DEFAULT_ADDRESS,
  98. uint8_t addressDisplayTwo = DEFAULT_NOTHING_ATTACHED,
  99. uint8_t addressDisplayThree = DEFAULT_NOTHING_ATTACHED,
  100. uint8_t addressDisplayFour = DEFAULT_NOTHING_ATTACHED,
  101. TwoWire &wirePort = Wire); // Sets the address of the device and opens the Wire port for communication
  102. bool isConnected(uint8_t displayNumber);
  103. bool initialize();
  104. uint8_t lookUpDisplayAddress(uint8_t displayNumber);
  105. //Display configuration functions
  106. bool clear();
  107. bool setBrightness(uint8_t duty);
  108. bool setBrightnessSingle(uint8_t displayNumber, uint8_t duty);
  109. bool setBlinkRate(float rate);
  110. bool setBlinkRateSingle(uint8_t displayNumber, float rate);
  111. bool displayOn();
  112. bool displayOff();
  113. bool displayOnSingle(uint8_t displayNumber);
  114. bool displayOffSingle(uint8_t displayNumber);
  115. bool setDisplayOnOff(uint8_t displayNumber, bool turnOnDisplay);
  116. bool enableSystemClock();
  117. bool disableSystemClock();
  118. bool enableSystemClockSingle(uint8_t displayNumber);
  119. bool disableSystemClockSingle(uint8_t displayNumber);
  120. // Light up functions
  121. void illuminateSegment(uint8_t segment, uint8_t digit);
  122. void illuminateChar(uint16_t disp, uint8_t digit);
  123. void printChar(uint8_t displayChar, uint8_t digit);
  124. bool updateDisplay();
  125. // Define Character Segment Map
  126. bool defineChar(uint8_t displayChar, uint16_t segmentsToTurnOn);
  127. uint16_t getSegmentsToTurnOn (uint8_t charPos);
  128. // Decimal functions
  129. bool decimalOn();
  130. bool decimalOff();
  131. bool decimalOnSingle(uint8_t displayNumber, bool updateNow = true);
  132. bool decimalOffSingle(uint8_t displayNumber, bool updateNow = true);
  133. bool setDecimalOnOff(uint8_t displayNumber, bool turnOnDecimal, bool updateNow = true);
  134. // Colon functions
  135. bool colonOn();
  136. bool colonOff();
  137. bool colonOnSingle(uint8_t displayNumber, bool updateNow = true);
  138. bool colonOffSingle(uint8_t displayNumber, bool updateNow = true);
  139. bool setColonOnOff(uint8_t displayNumber, bool turnOnColon, bool updateNow = true);
  140. // Shifting
  141. bool shiftRight(uint8_t shiftAmt = 1);
  142. bool shiftLeft(uint8_t shiftAmt = 1);
  143. // For overloading the print function
  144. virtual size_t write(uint8_t);
  145. virtual size_t write(const uint8_t *buffer, size_t size);
  146. virtual size_t write(const char *str);
  147. // I2C abstraction
  148. bool readRAM(uint8_t address, uint8_t reg, uint8_t *buff, uint8_t buffSize);
  149. bool writeRAM(uint8_t address, uint8_t reg, uint8_t *buff, uint8_t buffSize);
  150. bool writeRAM(uint8_t reg, uint8_t data);
  151. };
  152. #endif