InteruptIn testing with ticker and uSerialDebug

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
prussell
Date:
Sun Dec 21 15:33:39 2014 +0000
Parent:
0:77bda1d2acca
Commit message:
All OK: Button Interrupts, LED PWM, USB Serial Debug.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 77bda1d2acca -r c6699649a160 main.cpp
--- a/main.cpp	Sat Dec 20 15:02:40 2014 +0000
+++ b/main.cpp	Sun Dec 21 15:33:39 2014 +0000
@@ -1,179 +1,78 @@
-//Simple testing of InterruptIn
+//Simple testing of buttons and inputs
+// "//a" = Alternate Good Test Code
 
-#define ENABLE_SerialUSB_DEBUG_CONSOLE                  1  //PR: Enable Debug on mbed's USB Serial Debug, Setup: Serial 9600,8,N,1,NoFlowControl (TeraTerm: http://en.sourceforge.jp/projects/ttssh2/releases/)
+#define ENABLE_uSerial 1 //PR: Enable Debug on mbed's USB Serial
 
 #include "mbed.h"
-//#include "PinDetect.h"                  // Pin Input and Button Interupts
 
 //==========Debug Console==========
-#if ENABLE_SerialUSB_DEBUG_CONSOLE
-    //Restart TeraTerm just before Pressing Reset on mbed, Default:9600-8N1(No Flow Control)
+#if ENABLE_uSerial
+    //Restart TeraTerm just before Pressing Reset on mbed, Default Serual=9600-8N1(No Flow Control)
     //Using default baud rate to avoid issues with DEBUG in a constructor being at default baud rate before main()
-    Serial  debug_serial(USBTX, USBRX); //PR: DebugSerialOverMbedUSB
-    #define DEBUG(...) { debug_serial.printf(__VA_ARGS__); }
+    //TeraTerm: http://en.sourceforge.jp/projects/ttssh2/releases/
+    Serial  debug_userial(USBTX, USBRX); //PR: DebugSerialOverMbedUSB
+    #define DEBUG(...) { debug_userial.printf(__VA_ARGS__); }
 #else
     #define DEBUG(...) //Do Nothing, DEBUG() lines are ignored
 #endif 
 
 //========== IO Hardware: Buttons, LEDs, PWM ==========
 // Inputs:
-//DigitalIn       bB1in(BUTTON1);       //if(bB1in){}
-DigitalIn       bB2in(BUTTON2);         //if(bB2in){}
-InterruptIn     B1int(BUTTON1);         //B1int.rise(&onB1rise);  
-InterruptIn     B2int(BUTTON2);
-//PinDetect       B1detect(BUTTON1);    //pin.mode( PullDown ); B1detect.attach_asserted( &onB1pressed )
-//PinDetect       B2detect(BUTTON2);
+DigitalIn       bB1in(BUTTON1); //if(bB1in){}
+DigitalIn       bB2in(BUTTON2); //if(bB2in){}
+InterruptIn     B1int(BUTTON1); //B1int.rise(&onB1rise);  
+InterruptIn     B2int(BUTTON2); //B1int.fall(&onB1fall);
 
 // Outputs:
-//DigitalOut  out_led1(LED1);             //PR: Firmware heartbeat
-//DigitalOut  out_led2(LED2);             //PR: Firmware heartbeat
-//DigitalOut      bL1out(LED1);
-DigitalOut      bL2out(LED2);
-PwmOut fL1pwm(LED1); float fL1level = 0.1; //PR: LED with PWM Brightness, and Initial level (0.0~1.0)
-//PwmOut fL2pwm(LED2); float fL2level = 0.1; //PR: LED with PWM Brightness, and Initial level (0.0~1.0)
+DigitalOut      bL1out(LED1);   // Direct LED1 drive
+//DigitalOut    bL2out(LED2);   // Direct LED2 drive
+//PwmOut fL1pwm(LED1); float fL1level = 0.1; // PWM LED1, brightness=float(0.0~1.0)
+PwmOut fL2pwm(LED2); float fL2level = 0.1; // PWM LED2, brightness=float(0.0~1.0)
 
