HSA_LCD_Shield.cpp 20 KB

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