HSA_LCD_Shield.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // Programname: HSA_LCD_Shield - Code //
  4. // Date: 16.11.2018 //
  5. // Description: Code-File of the LCD-Shield, which was designed and built in //
  6. // the Modul "Elektronikdesign". In this file you can find the //
  7. // code of all functions mentioned in the Header-File of the //
  8. // class. //
  9. // //
  10. // Author: Tobias Müller, M. Eng. //
  11. // //
  12. ////////////////////////////////////////////////////////////////////////////////
  13. ////////////////////////////////////////////////////////////////////////////////
  14. //////////////////// Include Header-File ////////////////////
  15. ////////////////////////////////////////////////////////////////////////////////
  16. #include "HSA_LCD_Shield.h"
  17. ////////////////////////////////////////////////////////////////////////////////
  18. //////////////////// Protected Functions ////////////////////
  19. ////////////////////////////////////////////////////////////////////////////////
  20. //////////////////// GPIO Configuration ////////////////////
  21. bool HSA_LCD_Shield::__gpioConfig(void) {
  22. // Save Pin configuration, depending of LCD-Shield version
  23. if(this->__version == LCD_VERSION_5) {
  24. // Save Pin configuration for buttons
  25. this->__buttonUp = BUTTON_UP_V5;
  26. this->__buttonRight = BUTTON_RIGHT_V5;
  27. this->__buttonDown = BUTTON_DOWN_V5;
  28. this->__buttonLeft = BUTTON_LEFT_V5;
  29. // Save Pin configuration for LEDs
  30. this->__ledGreen = LED_GREEN_V5;
  31. this->__ledRed = LED_RED_V5;
  32. // Save Pin configuration for LCD-Backlight
  33. this->__lcdBacklight = LCD_BACKLIGHT_V5;
  34. }
  35. else if(this->__version == LCD_VERSION_6) {
  36. // Save Pin configuration for buttons
  37. this->__buttonUp = BUTTON_UP_V6;
  38. this->__buttonRight = BUTTON_RIGHT_V6;
  39. this->__buttonDown = BUTTON_DOWN_V6;
  40. this->__buttonLeft = BUTTON_LEFT_V6;
  41. // Save Pin configuration for LEDs
  42. this->__ledGreen = LED_GREEN_V6;
  43. this->__ledRed = LED_RED_V6;
  44. // Save Pin configuration for LCD-Backlight
  45. this->__lcdBacklight = LCD_BACKLIGHT_V6;
  46. }
  47. // If LCD-Shield version is unknown, exit function with error
  48. else return false;
  49. // Check config-value and setup the LEDs, depending of config-value
  50. if(strcmp(this->__config,CONFIG_L1B0) == false ||
  51. strcmp(this->__config,CONFIG_L1B1) == false) {
  52. // Setup LEDs
  53. pinMode(this->__ledGreen,OUTPUT);
  54. pinMode(this->__ledRed,OUTPUT);
  55. digitalWrite(this->__ledGreen,HIGH);
  56. digitalWrite(this->__ledRed,HIGH);
  57. // Store the value "true", if LEDs are configured
  58. this->__leds = true;
  59. }
  60. else {
  61. // Store the value "false", if LEDs are not configured
  62. this->__leds = false;
  63. }
  64. // Check config-value and setup the Buttons depending of config-value
  65. if(strcmp(this->__config,CONFIG_L0B1) == false ||
  66. strcmp(this->__config,CONFIG_L1B1) == false) {
  67. // Setup Buttons
  68. pinMode(this->__buttonUp,INPUT);
  69. pinMode(this->__buttonRight,INPUT);
  70. pinMode(this->__buttonDown,INPUT);
  71. pinMode(this->__buttonLeft,INPUT);
  72. // Store the value "true", if buttons are configured
  73. this->__buttons = true;
  74. }
  75. else {
  76. // Store the value "false", if buttons are not configured
  77. this->__buttons = false;
  78. }
  79. // If config-value is unknown, quit configuration, otherwise configure Display
  80. if(strcmp(this->__config,CONFIG_L1B1) != false &&
  81. strcmp(this->__config,CONFIG_L0B1) != false &&
  82. strcmp(this->__config,CONFIG_L1B0) != false &&
  83. strcmp(this->__config,CONFIG_L0B0) != false) return false;
  84. else {
  85. // Setup LCD-Backlight
  86. pinMode(this->__lcdBacklight,OUTPUT);
  87. // Initialize I²C as Master
  88. Wire.begin();
  89. // Define an array of Bytes for configure the Display
  90. byte buffer[] = {CONTROL_BYTE_CB,
  91. LCD_PARA_FUNC1,
  92. LCD_PARA_EXT_FUNC,
  93. LCD_PARA_ENTRY_MODE,
  94. LCD_PARA_BIAS_SET,
  95. LCD_PARA_FUNC2,
  96. LCD_PARA_INT_OSC,
  97. LCD_PARA_FOL_CON,
  98. LCD_PARA_POW_CON,
  99. LCD_PARA_CONTRAST,
  100. LCD_PARA_FUNC3,
  101. LCD_PARA_DIS_CON,
  102. LCD_PARA_CLR_DIS};
  103. // Send array of bytes
  104. _sendMessage(buffer, sizeof(buffer));
  105. // Exit function with positive feedback
  106. return true;
  107. }
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////
  110. //////////////////// Privat Functions ////////////////////
  111. ////////////////////////////////////////////////////////////////////////////////
  112. //////////////////// Send Message ////////////////////
  113. void HSA_LCD_Shield::_sendMessage(byte* bytes, byte sizeBytes) {
  114. // Small timeout before access the I²C-Bus
  115. delayMicroseconds(DELAY_TRANS_US);
  116. // Start I²C transmission
  117. Wire.beginTransmission(this->__i2cAddress);
  118. // Send an array of Bytes over I²C, depending of its size
  119. for(byte i = 0x00; i < sizeBytes; i++) Wire.write(bytes[i]);
  120. // Stop I²C transmission
  121. Wire.endTransmission();
  122. // Small timeout after access the I²C-Bus
  123. delayMicroseconds(DELAY_TRANS_US);
  124. // Exit function
  125. return;
  126. }
  127. ////////////////////////////////////////////////////////////////////////////////
  128. //////////////////// Public Functions ////////////////////
  129. ////////////////////////////////////////////////////////////////////////////////
  130. //////////////////// Constructor ////////////////////
  131. HSA_LCD_Shield::HSA_LCD_Shield(byte version, const char config[CONFIG_SIZE]) {
  132. // Save LCD-Shield version
  133. this->__version = version;
  134. // Save standard I²C-Address for LC-Display
  135. this->__i2cAddress = I2C_ADDRESS;
  136. // Save the configuration of Buttons/LEDs
  137. strcpy(this->__config,config);
  138. // Exit Constructor
  139. return;
  140. }
  141. HSA_LCD_Shield::HSA_LCD_Shield(byte version) {
  142. // Save LCD-Shield version
  143. this->__version = version;
  144. // Save standard I²C-Address for LC-Display
  145. this->__i2cAddress = I2C_ADDRESS;
  146. // Save the configuration of Buttons/LEDs
  147. strcpy(this->__config,CONFIG_L1B1);
  148. // Exit Constructor
  149. return;
  150. }
  151. HSA_LCD_Shield::HSA_LCD_Shield(const char config[CONFIG_SIZE]){
  152. // Save LCD-Shield version
  153. this->__version = LCD_VERSION_6;
  154. // Save standard I²C-Address for LC-Display
  155. this->__i2cAddress = I2C_ADDRESS;
  156. // Save the configuration of Buttons/LEDs
  157. strcpy(this->__config,config);
  158. // Exit Constructor
  159. return;
  160. }
  161. HSA_LCD_Shield::HSA_LCD_Shield(){
  162. // Save LCD-Shield version
  163. this->__version = LCD_VERSION_6;
  164. // Save standard I²C-Address for LC-Display
  165. this->__i2cAddress = I2C_ADDRESS;
  166. // Save the configuration of Buttons/LEDs
  167. strcpy(this->__config,CONFIG_L1B1);
  168. // Exit Constructor
  169. return;
  170. }
  171. //////////////////// Deconstructor ////////////////////
  172. HSA_LCD_Shield::~HSA_LCD_Shield(void) {
  173. // Exit function
  174. return;
  175. }
  176. //////////////////// Start Configuration ////////////////////
  177. bool HSA_LCD_Shield::begin(void) {
  178. // Configure GPIOs and exit
  179. return __gpioConfig();
  180. }
  181. bool HSA_LCD_Shield::begin(byte address) {
  182. // Save I²C-Address of LC-Display
  183. this->__i2cAddress = address;
  184. // Configure GPIOs and exit
  185. return __gpioConfig();
  186. }
  187. //////////////////// LCD-Backlight ////////////////////
  188. void HSA_LCD_Shield::lcdBacklight(bool value) {
  189. // Turning on/off LCD-Backlight
  190. digitalWrite(this->__lcdBacklight,value);
  191. // Exit function
  192. return;
  193. }
  194. //////////////////// Clear Display ////////////////////
  195. void HSA_LCD_Shield::clearDisplay(void) {
  196. // Define an array of Bytes for clearing the Display
  197. byte buffer[] = {CONTROL_BYTE_CB,
  198. LCD_PARA_CLR_DIS};
  199. // Send array of bytes
  200. _sendMessage(buffer, sizeof(buffer));
  201. // Exit function
  202. return;
  203. }
  204. /////////////////// Return Version ////////////////////
  205. byte HSA_LCD_Shield::returnVersion(void) {
  206. // Exit function and return the configured Version for the LCD-Shield
  207. return this->__version;
  208. }
  209. //////////////////// Return Config ////////////////////
  210. char* HSA_LCD_Shield::returnConfig(void) {
  211. // Exit function and return the configuration value
  212. return this->__config;
  213. }
  214. //////////////////// Return I²C-Address ////////////////////
  215. byte HSA_LCD_Shield::returnAddress(void){
  216. // Exit function and return the value of the I²C-Address
  217. return this->__i2cAddress;
  218. }
  219. //////////////////// Control LEDs ////////////////////
  220. bool HSA_LCD_Shield::controlLED(byte ledPin, bool state) {
  221. // Checks, whether LED-Pins are configured
  222. if(this->__leds) {
  223. // Checks, whether LED-Color is known and control LED
  224. if(ledPin == LED_GREEN) {
  225. digitalWrite(this->__ledGreen,!state);
  226. return true;
  227. }
  228. if(ledPin == LED_RED) {
  229. digitalWrite(this->__ledRed,!state);
  230. return true;
  231. }
  232. }
  233. // Exit function with error, if LED-Pins are not configured or LED-Color is
  234. // unknown
  235. return false;
  236. }
  237. //////////////////// Get LED state ////////////////////
  238. bool HSA_LCD_Shield::getLED(byte ledPin) {
  239. // Exit function and return the value of chosen LED
  240. if(ledPin == LED_GREEN) return !digitalRead(this->__ledGreen);
  241. else if(ledPin == LED_RED) return !digitalRead(this->__ledRed);
  242. else return false;
  243. }
  244. //////////////////// Get the pressed Button ////////////////////
  245. byte HSA_LCD_Shield::getButton() {
  246. // return 1, if only button "Up" is pressed and configured
  247. if(digitalRead(this->__buttonUp) &&
  248. !digitalRead(this->__buttonRight) &&
  249. !digitalRead(this->__buttonDown) &&
  250. !digitalRead(this->__buttonLeft) &&
  251. this->__buttons)
  252. return BUTTON_UP;
  253. // return 2, if only button "Right" is pressed and configured
  254. if(!digitalRead(this->__buttonUp) &&
  255. digitalRead(this->__buttonRight) &&
  256. !digitalRead(this->__buttonDown) &&
  257. !digitalRead(this->__buttonLeft) &&
  258. this->__buttons)
  259. return BUTTON_RIGHT;
  260. // return 3, if only button "Down" is pressed and configured
  261. if(!digitalRead(this->__buttonUp) &&
  262. !digitalRead(this->__buttonRight) &&
  263. digitalRead(this->__buttonDown) &&
  264. !digitalRead(this->__buttonLeft) &&
  265. this->__buttons)
  266. return BUTTON_DOWN;
  267. // return 4, if only button "Left" is pressed and configured
  268. if(!digitalRead(this->__buttonUp) &&
  269. !digitalRead(this->__buttonRight) &&
  270. !digitalRead(this->__buttonDown) &&
  271. digitalRead(this->__buttonLeft) &&
  272. this->__buttons)
  273. return BUTTON_LEFT;
  274. // return false, if no button or more than one button is pressed or
  275. // buttons are not configured
  276. return false;
  277. }
  278. //////////////////// Write Row ////////////////////
  279. bool HSA_LCD_Shield::writeRow(byte row, const char* text) {
  280. // If chosen Row does not exist, exit function with error
  281. if(!(row >= 0x01 && row <= 0x04)) return false;
  282. // Define buffer for test Message
  283. byte buffer[ARRAY_SIZE_ROW] = "";
  284. // Define variable to store a boolean value, if a control-byte was found
  285. bool controlByte = false;
  286. // Create data-array with message and send the message at the end
  287. for(byte i = 0x00; i < ARRAY_SIZE_ROW; i++) {
  288. // Set control-byte for changing parameter in the first iteration
  289. if(i == 0x00) buffer[i] = CONTROL_BYTE_CB;
  290. // Change the cursor position to row in the second iteration.
  291. if(i == 0x01 && row == 0x01) buffer[i] = LCD_PARA_DIS_ROW1;
  292. if(i == 0x01 && row == 0x02) buffer[i] = LCD_PARA_DIS_ROW2;
  293. if(i == 0x01 && row == 0x03) buffer[i] = LCD_PARA_DIS_ROW3;
  294. if(i == 0x01 && row == 0x04) buffer[i] = LCD_PARA_DIS_ROW4;
  295. // Set control-byte for writing to the display in the third iteration
  296. if(i == 0x02) buffer[i] = CONTROL_BYTE_DCB;
  297. // Copy text into the buffer between iteration 2 to 13
  298. if(i > 0x02 && i < ARRAY_SIZE_ROW) {
  299. // If no control-byte was found, copy byte into buffer
  300. if(!controlByte) {
  301. // Check byte for control-byte
  302. if(isControl(text[i - 0x03])) controlByte = true;
  303. // if not, copy byte into buffer
  304. else buffer[i] = text[i - 0x03];
  305. }
  306. // If control-byte was found, fill message with free space
  307. if(controlByte) {
  308. // Fill Buffer with free space
  309. buffer[i] = FREE_SPACE;
  310. }
  311. }
  312. }
  313. // Send message
  314. _sendMessage(buffer,sizeof(buffer));
  315. // Exit function with success
  316. return true;
  317. }
  318. bool HSA_LCD_Shield::writeRow(const char* text) {
  319. // Define buffer for test Message
  320. char buffer[ARRAY_SIZE_ROW] = "";
  321. // Define variable to store the actual row position
  322. byte rowPosition = LCD_PARA_DIS_ROW1;
  323. // Define variable, which count the copied bytes
  324. byte countBytes = 0x00;
  325. // Create data-array with messages for 4 rows and send the messages
  326. for(byte i = 0x00; i < 0x28; i++){
  327. // If the position of the row is not selected, skip following code
  328. if(rowPosition == false) continue;
  329. // If first row is selected
  330. if(rowPosition == LCD_PARA_DIS_ROW1) {
  331. // If counted bytes has not reach max value, copy byte into buffer
  332. if(countBytes < 0x0A) buffer[countBytes] = text[i];
  333. // Check, if copied byte was control-byte
  334. if(isControl(text[i])) {
  335. // Check if control-byte was \n or \r or end of string,
  336. // set counted bytes to max value
  337. if(text[i] == BACKSLASH_N ||
  338. text[i] == BACKSLASH_R ||
  339. text[i] == STRING_END)
  340. countBytes = 0x0A;
  341. // Check if control-byte was end of the string, deactivate row position
  342. if(text[i] == STRING_END) rowPosition = false;
  343. }
  344. // Otherwise increase counted bytes
  345. else countBytes++;
  346. // If count bytes is max value
  347. if(countBytes == 0x0A) {
  348. // Reset count bytes
  349. countBytes = 0x00;
  350. // write first row
  351. writeRow(0x01, buffer);
  352. // If row position is deactivated
  353. if(rowPosition == false) {
  354. // clear following rows
  355. writeRow(0x02, &text[i]);
  356. writeRow(0x03, &text[i]);
  357. writeRow(0x04, &text[i]);
  358. }
  359. // Otherwise set row position the row 2
  360. else rowPosition = LCD_PARA_DIS_ROW2;
  361. // skip the rest of the code in this iteration
  362. continue;
  363. }
  364. }
  365. // If second row is selected
  366. if(rowPosition == LCD_PARA_DIS_ROW2) {
  367. // If counted bytes has not reach max value, copy byte into buffer
  368. if(countBytes < 0x0A) buffer[countBytes] = text[i];
  369. // Check, if copied byte was control-byte
  370. if(isControl(text[i])) {
  371. // Check if control-byte was \n or \r or end of string,
  372. // set counted bytes to max value
  373. if(text[i] == BACKSLASH_N ||
  374. text[i] == BACKSLASH_R ||
  375. text[i] == STRING_END)
  376. countBytes = 0x0A;
  377. // Check if control-byte was end of the string, deactivate row position
  378. if(text[i] == STRING_END) rowPosition = false;
  379. }
  380. // Otherwise increase counted bytes
  381. else countBytes++;
  382. // If count bytes is max value
  383. if(countBytes == 0x0A) {
  384. // Reset count bytes
  385. countBytes = 0x00;
  386. // write second row
  387. writeRow(0x02, buffer);
  388. // If row position is deactivated
  389. if(rowPosition == false) {
  390. // clear following rows
  391. writeRow(0x03, &text[i]);
  392. writeRow(0x04, &text[i]);
  393. }
  394. // Otherwise set row position the row 3
  395. else rowPosition = LCD_PARA_DIS_ROW3;
  396. // skip the rest of the code in this iteration
  397. continue;
  398. }
  399. }
  400. // If third row is selected
  401. if(rowPosition == LCD_PARA_DIS_ROW3) {
  402. // If counted bytes has not reach max value, copy byte into buffer
  403. if(countBytes < 0x0A) buffer[countBytes] = text[i];
  404. // Check, if copied byte was control-byte
  405. if(isControl(text[i])) {
  406. // Check if control-byte was \n or \r or end of string,
  407. // set counted bytes to max value
  408. if(text[i] == BACKSLASH_N ||
  409. text[i] == BACKSLASH_R ||
  410. text[i] == STRING_END)
  411. countBytes = 0x0A;
  412. // Check if control-byte was end of the string, deactivate row position
  413. if(text[i] == STRING_END) rowPosition = false;
  414. }
  415. // Otherwise increase counted bytes
  416. else countBytes++;
  417. // If count bytes is max value
  418. if(countBytes == 0x0A) {
  419. // Reset count bytes
  420. countBytes = 0x00;
  421. // write third row
  422. writeRow(0x03, buffer);
  423. // If row position is deactivated, clear following row
  424. if(rowPosition == false) writeRow(0x04, &text[i]);
  425. // Otherwise set row position the row 4
  426. else rowPosition = LCD_PARA_DIS_ROW4;
  427. // skip the rest of the code in this iteration
  428. continue;
  429. }
  430. }
  431. // If fourth row is selected
  432. if(rowPosition == LCD_PARA_DIS_ROW4) {
  433. // If counted bytes has not reach max value, copy byte into buffer
  434. if(countBytes < 0x0A) buffer[countBytes] = text[i];
  435. // Check, if copied byte was control-byte
  436. if(isControl(text[i])) {
  437. // Check if control-byte was \n or \r or end of string,
  438. // set counted bytes to max value
  439. if(text[i] == BACKSLASH_N ||
  440. text[i] == BACKSLASH_R ||
  441. text[i] == STRING_END)
  442. countBytes = 0x0A;
  443. // Check if control-byte was end of the string, deactivate row position
  444. if(text[i] == STRING_END) rowPosition = false;
  445. }
  446. // Otherwise increase counted bytes
  447. else countBytes++;
  448. // If count bytes is max value
  449. if(countBytes == 0x0A) {
  450. // Reset count bytes
  451. countBytes = 0x00;
  452. // write fourth row
  453. writeRow(0x04, buffer);
  454. // If row position is not deactivated, deaktivate row position
  455. if(rowPosition != false) rowPosition = false;
  456. // skip the rest of the code in this iteration
  457. continue;
  458. }
  459. }
  460. }
  461. // Exit function
  462. return true;
  463. }
  464. //////////////////// Write Position XY ////////////////////
  465. bool HSA_LCD_Shield::writeXY(byte row, byte column, const char* text) {
  466. // If chosen Row does not exist, exit function with error
  467. if(!(row >= 0x01 && row <= 0x04)) return false;
  468. // If chosen Column does not exist, exit function with error
  469. if(!(column >= 0x01 && column <= 0x0A)) return false;
  470. // create variable to store count of chars
  471. byte count = 0x00;
  472. // Count chars inside of text
  473. for(byte i = 0x00; i < 0x0A && !isControl(text[i]); i++) count++;
  474. // Define buffer for text Message
  475. byte buffer[ARRAY_SIZE_ROW - (0x0A - count)] = "";
  476. // Create data-array with message and send the message at the end
  477. for(byte i = 0x00; i < sizeof(buffer); i++) {
  478. // Set control-byte for changing parameter in the first iteration
  479. if(i == 0x00) buffer[i] = CONTROL_BYTE_CB;
  480. // Change the cursor position to row/column in the second iteration.
  481. if(i == 0x01 && row == 0x01) buffer[i] = LCD_PARA_DIS_ROW1 | (column - 0x01);
  482. if(i == 0x01 && row == 0x02) buffer[i] = LCD_PARA_DIS_ROW2 | (column - 0x01);
  483. if(i == 0x01 && row == 0x03) buffer[i] = LCD_PARA_DIS_ROW3 | (column - 0x01);
  484. if(i == 0x01 && row == 0x04) buffer[i] = LCD_PARA_DIS_ROW4 | (column - 0x01);
  485. // Set control-byte for writing to the display
  486. if(i == 0x02) buffer[i] = CONTROL_BYTE_DCB;
  487. // Copy text into the buffer between iteration 2 to 13
  488. if(i >= 0x03 && i < sizeof(buffer)) buffer[i] = text[i - 0x03];
  489. }
  490. // Send message
  491. _sendMessage(buffer,sizeof(buffer));
  492. // Exit function with success
  493. return true;
  494. }