HSA_LCD_Shield.h 15 KB

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