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