HSA_LCD_Shield.cpp 20 KB

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