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