RoboCup Base Station
Dependencies: mbed mbed-rtos Wireless Drivers
main.cpp@3:c3114df544e8, 2014-12-31 (annotated)
- Committer:
- jjones646
- Date:
- Wed Dec 31 09:32:08 2014 +0000
- Revision:
- 3:c3114df544e8
- Parent:
- 2:7fd95eae5731
- Child:
- 4:ec95917c3211
making status led sequence cleaner for the eyes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jjones646 | 2:7fd95eae5731 | 1 | // RoboCup dual-frequency band base station |
jjones646 | 0:a606cf2249ad | 2 | |
jjones646 | 2:7fd95eae5731 | 3 | #include "BaseStation.h" |
jjones646 | 1:e5373c63f642 | 4 | |
jjones646 | 0:a606cf2249ad | 5 | // Function for writing a number to the 7-segment display |
jjones646 | 2:7fd95eae5731 | 6 | void writeSegment(uint8_t val, DigitalOut& latch) |
jjones646 | 0:a606cf2249ad | 7 | { |
jjones646 | 2:7fd95eae5731 | 8 | // Outputs used as input values to the 7-segment binary decoder - uses latching inputs |
jjones646 | 2:7fd95eae5731 | 9 | DigitalOut signal[4] = { RJ_7_SEG_PINS }; |
jjones646 | 2:7fd95eae5731 | 10 | |
jjones646 | 0:a606cf2249ad | 11 | // write out the new value |
jjones646 | 0:a606cf2249ad | 12 | for (int i=0; i<4; i++) |
jjones646 | 0:a606cf2249ad | 13 | signal[i] = ((1<<i) & (val)) & 0x0F; |
jjones646 | 0:a606cf2249ad | 14 | |
jjones646 | 0:a606cf2249ad | 15 | // latch the value |
jjones646 | 0:a606cf2249ad | 16 | for (int i=0; i<2; i++) |
jjones646 | 0:a606cf2249ad | 17 | latch = !latch; |
jjones646 | 0:a606cf2249ad | 18 | } |
jjones646 | 0:a606cf2249ad | 19 | |
jjones646 | 2:7fd95eae5731 | 20 | void seg_task(void const *arg) |
jjones646 | 2:7fd95eae5731 | 21 | { |
jjones646 | 2:7fd95eae5731 | 22 | // latch pin for 7-seg |
jjones646 | 2:7fd95eae5731 | 23 | DigitalOut latch( RJ_7_SEG_LATCH_PIN, 0 ); |
jjones646 | 3:c3114df544e8 | 24 | |
jjones646 | 2:7fd95eae5731 | 25 | // Decimal point initialized to OFF |
jjones646 | 2:7fd95eae5731 | 26 | DigitalOut decimal( RJ_7_SEG_DOT_PIN, 1 ); |
jjones646 | 2:7fd95eae5731 | 27 | |
jjones646 | 2:7fd95eae5731 | 28 | uint8_t channel = 8; |
jjones646 | 2:7fd95eae5731 | 29 | writeSegment(channel, latch); |
jjones646 | 2:7fd95eae5731 | 30 | channel = 0; // start from 0 once the main task's look begins |
jjones646 | 2:7fd95eae5731 | 31 | |
jjones646 | 2:7fd95eae5731 | 32 | // wait to be signaled before beginning |
jjones646 | 2:7fd95eae5731 | 33 | osSignalWait(0x01, osWaitForever); |
jjones646 | 3:c3114df544e8 | 34 | |
jjones646 | 2:7fd95eae5731 | 35 | // give a small delay to ensure the startup value stays lit for some time |
jjones646 | 2:7fd95eae5731 | 36 | Thread::wait(500); |
jjones646 | 3:c3114df544e8 | 37 | |
jjones646 | 2:7fd95eae5731 | 38 | // turn the decimal point off |
jjones646 | 2:7fd95eae5731 | 39 | decimal = 0; |
jjones646 | 2:7fd95eae5731 | 40 | |
jjones646 | 2:7fd95eae5731 | 41 | while(1) { // loop forever |
jjones646 | 2:7fd95eae5731 | 42 | // send numerical value to 7-segment & hold for a while |
jjones646 | 2:7fd95eae5731 | 43 | writeSegment(channel++, latch); |
jjones646 | 2:7fd95eae5731 | 44 | channel = (channel > 9) ? 0 : channel; // reset value if too high |
jjones646 | 2:7fd95eae5731 | 45 | Thread::wait(1000); |
jjones646 | 2:7fd95eae5731 | 46 | } |
jjones646 | 2:7fd95eae5731 | 47 | } |
jjones646 | 2:7fd95eae5731 | 48 | |
jjones646 | 0:a606cf2249ad | 49 | int main() |
jjones646 | 0:a606cf2249ad | 50 | { |
jjones646 | 2:7fd95eae5731 | 51 | // RGB Status LED |
jjones646 | 2:7fd95eae5731 | 52 | PwmOut rgb_led[3] = { RJ_RGB_LED_PINS }; |
jjones646 | 1:e5373c63f642 | 53 | |
jjones646 | 2:7fd95eae5731 | 54 | // Primary radio status LEDs |
jjones646 | 2:7fd95eae5731 | 55 | DigitalOut r1_led[3] = { RJ_PRIMARY_RADIO_LEDS }; |
jjones646 | 2:7fd95eae5731 | 56 | |
jjones646 | 2:7fd95eae5731 | 57 | // Secondary radio status LEDs |
jjones646 | 2:7fd95eae5731 | 58 | DigitalOut r2_led[3] = { RJ_SECONDARY_RADIO_LEDS }; |
jjones646 | 0:a606cf2249ad | 59 | |
jjones646 | 2:7fd95eae5731 | 60 | // Used for controlling power to the RGB LED's shared annode lead |
jjones646 | 2:7fd95eae5731 | 61 | DigitalOut rgb_pwr( RJ_RGB_LED_ANNODE, 0 ); |
jjones646 | 2:7fd95eae5731 | 62 | |
jjones646 | 2:7fd95eae5731 | 63 | // Start 7-segment task |
jjones646 | 2:7fd95eae5731 | 64 | Thread thread_seg_task(seg_task); |
jjones646 | 0:a606cf2249ad | 65 | |
jjones646 | 2:7fd95eae5731 | 66 | // turn all LEDs off initially - values are inverted since LEDs are sinking the current |
jjones646 | 2:7fd95eae5731 | 67 | for (int i=0; i<3; i++) { |
jjones646 | 2:7fd95eae5731 | 68 | rgb_led[i] = 1; |
jjones646 | 2:7fd95eae5731 | 69 | r1_led[i] = 1; |
jjones646 | 2:7fd95eae5731 | 70 | r2_led[i] = 1; |
jjones646 | 1:e5373c63f642 | 71 | } |
jjones646 | 1:e5373c63f642 | 72 | |
jjones646 | 2:7fd95eae5731 | 73 | // =========== Cyle primary & secondary radio status LEDs =========== |
jjones646 | 2:7fd95eae5731 | 74 | // turn on all radio status LEDs |
jjones646 | 1:e5373c63f642 | 75 | for (int i=0; i<3; i++) { |
jjones646 | 3:c3114df544e8 | 76 | |
jjones646 | 3:c3114df544e8 | 77 | // initialze off at the start of every iteration |
jjones646 | 3:c3114df544e8 | 78 | for(int j=0; j<3; j++) { |
jjones646 | 3:c3114df544e8 | 79 | r1_led[j] = 1; |
jjones646 | 3:c3114df544e8 | 80 | r2_led[j] = 1; |
jjones646 | 3:c3114df544e8 | 81 | } |
jjones646 | 3:c3114df544e8 | 82 | |
jjones646 | 3:c3114df544e8 | 83 | if (i != 2) { |
jjones646 | 3:c3114df544e8 | 84 | // cycle 2 LEDs |
jjones646 | 3:c3114df544e8 | 85 | for (int j=i; j<2; j++) { |
jjones646 | 3:c3114df544e8 | 86 | r1_led[j] = 0; |
jjones646 | 3:c3114df544e8 | 87 | r2_led[j] = 0; |
jjones646 | 3:c3114df544e8 | 88 | Thread::wait(50); |
jjones646 | 3:c3114df544e8 | 89 | } |
jjones646 | 3:c3114df544e8 | 90 | } else { |
jjones646 | 3:c3114df544e8 | 91 | r1_led[i] = 0; |
jjones646 | 3:c3114df544e8 | 92 | r2_led[i] = 0; |
jjones646 | 3:c3114df544e8 | 93 | Thread::wait(50); |
jjones646 | 3:c3114df544e8 | 94 | r1_led[i] = 1; |
jjones646 | 3:c3114df544e8 | 95 | r2_led[i] = 1; |
jjones646 | 3:c3114df544e8 | 96 | } |
jjones646 | 2:7fd95eae5731 | 97 | } |
jjones646 | 3:c3114df544e8 | 98 | /* |
jjones646 | 2:7fd95eae5731 | 99 | // turn off all radio status LEDs |
jjones646 | 2:7fd95eae5731 | 100 | for (int i=0; i<3; i++) { |
jjones646 | 2:7fd95eae5731 | 101 | r1_led[i] = 1; |
jjones646 | 1:e5373c63f642 | 102 | r2_led[i] = 1; |
jjones646 | 2:7fd95eae5731 | 103 | Thread::wait(50); |
jjones646 | 1:e5373c63f642 | 104 | } |
jjones646 | 3:c3114df544e8 | 105 | */ |
jjones646 | 2:7fd95eae5731 | 106 | // give power to all colors of the RGB LED and turn off decimal point |
jjones646 | 2:7fd95eae5731 | 107 | rgb_pwr = 1; |
jjones646 | 3:c3114df544e8 | 108 | |
jjones646 | 2:7fd95eae5731 | 109 | // tell the segment thread to begin its task |
jjones646 | 2:7fd95eae5731 | 110 | thread_seg_task.signal_set(0x01); |
jjones646 | 2:7fd95eae5731 | 111 | |
jjones646 | 2:7fd95eae5731 | 112 | // fade the RGB LED up to green and half power |
jjones646 | 2:7fd95eae5731 | 113 | for (float i=1.0; i>0.5; i-=0.01) { |
jjones646 | 2:7fd95eae5731 | 114 | rgb_led[G] = i; |
jjones646 | 2:7fd95eae5731 | 115 | Thread::wait(20); |
jjones646 | 2:7fd95eae5731 | 116 | } |
jjones646 | 1:e5373c63f642 | 117 | |
jjones646 | 2:7fd95eae5731 | 118 | // at led_intensity[3] = { 0, 0, 0 }; |
jjones646 | 2:7fd95eae5731 | 119 | srand(time(NULL)); |
jjones646 | 1:e5373c63f642 | 120 | |
jjones646 | 2:7fd95eae5731 | 121 | // loop forever ==================== |
jjones646 | 2:7fd95eae5731 | 122 | while(1) { |
jjones646 | 2:7fd95eae5731 | 123 | uint8_t color = rand()%2; |
jjones646 | 2:7fd95eae5731 | 124 | |
jjones646 | 2:7fd95eae5731 | 125 | // rgb_led[color] = (rand()%1000)/1000; |
jjones646 | 2:7fd95eae5731 | 126 | |
jjones646 | 2:7fd95eae5731 | 127 | // delay |
jjones646 | 2:7fd95eae5731 | 128 | Thread::wait(300); |
jjones646 | 0:a606cf2249ad | 129 | } |
jjones646 | 0:a606cf2249ad | 130 | } |