Browse Source

HSA_LCD_Shield: Functions for buttons and leds optimized

Tobias Müller 6 years ago
parent
commit
48c6ed176c
2 changed files with 181 additions and 56 deletions
  1. 141 55
      HSA_LCD_Shield.cpp
  2. 40 1
      HSA_LCD_Shield.h

+ 141 - 55
HSA_LCD_Shield.cpp

@@ -111,58 +111,86 @@ HSA_LCD_Shield::~HSA_LCD_Shield(void) {}
 
 bool HSA_LCD_Shield::begin(void) {
 
-// Check config-value and setup the Buttons and LEDs, depending of config-value
-if(strcmp(this->__config,CONFIG_L1B0) == 0 || 
-   strcmp(this->__config,CONFIG_L1B1) == 0) {
-  
-  // Setup LEDs
-  pinMode(LED_GREEN,OUTPUT);
-  pinMode(LED_RED,OUTPUT);
-  digitalWrite(LED_GREEN,HIGH);
-  digitalWrite(LED_RED,HIGH);
-  
-}
-if(strcmp(this->__config,CONFIG_L0B1) == 0 || 
-   strcmp(this->__config,CONFIG_L1B1) == 0) {
-  
-  // Setup Buttons
-  pinMode(BUTTON_UP,INPUT);
-  pinMode(BUTTON_RIGHT,INPUT);
-  pinMode(BUTTON_DOWN,INPUT);
-  pinMode(BUTTON_LEFT,INPUT);
-  
-}
-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 config-value unknown, exit function with negative feedback
-  return false;
-  
-}
-
-// Define an array of Bytes for configure the Display
-byte buffer[] = {CONTROL_BYTE_CB,
-                 LCD_PARA_FUNC1,
-                 LCD_PARA_EXT_FUNC,
-                 LCD_PARA_ENTRY_MODE,
-                 LCD_PARA_BIAS_SET,
-                 LCD_PARA_FUNC2,
-                 LCD_PARA_INT_OSC,
-                 LCD_PARA_FOL_CON,
-                 LCD_PARA_POW_CON,
-                 LCD_PARA_CONTRAST,
-                 LCD_PARA_FUNC3,
-                 LCD_PARA_DIS_CON,
-                 LCD_PARA_CLR_DIS};
-
-// Send array of bytes
-_sendMessage(buffer, sizeof(buffer));
-
-// Exit function with positive feedback
-return true;
-
+  // Check config-value and setup the LEDs, depending of config-value
+  if(strcmp(this->__config,CONFIG_L1B0) == 0 || 
+    strcmp(this->__config,CONFIG_L1B1) == 0) {
+    
+    // Setup LEDs
+    pinMode(LED_GREEN,OUTPUT);
+    pinMode(LED_RED,OUTPUT);
+    digitalWrite(LED_GREEN,HIGH);
+    digitalWrite(LED_RED,HIGH);
+
+    // Store the value "true", if LEDs are configured
+    this->__leds = true;
+    
+  }
+  else {
+    
+    // Store the value "false", if LEDs are not configured
+    this->__leds = false;
+
+  }
+
+  // Check config-value and setup the Buttons depending of config-value
+  if(strcmp(this->__config,CONFIG_L0B1) == 0 || 
+    strcmp(this->__config,CONFIG_L1B1) == 0) {
+    
+    // Setup Buttons
+    pinMode(BUTTON_UP,INPUT);
+    pinMode(BUTTON_RIGHT,INPUT);
+    pinMode(BUTTON_DOWN,INPUT);
+    pinMode(BUTTON_LEFT,INPUT);
+
+    // Store the value "true", if buttons are configured
+    this->__buttons = true;
+
+  }
+  else {
+    
+    // Store the value "false", if buttons are not configured
+    this->__buttons = false;
+    
+  }
+
+  // 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) {
+
+    // Store the value false for buttons and LEDs are not configured
+    this->__buttons = false;
+    this->__leds = false;
+
+    // If config-value unknown, exit function with negative feedback
+    return false;
+
+  }
+  else {
+
+    // Define an array of Bytes for configure the Display
+    byte buffer[] = {CONTROL_BYTE_CB,
+                     LCD_PARA_FUNC1,
+                     LCD_PARA_EXT_FUNC,
+                     LCD_PARA_ENTRY_MODE,
+                     LCD_PARA_BIAS_SET,
+                     LCD_PARA_FUNC2,
+                     LCD_PARA_INT_OSC,
+                     LCD_PARA_FOL_CON,
+                     LCD_PARA_POW_CON,
+                     LCD_PARA_CONTRAST,
+                     LCD_PARA_FUNC3,
+                     LCD_PARA_DIS_CON,
+                     LCD_PARA_CLR_DIS};
+
+    // Send array of bytes
+    _sendMessage(buffer, sizeof(buffer));
+
+    // Exit function with positive feedback
+    return true;
+
+  }
 }
 
 ////////////////////             LCD-Backlight              ////////////////////
