InteruptIn testing with ticker and uSerialDebug

Dependencies:   mbed

main.cpp

Committer:
prussell
Date:
2014-12-20
Revision:
0:77bda1d2acca
Child:
1:c6699649a160

File content as of revision 0:77bda1d2acca:

//Simple testing of InterruptIn

#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/)

#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)
    //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__); }
#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);

// 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)

//==========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: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);
    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
    
    B1int.rise(&onB1rise);
    B1int.fall(&onB1fall);
    B2int.rise(&onB2rise);
    B2int.fall(&onB2fall);
    
    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);      
    }
}

//========== 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