HSA_LCD_Shield.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 the configuration of Buttons/LEDs
  135. strcpy(this->__config,config);
  136. // Exit Constructor
  137. return;
  138. }
  139. HSA_LCD_Shield::HSA_LCD_Shield(byte version) {
  140. // Save LCD-Shield version
  141. this->__version = version;
  142. // Save the configuration of Buttons/LEDs
  143. strcpy(this->__config,CONFIG_L1B1);
  144. // Exit Constructor
  145. return;
  146. }
  147. HSA_LCD_Shield::HSA_LCD_Shield(const char config[CONFIG_SIZE]){
  148. // Save LCD-Shield version
  149. this->__version = LCD_VERSION_6;
  150. // Save the configuration of Buttons/LEDs
  151. strcpy(this->__config,config);
  152. // Exit Constructor
  153. return;
  154. }
  155. HSA_LCD_Shield::HSA_LCD_Shield(){
  156. // Save LCD-Shield version
  157. this->__version = LCD_VERSION_6;
  158. // Save the configuration of Buttons/LEDs
  159. strcpy(this->__config,CONFIG_L1B1);
  160. // Exit Constructor
  161. return;
  162. }
  163. //////////////////// Deconstructor ////////////////////
  164. HSA_LCD_Shield::~HSA_LCD_Shield(void) {
  165. // Exit function
  166. return;
  167. }
  168. //////////////////// Start Configuration ////////////////////
  169. bool HSA_LCD_Shield::begin(void) {
  170. // Save standard I²C-Address of LC-Display
  171. this->__i2cAddress = I2C_ADDRESS;
  172. // Configure GPIOs and exit
  173. return __gpioConfig();
  174. }
  175. bool HSA_LCD_Shield::begin(byte address) {
  176. // Save I²C-Address of LC-Display
  177. this->__i2cAddress = address;
  178. // Configure GPIOs and exit
  179. return __gpioConfig();
  180. }
  181. //////////////////// LCD-Backlight ////////////////////
  182. void HSA_LCD_Shield::lcdBacklight(bool value) {
  183. // Turning on/off LCD-Backlight
  184. digitalWrite(this->__lcdBacklight,value);
  185. // Exit function
  186. return;
  187. }
  188. //////////////////// Clear Display ////////////////////
  189. void HSA_LCD_Shield::clearDisplay(void) {
  190. // Define an array of Bytes for clearing the Display
  191. byte buffer[] = {CONTROL_BYTE_CB,
  192. LCD_PARA_CLR_DIS};
  193. // Send array of bytes
  194. _sendMessage(buffer, sizeof(buffer));
  195. // Exit function
  196. return;
  197. }
  198. /////////////////// Return Version ////////////////////
  199. byte HSA_LCD_Shield::returnVersion(void) {
  200. // Exit function and return the configured Version for the LCD-Shield
  201. return this->__version;
  202. }
  203. //////////////////// Return Config ////////////////////
  204. char* HSA_LCD_Shield::returnConfig(void) {
  205. // Exit function and return the configuration value
  206. return this->__config;
  207. }
  208. //////////////////// Return I²C-Address ////////////////////
  209. byte HSA_LCD_Shield::returnAddress(void){
  210. // Exit function and return the value of the I²C-Address
  211. return this->__i2cAddress;
  212. }
  213. //////////////////// Control LEDs ////////////////////
  214. bool HSA_LCD_Shield::controlLED(char ledPin, bool state) {
  215. // Checks, whether LED-Pins are configured
  216. if(this->__leds) {
  217. // Checks, whether LED-Color is known and control LED
  218. if(ledPin == LED_GREEN) {
  219. digitalWrite(this->__ledGreen,!state);
  220. return true;
  221. }
  222. if(ledPin == LED_RED) {
  223. digitalWrite(this->__ledRed,!state);
  224. return true;
  225. }
  226. }
  227. // Exit function with error, if LED-Pins are not configured or LED-Color is
  228. // unknown
  229. return false;
  230. }
  231. //////////////////// Get LED state ////////////////////
  232. bool HSA_LCD_Shield::getLED(char ledPin) {
  233. // Exit function and return the value of chosen LED
  234. if(ledPin == LED_GREEN) return !digitalRead(this->__ledGreen);
  235. else if(ledPin == LED_RED) return !digitalRead(this->__ledRed);
  236. else return false;
  237. }
  238. //////////////////// Get the pressed Button ////////////////////
  239. byte HSA_LCD_Shield::getButton() {
  240. // return 1, if only button "Up" is pressed and configured
  241. if(digitalRead(this->__buttonUp) &&
  242. !digitalRead(this->__buttonRight) &&
  243. !digitalRead(this->__buttonDown) &&
  244. !digitalRead(this->__buttonLeft) &&
  245. this->__buttons)
  246. return BUTTON_UP;
  247. // return 2, if only button "Right" is pressed and configured
  248. if(!digitalRead(this->__buttonUp) &&
  249. digitalRead(this->__buttonRight) &&
  250. !digitalRead(this->__buttonDown) &&
  251. !digitalRead(this->__buttonLeft) &&
  252. this->__buttons)
  253. return BUTTON_RIGHT;
  254. // return 3, if only button "Down" is pressed and configured
  255. if(!digitalRead(this->__buttonUp) &&
  256. !digitalRead(this->__buttonRight) &&
  257. digitalRead(this->__buttonDown) &&
  258. !digitalRead(this->__buttonLeft) &&
  259. this->__buttons)
  260. return BUTTON_DOWN;
  261. // return 4, if only button "Left" is pressed and configured
  262. if(!digitalRead(this->__buttonUp) &&
  263. !digitalRead(this->__buttonRight) &&
  264. !digitalRead(this->__buttonDown) &&
  265. digitalRead(this->__buttonLeft) &&
  266. this->__buttons)
  267. return BUTTON_LEFT;
  268. // return false, if no button or more than one button is pressed or
  269. // buttons are not configured
  270. return false;
  271. }
  272. //////////////////// Write Row ////////////////////
  273. bool HSA_LCD_Shield::writeRow(byte row, const char* text) {
  274. // If chosen Row does not exist, exit function with error
  275. if(!(row >= 0x01 && row <= 0x04)) return false;
  276. // Define buffer for test Message
  277. byte buffer[ARRAY_SIZE_ROW] = "";
  278. // Define variable to store a boolean value, if a control-byte was found
  279. bool controlByte = false;
  280. // Create data-array with message and send the message at the end
  281. for(byte i = 0x00; i < ARRAY_SIZE_ROW; i++) {
  282. // Set control-byte for changing parameter in the first iteration
  283. if(i == 0x00) buffer[i] = CONTROL_BYTE_CB;
  284. // Change the cursor position to row in the second iteration.
  285. if(i == 0x01 && row == 0x01) buffer[i] = LCD_PARA_DIS_ROW1;
  286. if(i == 0x01 && row == 0x02) buffer[i] = LCD_PARA_DIS_ROW2;
  287. if(i == 0x01 && row == 0x03) buffer[i] = LCD_PARA_DIS_ROW3;
  288. if(i == 0x01 && row == 0x04) buffer[i] = LCD_PARA_DIS_ROW4;
  289. // Set control-byte for writing to the display in the third iteration
  290. if(i == 0x02) buffer[i] = CONTROL_BYTE_DCB;
  291. // Copy text into the buffer between iteration 2 to 13
  292. if(i > 0x02 && i < ARRAY_SIZE_ROW) {
  293. // If no control-byte was found, copy byte into buffer
  294. if(!controlByte) {
  295. // Check byte for control-byte
  296. if(isControl(text[i - 0x03])) controlByte = true;
  297. // if not, copy byte into buffer
  298. else buffer[i] = text[i - 0x03];
  299. }
  300. // If control-byte was found, fill message with free space
  301. if(controlByte) {
  302. // Fill Buffer with free space
  303. buffer[i] = FREE_SPACE;
  304. }
  305. }
  306. }
  307. // Send message
  308. _sendMessage(buffer,sizeof(buffer));
  309. // Exit function with success
  310. return true;
  311. }
  312. bool HSA_LCD_Shield::writeRow(const char* text) {
  313. // Define buffer for test Message
  314. char buffer[ARRAY_SIZE_ROW] = "";
  315. // Define variable to store the actual row position
  316. byte rowPosition = LCD_PARA_DIS_ROW1;
  317. // Define variable, which count the copied bytes
  318. byte countBytes = 0x00;
  319. // Create data-array with messages for 4 rows and send the messages
  320. for(byte i = 0x00; i < 0x28; i++){
  321. // If the position of the row is not selected, skip following code
  322. if(rowPosition == false) continue;
  323. // If first row is selected
  324. if(rowPosition == LCD_PARA_DIS_ROW1) {
  325. // If counted bytes has not reach max value, copy byte into buffer
  326. if(countBytes < 0x0A) buffer[countBytes] = text[i];
  327. // Check, if copied byte was control-byte
  328. if(isControl(text[i])) {
  329. // Check if control-byte was \n or \r or end of string,
  330. // set counted bytes to max value
  331. if(text[i] == BACKSLASH_N ||
  332. text[i] == BACKSLASH_R ||
  333. text[i] == STRING_END)
  334. countBytes = 0x0A;
  335. // Check if control-byte was end of the string, deactivate row position
  336. if(text[i] == STRING_END) rowPosition = false;
  337. }
  338. // Otherwise increase counted bytes
  339. else countBytes++;
  340. // If count bytes is max value
  341. if(countBytes == 0x0A) {
  342. // Reset count bytes
  343. countBytes = 0x00;
  344. // write first row
  345. writeRow(0x01, buffer);
  346. // If row position is deactivated
  347. if(rowPosition == false) {
  348. // clear following rows
  349. writeRow(0x02, &text[i]);
  350. writeRow(0x03, &text[i]);
  351. writeRow(0x04, &text[i]);
  352. }
  353. // Otherwise set row position the row 2
  354. else rowPosition = LCD_PARA_DIS_ROW2;
  355. // skip the rest of the code in this iteration
  356. continue;
  357. }
  358. }
  359. // If second row is selected
  360. if(rowPosition == LCD_PARA_DIS_ROW2) {
  361. // If counted bytes has not reach max value, copy byte into buffer
  362. if(countBytes < 0x0A) buffer[countBytes] = text[i];
  363. // Check, if copied byte was control-byte
  364. if(isControl(text[i])) {
  365. // Check if control-byte was \n or \r or end of string,
  366. // set counted bytes to max value
  367. if(text[i] == BACKSLASH_N ||
  368. text[i] == BACKSLASH_R ||
  369. text[i] == STRING_END)
  370. countBytes = 0x0A;
  371. // Check if control-byte was end of the string, deactivate row position
  372. if(text[i] == STRING_END) rowPosition = false;
  373. }
  374. // Otherwise increase counted bytes
  375. else countBytes++;
  376. // If count bytes is max value
  377. if(countBytes == 0x0A) {
  378. // Reset count bytes
  379. countBytes = 0x00;
  380. // write second row
  381. writeRow(0x02, buffer);
  382. // If row position is deactivated
  383. if(rowPosition == false) {
  384. // clear following rows
  385. writeRow(0x03, &text[i]);
  386. writeRow(0x04, &text[i]);
  387. }
  388. // Otherwise set row position the row 3
  389. else rowPosition = LCD_PARA_DIS_ROW3;
  390. // skip the rest of the code in this iteration
  391. continue;
  392. }
  393. }
  394. // If third row is selected
  395. if(rowPosition == LCD_PARA_DIS_ROW3) {
  396. // If counted bytes has not reach max value, copy byte into buffer
  397. if(countBytes < 0x0A) buffer[countBytes] = text[i];
  398. // Check, if copied byte was control-byte
  399. if(isControl(text[i])) {
  400. // Check if control-byte was \n or \r or end of string,
  401. // set counted bytes to max value
  402. if(text[i] == BACKSLASH_N ||
  403. text[i] == BACKSLASH_R ||
  404. text[i] == STRING_END)
  405. countBytes = 0x0A;
  406. // Check if control-byte was end of the string, deactivate row position
  407. if(text[i] == STRING_END) rowPosition = false;
  408. }
  409. // Otherwise increase counted bytes
  410. else countBytes++;
  411. // If count bytes is max value
  412. if(countBytes == 0x0A) {
  413. // Reset count bytes
  414. countBytes = 0x00;
  415. // write third row
  416. writeRow(0x03, buffer);
  417. // If row position is deactivated, clear following row
  418. if(rowPosition == false) writeRow(0x04, &text[i]);
  419. // Otherwise set row position the row 4
  420. else rowPosition = LCD_PARA_DIS_ROW4;
  421. // skip the rest of the code in this iteration
  422. continue;
  423. }
  424. }
  425. // If fourth row is selected
  426. if(rowPosition == LCD_PARA_DIS_ROW4) {
  427. // If counted bytes has not reach max value, copy byte into buffer
  428. if(countBytes < 0x0A) buffer[countBytes] = text[i];
  429. // Check, if copied byte was control-byte
  430. if(isControl(text[i])) {
  431. // Check if control-byte was \n or \r or end of string,
  432. // set counted bytes to max value
  433. if(text[i] == BACKSLASH_N ||
  434. text[i] == BACKSLASH_R ||
  435. text[i] == STRING_END)
  436. countBytes = 0x0A;
  437. // Check if control-byte was end of the string, deactivate row position
  438. if(text[i] == STRING_END) rowPosition = false;
  439. }
  440. // Otherwise increase counted bytes
  441. else countBytes++;
  442. // If count bytes is max value
  443. if(countBytes == 0x0A) {
  444. // Reset count bytes
  445. countBytes = 0x00;
  446. // write fourth row
  447. writeRow(0x04, buffer);
  448. // If row position is not deactivated, deaktivate row position
  449. if(rowPosition != false) rowPosition = false;
  450. // skip the rest of the code in this iteration
  451. continue;
  452. }
  453. }
  454. }
  455. // Exit function
  456. return true;
  457. }
  458. //////////////////// Write Position XY ////////////////////
  459. bool HSA_LCD_Shield::writeXY(byte row, byte column, const char* text) {
  460. // If chosen Row does not exist, exit function with error
  461. if(!(row >= 0x01 && row <= 0x04)) return false;
  462. // If chosen Column does not exist, exit function with error
  463. if(!(column >= 0x01 && column <= 0x0A)) return false;
  464. // create variable to store count of chars
  465. byte count = 0x00;
  466. // Count chars inside of text
  467. for(byte i = 0x00; i < 0x0A && !isControl(text[i]); i++) count++;
  468. // Define buffer for text Message
  469. byte buffer[ARRAY_SIZE_ROW - (0x0A - count)] = "";
  470. // Create data-array with message and send the message at the end
  471. for(byte i = 0x00; i < sizeof(buffer); i++) {
  472. // Set control-byte for changing parameter in the first iteration
  473. if(i == 0x00) buffer[i] = CONTROL_BYTE_CB;
  474. // Change the cursor position to row/column in the second iteration.
  475. if(i == 0x01 && row == 0x01) buffer[i] = LCD_PARA_DIS_ROW1 | (column - 0x01);
  476. if(i == 0x01 && row == 0x02) buffer[i] = LCD_PARA_DIS_ROW2 | (column - 0x01);
  477. if(i == 0x01 && row == 0x03) buffer[i] = LCD_PARA_DIS_ROW3 | (column - 0x01);
  478. if(i == 0x01 && row == 0x04) buffer[i] = LCD_PARA_DIS_ROW4 | (column - 0x01);
  479. // Set control-byte for writing to the display
  480. if(i == 0x02) buffer[i] = CONTROL_BYTE_DCB;
  481. // Copy text into the buffer between iteration 2 to 13
  482. if(i >= 0x03 && i < sizeof(buffer)) buffer[i] = text[i - 0x03];
  483. }
  484. // Send message
  485. _sendMessage(buffer,sizeof(buffer));
  486. // Exit function with success
  487. return true;
  488. }