@@ -198,14 +226,72 @@ void HSA_LCD_Shield::clearDisplay(void) {
 
 char* HSA_LCD_Shield::returnConfig(void) {
 
-  // return the configuration value
+  // Exit function and return the configuration value
   return this->__config;
+
 }
 
 ////////////////////            Return I²C-Address          ////////////////////
 
 char HSA_LCD_Shield::returnAddress(void){
 
-  // return the value of the I²C-Address
+  // Exit function and return the value of the I²C-Address
   return this->__i2cAddress;
-}
+
+}
+
+////////////////////               Control LEDs             ////////////////////
+
+void HSA_LCD_Shield::controlLed(char ledPin, bool state) {
+
+  // Change state of a LED, if they are configured
+  if(this->__leds) digitalWrite(ledPin,!state);
+
+  // Exit function
+  return;
+
+}
+
+////////////////////          Get the pressed Button        ////////////////////
+
+char HSA_LCD_Shield::getButton() {
+
+  // return 1, if only button "Up" is pressed and configured
+  if(digitalRead(BUTTON_UP) &&
+     !digitalRead(BUTTON_RIGHT) &&
+     !digitalRead(BUTTON_DOWN) &&
+     !digitalRead(BUTTON_LEFT) &&
+     this->__buttons)
+     return 1;
+  
+  // return 2, if only button "Right" is pressed and configured
+  if(!digitalRead(BUTTON_UP) &&
+     digitalRead(BUTTON_RIGHT) &&
+     !digitalRead(BUTTON_DOWN) &&
+     !digitalRead(BUTTON_LEFT) &&
+     this->__buttons)
+     return 2;
+
+  // return 3, if only button "Down" is pressed and configured
+  if(!digitalRead(BUTTON_UP) &&
+     !digitalRead(BUTTON_RIGHT) &&
+     digitalRead(BUTTON_DOWN) &&
+     !digitalRead(BUTTON_LEFT) &&
+     this->__buttons)
+     return 3;
+
+  // return 4, if only button "Left" is pressed and configured
+  if(!digitalRead(BUTTON_UP) &&
+     !digitalRead(BUTTON_RIGHT) &&
+     !digitalRead(BUTTON_DOWN) &&
+     digitalRead(BUTTON_LEFT) &&
+     this->__buttons)
+     return 4;
+
+  // return 0, if no button or more than one button is pressed
+  return 0;
+
+}
+
+////////////////////          Get the pressed Button        ////////////////////
+////////////////////          Get the pressed Button        ////////////////////

+ 40 - 1
HSA_LCD_Shield.h

@@ -173,9 +173,23 @@
 
 // Free Space 
 #ifndef FREE_SPACE
-#define FREE_SPACE ' '
+#define FREE_SPACE 0x20
 #endif
 
+// Backslash 'n'
+#ifndef BACKSLASH_N
+#define BACKSLASH_N 0x0A
+#endif
+
+// Backslash 'r'
+#ifndef BACKSLASH_R
+#define BACKSLASH_R 0x0D
+#endif
+
+// Backslash 'r'
+#ifndef STRING_END
+#define STRING_END 0x00
+#endif
 
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////         Include Header-File            ////////////////////
@@ -204,6 +218,8 @@ protected:
 
 char __i2cAddress;  //! @brief Store the I²C-Address of the LC-Display
 char __config[5];   //! @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
 
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////       Privat Functions/Variables       ////////////////////
@@ -309,6 +325,29 @@ char* returnConfig(void);
  */
 char returnAddress(void);
 
+////////////////////               Control LEDs             ////////////////////
+
+/**
+ * @brief Function to control the state of a LED
+ * 
+ * @param ledPin Pin-Number/Name of the LED
+ * @param state State of the LED
+ */
+void controlLed(char ledPin, bool state);
+
+////////////////////          Get the pressed Button        ////////////////////
+
+/**
+ * @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
+ */
+char getButton();
+
 ////////////////////////////////////////////////////////////////////////////////
 ////////////////////             close Header               ////////////////////
 ////////////////////////////////////////////////////////////////////////////////