interrupt copy
Dependencies: PinDetect Data_Clock_Pair Seeed_Chainable_LED
main.cpp@5:54e66ad3c78a, 2017-05-21 (annotated)
- Committer:
- tulanthoar
- Date:
- Sun May 21 13:02:36 2017 -0600
- Revision:
- 5:54e66ad3c78a
- Parent:
- 4:52eaedac3d65
add chainable LEDs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tulanthoar | 4:52eaedac3d65 | 1 | /* |
tulanthoar | 4:52eaedac3d65 | 2 | Copyright (c) 2010 Andy Kirkham |
tulanthoar | 5:54e66ad3c78a | 3 | |
tulanthoar | 4:52eaedac3d65 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
tulanthoar | 4:52eaedac3d65 | 5 | of this software and associated documentation files (the "Software"), to deal |
tulanthoar | 4:52eaedac3d65 | 6 | in the Software without restriction, including without limitation the rights |
tulanthoar | 4:52eaedac3d65 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
tulanthoar | 4:52eaedac3d65 | 8 | copies of the Software, and to permit persons to whom the Software is |
tulanthoar | 4:52eaedac3d65 | 9 | furnished to do so, subject to the following conditions: |
tulanthoar | 5:54e66ad3c78a | 10 | |
tulanthoar | 4:52eaedac3d65 | 11 | The above copyright notice and this permission notice shall be included in |
tulanthoar | 4:52eaedac3d65 | 12 | all copies or substantial portions of the Software. |
tulanthoar | 5:54e66ad3c78a | 13 | |
tulanthoar | 4:52eaedac3d65 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
tulanthoar | 4:52eaedac3d65 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
tulanthoar | 4:52eaedac3d65 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
tulanthoar | 4:52eaedac3d65 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
tulanthoar | 4:52eaedac3d65 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
tulanthoar | 4:52eaedac3d65 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
tulanthoar | 4:52eaedac3d65 | 20 | THE SOFTWARE. |
tulanthoar | 4:52eaedac3d65 | 21 | */ |
tulanthoar | 4:52eaedac3d65 | 22 | |
tulanthoar | 4:52eaedac3d65 | 23 | /* #ifdef PINDETECT_EXAMPLE_COMPILE */ |
tulanthoar | 4:52eaedac3d65 | 24 | |
tulanthoar | 4:52eaedac3d65 | 25 | #include "mbed.h" |
tulanthoar | 4:52eaedac3d65 | 26 | #include "PinDetect.h" |
tulanthoar | 5:54e66ad3c78a | 27 | #include "SeeedChainableLED.h" |
tulanthoar | 4:52eaedac3d65 | 28 | |
tulanthoar | 5:54e66ad3c78a | 29 | PinDetect pin ( PF_15 ); |
tulanthoar | 5:54e66ad3c78a | 30 | DigitalOut chainStatus(LED2); |
tulanthoar | 5:54e66ad3c78a | 31 | DigitalOut pauseStatus(LED3); |
tulanthoar | 5:54e66ad3c78a | 32 | BusOut redLeds( PE_13, PF_14, PE_11, PE_9, PF_13 ); |
tulanthoar | 5:54e66ad3c78a | 33 | BusOut notRedLeds ( PF_12, PA_3, PC_0, PC_3, PF_3 ); |
tulanthoar | 5:54e66ad3c78a | 34 | DataClockPair iicChainLedsPins(PB_8, PB_9); |
tulanthoar | 5:54e66ad3c78a | 35 | SeeedChainableLED iicChainLeds(iicChainLedsPins); |
tulanthoar | 5:54e66ad3c78a | 36 | bool chainOn; |
tulanthoar | 5:54e66ad3c78a | 37 | bool pause = false; |
tulanthoar | 5:54e66ad3c78a | 38 | |
tulanthoar | 5:54e66ad3c78a | 39 | Thread statusThread; |
tulanthoar | 5:54e66ad3c78a | 40 | void blink() { |
tulanthoar | 5:54e66ad3c78a | 41 | for(DigitalOut led(LED1);; Thread::wait(1000) ) { |
tulanthoar | 5:54e66ad3c78a | 42 | led = !led; |
tulanthoar | 5:54e66ad3c78a | 43 | } |
tulanthoar | 5:54e66ad3c78a | 44 | } |
tulanthoar | 4:52eaedac3d65 | 45 | |
tulanthoar | 4:52eaedac3d65 | 46 | /* |
tulanthoar | 4:52eaedac3d65 | 47 | * Note, the PinDetect can be defined thus:- |
tulanthoar | 4:52eaedac3d65 | 48 | * PinDetect pin( p21, PullDown ); |
tulanthoar | 4:52eaedac3d65 | 49 | * This allows you to specify the DigitalIn pinmode |
tulanthoar | 4:52eaedac3d65 | 50 | * when you create the PinDetect object. This means |
tulanthoar | 4:52eaedac3d65 | 51 | * using pin.mode() later is then no longer required. |
tulanthoar | 4:52eaedac3d65 | 52 | */ |
tulanthoar | 4:52eaedac3d65 | 53 | |
tulanthoar | 4:52eaedac3d65 | 54 | // C function callbacks follow. |
tulanthoar | 4:52eaedac3d65 | 55 | |
tulanthoar | 4:52eaedac3d65 | 56 | void keyPressed( void ) { |
tulanthoar | 5:54e66ad3c78a | 57 | if (!pause) { |
tulanthoar | 5:54e66ad3c78a | 58 | redLeds = ~redLeds; |
tulanthoar | 5:54e66ad3c78a | 59 | notRedLeds = ~notRedLeds; |
tulanthoar | 5:54e66ad3c78a | 60 | if(!chainOn) iicChainLeds.set_color_rgb(255, 50, 2); |
tulanthoar | 5:54e66ad3c78a | 61 | else iicChainLeds.set_color_rgb(0, 0, 0); |
tulanthoar | 5:54e66ad3c78a | 62 | chainOn = !chainOn; |
tulanthoar | 5:54e66ad3c78a | 63 | pause = true; |
tulanthoar | 5:54e66ad3c78a | 64 | } |
tulanthoar | 4:52eaedac3d65 | 65 | } |
tulanthoar | 4:52eaedac3d65 | 66 | |
tulanthoar | 5:54e66ad3c78a | 67 | /* void keyReleased( void ) { */ |
tulanthoar | 5:54e66ad3c78a | 68 | /* led2 = 0; */ |
tulanthoar | 5:54e66ad3c78a | 69 | /* led3 = 0; */ |
tulanthoar | 5:54e66ad3c78a | 70 | /* led4 = 0; */ |
tulanthoar | 5:54e66ad3c78a | 71 | /* } */ |
tulanthoar | 4:52eaedac3d65 | 72 | |
tulanthoar | 4:52eaedac3d65 | 73 | void keyPressedHeld( void ) { |
tulanthoar | 5:54e66ad3c78a | 74 | pause = false; |
tulanthoar | 4:52eaedac3d65 | 75 | } |
tulanthoar | 5:54e66ad3c78a | 76 | /* void keyReleasedHeld( void ) { */ |
tulanthoar | 5:54e66ad3c78a | 77 | /* pause = false; */ |
tulanthoar | 5:54e66ad3c78a | 78 | /* } */ |
tulanthoar | 4:52eaedac3d65 | 79 | |
tulanthoar | 4:52eaedac3d65 | 80 | // The main program. |
tulanthoar | 4:52eaedac3d65 | 81 | |
tulanthoar | 4:52eaedac3d65 | 82 | int main() { |
tulanthoar | 4:52eaedac3d65 | 83 | |
tulanthoar | 5:54e66ad3c78a | 84 | statusThread.start(callback(blink)); |
tulanthoar | 5:54e66ad3c78a | 85 | |
tulanthoar | 5:54e66ad3c78a | 86 | iicChainLeds.set_color_rgb(0, 0, 0); |
tulanthoar | 5:54e66ad3c78a | 87 | chainOn = false; |
tulanthoar | 5:54e66ad3c78a | 88 | redLeds = 0; |
tulanthoar | 5:54e66ad3c78a | 89 | notRedLeds = ~0; |
tulanthoar | 5:54e66ad3c78a | 90 | |
tulanthoar | 4:52eaedac3d65 | 91 | pin.mode( PullDown ); |
tulanthoar | 4:52eaedac3d65 | 92 | pin.attach_asserted( &keyPressed ); |
tulanthoar | 4:52eaedac3d65 | 93 | pin.attach_asserted_held( &keyPressedHeld ); |
tulanthoar | 5:54e66ad3c78a | 94 | pin.setSamplesTillAssert( 10 ); |
tulanthoar | 5:54e66ad3c78a | 95 | pin.setSamplesTillHeld( 10000 ); |
tulanthoar | 5:54e66ad3c78a | 96 | pin.setSampleFrequency(1000); // Defaults to 20ms. |
tulanthoar | 5:54e66ad3c78a | 97 | |
tulanthoar | 4:52eaedac3d65 | 98 | // This callback will often be of little use as it's |
tulanthoar | 4:52eaedac3d65 | 99 | // called after every assertion/deassertion. However, |
tulanthoar | 4:52eaedac3d65 | 100 | // it's provided for completeness. You may find a use |
tulanthoar | 4:52eaedac3d65 | 101 | // for it. If not, just don't attach a callback and it |
tulanthoar | 4:52eaedac3d65 | 102 | // will not activate. |
tulanthoar | 5:54e66ad3c78a | 103 | /* pin.attach_deasserted_held( &keyReleasedHeld ); */ |
tulanthoar | 5:54e66ad3c78a | 104 | |
tulanthoar | 4:52eaedac3d65 | 105 | // You can define how many continuous samples must be |
tulanthoar | 4:52eaedac3d65 | 106 | // asserted before the attach_asserted() function is called. |
tulanthoar | 4:52eaedac3d65 | 107 | // This would mean 10 * 20ms debounce time = 200ms. |
tulanthoar | 4:52eaedac3d65 | 108 | |
tulanthoar | 4:52eaedac3d65 | 109 | // You can define how many continuous samples must be |
tulanthoar | 4:52eaedac3d65 | 110 | // asserted before the attach_asserted_held() function is called. |
tulanthoar | 4:52eaedac3d65 | 111 | // This would mean 200 * 20ms debounce time = 2seconds. |
tulanthoar | 4:52eaedac3d65 | 112 | |
tulanthoar | 4:52eaedac3d65 | 113 | // By default, "asserted" assumes the pin going high from 0volts to 5volts |
tulanthoar | 4:52eaedac3d65 | 114 | // and deasserted assumes going from 5volts to 0volts. You can invert this |
tulanthoar | 4:52eaedac3d65 | 115 | // logic so that going to 0volts is asserted and going to 5volts is deasserted |
tulanthoar | 4:52eaedac3d65 | 116 | // using this setup function:- |
tulanthoar | 4:52eaedac3d65 | 117 | // pin.setAssertValue( 0 ); |
tulanthoar | 4:52eaedac3d65 | 118 | |
tulanthoar | 4:52eaedac3d65 | 119 | // Sampling does NOT begin until you set the frequency. So, until |
tulanthoar | 4:52eaedac3d65 | 120 | // you call this function NO callbacks will be made. With no arguments |
tulanthoar | 4:52eaedac3d65 | 121 | // passed the default is 20000 microseconds (20ms). Specifiy the sampling |
tulanthoar | 4:52eaedac3d65 | 122 | // period in microseconds if you want a different value to 20ms. |
tulanthoar | 4:52eaedac3d65 | 123 | // For example, for a sampling period of 10ms do:- |
tulanthoar | 4:52eaedac3d65 | 124 | // pin.setSampleFrequency( 10000 ); |
tulanthoar | 4:52eaedac3d65 | 125 | // Note, if you change the sampling frequency you will probably also |
tulanthoar | 4:52eaedac3d65 | 126 | // want to change the number of samples till assert and held as show |
tulanthoar | 4:52eaedac3d65 | 127 | // above. |
tulanthoar | 4:52eaedac3d65 | 128 | |
tulanthoar | 5:54e66ad3c78a | 129 | for(;; Thread::wait(200) ) { |
tulanthoar | 5:54e66ad3c78a | 130 | chainStatus = chainOn; |
tulanthoar | 5:54e66ad3c78a | 131 | pauseStatus = pause; |
tulanthoar | 4:52eaedac3d65 | 132 | } |
tulanthoar | 4:52eaedac3d65 | 133 | } |
tulanthoar | 4:52eaedac3d65 | 134 | |
tulanthoar | 4:52eaedac3d65 | 135 | /* #endif */ |