Browse Source

Add new display features

- Optimize display update
- Add light, eco and stop signs
- Rework display of battery level
- Move acceleration level up
Alexey Edelev 4 years ago
parent
commit
927a795876

+ 1 - 0
Arduino/eScooterControl/accelerationcontrol.cpp

@@ -97,6 +97,7 @@ void AccelerationControl::dispatch()
 
 void AccelerationControl::stop() {
   m_stopState = digitalRead(StopSensorPin);
+  Display::instance()->setStop(!m_stopState);
   if (m_stopState == LOW) {
     m_cruiseLevel = 0; //reset cruise when stop
     setAccelerationLevel(0);

+ 3 - 0
Arduino/eScooterControl/buttoncontrol.cpp

@@ -25,6 +25,7 @@
 
 #include "buttoncontrol.h"
 #include "pinconfig.h"
+#include "display.h"
 
 const unsigned long ReactionTime = 700;
 
@@ -81,5 +82,7 @@ void ButtonControl::toggleLedState()
     m_ledState = HIGH;
   }
 
+  Display::instance()->setLightOn(m_ledState);
+
   digitalWrite(LedPin, m_ledState);
 }

+ 80 - 14
Arduino/eScooterControl/display.cpp

@@ -28,8 +28,6 @@
 
 #include <avr/pgmspace.h>
 
-const PROGMEM byte BatteryLevel = B00000010;
-
 const PROGMEM byte Digit[10][5] = {
   {
     B00000111,
@@ -168,6 +166,33 @@ const PROGMEM byte AcceleratorLevel[10][6] = {
   }
 };
 
+const PROGMEM byte StopSign[8] = {
+    B00011000,
+    B00011000,
+    B00011000,
+    B00011000,
+    B00011000,
+    B00000000,
+    B00011000,
+    B00011000
+};
+
+const PROGMEM byte LampSign[5] = {
+    B00000000,
+    B00000000,
+    B01000000,
+    B11100000,
+    B01000000
+};
+
+const PROGMEM byte EcoSign[5] = {
+    B00000000,
+    B00000000,
+    B00001100,
+    B00001100,
+    B00000100
+};
+
 Display::Display() : mLedControl(DisplayDataPin, DisplayClkPin, DisplayCsPin,2)
  ,mDisplayBuffer{
   B00000000,
@@ -186,9 +211,13 @@ Display::Display() : mLedControl(DisplayDataPin, DisplayClkPin, DisplayCsPin,2)
   B00000000,
   B00000000,
   B00000000}
+  , m_updateRequested(true)
   , m_acceleration(0)
   , m_battery(0)
   , m_speed(0)
+  , m_stop(false)
+  , m_lightOn(false)
+  , m_eco(false)
 {
   mLedControl.shutdown(0, false);
   mLedControl.setIntensity(0, 0);
@@ -206,14 +235,13 @@ void Display::drawAccelerationLevel()
   }
   
   for (byte i = 0; i < 6; ++i) {
-    mDisplayBuffer[8 + i] = 0;
+    mDisplayBuffer[5 + i] = 0;
     if (m_acceleration > 0) {
-      mDisplayBuffer[8 + i] = pgm_read_byte(&(AcceleratorLevel[m_acceleration - 1][i]));
+      mDisplayBuffer[5 + i] = pgm_read_byte(&(AcceleratorLevel[m_acceleration - 1][i]));
     }
   }
 }
 
