This is a souped up version of the basic mbed blinky, as a 2nd stepping stone up from the basic blinky and also because the documentation doesn't seem to really be that complete.
Fork of mbed_quadBlinky by
main.cpp@1:f4e48249b980, 2017-10-03 (annotated)
- Committer:
- shutay
- Date:
- Tue Oct 03 17:00:33 2017 +0000
- Revision:
- 1:f4e48249b980
- Parent:
- 0:efe13d6feef8
Added more comments for beginners.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shutay | 0:efe13d6feef8 | 1 | #include "mbed.h" |
shutay | 0:efe13d6feef8 | 2 | |
shutay | 1:f4e48249b980 | 3 | // You can write out debug strings via the mbed interface and the built-in mini-USB socket. |
shutay | 0:efe13d6feef8 | 4 | Serial pc(USBTX, USBRX); |
shutay | 0:efe13d6feef8 | 5 | |
shutay | 1:f4e48249b980 | 6 | // All 4 built-in LEDs defined. |
shutay | 0:efe13d6feef8 | 7 | DigitalOut led1(LED1); |
shutay | 0:efe13d6feef8 | 8 | DigitalOut led2(LED2); |
shutay | 0:efe13d6feef8 | 9 | DigitalOut led3(LED3); |
shutay | 0:efe13d6feef8 | 10 | DigitalOut led4(LED4); |
shutay | 0:efe13d6feef8 | 11 | |
shutay | 1:f4e48249b980 | 12 | // Example initialisation function |
shutay | 0:efe13d6feef8 | 13 | void initialise() |
shutay | 0:efe13d6feef8 | 14 | { |
shutay | 0:efe13d6feef8 | 15 | // Baud rate not required over USB. |
shutay | 0:efe13d6feef8 | 16 | led1 = 1; |
shutay | 0:efe13d6feef8 | 17 | led2 = 1; |
shutay | 0:efe13d6feef8 | 18 | led3 = 1; |
shutay | 0:efe13d6feef8 | 19 | led4 = 1; |
shutay | 0:efe13d6feef8 | 20 | wait(1.0f); |
shutay | 0:efe13d6feef8 | 21 | //*==========================================* |
shutay | 0:efe13d6feef8 | 22 | // Do initialisation here |
shutay | 0:efe13d6feef8 | 23 | //*==========================================* |
shutay | 0:efe13d6feef8 | 24 | |
shutay | 0:efe13d6feef8 | 25 | //*==========================================* |
shutay | 0:efe13d6feef8 | 26 | led1 = 0; |
shutay | 0:efe13d6feef8 | 27 | led2 = 0; |
shutay | 0:efe13d6feef8 | 28 | led3 = 0; |
shutay | 0:efe13d6feef8 | 29 | led4 = 0; |
shutay | 0:efe13d6feef8 | 30 | wait(1.0f); |
shutay | 0:efe13d6feef8 | 31 | } |
shutay | 0:efe13d6feef8 | 32 | |
shutay | 1:f4e48249b980 | 33 | // The LED blinking code was split out into 2 functions with different parameter |
shutay | 1:f4e48249b980 | 34 | // passing methods to incrementally add more sophistication and complexity into |
shutay | 1:f4e48249b980 | 35 | // the basic mbed_blinky example. Those familiar with C/C++ won't need this sort |
shutay | 1:f4e48249b980 | 36 | // of help, but if mbed is your first foray into C/C++, this will help... |
shutay | 1:f4e48249b980 | 37 | |
shutay | 1:f4e48249b980 | 38 | // Take the parameter passed to us in variable 'val' and use the value in it |
shutay | 1:f4e48249b980 | 39 | // to update the LEDs. We essentially use the value in 'val' as a bit field |
shutay | 1:f4e48249b980 | 40 | // telling us which LEDs to light up, in a way that corresponds directly to the |
shutay | 1:f4e48249b980 | 41 | // bit position. |
shutay | 0:efe13d6feef8 | 42 | void updateLEDs(int val) |
shutay | 0:efe13d6feef8 | 43 | { |
shutay | 0:efe13d6feef8 | 44 | led1 = val & 0x01; |
shutay | 0:efe13d6feef8 | 45 | led2 = (val & 0x02) >> 1; |
shutay | 0:efe13d6feef8 | 46 | led3 = (val & 0x04) >> 2; |
shutay | 0:efe13d6feef8 | 47 | led4 = (val & 0x08) >> 3; |
shutay | 0:efe13d6feef8 | 48 | } |
shutay | 0:efe13d6feef8 | 49 | |
shutay | 1:f4e48249b980 | 50 | // A simple bitwise shifting function to create an LED chaser effect. |
shutay | 1:f4e48249b980 | 51 | // This function is different as it takes a pointer to the original variable. |
shutay | 1:f4e48249b980 | 52 | // i.e., it is expecting the address to an int variable, not simply the value |
shutay | 1:f4e48249b980 | 53 | // itself. |
shutay | 0:efe13d6feef8 | 54 | void advanceLEDs(int *val) |
shutay | 0:efe13d6feef8 | 55 | { |
shutay | 0:efe13d6feef8 | 56 | *val = *val << 1; |
shutay | 0:efe13d6feef8 | 57 | if(*val>0x08) *val = 1; |
shutay | 0:efe13d6feef8 | 58 | } |
shutay | 0:efe13d6feef8 | 59 | |
shutay | 0:efe13d6feef8 | 60 | int main() |
shutay | 0:efe13d6feef8 | 61 | { |
shutay | 0:efe13d6feef8 | 62 | initialise(); |
shutay | 0:efe13d6feef8 | 63 | |
shutay | 0:efe13d6feef8 | 64 | int i = 1; |
shutay | 0:efe13d6feef8 | 65 | while(1) |
shutay | 0:efe13d6feef8 | 66 | { |
shutay | 0:efe13d6feef8 | 67 | updateLEDs(i); |
shutay | 0:efe13d6feef8 | 68 | advanceLEDs(&i); |
shutay | 0:efe13d6feef8 | 69 | wait(0.5f); |
shutay | 0:efe13d6feef8 | 70 | } |
shutay | 0:efe13d6feef8 | 71 | } |