HSA_LCD_Shield.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // Programname: HSA_LCD_Shield - Header //
  4. // Date: 16.11.2018 //
  5. // Description: Header-File of the LCD-Shield, which was designed and built //
  6. // in the Modul "Elektronikdesign". In this file you can find //
  7. // the description of all functions of the class HSA_LCD_Shield. //
  8. // //
  9. // Author: Tobias Müller, M. Eng. //
  10. // //
  11. ////////////////////////////////////////////////////////////////////////////////
  12. ////////////////////////////////////////////////////////////////////////////////
  13. //////////////////// Create Header ////////////////////
  14. ////////////////////////////////////////////////////////////////////////////////
  15. #ifndef HSA_LCD_SHIELD
  16. #define HSA_LCD_SHIELD
  17. ////////////////////////////////////////////////////////////////////////////////
  18. //////////////////// Privat Symbols ////////////////////
  19. ////////////////////////////////////////////////////////////////////////////////
  20. //////////////////// Pin-Config Version 5 ////////////////////
  21. // Button Right
  22. #ifndef BUTTON_RIGHT_V5
  23. #define BUTTON_RIGHT_V5 0x02
  24. #endif
  25. // Button Down
  26. #ifndef BUTTON_DOWN_V5
  27. #define BUTTON_DOWN_V5 0x03
  28. #endif
  29. // Button Up
  30. #ifndef BUTTON_UP_V5
  31. #define BUTTON_UP_V5 0x04
  32. #endif
  33. // Button Left
  34. #ifndef BUTTON_LEFT_V5
  35. #define BUTTON_LEFT_V5 0x05
  36. #endif
  37. // LED Red
  38. #ifndef LED_RED_V5
  39. #define LED_RED_V5 0x06
  40. #endif
  41. // LED Green
  42. #ifndef LED_GREEN_V5
  43. #define LED_GREEN_V5 0x07
  44. #endif
  45. // LCD-Backlight
  46. #ifndef LCD_BACKLIGHT_V5
  47. #define LCD_BACKLIGHT_V5 0x08
  48. #endif
  49. //////////////////// Pin-Config Version 6 ////////////////////
  50. // Button Right
  51. #ifndef BUTTON_RIGHT_V6
  52. #define BUTTON_RIGHT_V6 0x02
  53. #endif
  54. // Button Down
  55. #ifndef BUTTON_DOWN_V6
  56. #define BUTTON_DOWN_V6 0x03
  57. #endif
  58. // Button Up
  59. #ifndef BUTTON_UP_V6
  60. #define BUTTON_UP_V6 0x05
  61. #endif
  62. // Button Left
  63. #ifndef BUTTON_LEFT_V6
  64. #define BUTTON_LEFT_V6 0x06
  65. #endif
  66. // LED Red
  67. #ifndef LED_RED_V6
  68. #define LED_RED_V6 0x07
  69. #endif
  70. // LED Green
  71. #ifndef LED_GREEN_V6
  72. #define LED_GREEN_V6 0x08
  73. #endif
  74. // LCD-Backlight
  75. #ifndef LCD_BACKLIGHT_V6
  76. #define LCD_BACKLIGHT_V6 0x09
  77. #endif
  78. //////////////////// Display Parameters ////////////////////
  79. // Control Byte: No Bit set
  80. #ifndef CONTROL_BYTE
  81. #define CONTROL_BYTE 0x00
  82. #endif
  83. // Control Byte: Continue Bit set
  84. #ifndef CONTROL_BYTE_CB
  85. #define CONTROL_BYTE_CB 0x80
  86. #endif
  87. // Control Byte: Data/Command Selection Bit set
  88. #ifndef CONTROL_BYTE_DCB
  89. #define CONTROL_BYTE_DCB 0x40 // write RAM Data
  90. #endif
  91. // LCD-Parameter: Function Set 1
  92. #ifndef LCD_PARA_FUNC1
  93. #define LCD_PARA_FUNC1 0x3A // 8 Bit, RE = 1, IS = 0
  94. #endif
  95. // LCD-Parameter: Function Set 2
  96. #ifndef LCD_PARA_FUNC2
  97. #define LCD_PARA_FUNC2 0x39 // 8 Bit, RE = 0, IS = 1
  98. #endif
  99. // LCD-Parameter: Function Set 3
  100. #ifndef LCD_PARA_FUNC3
  101. #define LCD_PARA_FUNC3 0x38 // 8 Bit, RE = 0, IS = 0
  102. #endif
  103. // LCD-Parameter: Extended Function
  104. #ifndef LCD_PARA_EXT_FUNC
  105. #define LCD_PARA_EXT_FUNC 0x09 // 4-Line Display
  106. #endif
  107. // LCD-Parameter: Entry Mode
  108. #ifndef LCD_PARA_ENTRY_MODE
  109. #define LCD_PARA_ENTRY_MODE 0x06 // Bottom View
  110. #endif
  111. // LCD-Parameter: Bias Setting
  112. #ifndef LCD_PARA_BIAS_SET
  113. #define LCD_PARA_BIAS_SET 0x1E // BS1 = 1
  114. #endif
  115. // LCD-Parameter: Internal OSC
  116. #ifndef LCD_PARA_INT_OSC
  117. #define LCD_PARA_INT_OSC 0x1B // BS0 = 1, Bias = 1/6
  118. #endif
  119. // LCD-Parameter: Follower Control
  120. #ifndef LCD_PARA_FOL_CON
  121. #define LCD_PARA_FOL_CON 0x6E // Devider on, Set Value
  122. #endif
  123. // LCD-Parameter: Power Control
  124. #ifndef LCD_PARA_POW_CON
  125. #define LCD_PARA_POW_CON 0x56 // Booster on, Set Contrast
  126. #endif
  127. // LCD-Parameter: Contrast Set
  128. #ifndef LCD_PARA_CONTRAST
  129. #define LCD_PARA_CONTRAST 0x7A
  130. #endif
  131. // LCD-Parameter: Display on/off Control
  132. #ifndef LCD_PARA_DIS_CON
  133. #define LCD_PARA_DIS_CON 0x0C // Display on, Cursor off, Blink off
  134. #endif
  135. // LCD-Parameter: Clear Display
  136. #ifndef LCD_PARA_CLR_DIS
  137. #define LCD_PARA_CLR_DIS 0x01
  138. #endif
  139. // LCD-Parameter: ROM Selection
  140. #ifndef LCD_PARA_ROM_SEL
  141. #define LCD_PARA_ROM_SEL 0x72
  142. #endif
  143. // LCD-Parameter: Display Row 1
  144. #ifndef LCD_PARA_DIS_ROW1
  145. #define LCD_PARA_DIS_ROW1 0x80
  146. #endif
  147. // LCD-Parameter: Display Row 2
  148. #ifndef LCD_PARA_DIS_ROW2
  149. #define LCD_PARA_DIS_ROW2 0xA0
  150. #endif
  151. // LCD-Parameter: Display Row 3
  152. #ifndef LCD_PARA_DIS_ROW3
  153. #define LCD_PARA_DIS_ROW3 0xC0
  154. #endif
  155. // LCD-Parameter: Display Row 4
  156. #ifndef LCD_PARA_DIS_ROW4
  157. #define LCD_PARA_DIS_ROW4 0xE0
  158. #endif
  159. //////////////////// ASCII-Char Symbols ////////////////////
  160. // Free Space
  161. #ifndef FREE_SPACE
  162. #define FREE_SPACE 0x20
  163. #endif
  164. // End of a String
  165. #ifndef STRING_END
  166. #define STRING_END 0x00
  167. #endif
  168. // Backslash n (\n)
  169. #ifndef BACKSLASH_N
  170. #define BACKSLASH_N 0x0A
  171. #endif
  172. // Backslash r (\r)
  173. #ifndef BACKSLASH_R
  174. #define BACKSLASH_R 0x0D
  175. #endif
  176. //////////////////// Additional Symbols ////////////////////
  177. // Delay before and after transmission in µs
  178. #ifndef DELAY_TRANS_US
  179. #define DELAY_TRANS_US 0x64
  180. #endif
  181. // Array size for writing a row
  182. #ifndef ARRAY_SIZE_ROW
  183. #define ARRAY_SIZE_ROW 0x0D
  184. #endif
  185. // Const Char Size for Configuration
  186. #ifndef CONFIG_SIZE
  187. #define CONFIG_SIZE 0x05
  188. #endif
  189. ////////////////////////////////////////////////////////////////////////////////
  190. //////////////////// Public Symbols ////////////////////
  191. ////////////////////////////////////////////////////////////////////////////////
  192. //////////////////// Display Parameters ////////////////////
  193. // I²C Addresse (Default)
  194. #ifndef I2C_ADDRESS
  195. #define I2C_ADDRESS 0x3C
  196. #endif
  197. //////////////////// Pin-Symbols ////////////////////
  198. // Button Up
  199. #ifndef BUTTON_UP
  200. #define BUTTON_UP 0x01
  201. #endif
  202. // Button Right
  203. #ifndef BUTTON_RIGHT
  204. #define BUTTON_RIGHT 0x02
  205. #endif
  206. // Button Down
  207. #ifndef BUTTON_DOWN
  208. #define BUTTON_DOWN 0x03
  209. #endif
  210. // Button Left
  211. #ifndef BUTTON_LEFT
  212. #define BUTTON_LEFT 0x04
  213. #endif
  214. // LED Red
  215. #ifndef LED_RED
  216. #define LED_RED 0x05
  217. #endif
  218. // LED Green
  219. #ifndef LED_GREEN
  220. #define LED_GREEN 0x06
  221. #endif
  222. //////////////////// LCD-Shield Configurations ////////////////////
  223. // Config: LEDs = On, Buttons = On (Default)
  224. #ifndef CONFIG_L1B1
  225. #define CONFIG_L1B1 "L1B1"
  226. #endif
  227. // Config: LEDs = On, Buttons = Off
  228. #ifndef CONFIG_L1B0
  229. #define CONFIG_L1B0 "L1B0"
  230. #endif
  231. // Config: LEDs = Off, Buttons = On
  232. #ifndef CONFIG_L0B1
  233. #define CONFIG_L0B1 "L0B1"
  234. #endif
  235. // Config: LEDs = Off, Buttons = Off
  236. #ifndef CONFIG_L0B0
  237. #define CONFIG_L0B0 "L0B0"
  238. #endif
  239. //////////////////// Version Symbols ////////////////////
  240. // Version 5
  241. #ifndef LCD_VERSION_5
  242. #define LCD_VERSION_5 0x05
  243. #endif
  244. // Version 6
  245. #ifndef LCD_VERSION_6
  246. #define LCD_VERSION_6 0x06
  247. #endif
  248. ////////////////////////////////////////////////////////////////////////////////
  249. //////////////////// Include Header-File ////////////////////
  250. ////////////////////////////////////////////////////////////////////////////////
  251. #include <Arduino.h>
  252. #include <Wire.h>
  253. #include <stdio.h>
  254. ////////////////////////////////////////////////////////////////////////////////
  255. //////////////////// Define Class ////////////////////
  256. ////////////////////////////////////////////////////////////////////////////////
  257. /**
  258. * @brief LCD-Shield Class to control all functionalities of the Shield
  259. */
  260. class HSA_LCD_Shield {
  261. ////////////////////////////////////////////////////////////////////////////////
  262. //////////////////// protected Functions/Variables ////////////////////
  263. ////////////////////////////////////////////////////////////////////////////////
  264. protected:
  265. //////////////////// protected Variables ////////////////////
  266. byte __version; //! @brief Store used LCD-Shield version
  267. byte __i2cAddress; //! @brief Store the I²C-Address of the LC-Display
  268. char __config[CONFIG_SIZE]; //! @brief Store the Buttons/LEDs-Configuration
  269. bool __buttons; //! @brief Store the value "true", if the buttons configured
  270. bool __leds; //! @brief Store the value "true", if the LEDs configured
  271. byte __buttonUp; //! @brief Store address of button up
  272. byte __buttonDown; //! @brief Store address of button down
  273. byte __buttonRight; //! @brief Store address of button right
  274. byte __buttonLeft; //! @brief Store adress of button left
  275. byte __ledGreen; //! @brief Store address of LED green
  276. byte __ledRed; //! @brief Store address of LED red
  277. byte __lcdBacklight;//! @brief Store address of LCD-Backlight
  278. //////////////////// GPIO Configuration ////////////////////
  279. /**
  280. * @brief Configure GPIOs, depending of LCD-Shield version
  281. *
  282. * @return true Configuration is accepted
  283. * @return false Configuration is not accepted, check construction
  284. */
  285. bool __gpioConfig(void);
  286. ////////////////////////////////////////////////////////////////////////////////
  287. //////////////////// Privat Functions/Variables ////////////////////
  288. ////////////////////////////////////////////////////////////////////////////////
  289. private:
  290. //////////////////// Send Message ////////////////////
  291. /**
  292. * @brief Used, to send an array of bytes to the LC-Display
  293. *
  294. * @param bytes Array of bytes
  295. * @param sizeBytes Size of the array of Bytes
  296. */
  297. void _sendMessage(byte* bytes, byte sizeBytes);
  298. ////////////////////////////////////////////////////////////////////////////////
  299. //////////////////// Public Functions/Variables ////////////////////
  300. ////////////////////////////////////////////////////////////////////////////////
  301. public:
  302. //////////////////// Constructor ////////////////////
  303. /**
  304. * @brief Construct a new HSA_LCD_Shield object with default Shield-ver./Config
  305. */
  306. HSA_LCD_Shield();
  307. /**
  308. * @brief Construct a new HSA_LCD_Shield object with different Shield version
  309. *
  310. * @param version Version of LCD-Shield
  311. */
  312. HSA_LCD_Shield(byte version);
  313. /**
  314. * @brief Construct a new HSA_LCD_Shield object with different Config
  315. *
  316. * @param config Configuration of Buttons and LEDs
  317. */
  318. HSA_LCD_Shield(const char config[CONFIG_SIZE]);
  319. /**
  320. * @brief Construct a new HSA_LCD_Shield object with different Shield-ver./Config
  321. *
  322. * @param version Version of LCD-Shield
  323. * @param config Configuration of Buttons and LEDs
  324. */
  325. HSA_LCD_Shield(byte version, const char config[CONFIG_SIZE]);
  326. //////////////////// Deconstructor ////////////////////
  327. /**
  328. * @brief Destroy the HSA_LCD_Shield object
  329. */
  330. ~HSA_LCD_Shield(void);
  331. //////////////////// Start Configuration ////////////////////
  332. /**
  333. * @brief Start Configuration of LC-Display and Buttons/LEDS
  334. *
  335. * @return true Configuration is accepted
  336. * @return false Configuration is not accepted, check construction
  337. */
  338. bool begin(void);
  339. /**
  340. * @brief Start Configuration of LC-Display and Buttons/LEDS
  341. *
  342. * @param address I²C-Address of LC-Display
  343. *
  344. * @return true Configuration is accepted
  345. * @return false Configuration is not accepted, check construction
  346. */
  347. bool begin(byte address);
  348. //////////////////// LCD-Backlight ////////////////////
  349. /**
  350. * @brief Turning on/off the LCD-Backlight
  351. *
  352. * @param value Boolean value for turning on/off the LCD-Backlight
  353. */
  354. void lcdBacklight(bool value);
  355. //////////////////// Clear Display ////////////////////
  356. /**
  357. * @brief Clear the LC-Display
  358. */
  359. void clearDisplay(void);
  360. /////////////////// Return Version ////////////////////
  361. /**
  362. * @brief Return the configured LCD-Shield version
  363. *
  364. * @return byte Return the configured LCD-Shield version
  365. */
  366. byte returnVersion(void);
  367. //////////////////// Return Config ////////////////////
  368. /**
  369. * @brief Return the Configuration of Buttons/LEDs
  370. *
  371. * @return char* Return the Address of the Configuration Buttons/LEDs
  372. */
  373. char* returnConfig(void);
  374. //////////////////// Return I²C-Address ////////////////////
  375. /**
  376. * @brief Return the I²C-Address of the LC-Display
  377. *
  378. * @return byte Return the I²C-Address of the LC-Display
  379. */
  380. byte returnAddress(void);
  381. //////////////////// Control LEDs ////////////////////
  382. /**
  383. * @brief Function to control the state of a LED
  384. *
  385. * @param ledPin Pin-Number/Name of the LED
  386. * @param state State of the LED
  387. *
  388. * @return true LED control success
  389. * @return false LED control not success
  390. */
  391. bool controlLED(char ledPin, bool state);
  392. //////////////////// Get LED state ////////////////////
  393. /**
  394. * @brief Function to get the state of a LED
  395. *
  396. * @param ledPin Pin-Number/Name of the LED
  397. *
  398. * @return false LED is off or LED is unknown
  399. * @return true LED in on
  400. */
  401. bool getLED(char ledPin);
  402. //////////////////// Get the pressed Button ////////////////////
  403. /**
  404. * @brief Return the number of the button, which is pressed
  405. *
  406. * @return char 0x00, if no or more than one button is pressed or buttons are
  407. * not configured
  408. * @return char 0x01, if only button "Up" is pressed
  409. * @return char 0x02, if only button "Right" is pressed
  410. * @return char 0x03, if only button "Down" is pressed
  411. * @return char 0x04, if only button "Left" is pressed
  412. */
  413. byte getButton();
  414. //////////////////// Write Row ////////////////////
  415. /**
  416. * @brief Write words over all rows.
  417. *
  418. * @param text Array of bytes, which should be written to the rows.
  419. *
  420. * @return false Write to Rows was not possible
  421. * @return true Write to Rows was successful
  422. */
  423. bool writeRow(const char* text);
  424. /**
  425. * @brief Write words to a targeted row.
  426. *
  427. * @param row Choose the row to write
  428. * @param text Array of bytes, which should be written to the targeted row.
  429. *
  430. * @return false Write to chosen Row was not possible
  431. * @return true Write to chosen Row was successful
  432. */
  433. bool writeRow(byte row, const char* text);
  434. //////////////////// Write Position XY ////////////////////
  435. /**
  436. * @brief Write words to a targeted Position (row/column)
  437. *
  438. * @param row Choose the row to write
  439. * @param column Choose the column to write
  440. * @param text Array of bytes, which should be written.
  441. *
  442. * @return false Write to chosen Row/Column was not possible
  443. * @return true Write to chosen Row/Column was successful
  444. */
  445. bool writeXY(byte row, byte column, const char* text);
  446. ////////////////////////////////////////////////////////////////////////////////
  447. //////////////////// close Header ////////////////////
  448. ////////////////////////////////////////////////////////////////////////////////
  449. };
  450. #endif