interrupt copy

Dependencies:   PinDetect Data_Clock_Pair Seeed_Chainable_LED

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003 
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010 
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013 
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 /* #ifdef PINDETECT_EXAMPLE_COMPILE */
00024 
00025 #include "mbed.h"
00026 #include "PinDetect.h"
00027 #include "SeeedChainableLED.h"
00028 
00029 PinDetect  pin ( PF_15 );
00030 DigitalOut chainStatus(LED2);
00031 DigitalOut pauseStatus(LED3);
00032 BusOut redLeds( PE_13, PF_14, PE_11, PE_9, PF_13 );
00033 BusOut notRedLeds ( PF_12, PA_3, PC_0, PC_3, PF_3 );
00034 DataClockPair iicChainLedsPins(PB_8, PB_9);
00035 SeeedChainableLED iicChainLeds(iicChainLedsPins);
00036 bool chainOn;
00037 bool pause = false;
00038 
00039 Thread statusThread;
00040 void blink() {
00041     for(DigitalOut led(LED1);; Thread::wait(1000) ) {
00042         led = !led;
00043     }
00044 }
00045 
00046 /*
00047  * Note, the PinDetect can be defined thus:-
00048  *     PinDetect pin( p21, PullDown );
00049  * This allows you to specify the DigitalIn pinmode
00050  * when you create the PinDetect object. This means
00051  * using pin.mode() later is then no longer required.
00052  */
00053 
00054 // C function callbacks follow.
00055 
00056 void keyPressed( void ) {
00057     if (!pause) {
00058         redLeds = ~redLeds;
00059         notRedLeds = ~notRedLeds;
00060         if(!chainOn) iicChainLeds.set_color_rgb(255, 50, 2);
00061         else iicChainLeds.set_color_rgb(0, 0, 0);
00062         chainOn = !chainOn;
00063         pause = true;
00064     }
00065 }
00066 
00067 /* void keyReleased( void ) { */
00068 /*     led2 = 0; */
00069 /*     led3 = 0; */
00070 /*     led4 = 0; */
00071 /* } */
00072 
00073 void keyPressedHeld( void ) {
00074     pause = false;
00075 }
00076 /* void keyReleasedHeld( void ) { */
00077 /*     pause = false; */
00078 /* } */
00079 
00080 // The main program.
00081 
00082 int main() {
00083 
00084     statusThread.start(callback(blink));
00085 
00086     iicChainLeds.set_color_rgb(0, 0, 0);
00087     chainOn = false;
00088     redLeds = 0;
00089     notRedLeds = ~0;
00090 
00091     pin.mode( PullDown );
00092     pin.attach_asserted( &keyPressed );
00093     pin.attach_asserted_held( &keyPressedHeld );
00094     pin.setSamplesTillAssert( 10 );
00095     pin.setSamplesTillHeld( 10000 );
00096     pin.setSampleFrequency(1000); // Defaults to 20ms.
00097 
00098     // This callback will often be of little use as it's
00099     // called after every assertion/deassertion. However,
00100     // it's provided for completeness. You may find a use
00101     // for it. If not, just don't attach a callback and it
00102     // will not activate.
00103     /* pin.attach_deasserted_held( &keyReleasedHeld ); */
00104 
00105     // You can define how many continuous samples must be
00106     // asserted before the attach_asserted() function is called.
00107     // This would mean 10 * 20ms debounce time = 200ms.
00108 
00109     // You can define how many continuous samples must be
00110     // asserted before the attach_asserted_held() function is called.
00111     // This would mean 200 * 20ms debounce time = 2seconds.
00112 
00113     // By default, "asserted" assumes the pin going high from 0volts to 5volts
00114     // and deasserted assumes going from 5volts to 0volts. You can invert this
00115     // logic so that going to 0volts is asserted and going to 5volts is deasserted
00116     // using this setup function:-
00117     //     pin.setAssertValue( 0 );
00118 
00119     // Sampling does NOT begin until you set the frequency. So, until
00120     // you call this function NO callbacks will be made. With no arguments
00121     // passed the default is 20000 microseconds (20ms). Specifiy the sampling
00122     // period in microseconds if you want a different value to 20ms.
00123     // For example, for a sampling period of 10ms do:-
00124     //     pin.setSampleFrequency( 10000 );
00125     // Note, if you change the sampling frequency you will probably also
00126     // want to change the number of samples till assert and held as show
00127     // above.
00128 
00129     for(;; Thread::wait(200) ) {
00130         chainStatus = chainOn;
00131         pauseStatus = pause;
00132     }
00133 }
00134 
00135 /* #endif */