-
 void Display::drawSpeed()
 {
   if (m_speed > 99) {
@@ -232,25 +260,63 @@ void Display::drawSpeed()
 
 void Display::drawBatteryLevel()
 { 
-  if (m_battery > 5) {
-    m_battery = 5;
+  if (m_battery > 6) {
+    m_battery = 6;
   }
 
-  mDisplayBuffer[6] = 0;
-  mDisplayBuffer[7] = 0;
+  mDisplayBuffer[15] = 0;
+  for (byte i = 1; i < (m_battery + 1); ++i) {
+    mDisplayBuffer[15] |= 1 << i;
+  }
+}
 
-  byte batteryLevelChar = pgm_read_byte(&(BatteryLevel));
-  for (byte i = 0; i < m_battery; ++i) {
-    mDisplayBuffer[6] |= batteryLevelChar << i;
-    mDisplayBuffer[7] |= batteryLevelChar << (i + 1);
+void Display::drawLampSign()
+{
+  for (byte i = 0; i < 5; ++i) {
+    if (m_lightOn) {
+      mDisplayBuffer[9 + i] |= pgm_read_byte(&(LampSign[i]));
+    } else {
+      mDisplayBuffer[9 + i] &= ~pgm_read_byte(&(LampSign[i]));      
+    }
+  }
+}
+
+void Display::drawEcoSign()
+{
+  for (byte i = 0; i < 5; ++i) {
+    if (m_eco) {
+      mDisplayBuffer[9 + i] |= pgm_read_byte(&(EcoSign[i]));
+    } else {
+      mDisplayBuffer[9 + i] &= ~pgm_read_byte(&(EcoSign[i]));      
+    }
   }
 }
 
+void Display::drawStopSign()
+{
+  for (byte i = 0; i < 8; ++i) {
+    mDisplayBuffer[6 + i] = pgm_read_byte(&(StopSign[i]));
+  }  
+}
+
 void Display::updateDisplayBuffer()
 {
-  drawAccelerationLevel();
+  if (!m_updateRequested) {
+    return;
+  }
+  m_updateRequested = false;
+
   drawSpeed();
   drawBatteryLevel();
+
+  if (!m_stop) {
+    drawAccelerationLevel();
+    drawLampSign();
+    drawEcoSign();
+  } else {
+    drawStopSign();
+  }
+  
   for(byte i = 0; i < 8; ++i) {
     mLedControl.setColumn(1, i, mDisplayBuffer[i]);    
     mLedControl.setColumn(0, i, mDisplayBuffer[i + 8]);    

+ 31 - 4
Arduino/eScooterControl/display.h

@@ -34,15 +34,35 @@ public:
   void updateDisplayBuffer();
 
   void setAcceleration(byte level) {
-    m_acceleration = level;
+    updateValue(m_acceleration, level);
   }
 
   void setSpeed(byte speed) {
-    m_speed = speed;
+    updateValue(m_speed, speed);
   }
 
   void setBatteryLevel(byte level) {
-    m_battery = level;
+    updateValue(m_battery, level);
+  }
+
+  void setStop(bool stop) {
+    updateValue(m_stop, stop);
+  }
+
+  void setLightOn(bool lightOn) {
+    updateValue(m_lightOn, lightOn);
+  }
+
+  void setEco(bool eco) {
+    updateValue(m_eco, eco);
+  }
+
+  template<typename T>
+  void updateValue(T &value, const T newValue) {
+    if (value != newValue) {
+      value = newValue;
+      m_updateRequested = true;      
+    }
   }
 
 private:
@@ -52,11 +72,18 @@ private:
   void drawAccelerationLevel();
   void drawSpeed();
   void drawBatteryLevel();
-  
+  void drawLampSign();
+  void drawEcoSign();
+  void drawStopSign();
+
   byte mDisplayBuffer[16];
   LedControl mLedControl;
+  bool m_updateRequested;
 
   byte m_acceleration;
   byte m_battery;
   byte m_speed;
+  bool m_stop;
+  bool m_lightOn;
+  bool m_eco;
 };

+ 1 - 1
Arduino/eScooterControl/eScooterControl.ino

@@ -68,7 +68,7 @@ void setup() {
   gBatteryThread.assignCallback([](){
     Display::instance()->setBatteryLevel(fakeBatteryLevel);
     fakeBatteryLevel++;
-    if (fakeBatteryLevel > 5) {
+    if (fakeBatteryLevel > 6) {
       fakeBatteryLevel = 0;
     }
   });