![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
perturh room legacy
Dependencies: SMARTWAV USBDevice mbed stateScript
Fork of stateScript by
main.cpp@4:34aca2142df9, 2014-09-25 (annotated)
- Committer:
- mkarlsso
- Date:
- Thu Sep 25 23:42:30 2014 +0000
- Revision:
- 4:34aca2142df9
- Parent:
- 3:ae33b7f5a7c1
- Child:
- 5:e62cd80aa22f
Longer script loading without hanging due to memory limitations
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:fb6bbc10ffa0 | 1 | #include "mbed.h" |
mkarlsso | 2:298679fff37c | 2 | #include <stdint.h> |
mkarlsso | 2:298679fff37c | 3 | #include "behave.h" |
mkarlsso | 2:298679fff37c | 4 | #include <string.h> |
mkarlsso | 2:298679fff37c | 5 | #include <sstream> |
mkarlsso | 2:298679fff37c | 6 | #include "SMARTWAV.h" |
mkarlsso | 2:298679fff37c | 7 | |
mkarlsso | 2:298679fff37c | 8 | |
mkarlsso | 2:298679fff37c | 9 | uint32_t timeKeeper; //the main clock (updated every ms) |
mkarlsso | 2:298679fff37c | 10 | bool resetTimer = false; //if true, the clock is reset |
mkarlsso | 2:298679fff37c | 11 | bool clockSlave = false; //slave mode |
mkarlsso | 2:298679fff37c | 12 | bool changeToSlave = false; |
mkarlsso | 2:298679fff37c | 13 | bool changeToStandAlone = false; |
mkarlsso | 2:298679fff37c | 14 | |
mkarlsso | 2:298679fff37c | 15 | //static char buf1[0x2000] __attribute__((section("AHBSRAM0"))); |
mkarlsso | 2:298679fff37c | 16 | __attribute((section("AHBSRAM0"),aligned)) outputStream textDisplay(512); |
mkarlsso | 4:34aca2142df9 | 17 | __attribute((section("AHBSRAM0"),aligned)) char buffer[256]; |
mkarlsso | 2:298679fff37c | 18 | |
mkarlsso | 2:298679fff37c | 19 | |
mkarlsso | 2:298679fff37c | 20 | __attribute((section("AHBSRAM1"),aligned)) event eventBlock[NUMEVENTS]; |
mkarlsso | 2:298679fff37c | 21 | |
mkarlsso | 2:298679fff37c | 22 | __attribute((section("AHBSRAM1"),aligned)) condition conditionBlock[NUMCONDITIONS]; |
mkarlsso | 2:298679fff37c | 23 | |
mkarlsso | 2:298679fff37c | 24 | __attribute((section("AHBSRAM1"),aligned)) intCompare intCompareBlock[NUMINTCOMPARE]; |
mkarlsso | 2:298679fff37c | 25 | |
mkarlsso | 2:298679fff37c | 26 | __attribute((section("AHBSRAM0"),aligned)) action actionBlock[NUMACTIONS]; |
mkarlsso | 2:298679fff37c | 27 | |
mkarlsso | 2:298679fff37c | 28 | __attribute((section("AHBSRAM0"),aligned)) portMessage portMessageBlock[NUMPORTMESSAGES]; |
mkarlsso | 2:298679fff37c | 29 | |
mkarlsso | 2:298679fff37c | 30 | //__attribute((section("AHBSRAM1"),aligned)) intVariable intVariableBlock[10]; |
mkarlsso | 2:298679fff37c | 31 | |
mkarlsso | 2:298679fff37c | 32 | __attribute((section("AHBSRAM0"),aligned)) intOperation intOperationBlock[NUMINTOPERATIONS]; |
mkarlsso | 2:298679fff37c | 33 | |
mkarlsso | 2:298679fff37c | 34 | __attribute((section("AHBSRAM0"),aligned)) displayAction displayActionBlock[NUMDISPLAYACTIONS]; |
mkarlsso | 2:298679fff37c | 35 | |
mkarlsso | 2:298679fff37c | 36 | |
mkarlsso | 2:298679fff37c | 37 | |
mkarlsso | 2:298679fff37c | 38 | Ticker clockBroadCast; //timer used when sending out timestamps on a GPIO |
mkarlsso | 2:298679fff37c | 39 | uint32_t currentBroadcastTime; |
mkarlsso | 2:298679fff37c | 40 | int currentBroadcastBit = 0; |
mkarlsso | 2:298679fff37c | 41 | bool broadcastHigh = false; |
mkarlsso | 2:298679fff37c | 42 | |
mkarlsso | 2:298679fff37c | 43 | int currentDIOstate[2] = {0,0}; //the first number is a bit-wise representaion of the digital inputs, the 2nd is for the outputs |
mkarlsso | 2:298679fff37c | 44 | bool digitalInChanged = false; |
mkarlsso | 2:298679fff37c | 45 | bool digitalOutChanged = false; |
mkarlsso | 2:298679fff37c | 46 | bool broadCastStateChanges = true; |
mkarlsso | 2:298679fff37c | 47 | bool textStreaming = true; |
mkarlsso | 2:298679fff37c | 48 | uint32_t changeTime; |
mkarlsso | 2:298679fff37c | 49 | |
mkarlsso | 2:298679fff37c | 50 | LocalFileSystem local("local"); |
mkarlsso | 2:298679fff37c | 51 | |
mkarlsso | 2:298679fff37c | 52 | digitalPort* portVector[NUMPORTS+1]; //create the digital ports |
mkarlsso | 2:298679fff37c | 53 | |
mkarlsso | 2:298679fff37c | 54 | float brightness = 0.0; |
mkarlsso | 2:298679fff37c | 55 | |
mkarlsso | 2:298679fff37c | 56 | //Define the digial ports |
mkarlsso | 2:298679fff37c | 57 | |
mkarlsso | 2:298679fff37c | 58 | //Pins for clock syncing |
mkarlsso | 2:298679fff37c | 59 | InterruptIn clockResetInt(p5); |
mkarlsso | 2:298679fff37c | 60 | DigitalOut clockOutSync(p6); |
mkarlsso | 2:298679fff37c | 61 | DigitalOut clockOutSignal(p7); |
mkarlsso | 2:298679fff37c | 62 | InterruptIn clockExternalIncrement(p8); |
mkarlsso | 2:298679fff37c | 63 | |
mkarlsso | 4:34aca2142df9 | 64 | //Camera trigger signal |
mkarlsso | 4:34aca2142df9 | 65 | //DigitalOut camera30Hz(p27); |
mkarlsso | 4:34aca2142df9 | 66 | |
mkarlsso | 2:298679fff37c | 67 | |
mkarlsso | 2:298679fff37c | 68 | //Pins for digital ports. Each port has 1 out and 1 in |
mkarlsso | 2:298679fff37c | 69 | //DigitalOut out1(LED1); //route to LED for debugging |
mkarlsso | 2:298679fff37c | 70 | DigitalOut out1(p11); |
mkarlsso | 2:298679fff37c | 71 | DigitalIn in1(p12); |
mkarlsso | 2:298679fff37c | 72 | InterruptIn int1(p12); |
mkarlsso | 2:298679fff37c | 73 | __attribute((section("AHBSRAM0"),aligned)) digitalPort port1(&out1, &in1); |
mkarlsso | 2:298679fff37c | 74 | |
mkarlsso | 2:298679fff37c | 75 | DigitalOut out2(p13); |
mkarlsso | 2:298679fff37c | 76 | DigitalIn in2(p14); |
mkarlsso | 2:298679fff37c | 77 | InterruptIn int2(p14); |
mkarlsso | 2:298679fff37c | 78 | __attribute((section("AHBSRAM0"),aligned)) digitalPort port2(&out2, &in2); |
mkarlsso | 2:298679fff37c | 79 | |
mkarlsso | 2:298679fff37c | 80 | |
mkarlsso | 2:298679fff37c | 81 | DigitalOut out3(p15); |
mkarlsso | 2:298679fff37c | 82 | DigitalIn in3(p16); |
mkarlsso | 2:298679fff37c | 83 | InterruptIn int3(p16); |
mkarlsso | 2:298679fff37c | 84 | __attribute((section("AHBSRAM0"),aligned)) digitalPort port3(&out3, &in3); |
mkarlsso | 2:298679fff37c | 85 | |
mkarlsso | 2:298679fff37c | 86 | DigitalOut out4(p18); |
mkarlsso | 2:298679fff37c | 87 | DigitalIn in4(p17); |
mkarlsso | 2:298679fff37c | 88 | InterruptIn int4(p17); |
mkarlsso | 2:298679fff37c | 89 | __attribute((section("AHBSRAM0"),aligned)) digitalPort port4(&out4, &in4); |
mkarlsso | 2:298679fff37c | 90 | |
mkarlsso | 2:298679fff37c | 91 | DigitalOut out5(p21); |
mkarlsso | 2:298679fff37c | 92 | DigitalIn in5(p22); |
mkarlsso | 2:298679fff37c | 93 | InterruptIn int5(p22); |
mkarlsso | 2:298679fff37c | 94 | __attribute((section("AHBSRAM0"),aligned)) digitalPort port5(&out5, &in5); |
mkarlsso | 2:298679fff37c | 95 | |
mkarlsso | 2:298679fff37c | 96 | DigitalOut out6(p23); |
mkarlsso | 2:298679fff37c | 97 | DigitalIn in6(p24); |
mkarlsso | 2:298679fff37c | 98 | InterruptIn int6(p24); |
mkarlsso | 2:298679fff37c | 99 | __attribute((section("AHBSRAM0"),aligned)) digitalPort port6(&out6, &in6); |
mkarlsso | 2:298679fff37c | 100 | |
mkarlsso | 2:298679fff37c | 101 | DigitalOut out7(p25); |
mkarlsso | 2:298679fff37c | 102 | DigitalIn in7(p26); |
mkarlsso | 2:298679fff37c | 103 | InterruptIn int7(p26); |
mkarlsso | 2:298679fff37c | 104 | __attribute((section("AHBSRAM0"),aligned)) digitalPort port7(&out7, &in7); |
mkarlsso | 2:298679fff37c | 105 | |
mkarlsso | 2:298679fff37c | 106 | DigitalOut out8(p29); |
mkarlsso | 2:298679fff37c | 107 | DigitalIn in8(p30); |
mkarlsso | 2:298679fff37c | 108 | InterruptIn int8(p30); |
mkarlsso | 2:298679fff37c | 109 | __attribute((section("AHBSRAM0"),aligned)) digitalPort port8(&out8, &in8); |
mkarlsso | 2:298679fff37c | 110 | //Serial communication |
mkarlsso | 2:298679fff37c | 111 | Serial pc(USBTX, USBRX); // tx, rx |
mkarlsso | 2:298679fff37c | 112 | |
mkarlsso | 2:298679fff37c | 113 | //Main event queue |
mkarlsso | 2:298679fff37c | 114 | eventQueue mainQueue(portVector, &timeKeeper); |
mkarlsso | 2:298679fff37c | 115 | |
mkarlsso | 2:298679fff37c | 116 | //The script parser |
mkarlsso | 2:298679fff37c | 117 | scriptStream parser(&pc, portVector, NUMPORTS, &mainQueue); |
mkarlsso | 2:298679fff37c | 118 | |
mkarlsso | 2:298679fff37c | 119 | //The sound output uses a SmartWav device and their simple serial library |
mkarlsso | 2:298679fff37c | 120 | SMARTWAV sWav(p9,p10,p19); //(TX,RX,Reset); |
mkarlsso | 2:298679fff37c | 121 | |
mkarlsso | 2:298679fff37c | 122 | //Erases the input buffer for serial input |
mkarlsso | 2:298679fff37c | 123 | void eraseBuffer(char* buffer,int numToErase) { |
mkarlsso | 2:298679fff37c | 124 | for (int i = 0; i < numToErase; i++) { |
mkarlsso | 2:298679fff37c | 125 | buffer[i] = NULL; |
mkarlsso | 2:298679fff37c | 126 | } |
mkarlsso | 2:298679fff37c | 127 | } |
mkarlsso | 2:298679fff37c | 128 | |
simon | 0:fb6bbc10ffa0 | 129 | |
mkarlsso | 2:298679fff37c | 130 | //Called by clockBroadCast timer to output a 32-bit timestamp. When the timestmap has been |
mkarlsso | 2:298679fff37c | 131 | //sent, the function is detached from the timer. |
mkarlsso | 2:298679fff37c | 132 | void broadcastNextBit() { |
mkarlsso | 2:298679fff37c | 133 | |
mkarlsso | 2:298679fff37c | 134 | if (currentBroadcastBit < 32) { |
mkarlsso | 2:298679fff37c | 135 | broadcastHigh = !broadcastHigh; //flip the sync signal |
mkarlsso | 2:298679fff37c | 136 | if (broadcastHigh) { |
mkarlsso | 2:298679fff37c | 137 | clockOutSync = 1; |
mkarlsso | 2:298679fff37c | 138 | clockOutSignal = (currentBroadcastTime & ( 1 << currentBroadcastBit)) >> currentBroadcastBit; |
mkarlsso | 2:298679fff37c | 139 | |
mkarlsso | 2:298679fff37c | 140 | } else { |
mkarlsso | 2:298679fff37c | 141 | clockOutSync = 0; |
mkarlsso | 2:298679fff37c | 142 | clockOutSignal = 0; |
mkarlsso | 2:298679fff37c | 143 | currentBroadcastBit++; |
mkarlsso | 2:298679fff37c | 144 | } |
mkarlsso | 2:298679fff37c | 145 | } |
mkarlsso | 2:298679fff37c | 146 | } |
mkarlsso | 2:298679fff37c | 147 | |
mkarlsso | 2:298679fff37c | 148 | |
mkarlsso | 2:298679fff37c | 149 | //intiatiation of timer2 (specific to the LPC17xx chip). This is used in |
mkarlsso | 2:298679fff37c | 150 | //standalone mode to increment the clock every ms. |
mkarlsso | 2:298679fff37c | 151 | //we use lower-lever code here to get better control over the timer if we reset it |
mkarlsso | 2:298679fff37c | 152 | void timer0_init(void) |
mkarlsso | 2:298679fff37c | 153 | { |
mkarlsso | 2:298679fff37c | 154 | //LPC_SC->PCLKSEL1 &= (3 << 12); //mask |
mkarlsso | 2:298679fff37c | 155 | //LPC_SC->PCLKSEL1 |= (1 << 12); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual) |
mkarlsso | 2:298679fff37c | 156 | |
mkarlsso | 2:298679fff37c | 157 | //LPC_SC->PCLKSEL0 &= (3 << 3); //mask |
mkarlsso | 2:298679fff37c | 158 | //LPC_SC->PCLKSEL0 |= (1 << 3); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual) |
mkarlsso | 2:298679fff37c | 159 | LPC_SC->PCONP |=1<1; //timer0 power on |
mkarlsso | 2:298679fff37c | 160 | LPC_TIM0->MR0 = 23980; //1 msec |
mkarlsso | 2:298679fff37c | 161 | //LPC_TIM0->PR = (SystemCoreClock / 1000000); //microsecond steps |
mkarlsso | 2:298679fff37c | 162 | //LPC_TIM0->MR0 = 1000; //100 msec |
mkarlsso | 2:298679fff37c | 163 | //LPC_TIM0->MR0 = (SystemCoreClock / 1000000); //microsecond steps |
mkarlsso | 2:298679fff37c | 164 | LPC_TIM0->MCR = 3; //interrupt and reset control |
mkarlsso | 2:298679fff37c | 165 | //3 = Interrupt & reset timer0 on match |
mkarlsso | 2:298679fff37c | 166 | //1 = Interrupt only, no reset of timer0 |
mkarlsso | 2:298679fff37c | 167 | NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt |
mkarlsso | 2:298679fff37c | 168 | LPC_TIM0->TCR = 1; //enable Timer0 |
mkarlsso | 2:298679fff37c | 169 | |
mkarlsso | 2:298679fff37c | 170 | |
mkarlsso | 2:298679fff37c | 171 | /* |
mkarlsso | 2:298679fff37c | 172 | LPC_SC->PCONP |= (0x1<<22); // turn on power for timer 2 |
mkarlsso | 2:298679fff37c | 173 | LPC_TIM2->TCR = 0x02; // reset timer |
mkarlsso | 2:298679fff37c | 174 | LPC_TIM2->PR = (SystemCoreClock / 1000000); //microsecond steps |
mkarlsso | 2:298679fff37c | 175 | LPC_TIM2->MR0 = 1000; // 1000 microsecond interval interrupts |
mkarlsso | 2:298679fff37c | 176 | LPC_TIM2->IR = 0x3f; // reset all interrrupts |
mkarlsso | 2:298679fff37c | 177 | LPC_TIM2->MCR = 0x03; // reset timer on match and generate interrupt (MR0) |
mkarlsso | 2:298679fff37c | 178 | LPC_TIM2->TCR = 0x01; // start timer |
mkarlsso | 2:298679fff37c | 179 | |
mkarlsso | 2:298679fff37c | 180 | NVIC_EnableIRQ(TIMER2_IRQn); // Enable the interrupt |
mkarlsso | 2:298679fff37c | 181 | */ |
mkarlsso | 2:298679fff37c | 182 | //pc.printf("Done timer_init\n\r"); |
mkarlsso | 2:298679fff37c | 183 | } |
mkarlsso | 2:298679fff37c | 184 | |
mkarlsso | 2:298679fff37c | 185 | //This is the callback for timer2 |
mkarlsso | 2:298679fff37c | 186 | extern "C" void TIMER0_IRQHandler (void) { |
mkarlsso | 2:298679fff37c | 187 | |
mkarlsso | 2:298679fff37c | 188 | if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed |
mkarlsso | 2:298679fff37c | 189 | |
mkarlsso | 2:298679fff37c | 190 | LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag |
mkarlsso | 2:298679fff37c | 191 | timeKeeper++; |
mkarlsso | 2:298679fff37c | 192 | |
mkarlsso | 2:298679fff37c | 193 | if (resetTimer) { |
mkarlsso | 2:298679fff37c | 194 | timeKeeper = 0; |
mkarlsso | 2:298679fff37c | 195 | resetTimer = false; |
mkarlsso | 2:298679fff37c | 196 | } |
mkarlsso | 2:298679fff37c | 197 | |
mkarlsso | 2:298679fff37c | 198 | if (currentBroadcastBit > 31) { |
mkarlsso | 2:298679fff37c | 199 | clockBroadCast.detach(); |
mkarlsso | 2:298679fff37c | 200 | currentBroadcastBit = 0; |
mkarlsso | 2:298679fff37c | 201 | } |
mkarlsso | 2:298679fff37c | 202 | |
mkarlsso | 4:34aca2142df9 | 203 | |
mkarlsso | 2:298679fff37c | 204 | //Every second, we broadcast out the current time |
mkarlsso | 2:298679fff37c | 205 | if ((timeKeeper % 1000) == 0) { |
mkarlsso | 2:298679fff37c | 206 | currentBroadcastTime = timeKeeper; |
mkarlsso | 2:298679fff37c | 207 | clockOutSync = 1; |
mkarlsso | 2:298679fff37c | 208 | |
mkarlsso | 2:298679fff37c | 209 | currentBroadcastBit = 0; |
mkarlsso | 2:298679fff37c | 210 | |
mkarlsso | 2:298679fff37c | 211 | clockOutSignal = (currentBroadcastTime & ( 1 << currentBroadcastBit)) >> currentBroadcastBit; |
mkarlsso | 2:298679fff37c | 212 | broadcastHigh = true; |
mkarlsso | 2:298679fff37c | 213 | clockBroadCast.attach_us(&broadcastNextBit, 1000); |
mkarlsso | 2:298679fff37c | 214 | } |
mkarlsso | 2:298679fff37c | 215 | } |
mkarlsso | 2:298679fff37c | 216 | } |
mkarlsso | 2:298679fff37c | 217 | |
mkarlsso | 2:298679fff37c | 218 | |
mkarlsso | 2:298679fff37c | 219 | //In slave mode, the clock is updated with an external trigger every ms. No need for |
mkarlsso | 2:298679fff37c | 220 | //100us resolution. |
mkarlsso | 2:298679fff37c | 221 | void callback_clockExternalIncrement(void) { |
mkarlsso | 2:298679fff37c | 222 | |
mkarlsso | 2:298679fff37c | 223 | timeKeeper++; |
mkarlsso | 2:298679fff37c | 224 | |
mkarlsso | 2:298679fff37c | 225 | if (resetTimer) { |
mkarlsso | 2:298679fff37c | 226 | timeKeeper = 0; |
mkarlsso | 2:298679fff37c | 227 | resetTimer = false; |
mkarlsso | 2:298679fff37c | 228 | } |
mkarlsso | 2:298679fff37c | 229 | |
mkarlsso | 2:298679fff37c | 230 | if (currentBroadcastBit > 31) { |
mkarlsso | 2:298679fff37c | 231 | clockBroadCast.detach(); |
mkarlsso | 2:298679fff37c | 232 | currentBroadcastBit = 0; |
mkarlsso | 2:298679fff37c | 233 | } |
mkarlsso | 2:298679fff37c | 234 | |
mkarlsso | 2:298679fff37c | 235 | //Every second, we broadcast out the current time |
mkarlsso | 2:298679fff37c | 236 | if ((timeKeeper % 1000) == 0) { |
mkarlsso | 2:298679fff37c | 237 | currentBroadcastTime = timeKeeper; |
mkarlsso | 2:298679fff37c | 238 | clockOutSync = 1; |
mkarlsso | 2:298679fff37c | 239 | |
mkarlsso | 2:298679fff37c | 240 | currentBroadcastBit = 0; |
mkarlsso | 2:298679fff37c | 241 | |
mkarlsso | 2:298679fff37c | 242 | clockOutSignal = (currentBroadcastTime & ( 1 << currentBroadcastBit)) >> currentBroadcastBit; |
mkarlsso | 2:298679fff37c | 243 | broadcastHigh = true; |
mkarlsso | 2:298679fff37c | 244 | clockBroadCast.attach_us(&broadcastNextBit, 1000); |
mkarlsso | 2:298679fff37c | 245 | } |
mkarlsso | 2:298679fff37c | 246 | } |
mkarlsso | 2:298679fff37c | 247 | |
mkarlsso | 2:298679fff37c | 248 | //Every digital port's in pin has a hardware interrupt. We use a callback stub for each port |
mkarlsso | 2:298679fff37c | 249 | //that routes the int_callback. |
mkarlsso | 2:298679fff37c | 250 | void int_callback(int portNum, int direction) { |
mkarlsso | 2:298679fff37c | 251 | portVector[portNum]->addStateChange(direction, timeKeeper); |
mkarlsso | 2:298679fff37c | 252 | } |
mkarlsso | 2:298679fff37c | 253 | |
mkarlsso | 2:298679fff37c | 254 | //Callback stubs |
mkarlsso | 2:298679fff37c | 255 | void callback_port1_rise(void) { int_callback(1, 1); } |
mkarlsso | 2:298679fff37c | 256 | void callback_port1_fall(void) { int_callback(1, 0); } |
mkarlsso | 2:298679fff37c | 257 | void callback_port2_rise(void) { int_callback(2, 1); } |
mkarlsso | 2:298679fff37c | 258 | void callback_port2_fall(void) { int_callback(2, 0); } |
mkarlsso | 2:298679fff37c | 259 | void callback_port3_rise(void) { int_callback(3, 1); } |
mkarlsso | 2:298679fff37c | 260 | void callback_port3_fall(void) { int_callback(3, 0); } |
mkarlsso | 2:298679fff37c | 261 | void callback_port4_rise(void) { int_callback(4, 1); } |
mkarlsso | 2:298679fff37c | 262 | void callback_port4_fall(void) { int_callback(4, 0); } |
mkarlsso | 2:298679fff37c | 263 | void callback_port5_rise(void) { int_callback(5, 1); } |
mkarlsso | 2:298679fff37c | 264 | void callback_port5_fall(void) { int_callback(5, 0); } |
mkarlsso | 2:298679fff37c | 265 | void callback_port6_rise(void) { int_callback(6, 1); } |
mkarlsso | 2:298679fff37c | 266 | void callback_port6_fall(void) { int_callback(6, 0); } |
mkarlsso | 2:298679fff37c | 267 | void callback_port7_rise(void) { int_callback(7, 1); } |
mkarlsso | 2:298679fff37c | 268 | void callback_port7_fall(void) { int_callback(7, 0); } |
mkarlsso | 2:298679fff37c | 269 | void callback_port8_rise(void) { int_callback(8, 1); } |
mkarlsso | 2:298679fff37c | 270 | void callback_port8_fall(void) { int_callback(8, 0); } |
mkarlsso | 2:298679fff37c | 271 | |
mkarlsso | 2:298679fff37c | 272 | //This function is attached to an interrupt pin for external clock reset |
mkarlsso | 2:298679fff37c | 273 | void callback_clockReset(void) { |
mkarlsso | 2:298679fff37c | 274 | if (timeKeeper > 100) { |
mkarlsso | 2:298679fff37c | 275 | LPC_TIM2->TCR = 0x02; // reset timer |
mkarlsso | 2:298679fff37c | 276 | timeKeeper = 0; |
mkarlsso | 2:298679fff37c | 277 | pc.printf("%d Clock reset\r\n", timeKeeper); |
mkarlsso | 2:298679fff37c | 278 | } |
mkarlsso | 2:298679fff37c | 279 | } |
mkarlsso | 2:298679fff37c | 280 | |
simon | 0:fb6bbc10ffa0 | 281 | |
simon | 0:fb6bbc10ffa0 | 282 | int main() { |
mkarlsso | 2:298679fff37c | 283 | timeKeeper = 0; //set main clock to 0; |
mkarlsso | 2:298679fff37c | 284 | sWav.reset(); |
mkarlsso | 2:298679fff37c | 285 | pc.baud(115200); |
mkarlsso | 2:298679fff37c | 286 | //pc.baud(9600); |
mkarlsso | 2:298679fff37c | 287 | |
mkarlsso | 2:298679fff37c | 288 | for (int i = 0; i < 9; i++) { |
mkarlsso | 2:298679fff37c | 289 | portVector[i] = NULL; |
mkarlsso | 2:298679fff37c | 290 | } |
mkarlsso | 2:298679fff37c | 291 | //We keep portVector 1-based to eliminate confusion |
mkarlsso | 2:298679fff37c | 292 | portVector[1] = &port1; |
mkarlsso | 2:298679fff37c | 293 | portVector[2] = &port2; |
mkarlsso | 2:298679fff37c | 294 | portVector[3] = &port3; |
mkarlsso | 2:298679fff37c | 295 | portVector[4] = &port4; |
mkarlsso | 2:298679fff37c | 296 | portVector[5] = &port5; |
mkarlsso | 2:298679fff37c | 297 | portVector[6] = &port6; |
mkarlsso | 2:298679fff37c | 298 | portVector[7] = &port7; |
mkarlsso | 2:298679fff37c | 299 | portVector[8] = &port8; |
mkarlsso | 2:298679fff37c | 300 | |
mkarlsso | 2:298679fff37c | 301 | //Callback to update the main clock |
mkarlsso | 2:298679fff37c | 302 | //timeTick1.attach_us(&incrementTime, 100); |
mkarlsso | 2:298679fff37c | 303 | |
mkarlsso | 2:298679fff37c | 304 | timer0_init(); |
mkarlsso | 2:298679fff37c | 305 | |
mkarlsso | 2:298679fff37c | 306 | |
mkarlsso | 2:298679fff37c | 307 | //Set up callbacks for the port interrupts |
mkarlsso | 2:298679fff37c | 308 | int1.rise(&callback_port1_rise); |
mkarlsso | 2:298679fff37c | 309 | int1.fall(&callback_port1_fall); |
mkarlsso | 2:298679fff37c | 310 | int2.rise(&callback_port2_rise); |
mkarlsso | 2:298679fff37c | 311 | int2.fall(&callback_port2_fall); |
mkarlsso | 2:298679fff37c | 312 | int3.rise(&callback_port3_rise); |
mkarlsso | 2:298679fff37c | 313 | int3.fall(&callback_port3_fall); |
mkarlsso | 2:298679fff37c | 314 | int4.rise(&callback_port4_rise); |
mkarlsso | 2:298679fff37c | 315 | int4.fall(&callback_port4_fall); |
mkarlsso | 2:298679fff37c | 316 | int5.rise(&callback_port5_rise); |
mkarlsso | 2:298679fff37c | 317 | int5.fall(&callback_port5_fall); |
mkarlsso | 2:298679fff37c | 318 | int6.rise(&callback_port6_rise); |
mkarlsso | 2:298679fff37c | 319 | int6.fall(&callback_port6_fall); |
mkarlsso | 2:298679fff37c | 320 | int7.rise(&callback_port7_rise); |
mkarlsso | 2:298679fff37c | 321 | int7.fall(&callback_port7_fall); |
mkarlsso | 2:298679fff37c | 322 | int8.rise(&callback_port8_rise); |
mkarlsso | 2:298679fff37c | 323 | int8.fall(&callback_port8_fall); |
mkarlsso | 2:298679fff37c | 324 | |
mkarlsso | 2:298679fff37c | 325 | clockResetInt.rise(&callback_clockReset); |
mkarlsso | 2:298679fff37c | 326 | clockResetInt.mode(PullDown); |
mkarlsso | 2:298679fff37c | 327 | |
mkarlsso | 2:298679fff37c | 328 | clockExternalIncrement.mode(PullDown); |
mkarlsso | 2:298679fff37c | 329 | |
mkarlsso | 2:298679fff37c | 330 | //The inputs are set for pull-up mode (might need to change this) |
mkarlsso | 2:298679fff37c | 331 | in1.mode(PullDown); |
mkarlsso | 2:298679fff37c | 332 | in2.mode(PullDown); |
mkarlsso | 2:298679fff37c | 333 | in3.mode(PullDown); |
mkarlsso | 2:298679fff37c | 334 | in4.mode(PullDown); |
mkarlsso | 2:298679fff37c | 335 | in5.mode(PullDown); |
mkarlsso | 2:298679fff37c | 336 | in6.mode(PullDown); |
mkarlsso | 2:298679fff37c | 337 | in7.mode(PullDown); |
mkarlsso | 2:298679fff37c | 338 | in8.mode(PullDown); |
mkarlsso | 2:298679fff37c | 339 | |
mkarlsso | 2:298679fff37c | 340 | //Set up input buffer for the serial port |
mkarlsso | 2:298679fff37c | 341 | //char buffer[128]; |
mkarlsso | 2:298679fff37c | 342 | int bufferPos = 0; |
mkarlsso | 4:34aca2142df9 | 343 | eraseBuffer(buffer,256); |
mkarlsso | 2:298679fff37c | 344 | |
mkarlsso | 2:298679fff37c | 345 | ostringstream timeConvert; // stream used for the conversion |
mkarlsso | 2:298679fff37c | 346 | ostringstream stateConvert; |
mkarlsso | 2:298679fff37c | 347 | char junkChar; |
mkarlsso | 2:298679fff37c | 348 | int tmpChar; |
mkarlsso | 2:298679fff37c | 349 | |
mkarlsso | 2:298679fff37c | 350 | while (pc.readable()) { |
mkarlsso | 2:298679fff37c | 351 | junkChar = pc.getc(); |
mkarlsso | 2:298679fff37c | 352 | } |
mkarlsso | 2:298679fff37c | 353 | |
mkarlsso | 2:298679fff37c | 354 | FILE *fp = fopen("/local/STARTUP.TXT", "r"); |
mkarlsso | 2:298679fff37c | 355 | if (fp != NULL) { |
mkarlsso | 2:298679fff37c | 356 | pc.printf("Executing startup script...\r\n"); |
mkarlsso | 2:298679fff37c | 357 | do { |
mkarlsso | 2:298679fff37c | 358 | tmpChar = fgetc(fp); |
mkarlsso | 2:298679fff37c | 359 | if ((tmpChar >= 32) && (tmpChar <= 126)) { |
mkarlsso | 2:298679fff37c | 360 | buffer[bufferPos] = tmpChar; |
mkarlsso | 2:298679fff37c | 361 | bufferPos++; |
mkarlsso | 2:298679fff37c | 362 | } |
mkarlsso | 2:298679fff37c | 363 | if ((tmpChar == 13) || (tmpChar == 10)) { //carrriage return |
mkarlsso | 2:298679fff37c | 364 | parser.addLineToCurrentBlock(buffer); |
mkarlsso | 2:298679fff37c | 365 | bufferPos = 0; |
mkarlsso | 4:34aca2142df9 | 366 | eraseBuffer(buffer,256); |
mkarlsso | 2:298679fff37c | 367 | } |
mkarlsso | 2:298679fff37c | 368 | //pc.putc(tmpChar); |
mkarlsso | 2:298679fff37c | 369 | } while (tmpChar != EOF); |
mkarlsso | 2:298679fff37c | 370 | |
mkarlsso | 2:298679fff37c | 371 | buffer[bufferPos] = 59; |
mkarlsso | 2:298679fff37c | 372 | parser.addLineToCurrentBlock(buffer); |
mkarlsso | 4:34aca2142df9 | 373 | eraseBuffer(buffer,256); |
mkarlsso | 2:298679fff37c | 374 | fclose(fp); |
mkarlsso | 2:298679fff37c | 375 | } else { |
mkarlsso | 2:298679fff37c | 376 | pc.printf("No startup script found.\r\n"); |
mkarlsso | 2:298679fff37c | 377 | } |
mkarlsso | 2:298679fff37c | 378 | |
mkarlsso | 2:298679fff37c | 379 | //main loop |
simon | 0:fb6bbc10ffa0 | 380 | while(1) { |
mkarlsso | 2:298679fff37c | 381 | //check the main event queue to see if anything needs to be done |
mkarlsso | 2:298679fff37c | 382 | mainQueue.check(); |
mkarlsso | 2:298679fff37c | 383 | |
mkarlsso | 2:298679fff37c | 384 | //check if anything has been written to the serial input |
mkarlsso | 2:298679fff37c | 385 | if (pc.readable()) { |
mkarlsso | 2:298679fff37c | 386 | |
mkarlsso | 2:298679fff37c | 387 | buffer[bufferPos] = pc.getc(); |
mkarlsso | 2:298679fff37c | 388 | bufferPos++; |
mkarlsso | 2:298679fff37c | 389 | |
mkarlsso | 2:298679fff37c | 390 | //'Return' key pressed |
mkarlsso | 2:298679fff37c | 391 | if ((buffer[bufferPos-1] == 13) || (buffer[bufferPos-1] == 10)) { |
mkarlsso | 2:298679fff37c | 392 | //pc.printf("\r\n"); |
mkarlsso | 2:298679fff37c | 393 | buffer[bufferPos-1] = '\0'; |
mkarlsso | 2:298679fff37c | 394 | parser.addLineToCurrentBlock(buffer); |
mkarlsso | 2:298679fff37c | 395 | bufferPos = 0; |
mkarlsso | 2:298679fff37c | 396 | eraseBuffer(buffer,128); |
mkarlsso | 2:298679fff37c | 397 | |
mkarlsso | 2:298679fff37c | 398 | } else { |
mkarlsso | 2:298679fff37c | 399 | //pc.putc(buffer[bufferPos-1]); |
mkarlsso | 2:298679fff37c | 400 | //Backspace was pressed |
mkarlsso | 3:ae33b7f5a7c1 | 401 | if (((buffer[bufferPos-1] == 127)||(buffer[bufferPos-1] == 8)) && (bufferPos > 0)) { |
mkarlsso | 3:ae33b7f5a7c1 | 402 | |
mkarlsso | 2:298679fff37c | 403 | bufferPos = bufferPos-2; |
mkarlsso | 2:298679fff37c | 404 | } |
mkarlsso | 2:298679fff37c | 405 | } |
mkarlsso | 2:298679fff37c | 406 | } |
mkarlsso | 2:298679fff37c | 407 | |
mkarlsso | 2:298679fff37c | 408 | // __disable_irq(); |
mkarlsso | 2:298679fff37c | 409 | |
mkarlsso | 2:298679fff37c | 410 | |
mkarlsso | 2:298679fff37c | 411 | //Check all the digital ports to see if anything has changed. In the update routine, the port's |
mkarlsso | 2:298679fff37c | 412 | //script callbacks are called if the port was triggered |
mkarlsso | 2:298679fff37c | 413 | digitalInChanged = false; |
mkarlsso | 2:298679fff37c | 414 | digitalOutChanged = false; |
mkarlsso | 2:298679fff37c | 415 | changeTime = timeKeeper; |
mkarlsso | 2:298679fff37c | 416 | for (int i = 0; i < NUMPORTS; i++) { |
mkarlsso | 2:298679fff37c | 417 | if (portVector[i+1]->update()) { |
mkarlsso | 2:298679fff37c | 418 | digitalInChanged = true; |
mkarlsso | 2:298679fff37c | 419 | changeTime = min(changeTime,portVector[i+1]->lastChangeTime); |
mkarlsso | 2:298679fff37c | 420 | |
mkarlsso | 2:298679fff37c | 421 | //The input state of all the ports in condensed into one number (each bit contains the info) |
mkarlsso | 2:298679fff37c | 422 | if (portVector[i+1]->getLastChangeState() == 1) { |
mkarlsso | 2:298679fff37c | 423 | currentDIOstate[0] = currentDIOstate[0] | (1 << i); |
mkarlsso | 2:298679fff37c | 424 | } else { |
mkarlsso | 2:298679fff37c | 425 | currentDIOstate[0] = currentDIOstate[0] & (255^(1 << i)); |
mkarlsso | 2:298679fff37c | 426 | } |
mkarlsso | 2:298679fff37c | 427 | } |
mkarlsso | 2:298679fff37c | 428 | if (portVector[i+1]->outStateChanged) { |
mkarlsso | 2:298679fff37c | 429 | digitalOutChanged = true; |
mkarlsso | 2:298679fff37c | 430 | changeTime = min(changeTime,portVector[i+1]->lastOutChangeTime); |
mkarlsso | 2:298679fff37c | 431 | //The out state of all the ports in condensed into one number (each bit contains the info) |
mkarlsso | 2:298679fff37c | 432 | if (portVector[i+1]->outState == 1) { |
mkarlsso | 2:298679fff37c | 433 | currentDIOstate[1] = currentDIOstate[1] | (1 << i); |
mkarlsso | 2:298679fff37c | 434 | } else { |
mkarlsso | 2:298679fff37c | 435 | currentDIOstate[1] = currentDIOstate[1] & (255^(1 << i)); |
mkarlsso | 2:298679fff37c | 436 | } |
mkarlsso | 2:298679fff37c | 437 | portVector[i+1]->outStateChanged = false; |
mkarlsso | 2:298679fff37c | 438 | } |
mkarlsso | 2:298679fff37c | 439 | } |
mkarlsso | 2:298679fff37c | 440 | |
mkarlsso | 2:298679fff37c | 441 | //If anything changed, we write the new values to the serial port (this can be turned off |
mkarlsso | 2:298679fff37c | 442 | //with broadCastStateChanges) |
mkarlsso | 2:298679fff37c | 443 | if ( (digitalInChanged||digitalOutChanged) && broadCastStateChanges) { |
mkarlsso | 2:298679fff37c | 444 | timeConvert << changeTime; //broadcast the earliest timestamp when a change occured |
mkarlsso | 2:298679fff37c | 445 | //stateConvert << currentDIOstate[0] << " " << currentDIOstate[1]; |
mkarlsso | 2:298679fff37c | 446 | stateConvert << currentDIOstate[0] << " " << currentDIOstate[1] << " "; |
mkarlsso | 2:298679fff37c | 447 | textDisplay.send(timeConvert.str() + " " + stateConvert.str() + "\r\n"); |
mkarlsso | 2:298679fff37c | 448 | timeConvert.clear(); |
mkarlsso | 2:298679fff37c | 449 | timeConvert.seekp(0); |
mkarlsso | 2:298679fff37c | 450 | stateConvert.clear(); |
mkarlsso | 2:298679fff37c | 451 | stateConvert.seekp(0); |
mkarlsso | 2:298679fff37c | 452 | digitalInChanged = false; |
mkarlsso | 2:298679fff37c | 453 | digitalOutChanged = false; |
mkarlsso | 2:298679fff37c | 454 | } |
mkarlsso | 2:298679fff37c | 455 | |
mkarlsso | 2:298679fff37c | 456 | //We use a buffer to send text via the serial port. For every loop |
mkarlsso | 2:298679fff37c | 457 | //in the main loop, we send one character if there is enything to send. |
mkarlsso | 2:298679fff37c | 458 | //This way, outputting text to serial does not hold up other time-sensitive |
mkarlsso | 2:298679fff37c | 459 | //things in the event queue |
mkarlsso | 2:298679fff37c | 460 | if ((textDisplay.unsentData) && (textStreaming)) { |
mkarlsso | 2:298679fff37c | 461 | pc.printf("%c", textDisplay.getNextChar()); |
mkarlsso | 2:298679fff37c | 462 | } |
mkarlsso | 2:298679fff37c | 463 | |
mkarlsso | 2:298679fff37c | 464 | //Here is how we toggle between standalone and slave mode for the clock updating. |
mkarlsso | 2:298679fff37c | 465 | if (changeToSlave) { |
mkarlsso | 2:298679fff37c | 466 | //timeTick1.detach(); |
mkarlsso | 2:298679fff37c | 467 | NVIC_DisableIRQ(TIMER2_IRQn); // Disable the interrupt |
mkarlsso | 2:298679fff37c | 468 | clockExternalIncrement.rise(&callback_clockExternalIncrement); |
mkarlsso | 2:298679fff37c | 469 | clockSlave = true; |
mkarlsso | 2:298679fff37c | 470 | changeToSlave = false; |
mkarlsso | 2:298679fff37c | 471 | changeToStandAlone = false; |
mkarlsso | 2:298679fff37c | 472 | } else if (changeToStandAlone) { |
mkarlsso | 2:298679fff37c | 473 | //timeTick1.attach_us(&incrementTime, 100); |
mkarlsso | 2:298679fff37c | 474 | timer0_init(); |
mkarlsso | 2:298679fff37c | 475 | clockExternalIncrement.rise(NULL); //remove the callback to the external interrupt |
mkarlsso | 2:298679fff37c | 476 | clockSlave = false; |
mkarlsso | 2:298679fff37c | 477 | changeToSlave = false; |
mkarlsso | 2:298679fff37c | 478 | changeToStandAlone = false; |
mkarlsso | 2:298679fff37c | 479 | } |
mkarlsso | 2:298679fff37c | 480 | |
mkarlsso | 2:298679fff37c | 481 | //__enable_irq(); |
mkarlsso | 2:298679fff37c | 482 | |
simon | 0:fb6bbc10ffa0 | 483 | } |
simon | 0:fb6bbc10ffa0 | 484 | } |