-//==========Functions: onButton Callbacks==========
-// *When direct driving hardware may be better to cut power within the on to reduce response time
-volatile uint8_t uB1rise;
-void onB1rise(void){ uB1rise++; };   //Flag Event, Counter helps detect missed events since last cleared
-volatile uint8_t uB1fall;
-void onB1fall(void){ uB1fall++; };   //Flag Event, Counter helps detect missed events since last cleared
-uint8_t uB2rise;    //volatile?
-void onB2rise(void){ uB2rise++; };   //Flag Event, Counter helps detect missed events since last cleared
-uint8_t uB2fall;    //volatile?
-void onB2fall(void){ uB2fall++; };   //Flag Event, Counter helps detect missed events since last cleared
+//==========Functions: onButton Callbacks for InterruptIn for Buttons==========
+// *When direct driving hardware consider adjusting drive within the onCallback to reduce response time
+//TODO: Check need of volatile for changes in interrupts affecting variables in non-interrupt code?
+volatile uint8_t uB1rise; void onB1rise(void){ uB1rise++; };// Flag Event, Counter helps detect missed events since last cleared
+volatile uint8_t uB1fall; void onB1fall(void){ uB1fall++; };// Flag Event, Counter helps detect missed events since last cleared
+volatile uint8_t uB2rise; void onB2rise(void){ uB2rise++; };// Flag Event, Counter helps detect missed events since last cleared
+volatile uint8_t uB2fall; void onB2fall(void){ uB2fall++; };// Flag Event, Counter helps detect missed events since last cleared
 
 //==========Functions:Timer==========
 static volatile uint8_t uTicker1;   //Volatile, don't optimize, changes under interrupt control
-Ticker ticker1;                             //PR: Timer Object(Structure)
 void onTicker1(void)
 {
-    static uint32_t    u32_Counter; // Counter for Debug Output
-    fL1level+=0.2; if (fL1level>0.3){fL1level = 0.0;}; fL1pwm=fL1level;//PR: Ramp Blink
-    DEBUG("onT(%u) ", ++u32_Counter);
+    static uint32_t u32_Counter;
+    fL2level+=0.2; if (fL2level>0.5){fL2level = 0.0;}; fL2pwm=fL2level;//LED2=Ramp Blink
+    ++u32_Counter;
+    //DEBUG("onT(%u) ", u32_Counter); //Count of Ticker events to support long term timer evaluation
     uTicker1++;   //PR: Flag: count of Ticker1 Events since last cleared (Helps detect missed events).
 }
 
 //========== main ==========
