Browse Source

Add acceleration level display

- Enable second display
- Add acceleration level display on second display
- Fix moment speed calculation, add pulse per cicle divider for
  speed pulse sensor.
Alexey Edelev 4 years ago
parent
commit
61dbd99706

+ 4 - 0
Arduino/eScooterControl/accelerationcontrol.cpp

@@ -26,6 +26,7 @@
 #include "accelerationcontrol.h"
 #include "pinconfig.h"
 #include "Wire.h"
+#include "display.h"
 
 const int AcceleratorSensorDivider = AcceleratorSensorDiff * AcceleratorSensorStep;
 
@@ -112,6 +113,9 @@ void AccelerationControl::updateAccelerationVoltage() {
     level = m_cruiseLevel;
   }
 
+  Display::instance()->drawAccelerationLevel(level);
+
+
   int expectedVoltage = round(float(0x0fff) / float(AcceleratorSensorStep) * float(level));
   Wire.beginTransmission(AcceleratorAddress);
   Wire.write(64);

+ 104 - 1
Arduino/eScooterControl/display.cpp

@@ -94,8 +94,90 @@ const PROGMEM byte Digit[10][5] = {
   }
 };
 
+const PROGMEM byte AcceleratorLevel[10][6] = {
+    {
+    B00000000,
+    B00000000,
+    B00000000,
+    B00000000,
+    B00000000,
+    B00000001
+  },{
+    B00000000,
+    B00000000,
+    B00000000,
+    B00000001,
+    B00000001,
+    B00000001
+  },{
+    B00000000,
+    B00000000,
+    B00000000,
+    B00000011,
+    B00000011,
+    B00000001
+  },{
+    B00000000,
+    B00000000,
+    B00000010,
+    B00000111,
+    B00000011,
+    B00000001
+  },{
+    B00000000,
+    B00000100,
+    B00000110,
+    B00000111,
+    B00000011,
+    B00000001
+  },{
+    B00000000,
+    B00001100,
+    B00001110,
+    B00000111,
+    B00000011,
+    B00000001
+  },{
+    B00000000,
+    B00011100,
+    B00011110,
+    B00000111,
+    B00000011,
+    B00000001
+  },{
+    B00000000,
+    B00111100,
+    B00111110,
+    B00000111,
+    B00000011,
+    B00000001
+  },{
+    B00000000,
+    B01111100,
+    B01111110,
+    B00000111,
+    B00000011,
+    B00000001
+  },{
+    B00000000,
+    B11111100,
+    B11111110,
+    B00000111,
+    B00000011,
+    B00000001
+  }
+};
+
 Display::Display() : mLedControl(DisplayDataPin, DisplayClkPin, DisplayCsPin,2)
  ,mDisplayBuffer{
+  B00000000,
+  B00000000,
+  B00000000,
+  B00000000,
+  B00000000,
+  B00000000,
+  B00000000,
+  B00000000,
   B00000000,
   B00000000,
   B00000000,
@@ -114,6 +196,22 @@ Display::Display() : mLedControl(DisplayDataPin, DisplayClkPin, DisplayCsPin,2)
   mLedControl.clearDisplay(1);  
 }
 
+void Display::drawAccelerationLevel(unsigned char level)
+{
+  if (level > 10) {
+    level = 10;
+  }
+  
+  for (int i = 0; i < 6; i++) {
+    mDisplayBuffer[8 + i] = 0;
+    if (level > 0) {
+      byte value = pgm_read_byte(&(AcceleratorLevel[level - 1][i]));
+      mDisplayBuffer[8 + i] = value;
+    }
+  }
+}
+
+
 void Display::drawSpeed(unsigned char number)
 {
   if (number > 99) {
@@ -126,7 +224,7 @@ void Display::drawSpeed(unsigned char number)
     mDisplayBuffer[i] = 0;
     byte tensDigit = pgm_read_byte(&(Digit[tens][i]));
     byte onesDigit = pgm_read_byte(&(Digit[ones][i]));
-    mDisplayBuffer[i] |= (tensDigit | onesDigit << 4);
+    mDisplayBuffer[i] = (tensDigit | onesDigit << 4);
   }
 }
 
@@ -151,4 +249,9 @@ void Display::updateDisplayBuffer()
   for(int i = 0; i < 8; i++) {
     mLedControl.setColumn(1, i, mDisplayBuffer[i]);    
   }
+
+  
+  for(int i = 0; i < 8; i++) {
+    mLedControl.setColumn(0, i, mDisplayBuffer[i + 8]);    
+  }
 }

+ 2 - 1
Arduino/eScooterControl/display.h

@@ -31,6 +31,7 @@
 class Display : public Singleton<Display>
 {
 public:
+  void drawAccelerationLevel(unsigned char level);
   void drawSpeed(unsigned char number);
   void drawBatteryLevel(unsigned char level);
   void updateDisplayBuffer();
@@ -39,6 +40,6 @@ private:
   Display();
   friend class Singleton;
 
-  byte mDisplayBuffer[8];
+  byte mDisplayBuffer[16];
   LedControl mLedControl;
 };

+ 1 - 1
Arduino/eScooterControl/eScooterControl.ino

@@ -29,7 +29,7 @@
 
 /* we always wait a bit between updates of the display */
 const unsigned long AcceleratorPedalUpdateTime = 200;
-const unsigned long DisplayUpdateTime = 500;
+const unsigned long DisplayUpdateTime = 250;
 const unsigned long BatteryUpdateTime = 1000;
 
 Thread gDisplayThread = Thread(DisplayUpdateTime);

+ 2 - 0
Arduino/eScooterControl/pinconfig.h

@@ -31,6 +31,8 @@
 #endif
 
 const int HallSensorPin = 2;
+const int PulsePerCircle = 45;
+
 const int StopSensorPin = 3;
 
 const int DisplayDataPin = 10;

+ 10 - 6
Arduino/eScooterControl/speedometer.cpp

@@ -56,11 +56,15 @@ void Speedometer::attachToDisplay(Display *display) {
 void Speedometer::incrementHallCounter()
 {
   mHallCounter++;
-  unsigned long currentTime = millis();
-  mMomentSpeed = TireLengthCoef/(currentTime - mLastHallTime);
-  mLastHallTime = currentTime;
-
-  if (mDisplay != nullptr) {
-    mDisplay->drawSpeed(mMomentSpeed);
+  int currentTick = mHallCounter % PulsePerCircle;
+  if (currentTick == 0) {
+    mHallCounter = 0;
+    unsigned long currentTime = millis();
+    mMomentSpeed = TireLengthCoef / (currentTime - mLastHallTime);
+    mLastHallTime = currentTime;
+  
+    if (mDisplay != nullptr) {
+      mDisplay->drawSpeed(mMomentSpeed);
+    }
   }
 }