Creating a CoxBox with the mbed

Dependencies:   mbed C12832 SMARTGPU2

Files at this revision

API Documentation at this revision

Comitter:
jad19925
Date:
Mon May 05 23:18:21 2014 +0000
Parent:
13:2eb614f52fa6
Commit message:
Updated all code to merge in LCD still needs some work to fix flicker on the time,but otherwise it is updating much smoother. Can't use interrupts for LCD as it is too slow, but everything else is interrupt driven so it is ok to have an infinite loop

Changed in this revision

TimerCB.cpp Show annotated file Show diff for this revision Revisions of this file
TimerCB.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/TimerCB.cpp	Mon May 05 19:43:56 2014 +0000
+++ b/TimerCB.cpp	Mon May 05 23:18:21 2014 +0000
@@ -3,7 +3,7 @@
 #include "TimerCB.h"
 
 TimerCB::TimerCB(){
-    
+    running = false;
 }
 
 int TimerCB::getMinutes(){
@@ -26,13 +26,20 @@
 
 void TimerCB::start(){
     timer.start();
+    running = true;
 }
 
 void TimerCB::stop(){
     timer.stop();
+    running = false;
 }
 
 void TimerCB::reset(){
     timer.reset();
     timer.stop();
+    running = false;
+}
+
+bool TimerCB::isRunning(){
+    return running;
 }
\ No newline at end of file
--- a/TimerCB.h	Mon May 05 19:43:56 2014 +0000
+++ b/TimerCB.h	Mon May 05 23:18:21 2014 +0000
@@ -6,6 +6,7 @@
 class TimerCB{
 private:
     Timer timer;
+    bool running;
     
 public:
     TimerCB();
@@ -15,6 +16,7 @@
     void start();
     void stop();
     void reset();
+    bool isRunning();
 };
 
 #endif /* defined(__TimerCB__) */
\ No newline at end of file
--- a/main.cpp	Mon May 05 19:43:56 2014 +0000
+++ b/main.cpp	Mon May 05 23:18:21 2014 +0000
@@ -1,65 +1,125 @@
 #include "mbed.h"
 #include "C12832.h"
+#include "SMARTGPU2.h"
 #include "HallEffect.h"
-#include "SMARTGPU2.h"
+#include "TimerCB.h"
 
 AnalogIn Ain(p17);
 AnalogOut Aout(p18);
 Ticker s20khz_tick;
 Ticker s1hz_tick;
-C12832 lcd(p5, p7, p6, p8, p11);
+Ticker s30hz_tick;
+C12832 lcdX(p5, p7, p6, p8, p11);
+SMARTGPU2 lcd(TXPIN, RXPIN, RESETPIN);
 InterruptIn interrupt(p9);
 
+//DigitalIn joyDown(p12);
+//DigitalIn joyRight(p16);
+InterruptIn startStop(p12); //joystick down
+InterruptIn reset(p16); //joystick right
+
 void s20khz_task(void);
 void s1hz_task(void);
+void screenRefresh(void);
 void hallISR(void);
+void startStopTimer(void);
+void resetTimer(void);
 
 float data_in, data_out;
 HallEffect hall;
+TimerCB timerCB;
 
+//cache data for fixing LCD flicker
+int lastCount;
+int lastSPM;
+int lastMin;
+int lastSec;
+int lastMS;
+
+void lcdInit() {
+    lcd.reset();
+    lcd.start();
+    lcd.erase();
+    lastCount = -1;
+    lastSPM = -1;
+    lastMin = -1;
+    lastSec = -1;
+    lastMS = -1;
+}
 
 void drawOut()  {
-   NUMBEROFBYTES bytes;
-   float strokes;
-   float time;
-   float SPM; 
-   int temp;  
-  lcd.baudChange(BAUD7);             //set a fast baud! for fast drawing
-  //config strings
-  lcd.setTextSize(FONT8);  
-  strokes = getStrokeCount(); 
-  SPM = getSPM();
-  if (strokes < 10) {      lcd.printNumber(100,50,strokes);  }
-  else if(strokes < 100) {     lcd.printNumber(75,50,strokes);  }  
-  else  {     lcd.printNumber(50,50,strokes);  }
-  time = getMin();
-  if (time < 10)  {      lcd.printNumber(100,200,time);  }
-  else  {      lcd.printNumber(50,200,time);  }  
-  time = getSec();
-  if (time < 10)  {      lcd.printNumber(200,200,time);  }
-  else  {      lcd.printNumber(175,200,time);  } 
-  temp = getMS();
-  temp = temp/10;
-  time = temp;
-  if (time < 10)  {      lcd.printNumber(325,200,time);  }
-  else  {      lcd.printNumber(285,200,time);  }  
-  if (SPM < 10)  {      lcd.printNumber(325,50,SPM);  }
-  else  {      lcd.printNumber(300,50,SPM);  }    
-  lcd.string(LCD_WIDTH/8+90,LCD_HEIGHT/4+120,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,":",&bytes); 
-  lcd.string(LCD_WIDTH/8+210,LCD_HEIGHT/4+120,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,".",&bytes);        
-  lcd.setTextSize(FONT4);
-  lcd.string(LCD_WIDTH/8,LCD_HEIGHT/4+70,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,"Time",&bytes); 
-  lcd.string(LCD_WIDTH/8,LCD_HEIGHT/4-60,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,"Count",&bytes);    
-  lcd.string(LCD_WIDTH/8+240,LCD_HEIGHT/4-60,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,"SPM",&bytes);    
-  }
+    NUMBEROFBYTES bytes;
+    //config strings
+    lcd.setTextSize(FONT8);
+    //high baud rate actually slows the system down
+//    lcd.baudChange(BAUD7);             //set a fast baud! for fast drawing
+    
+    int spm = hall.getSPM();
+    if(spm != lastSPM){
+        lastSPM = spm;
+        //draw black box over where rating is to clear it for redrawing
+        lcd.drawRectangle(50,50,240,120,BLACK,FILL);
+        //stroke rating using snprintf
+        char spmBuffer[3];
+        snprintf(spmBuffer, 3, "%02d", spm);
+        lcd.string(50,50,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,spmBuffer,&bytes);
+    }
+    
+    int count = hall.getCount();
+    if(count != lastCount){
+        lastCount = count;
+        //draw black box over where count is to clear it for redrawing
+        lcd.drawRectangle(300,50,MAX_X_LANDSCAPE,120,BLACK,FILL);
+        //stroke count using snprintf
+        char strokesBuffer[4];
+        snprintf(strokesBuffer, 4, "%03d", count);
+        lcd.string(300,50,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,strokesBuffer,&bytes);
+    }
+    
+    /*
+        TODO: make intelligent to only update (draw box):
+        minutes if minutes changed
+        seconds if sectonds changed
+        ms if ms changed
+        
+        Adapt to number of ones present in higher places
+    */
+    
+    int tenthSec = timerCB.getMS()/100;
+    if(tenthSec != lastMS){
+        lastMS = tenthSec;
+        //draw black box over where the time is to clear it for redrawing
+        lcd.drawRectangle(50,200,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,BLACK,FILL);
+        //time using snprintf
+        char timeBuffer[8];
+        snprintf(timeBuffer, 8, "%02d:%02d.%d",timerCB.getMinutes(),timerCB.getSeconds(),tenthSec);
+        lcd.string(50,200,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,timeBuffer,&bytes);
+    }
+    
+    //print labels
+    lcd.setTextSize(FONT4);
+    lcd.string(50,170,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,"Time",&bytes); 
+    lcd.string(300,20,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,"Count",&bytes);    
+    lcd.string(50,20,MAX_X_LANDSCAPE,MAX_Y_LANDSCAPE,"SPM",&bytes);
+}
 
 int main(){
-    lcd.printf("Audio In");
+//    lcd.printf("Audio In");
+    //initialize big LCD
+    lcdInit();
+    
     s20khz_tick.attach_us(&s20khz_task,50);
-    s1hz_tick.attach(&s1hz_task,.5);
-//    HallEffect hall(interrupt);
-    interrupt.rise(hallISR);
-    lcd.cls();
+    //refresh app board lcd at .5hz
+//    s1hz_tick.attach(&s1hz_task,.5);
+    //refresh screen at 30hz
+//    s30hz_tick.attach(&screenRefresh,.5);
+
+    interrupt.rise(&hallISR);
+    startStop.rise(&startStopTimer);
+    reset.rise(&resetTimer);
+    while(true){
+      drawOut();
+    }
 }
 
 void s20khz_task(void) {
@@ -69,11 +129,33 @@
 }
 
 void s1hz_task(void) {
-    lcd.cls();
-    lcd.locate(0,0);
-    lcd.printf("StrokeCount: %d\r\nStroke Rating: %d", hall.getCount(), hall.getSPM());
+    lcdX.cls();
+    lcdX.locate(0,0);
+    lcdX.printf("StrokeCount: %d\r\nStroke Rating: %d\n", hall.getCount(), hall.getSPM());
+    lcdX.printf("time: %02d:%02d:%02d",timerCB.getMinutes(),timerCB.getSeconds(),(timerCB.getMS()/10));
+}
+
+void screenRefresh(void) {
+    drawOut();
 }
 
 void hallISR() {
     hall.recordStroke();
+    if(!timerCB.isRunning()){
+        timerCB.start();
+    }
+}
+
+void startStopTimer() {
+    if(timerCB.isRunning()){
+        timerCB.stop();
+    }
+    else {
+        timerCB.start();
+    }
+}
+
+void resetTimer() {
+    timerCB.reset();
+    hall.resetCount();
 }
\ No newline at end of file