-int main() {
-    
-    DEBUG("\n\nButtons\n");
-    
-    ticker1.attach(onTicker1, 5.0);       //PR: Timer Handler, Float=PeriodSeconds
+int main() 
+{  
+    DEBUG("\n\nButtonsV05 - Direct and using InterruptIn, LED1=Button1, LED2 is PWM\n");
+    DEBUG(" Built:[ %s %s ]  Compiler: [ %s ]  Version:[ %s ]\n", __DATE__, __TIME__, __VERSION__);
     
-    B1int.rise(&onB1rise);
-    B1int.fall(&onB1fall);
-    B2int.rise(&onB2rise);
-    B2int.fall(&onB2fall);
+    Ticker ticker1;                 //PR: Timer Object(Structure)
+    ticker1.attach(onTicker1, 0.5); //PR: Timer Handler, Float=PeriodSeconds
     
+    // Setup InterruptIn for Buttons
+    B1int.fall(&onB1fall);// Press   Button1
+    B1int.rise(&onB1rise);// Release Button1
+    B2int.fall(&onB2fall);// Press   Button2
+    B2int.rise(&onB2rise);// Release Button2
+      
     while(1) {
-        sleep();    // Wait for any interrupt, allow onCallback()
-
-        if(uTicker1){DEBUG("T%d\n", uTicker1); uTicker1=0;}
-        if(uB1rise){ DEBUG("B1r%d ", uB1rise); uB1rise=0; } // (*When direct driving hardware may be better to cut power within the on to reduce response time)
-        if(uB1fall){ DEBUG("B1f%d ", uB1fall); uB1fall=0; }
-        if(uB2rise){ DEBUG("B2r%d ", uB2rise); uB2rise=0; }
-        if(uB2fall){ DEBUG("B2f%d ", uB2fall); uB2fall=0; }
-        
-        //DEBUG(" Loop\n"); // This can trigger a Serial Interrupt
-
-        bL2out=bB2in;  //OK: Direct feedback when sampled - This only works with short wait(time)
-
-        //a led2out=B2in;  //OK: Direct feedback when sampled
-        //wait(0.2);      
+        sleep();        // Wait for any interrupt, allows registered onCallback() for timer/buttons/etc.
+        bL1out=bB1in;   //Direct LED1=Button1
+        //a if(uTicker1){DEBUG("T%d ",  uTicker1); uTicker1=0;}
+        if(uB1fall){ DEBUG("B1f%d ", uB1fall); uB1fall=0; } //PressB1
+        if(uB1rise){ DEBUG("B1r%d ", uB1rise); uB1rise=0; } //ReleaseB1
+        if(uB2fall){ DEBUG("B2f%d ", uB2fall); uB2fall=0; } //PressB2
+        //a if(uB2rise){ DEBUG("B2r%d\n",uB2rise); uB2rise=0; } //ReleaseB2 == Newline
+        if(uB2rise){ DEBUG("B2r%d\nTicker%04d ",uB2rise, uTicker1); uB2rise=0; uTicker1=0;} //ReleaseB2, +Ticks
+        //DEBUG(" Loop "); // This can trigger a Serial Interrupt leading to continuous waking from sleep()
     }
 }
-
 //========== end main.cpp ==========
-
-//========================
-#ifdef NOT_DEFINED //pindetect
-#include "mbed.h"
-#include "PinDetect.h"
-
-PinDetect  pin ( p21  );
-DigitalOut led1( LED1 );
-DigitalOut led2( LED2 );
-DigitalOut led3( LED3 );
-DigitalOut led4( LED4 );
-
-/*
- * Note, the PinDetect can be defined thus:-
- *     PinDetect pin( p21, PullDown );
- * This allows you to specify the DigitalIn pinmode
- * when you create the PinDetect object. This means
- * using pin.mode() later is then no longer required.
- */
-
-// C function ons follow.
-
-void keyPressed( void ) {
-    led2 = 1;
-    led3 = 0;
-    led4 = 0;
-}
-
-void keyReleased( void ) {
-    led2 = 0;
-    led3 = 0;
-    led4 = 0;
-}
-
-void keyPressedHeld( void ) {
-    led3 = 1;
-}
-
-void keyReleasedHeld( void ) {
-    led4 = 1;
-}
-
-// The main program.
-
-int main() {
-
-    pin.mode( PullDown );
-    pin.attach_asserted( &keyPressed );
-    pin.attach_deasserted( &keyReleased );
-    pin.attach_asserted_held( &keyPressedHeld );
-    
-    // This on will often be of little use as it's
-    // called after every assertion/deassertion. However,
-    // it's provided for completeness. You may find a use
-    // for it. If not, just don't attach a on and it
-    // will not activate.
-    pin.attach_deasserted_held( &keyReleasedHeld );
-    
-    // You can define how many continuous samples must be
-    // asserted before the attach_asserted() function is called.
-    //     pin.setSamplesTillAssert( 10 );
-    // This would mean 10 * 20ms debounce time = 200ms.
-
-    // You can define how many continuous samples must be
-    // asserted before the attach_asserted_held() function is called.
-    //     pin.setSamplesTillHeld( 200 );
-    // This would mean 200 * 20ms debounce time = 2seconds.
-
-    // By default, "asserted" assumes the pin going high from 0volts to 5volts
-    // and deasserted assumes going from 5volts to 0volts. You can invert this
-    // logic so that going to 0volts is asserted and going to 5volts is deasserted
-    // using this setup function:-
-    //     pin.setAssertValue( 0 );
-
-    // Sampling does NOT begin until you set the frequency. So, until
-    // you call this function NO ons will be made. With no arguments
-    // passed the default is 20000 microseconds (20ms). Specifiy the sampling
-    // period in microseconds if you want a different value to 20ms.
-    // For example, for a sampling period of 10ms do:-
-    //     pin.setSampleFrequency( 10000 );
-    // Note, if you change the sampling frequency you will probably also
-    // want to change the number of samples till assert and held as show
-    // above.
-    pin.setSampleFrequency(); // Defaults to 20ms.
-
-    while( 1 ) {
-        led1 = !led1;
-        wait( 0.2 );
-    }
-}
-
-#endif //pindetect
\ No newline at end of file