Browse Source

HSA_LCD_Shield: complete Class

Tobias Müller 6 years ago
parent
commit
d13c21d2b6
2 changed files with 481 additions and 39 deletions
  1. 453 21
      HSA_LCD_Shield.cpp
  2. 28 18
      HSA_LCD_Shield.h

+ 453 - 21
HSA_LCD_Shield.cpp

@@ -26,19 +26,19 @@
 void HSA_LCD_Shield::_sendMessage(byte* bytes, byte sizeBytes){
 
   // Small timeout before access the I²C-Bus
-  delayMicroseconds(100);
+  delayMicroseconds(DELAY_TRANS_US);
 
   // Start I²C transmission
   Wire.beginTransmission(this->__i2cAddress);
 
   // Send an array of Bytes over I²C, depending of its size
-  for(byte i = 0;i < sizeBytes;i++) Wire.write(bytes[i]);
+  for(byte i = 0x00; i < sizeBytes; i++) Wire.write(bytes[i]);
 
   // Stop I²C transmission
   Wire.endTransmission();
 
   // Small timeout after access the I²C-Bus
-  delayMicroseconds(100);
+  delayMicroseconds(DELAY_TRANS_US);
 
   // Exit function
   return;
@@ -51,7 +51,7 @@ void HSA_LCD_Shield::_sendMessage(byte* bytes, byte sizeBytes){
 
 ////////////////////               Constructor              ////////////////////
 
-HSA_LCD_Shield::HSA_LCD_Shield(char address, const char config[5]) {
+HSA_LCD_Shield::HSA_LCD_Shield(char address, const char config[0x05]) {
   
   // Save I²C-Address
   this->__i2cAddress = address;
@@ -77,7 +77,7 @@ HSA_LCD_Shield::HSA_LCD_Shield(char address) {
 
 }
 
-HSA_LCD_Shield::HSA_LCD_Shield(const char config[5]){
+HSA_LCD_Shield::HSA_LCD_Shield(const char config[0x05]){
 
   // Save I²C-Address
   this->__i2cAddress = I2C_ADDRESS;
@@ -105,15 +105,20 @@ HSA_LCD_Shield::HSA_LCD_Shield(){
 
 ////////////////////             Deconstructor              ////////////////////
 
-HSA_LCD_Shield::~HSA_LCD_Shield(void) {}
+HSA_LCD_Shield::~HSA_LCD_Shield(void) {
+
+  // Exit function
+  return;
+
+}
 
 ////////////////////          Start Configuration           ////////////////////
 
 bool HSA_LCD_Shield::begin(void) {
 
   // Check config-value and setup the LEDs, depending of config-value
-  if(strcmp(this->__config,CONFIG_L1B0) == 0 || 
-    strcmp(this->__config,CONFIG_L1B1) == 0) {
+  if(strcmp(this->__config,CONFIG_L1B0) == false || 
+    strcmp(this->__config,CONFIG_L1B1) == false) {
     
     // Setup LEDs
     pinMode(LED_GREEN,OUTPUT);
@@ -133,8 +138,8 @@ bool HSA_LCD_Shield::begin(void) {
   }
 
   // Check config-value and setup the Buttons depending of config-value
-  if(strcmp(this->__config,CONFIG_L0B1) == 0 || 
-    strcmp(this->__config,CONFIG_L1B1) == 0) {
+  if(strcmp(this->__config,CONFIG_L0B1) == false || 
+    strcmp(this->__config,CONFIG_L1B1) == false) {
     
     // Setup Buttons
     pinMode(BUTTON_UP,INPUT);
@@ -154,10 +159,10 @@ bool HSA_LCD_Shield::begin(void) {
   }
 
   // If config-value is unknown, quit configuration, otherwise configure I²C
-  if(strcmp(this->__config,CONFIG_L1B1) != 0 &&
-    strcmp(this->__config,CONFIG_L0B1) != 0 &&
-    strcmp(this->__config,CONFIG_L1B0) != 0 &&
-    strcmp(this->__config,CONFIG_L0B0) != 0) {
+  if(strcmp(this->__config,CONFIG_L1B1) != false &&
+    strcmp(this->__config,CONFIG_L0B1) != false &&
+    strcmp(this->__config,CONFIG_L1B0) != false &&
+    strcmp(this->__config,CONFIG_L0B0) != false) {
 
     // Store the value false for buttons and LEDs are not configured
     this->__buttons = false;
@@ -262,7 +267,7 @@ char HSA_LCD_Shield::getButton() {
      !digitalRead(BUTTON_DOWN) &&
      !digitalRead(BUTTON_LEFT) &&
      this->__buttons)
-     return 1;
+     return 0x01;
   
   // return 2, if only button "Right" is pressed and configured
   if(!digitalRead(BUTTON_UP) &&
@@ -270,7 +275,7 @@ char HSA_LCD_Shield::getButton() {
      !digitalRead(BUTTON_DOWN) &&
      !digitalRead(BUTTON_LEFT) &&
      this->__buttons)
-     return 2;
+     return 0x02;
 
   // return 3, if only button "Down" is pressed and configured
   if(!digitalRead(BUTTON_UP) &&
@@ -278,7 +283,7 @@ char HSA_LCD_Shield::getButton() {
      digitalRead(BUTTON_DOWN) &&
      !digitalRead(BUTTON_LEFT) &&
      this->__buttons)
-     return 3;
+     return 0x03;
 
   // return 4, if only button "Left" is pressed and configured
   if(!digitalRead(BUTTON_UP) &&
@@ -286,12 +291,439 @@ char HSA_LCD_Shield::getButton() {
      !digitalRead(BUTTON_DOWN) &&
      digitalRead(BUTTON_LEFT) &&
      this->__buttons)
-     return 4;
+     return 0x04;
 
   // return 0, if no button or more than one button is pressed
-  return 0;
+  return 0x00;
 
 }
 
-////////////////////          Get the pressed Button        ////////////////////
-////////////////////          Get the pressed Button        ////////////////////
+////////////////////                Write Row 1             ////////////////////
+
+void HSA_LCD_Shield::writeRow1(const char* bytes) {
+
+  // Define buffer for test Message
+  byte buffer[ARRAY_SIZE_ROW] = ""; 
+
+  // Define variable to store a boolean value, if a control-byte was found
+  bool controlByte = false; 
+
+  // Create data-array with message and send the message at the end
+  for(byte i = 0x00; i <= ARRAY_SIZE_ROW; i++){
+
+    // Set control-byte for changing parameter in the first iteration
+    if(i == 0x00) buffer[i] = CONTROL_BYTE_CB;
+
+    // Change the cursor position to row 1 in the second iteration
+    if(i == 0x01) buffer[i] = LCD_PARA_DIS_ROW1;
+
+    // Set control-byte for writing to the display in the third iteration
+    if(i == 0x02) buffer[i] = CONTROL_BYTE_DCB;
+
+    // Copy bytes into the buffer between iteration 2 to 13
+    if(i > 0x02 && i < ARRAY_SIZE_ROW) {
+
+      // If no control-byte was found, copy byte into buffer
+      if(!controlByte) {
+
+        // Check byte for control-byte, if not, copy byte into buffer
+        if(isControl(bytes[i - 0x03])) controlByte = true;
+        else buffer[i] = bytes[i - 0x03];
+
+      }
+
+      // If control-byte was found, fill message with free space
+      if(controlByte) {
+        
+        // Fill Buffer with free space
+        buffer[i] = FREE_SPACE;
+
+      }
+    }
+
+    // Send message in the last iteration
+    if(i == ARRAY_SIZE_ROW) _sendMessage(buffer,sizeof(buffer));
+
+  }
+
+  // Exit function
+  return;
+
+}
+
+////////////////////                Write Row 2             ////////////////////
+
+void HSA_LCD_Shield::writeRow2(const char* bytes) {
+
+  // Define buffer for test Message
+  byte buffer[ARRAY_SIZE_ROW] = ""; 
+
+  // Define variable to store a boolean value, if a control-byte was found
+  bool controlByte = false; 
+
+  // Create data-array with message and send the message at the end
+  for(byte i = 0x00; i <= ARRAY_SIZE_ROW; i++){
+
+    // Set control-byte for changing parameter in the first iteration
+    if(i == 0x00) buffer[i] = CONTROL_BYTE_CB;
+
+    // Change the cursor position to row 2 in the second iteration
+    if(i == 0x01) buffer[i] = LCD_PARA_DIS_ROW2;
+
+    // Set control-byte for writing to the display in the third iteration
+    if(i == 0x02) buffer[i] = CONTROL_BYTE_DCB;
+
+    // Copy bytes into the buffer between iteration 2 to 13
+    if(i > 0x02 && i < ARRAY_SIZE_ROW) {
+
+      // If no control-byte was found, copy byte into buffer
+      if(!controlByte) {
+
+        // Check byte for control-byte, if not, copy byte into buffer
+        if(isControl(bytes[i - 0x03])) controlByte = true;
+        else buffer[i] = bytes[i - 0x03];
+
+      }
+
+      // If control-byte was found, fill message with free space
+      if(controlByte) {
+        
+        // Fill Buffer with free space
+        buffer[i] = FREE_SPACE;
+
+      }
+    }
+
+    // Send message in the last iteration
+    if(i == ARRAY_SIZE_ROW) _sendMessage(buffer,sizeof(buffer));
+
+  }
+
+  // Exit function
+  return;
+
+}
+
+////////////////////                Write Row 3             ////////////////////
+
+void HSA_LCD_Shield::writeRow3(const char* bytes) {
+
+  // Define buffer for test Message
+  byte buffer[ARRAY_SIZE_ROW] = ""; 
+
+  // Define variable to store a boolean value, if a control-byte was found
+  bool controlByte = false; 
+
+  // Create data-array with message and send the message at the end
+  for(byte i = 0x00; i <= ARRAY_SIZE_ROW; i++){
+
+    // Set control-byte for changing parameter in the first iteration
+    if(i == 0x00) buffer[i] = CONTROL_BYTE_CB;
+
+    // Change the cursor position to row 3 in the second iteration
+    if(i == 0x01) buffer[i] = LCD_PARA_DIS_ROW3;
+
+    // Set control-byte for writing to the display in the third iteration
+    if(i == 0x02) buffer[i] = CONTROL_BYTE_DCB;
+
+    // Copy bytes into the buffer between iteration 2 to 13
+    if(i > 0x02 && i < ARRAY_SIZE_ROW) {
+
+      // If no control-byte was found, copy byte into buffer
+      if(!controlByte) {
+
+        // Check byte for control-byte
+        if(isControl(bytes[i - 0x03])) controlByte = true;
+
+        // If not, copy byte into buffer
+        else buffer[i] = bytes[i - 0x03];
+
+      }
+
+      // If control-byte was found, fill message with free space
+      if(controlByte) {
+        
+        // Fill Buffer with free space
+        buffer[i] = FREE_SPACE;
+
+      }
+    }
+
+    // Send message in the last iteration
+    if(i == ARRAY_SIZE_ROW) _sendMessage(buffer,sizeof(buffer));
+
+  }
+
+  // Exit function
+  return;
+}
+
+////////////////////                Write Row 4             ////////////////////
+
+void HSA_LCD_Shield::writeRow4(const char* bytes) {
+
+  // Define buffer for test Message
+  byte buffer[ARRAY_SIZE_ROW] = ""; 
+
+  // Define variable to store a boolean value, if a control-byte was found
+  bool controlByte = false; 
+
+  // Create data-array with message and send the message at the end
+  for(byte i = 0x00; i <= ARRAY_SIZE_ROW; i++){
+
+    // Set control-byte for changing parameter in the first iteration
+    if(i == 0x00) buffer[i] = CONTROL_BYTE_CB;
+
+    // Change the cursor position to row 4 in the second iteration
+    if(i == 0x01) buffer[i] = LCD_PARA_DIS_ROW4;
+
+    // Set control-byte for writing to the display in the third iteration
+    if(i == 0x02) buffer[i] = CONTROL_BYTE_DCB;
+
+    // Copy bytes into the buffer between iteration 2 to 13
+    if(i > 0x02 && i < ARRAY_SIZE_ROW) {
+
+      // If no control-byte was found, copy byte into buffer
+      if(!controlByte) {
+
+        // Check byte for control-byte
+        if(isControl(bytes[i - 0x03])) controlByte = true;
+
+        // if not, copy byte into buffer
+        else buffer[i] = bytes[i - 0x03];
+
+      }
+
+      // If control-byte was found, fill message with free space
+      if(controlByte) {
+        
+        // Fill Buffer with free space
+        buffer[i] = FREE_SPACE;
+
+      }
+    }
+
+    // Send message in the last iteration
+    if(i == ARRAY_SIZE_ROW) _sendMessage(buffer,sizeof(buffer));
+
+  }
+
+  // Exit function
+  return;
+
+}
+
+////////////////////                Write 4 Rows            ////////////////////
+
+void HSA_LCD_Shield::write4Rows(const char* bytes) {
+
+  // Define buffer for test Message
+  char buffer[ARRAY_SIZE_ROW] = ""; 
+
+  // Define variable to store the actual row position
+  byte rowPosition = LCD_PARA_DIS_ROW1; 
+
+  // Define variable, which count the copied bytes
+  byte countBytes = 0x00;
+
+  // Create data-array with messages for 4 rows and send the messages
+  for(byte i = 0x00; i < 0x28; i++){
+
+    // If the position of the row is not selected, skip following code
+    if(rowPosition == false) continue; 
+
+    // If first row is selected
+    if(rowPosition == LCD_PARA_DIS_ROW1) {
+
+      // If counted bytes has not reach max value, copy byte into buffer
+      if(countBytes < 0x0A) buffer[countBytes] = bytes[i];
+
+      // Check, if copied byte was control-byte
+      if(isControl(bytes[i])) {
+        
+        // Check if control-byte was \n or \r or end of string, 
+        // set counted bytes to max value
+        if(bytes[i] == BACKSLASH_N || 
+           bytes[i] == BACKSLASH_R ||
+           bytes[i] == STRING_END) 
+           countBytes = 0x0A;
+
+        // Check if control-byte was end of the string, deactivate row position
+        if(bytes[i] == STRING_END) rowPosition = false;
+        
+      }
+
+      // Otherwise increase counted bytes
+      else countBytes++;
+
+      // If count bytes is max value
+      if(countBytes == 0x0A) {
+
+        // Reset count bytes 
+        countBytes = 0x00;
+
+        // write first row
+        writeRow1(buffer);
+
+        // If row position is deactivated
+        if(rowPosition == false) {
+
+          // clear following rows
+          writeRow2(&bytes[i]);
+          writeRow3(&bytes[i]);
+          writeRow4(&bytes[i]);
+
+        }
+        
+        // Otherwise set row position the row 2
+        else rowPosition = LCD_PARA_DIS_ROW2;
+
+        // skip the rest of the code in this iteration
+        continue;
+
+      }
+    }
+
+    // If second row is selected
+    if(rowPosition == LCD_PARA_DIS_ROW2) {
+
+      // If counted bytes has not reach max value, copy byte into buffer
+      if(countBytes < 0x0A) buffer[countBytes] = bytes[i];
+
+      // Check, if copied byte was control-byte
+      if(isControl(bytes[i])) {
+        
+        // Check if control-byte was \n or \r or end of string, 
+        // set counted bytes to max value
+        if(bytes[i] == BACKSLASH_N || 
+           bytes[i] == BACKSLASH_R ||
+           bytes[i] == STRING_END) 
+           countBytes = 0x0A;
+
+        // Check if control-byte was end of the string, deactivate row position
+        if(bytes[i] == STRING_END) rowPosition = false;
+        
+      }
+
+      // Otherwise increase counted bytes
+      else countBytes++;
+
+      // If count bytes is max value
+      if(countBytes == 0x0A) {
+
+        // Reset count bytes 
+        countBytes = 0x00;
+
+        // write second row
+        writeRow2(buffer);
+
+        // If row position is deactivated
+        if(rowPosition == false) {
+
+          // clear following rows
+          writeRow3(&bytes[i]);
+          writeRow4(&bytes[i]);
+
+        }
+        
+        // Otherwise set row position the row 3
+        else rowPosition = LCD_PARA_DIS_ROW3;
+
+        // skip the rest of the code in this iteration
+        continue;
+
+      }
+    }
+
+    // If third row is selected
+    if(rowPosition == LCD_PARA_DIS_ROW3) {
+
+      // If counted bytes has not reach max value, copy byte into buffer
+      if(countBytes < 0x0A) buffer[countBytes] = bytes[i];
+
+      // Check, if copied byte was control-byte
+      if(isControl(bytes[i])) {
+        
+        // Check if control-byte was \n or \r or end of string, 
+        // set counted bytes to max value
+        if(bytes[i] == BACKSLASH_N || 
+           bytes[i] == BACKSLASH_R ||
+           bytes[i] == STRING_END) 
+           countBytes = 0x0A;
+
+        // Check if control-byte was end of the string, deactivate row position
+        if(bytes[i] == STRING_END) rowPosition = false;
+        
+      }
+
+      // Otherwise increase counted bytes
+      else countBytes++;
+
+      // If count bytes is max value
+      if(countBytes == 0x0A) {
+
+        // Reset count bytes 
+        countBytes = 0x00;
+
+        // write third row
+        writeRow3(buffer);
+
+        // If row position is deactivated, clear following row
+        if(rowPosition == false) writeRow4(&bytes[i]);
+
+        // Otherwise set row position the row 4
+        else rowPosition = LCD_PARA_DIS_ROW4;
+
+        // skip the rest of the code in this iteration
+        continue;
+
+      }
+    }
+
+    // If fourth row is selected
+    if(rowPosition == LCD_PARA_DIS_ROW4) {
+
+      // If counted bytes has not reach max value, copy byte into buffer
+      if(countBytes < 0x0A) buffer[countBytes] = bytes[i];
+
+      // Check, if copied byte was control-byte
+      if(isControl(bytes[i])) {
+        
+        // Check if control-byte was \n or \r or end of string, 
+        // set counted bytes to max value
+        if(bytes[i] == BACKSLASH_N || 
+           bytes[i] == BACKSLASH_R ||
+           bytes[i] == STRING_END) 
+           countBytes = 0x0A;
+
+        // Check if control-byte was end of the string, deactivate row position
+        if(bytes[i] == STRING_END) rowPosition = false;
+        
+      }
+
+      // Otherwise increase counted bytes
+      else countBytes++;
+
+      // If count bytes is max value
+      if(countBytes == 0x0A) {
+
+        // Reset count bytes 
+        countBytes = 0x00;
+
+        // write fourth row
+        writeRow4(buffer);
+
+        // If row position is not deactivated, deaktivate row position
+        if(rowPosition != false) rowPosition = false;
+
+        // skip the rest of the code in this iteration
+        continue;
+
+      }
+    }
+  }
+
+  // Exit function
+  return;
+
+}

+ 28 - 18
HSA_LCD_Shield.h

@@ -176,19 +176,29 @@
 #define FREE_SPACE 0x20
 #endif
 
-// Backslash 'n'
+// End of a String
+#ifndef STRING_END
+#define STRING_END 0x00
+#endif
+
+// Backslash n (\n)
 #ifndef BACKSLASH_N
 #define BACKSLASH_N 0x0A
 #endif
 
-// Backslash 'r'
+// Backslash r (\r)
 #ifndef BACKSLASH_R
 #define BACKSLASH_R 0x0D
 #endif
 
-// Backslash 'r'
-#ifndef STRING_END
-#define STRING_END 0x00
+// Delay before and after transmission in µs
+#ifndef DELAY_TRANS_US
+#define DELAY_TRANS_US 0x64
+#endif
+
+// Array size for writing a row
+#ifndef ARRAY_SIZE_ROW
+#define ARRAY_SIZE_ROW 0x0D
 #endif
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -217,7 +227,7 @@ protected:
 ////////////////////          protected Variables           ////////////////////
 
 char __i2cAddress;  //! @brief Store the I²C-Address of the LC-Display
-char __config[5];   //! @brief Store the Buttons/LEDs-Configuration
+char __config[0x05];//! @brief Store the Buttons/LEDs-Configuration
 bool __buttons;     //! @brief Store the value "true", if the buttons configured
 bool __leds;        //! @brief Store the value "true", if the LEDs configured
 
@@ -264,7 +274,7 @@ HSA_LCD_Shield(char address);
  * 
  * @param config Configuration of Buttons and LEDs 
  */
-HSA_LCD_Shield(const char config[5]);
+HSA_LCD_Shield(const char config[0x05]);
 
 /**
  * @brief Construct a new HSA_LCD_Shield object with different I²C-Address/Config
@@ -272,7 +282,7 @@ HSA_LCD_Shield(const char config[5]);
  * @param address Address of the LC-Display  
  * @param config Configuration of Buttons and LEDs 
  */
-HSA_LCD_Shield(char address,const char config[5]);
+HSA_LCD_Shield(char address,const char config[0x05]);
 
 ////////////////////             Deconstructor              ////////////////////
 
@@ -340,11 +350,11 @@ void controlLed(char ledPin, bool state);
 /**
  * @brief Return the number of the button, which is pressed
  * 
- * @return char 0, if no or more than one button is pressed
- * @return char 1, if only button "Up" is pressed
- * @return char 2, if only button "Right" is pressed
- * @return char 3, if only button "Down" is pressed
- * @return char 4, if only button "Left" is pressed
+ * @return char 0x00, if no or more than one button is pressed
+ * @return char 0x01, if only button "Up" is pressed
+ * @return char 0x02, if only button "Right" is pressed
+ * @return char 0x03, if only button "Down" is pressed
+ * @return char 0x04, if only button "Left" is pressed
  */
 char getButton();
 
@@ -355,7 +365,7 @@ char getButton();
  * 
  * @param bytes Array of bytes, which should be written to the first row.
  */
-void writeRow1(char* bytes);
+void writeRow1(const char* bytes);
 
 ////////////////////                Write Row 2             ////////////////////
 
@@ -364,7 +374,7 @@ void writeRow1(char* bytes);
  * 
  * @param bytes Array of bytes, which should be written to the second row.
  */
-void writeRow2(char* bytes);
+void writeRow2(const char* bytes);
 
 ////////////////////                Write Row 3             ////////////////////
 
@@ -373,7 +383,7 @@ void writeRow2(char* bytes);
  * 
  * @param bytes Array of bytes, which should be written to the third row.
  */
-void writeRow3(char* bytes);
+void writeRow3(const char* bytes);
 
 ////////////////////                Write Row 4             ////////////////////
 
@@ -382,7 +392,7 @@ void writeRow3(char* bytes);
  * 
  * @param bytes Array of bytes, which should be written to the fourth row.
  */
-void writeRow4(char* bytes);
+void writeRow4(const char* bytes);
 
 ////////////////////                Write 4 Rows            ////////////////////
 
@@ -391,7 +401,7 @@ void writeRow4(char* bytes);
  * 
  * @param bytes Array of bytes, which should be written to the four rows.
  */
-void write4Rows(char* bytes);
+void write4Rows(const char* bytes);
 
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////             close Header               ////////////////////