HSA_LCD_Shield.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 buttons are configured
  270. bool __leds; //! @brief Store the value "true", if LEDs are configured
  271. bool __lcd; //! @brief Store the value "true", if the lcd is configured
  272. byte __buttonUp; //! @brief Store address of button up
  273. byte __buttonDown; //! @brief Store address of button down
  274. byte __buttonRight; //! @brief Store address of button right
  275. byte __buttonLeft; //! @brief Store adress of button left
  276. byte __ledGreen; //! @brief Store address of LED green
  277. byte __ledRed; //! @brief Store address of LED red
  278. byte __lcdBacklight;//! @brief Store address of LCD-Backlight
  279. //////////////////// GPIO Configuration ////////////////////
  280. /**
  281. * @brief Configure GPIOs, depending of LCD-Shield version
  282. *
  283. * @return true Configuration is accepted
  284. * @return false Configuration is not accepted, check construction
  285. */
  286. bool __gpioConfig(void);
  287. ////////////////////////////////////////////////////////////////////////////////
  288. //////////////////// Privat Functions/Variables ////////////////////
  289. ////////////////////////////////////////////////////////////////////////////////
  290. private:
  291. //////////////////// Send Message ////////////////////
  292. /**
  293. * @brief Used, to send an array of bytes to the LC-Display
  294. *
  295. * @param bytes Array of bytes
  296. * @param sizeBytes Size of the array of Bytes
  297. */
  298. void _sendMessage(byte* bytes, byte sizeBytes);
  299. ////////////////////////////////////////////////////////////////////////////////
  300. //////////////////// Public Functions/Variables ////////////////////
  301. ////////////////////////////////////////////////////////////////////////////////
  302. public:
  303. //////////////////// Constructor ////////////////////
  304. /**
  305. * @brief Construct a new HSA_LCD_Shield object with default Shield-ver./Config
  306. */
  307. HSA_LCD_Shield(void);
  308. /**
  309. * @brief Construct a new HSA_LCD_Shield object with different Shield version
  310. *
  311. * @param version Version of LCD-Shield
  312. */
  313. HSA_LCD_Shield(byte version);
  314. /**
  315. * @brief Construct a new HSA_LCD_Shield object with different Config
  316. *
  317. * @param config Configuration of Buttons and LEDs
  318. */
  319. HSA_LCD_Shield(const char config[CONFIG_SIZE]);
  320. /**
  321. * @brief Construct a new HSA_LCD_Shield object with different Shield-ver./Config
  322. *
  323. * @param version Version of LCD-Shield
  324. * @param config Configuration of Buttons and LEDs
  325. */
  326. HSA_LCD_Shield(byte version, const char config[CONFIG_SIZE]);
  327. //////////////////// Deconstructor ////////////////////
  328. /**
  329. * @brief Destroy the HSA_LCD_Shield object
  330. */
  331. ~HSA_LCD_Shield(void);
  332. //////////////////// Start Configuration ////////////////////
  333. /**
  334. * @brief Start Configuration of LC-Display and Buttons/LEDS
  335. *
  336. * @return true Configuration is accepted
  337. * @return false Configuration is not accepted, check construction
  338. */
  339. bool begin(void);
  340. /**
  341. * @brief Start Configuration of LC-Display and Buttons/LEDS
  342. *
  343. * @param address I²C-Address of LC-Display
  344. *
  345. * @return true Configuration is accepted
  346. * @return false Configuration is not accepted, check construction
  347. */
  348. bool begin(byte address);
  349. //////////////////// LCD-Backlight ////////////////////
  350. /**
  351. * @brief Turning on/off the LCD-Backlight
  352. *
  353. * @param value Boolean value for turning on/off the LCD-Backlight
  354. *
  355. * @return true control LCD_Backlight successful
  356. * @return false LCD_Backlight was not configured
  357. */
  358. bool lcdBacklight(bool value);
  359. //////////////////// Clear Display ////////////////////
  360. /**
  361. * @brief Clear the LC-Display
  362. *
  363. * @return true clear LC-Display successful
  364. * @return false LCD was not configured
  365. */
  366. bool clearDisplay(void);
  367. /////////////////// Return Version ////////////////////
  368. /**
  369. * @brief Return the configured LCD-Shield version
  370. *
  371. * @return byte Return the configured LCD-Shield version
  372. */
  373. byte returnVersion(void);
  374. //////////////////// Return Config ////////////////////
  375. /**
  376. * @brief Return the Configuration of Buttons/LEDs
  377. *
  378. * @return char* Return the Address of the Configuration Buttons/LEDs
  379. */
  380. char* returnConfig(void);
  381. //////////////////// Return I²C-Address ////////////////////
  382. /**
  383. * @brief Return the I²C-Address of the LC-Display
  384. *
  385. * @return byte Return the I²C-Address of the LC-Display
  386. */
  387. byte returnAddress(void);
  388. //////////////////// Control LEDs ////////////////////
  389. /**
  390. * @brief Function to control the state of a LED
  391. *
  392. * @param ledPin Pin-Number/Name of the LED
  393. * @param state State of the LED
  394. *
  395. * @return true LED control success
  396. * @return false LED control not success
  397. */
  398. bool controlLED(byte ledPin, bool state);
  399. //////////////////// Get LED state ////////////////////
  400. /**
  401. * @brief Function to get the state of a LED
  402. *
  403. * @param ledPin Pin-Number/Name of the LED
  404. *
  405. * @return false LED is off or LED is unknown
  406. * @return true LED in on
  407. */
  408. bool getLED(byte ledPin);
  409. //////////////////// Get the pressed Button ////////////////////
  410. /**
  411. * @brief Return the number of the button, which is pressed
  412. *
  413. * @return byte 0x00, if no or more than one button is pressed or buttons are
  414. * not configured
  415. * @return byte 0x01, if only button "Up" is pressed
  416. * @return byte 0x02, if only button "Right" is pressed
  417. * @return byte 0x03, if only button "Down" is pressed
  418. * @return byte 0x04, if only button "Left" is pressed
  419. */
  420. byte getButton();
  421. //////////////////// Write Row ////////////////////
  422. /**
  423. * @brief Write words over all rows.
  424. *
  425. * @param text Array of bytes, which should be written to the rows.
  426. *
  427. * @return false Write to Rows was not possible
  428. * @return true Write to Rows was successful
  429. */
  430. bool writeRow(const char* text);
  431. /**
  432. * @brief Write words to a targeted row.
  433. *
  434. * @param row Choose the row to write
  435. * @param text Array of bytes, which should be written to the targeted row.
  436. *
  437. * @return false Write to chosen Row was not possible
  438. * @return true Write to chosen Row was successful
  439. */
  440. bool writeRow(byte row, const char* text);
  441. //////////////////// Write Position XY ////////////////////
  442. /**
  443. * @brief Write words to a targeted Position (row/column)
  444. *
  445. * @param row Choose the row to write
  446. * @param column Choose the column to write
  447. * @param text Array of bytes, which should be written.
  448. *
  449. * @return false Write to chosen Row/Column was not possible
  450. * @return true Write to chosen Row/Column was successful
  451. */
  452. bool writeXY(byte row, byte column, const char* text);
  453. ////////////////////////////////////////////////////////////////////////////////
  454. //////////////////// close Header ////////////////////
  455. ////////////////////////////////////////////////////////////////////////////////
  456. };
  457. #endif