Karpova Lab fork of stateScript

Dependencies:   SMARTWAV SOMO_II mbed

Fork of stateScript_v2_karpova by Andy Lustig

Committer:
mkarlsso
Date:
Fri Jan 15 22:13:23 2016 +0000
Revision:
5:abee20c0bf2a
Parent:
3:d7b0a0890d96
Child:
8:436cf2ab961c
Fixed an issue where syntax errors caused the program to crash instead of just reporting the syntax error.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mkarlsso 0:8dbd6bd9167f 1 #include "mbedInterface.h"
mkarlsso 0:8dbd6bd9167f 2 #include "mbed.h"
mkarlsso 0:8dbd6bd9167f 3
mkarlsso 0:8dbd6bd9167f 4
mkarlsso 0:8dbd6bd9167f 5
mkarlsso 0:8dbd6bd9167f 6 //--------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 7 //--------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 8 //This section is required for all custom harware interfaces
mkarlsso 0:8dbd6bd9167f 9
mkarlsso 0:8dbd6bd9167f 10 //globals defined in hardwareInterface.cpp
mkarlsso 0:8dbd6bd9167f 11 extern uint32_t timeKeeper; //the master clock
mkarlsso 3:d7b0a0890d96 12 uint32_t uSec_SinceLastClockInc = 0;
mkarlsso 3:d7b0a0890d96 13 uint32_t uSec_SinceLastReset = 0;
mkarlsso 3:d7b0a0890d96 14
mkarlsso 0:8dbd6bd9167f 15 extern bool resetTimer;
mkarlsso 0:8dbd6bd9167f 16 extern bool clockSlave;
mkarlsso 0:8dbd6bd9167f 17 extern bool changeToSlave;
mkarlsso 0:8dbd6bd9167f 18 extern bool changeToStandAlone;
mkarlsso 0:8dbd6bd9167f 19 extern outputStream textDisplay;
mkarlsso 0:8dbd6bd9167f 20
mkarlsso 5:abee20c0bf2a 21 int externalIncrementMod = 1;
mkarlsso 0:8dbd6bd9167f 22 int externalIncrementCounter = 0;
mkarlsso 0:8dbd6bd9167f 23
mkarlsso 0:8dbd6bd9167f 24
mkarlsso 0:8dbd6bd9167f 25 #ifdef MBED_RF
mkarlsso 3:d7b0a0890d96 26 //We are listening to an RF signal for hardware syncing----------------------------------------
mkarlsso 0:8dbd6bd9167f 27 //Uses DB9->RJ45 connector to map the following channels:
mkarlsso 0:8dbd6bd9167f 28 //1: P28 Clock Signal In
mkarlsso 0:8dbd6bd9167f 29 //2: P27 Data Signal In
mkarlsso 0:8dbd6bd9167f 30 //3: P20 FiberLED Out Signal
mkarlsso 0:8dbd6bd9167f 31 //4: 5V
mkarlsso 0:8dbd6bd9167f 32 //5: GND
mkarlsso 0:8dbd6bd9167f 33 //6: P6 NC
mkarlsso 0:8dbd6bd9167f 34 //7: P7 NC
mkarlsso 0:8dbd6bd9167f 35 //8: P8 NC
mkarlsso 0:8dbd6bd9167f 36
mkarlsso 0:8dbd6bd9167f 37 DigitalOut mainLED(LED1);
mkarlsso 0:8dbd6bd9167f 38 DigitalOut secondaryLED(LED2);
mkarlsso 0:8dbd6bd9167f 39
mkarlsso 0:8dbd6bd9167f 40 bool lightOn = false;
mkarlsso 0:8dbd6bd9167f 41 bool lightOn2 = false;
mkarlsso 0:8dbd6bd9167f 42 int pulseCounter = 0;
mkarlsso 0:8dbd6bd9167f 43 uint32_t lastPulse = 0;
mkarlsso 0:8dbd6bd9167f 44 uint32_t currentRFTime = 0;
mkarlsso 0:8dbd6bd9167f 45 uint32_t lastRFTime = 0;
mkarlsso 0:8dbd6bd9167f 46 uint32_t timeKeeperAtCurrentRFTime = 0;
mkarlsso 0:8dbd6bd9167f 47 uint32_t timeKeeperAtLastRFTime = 0;
mkarlsso 0:8dbd6bd9167f 48 int RFSyncCounter = 0;
mkarlsso 0:8dbd6bd9167f 49 bool RFSyncReadable = false;
mkarlsso 0:8dbd6bd9167f 50 bool RFSyncWritable = true;
mkarlsso 0:8dbd6bd9167f 51
mkarlsso 0:8dbd6bd9167f 52 //Recieve clock signal from RF system
mkarlsso 0:8dbd6bd9167f 53 InterruptIn RFClock(p28);
mkarlsso 0:8dbd6bd9167f 54 DigitalIn RFData(p27);
mkarlsso 0:8dbd6bd9167f 55
mkarlsso 0:8dbd6bd9167f 56
mkarlsso 0:8dbd6bd9167f 57 void callback_RFClock(void) {
mkarlsso 0:8dbd6bd9167f 58
mkarlsso 0:8dbd6bd9167f 59 //if this amount of time has passed since the last pulse, we have a new timestamp
mkarlsso 0:8dbd6bd9167f 60 if ((timeKeeper-lastPulse) > 4) {
mkarlsso 0:8dbd6bd9167f 61
mkarlsso 0:8dbd6bd9167f 62 //make sure the previous timestamp was 32 pulses
mkarlsso 0:8dbd6bd9167f 63 //if so, update lastRFTime
mkarlsso 0:8dbd6bd9167f 64 //we only process every 100th stamp (once every 10 seconds with 10 Hz timestamps)
mkarlsso 0:8dbd6bd9167f 65 if ((pulseCounter == 31) && (RFSyncCounter == 99)){
mkarlsso 0:8dbd6bd9167f 66 if (RFSyncWritable) {
mkarlsso 0:8dbd6bd9167f 67 lastRFTime = currentRFTime;
mkarlsso 0:8dbd6bd9167f 68 timeKeeperAtLastRFTime = timeKeeperAtCurrentRFTime;
mkarlsso 0:8dbd6bd9167f 69 RFSyncReadable = true;
mkarlsso 0:8dbd6bd9167f 70 RFSyncWritable = false;
mkarlsso 0:8dbd6bd9167f 71 }
mkarlsso 0:8dbd6bd9167f 72 }
mkarlsso 0:8dbd6bd9167f 73
mkarlsso 0:8dbd6bd9167f 74 pulseCounter = 0;
mkarlsso 0:8dbd6bd9167f 75 currentRFTime = 0;
mkarlsso 0:8dbd6bd9167f 76 timeKeeperAtCurrentRFTime = timeKeeper;
mkarlsso 0:8dbd6bd9167f 77 RFSyncCounter = (RFSyncCounter+1)%100;
mkarlsso 0:8dbd6bd9167f 78
mkarlsso 0:8dbd6bd9167f 79
mkarlsso 0:8dbd6bd9167f 80 if (lightOn) {
mkarlsso 0:8dbd6bd9167f 81 mainLED = 0;
mkarlsso 0:8dbd6bd9167f 82 lightOn = false;
mkarlsso 0:8dbd6bd9167f 83 } else {
mkarlsso 0:8dbd6bd9167f 84 mainLED = 1;
mkarlsso 0:8dbd6bd9167f 85 lightOn = true;
mkarlsso 0:8dbd6bd9167f 86 }
mkarlsso 0:8dbd6bd9167f 87 } else {
mkarlsso 0:8dbd6bd9167f 88 if (pulseCounter < 32) {
mkarlsso 0:8dbd6bd9167f 89 currentRFTime = (currentRFTime | ( RFData.read() << pulseCounter));
mkarlsso 0:8dbd6bd9167f 90 pulseCounter++;
mkarlsso 0:8dbd6bd9167f 91 }
mkarlsso 0:8dbd6bd9167f 92 }
mkarlsso 0:8dbd6bd9167f 93 lastPulse = timeKeeper;
mkarlsso 0:8dbd6bd9167f 94 }
mkarlsso 0:8dbd6bd9167f 95
mkarlsso 0:8dbd6bd9167f 96 //------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 97 #endif
mkarlsso 0:8dbd6bd9167f 98
mkarlsso 3:d7b0a0890d96 99 void externalClockIncDown() {
mkarlsso 0:8dbd6bd9167f 100
mkarlsso 3:d7b0a0890d96 101 //The external clock increment signal pulse has come back down. If the pulse was long
mkarlsso 3:d7b0a0890d96 102 //enough, then we condsider it a valid pulse (the pulses should be 0.5 ms long)
mkarlsso 3:d7b0a0890d96 103 if ((clockSlave)&&(uSec_SinceLastClockInc >= 300)) {
mkarlsso 3:d7b0a0890d96 104 uSec_SinceLastClockInc = 0;
mkarlsso 3:d7b0a0890d96 105 timeKeeper++;
mkarlsso 3:d7b0a0890d96 106
mkarlsso 3:d7b0a0890d96 107 //Clock resets happen upon update so we dont get a partial first ms
mkarlsso 3:d7b0a0890d96 108 if (resetTimer) {
mkarlsso 3:d7b0a0890d96 109 uSec_SinceLastReset = 0;
mkarlsso 3:d7b0a0890d96 110 timeKeeper = 0;
mkarlsso 3:d7b0a0890d96 111 resetTimer = false;
mkarlsso 3:d7b0a0890d96 112 }
mkarlsso 3:d7b0a0890d96 113 }
mkarlsso 3:d7b0a0890d96 114
mkarlsso 3:d7b0a0890d96 115 }
mkarlsso 3:d7b0a0890d96 116
mkarlsso 3:d7b0a0890d96 117 void externalResetDown() {
mkarlsso 3:d7b0a0890d96 118
mkarlsso 3:d7b0a0890d96 119 //The external clock reset signal pulse has come back down. If the pulse was long
mkarlsso 3:d7b0a0890d96 120 //enough, then we condsider it a valid pulse (the pulses should be 1 ms long)
mkarlsso 5:abee20c0bf2a 121 /*
mkarlsso 5:abee20c0bf2a 122 textDisplay << uSec_SinceLastReset;
mkarlsso 3:d7b0a0890d96 123 if ((clockSlave)&&(uSec_SinceLastReset >= 700)) {
mkarlsso 3:d7b0a0890d96 124 uSec_SinceLastReset = 0;
mkarlsso 3:d7b0a0890d96 125 timeKeeper = 1; //It has been 1ms since the reset pulse went up
mkarlsso 3:d7b0a0890d96 126 textDisplay << timeKeeper << " Clock reset\r\n";
mkarlsso 5:abee20c0bf2a 127 }*/
mkarlsso 3:d7b0a0890d96 128 }
mkarlsso 0:8dbd6bd9167f 129
mkarlsso 0:8dbd6bd9167f 130 //------------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 131 //------------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 132
mkarlsso 0:8dbd6bd9167f 133 //MBED-specific stuff
mkarlsso 0:8dbd6bd9167f 134 //---------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 135
mkarlsso 0:8dbd6bd9167f 136 //translate pin numbers to hardware pins
mkarlsso 5:abee20c0bf2a 137 PinName outPins[NUMOUTPORTS] = {p11,p13,p15,p18,p21,p23,p25,p29,p20,p6}; //Old board output pins
mkarlsso 5:abee20c0bf2a 138 //PinName outPins[NUMPORTS] = {p18,p15,p13,p11,p29,p25,p23,p21,p20}; //New board output pins
mkarlsso 5:abee20c0bf2a 139 PinName inPins[NUMINPORTS] = {p12,p14,p16,p17,p22,p24,p26,p30,p7};
mkarlsso 0:8dbd6bd9167f 140
mkarlsso 0:8dbd6bd9167f 141
mkarlsso 3:d7b0a0890d96 142
mkarlsso 0:8dbd6bd9167f 143 //The sound output uses a SmartWav device and their simple serial library
mkarlsso 0:8dbd6bd9167f 144 SMARTWAV sWav(p9,p10,p19); //(TX,RX,Reset);
mkarlsso 0:8dbd6bd9167f 145
mkarlsso 0:8dbd6bd9167f 146 //This is the callback for the MBED timer
mkarlsso 0:8dbd6bd9167f 147 extern "C" void TIMER0_IRQHandler (void) {
mkarlsso 0:8dbd6bd9167f 148
mkarlsso 5:abee20c0bf2a 149 /*if (clockSlave) {
mkarlsso 5:abee20c0bf2a 150
mkarlsso 3:d7b0a0890d96 151 //The function is called every 100 us
mkarlsso 5:abee20c0bf2a 152 if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed
mkarlsso 5:abee20c0bf2a 153
mkarlsso 5:abee20c0bf2a 154 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag
mkarlsso 5:abee20c0bf2a 155 uSec_SinceLastClockInc = uSec_SinceLastClockInc+100;
mkarlsso 5:abee20c0bf2a 156 uSec_SinceLastReset = uSec_SinceLastReset+100;
mkarlsso 5:abee20c0bf2a 157
mkarlsso 5:abee20c0bf2a 158 }
mkarlsso 5:abee20c0bf2a 159
mkarlsso 5:abee20c0bf2a 160
mkarlsso 5:abee20c0bf2a 161 } else {*/
mkarlsso 3:d7b0a0890d96 162 //The function is called every 1 ms
mkarlsso 3:d7b0a0890d96 163 if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed
mkarlsso 0:8dbd6bd9167f 164
mkarlsso 3:d7b0a0890d96 165 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag
mkarlsso 3:d7b0a0890d96 166 timeKeeper++;
mkarlsso 0:8dbd6bd9167f 167
mkarlsso 3:d7b0a0890d96 168 if (resetTimer) {
mkarlsso 3:d7b0a0890d96 169 timeKeeper = 0;
mkarlsso 3:d7b0a0890d96 170 resetTimer = false;
mkarlsso 3:d7b0a0890d96 171 }
mkarlsso 0:8dbd6bd9167f 172 }
mkarlsso 5:abee20c0bf2a 173 //}
mkarlsso 3:d7b0a0890d96 174
mkarlsso 0:8dbd6bd9167f 175 }
mkarlsso 0:8dbd6bd9167f 176 //-----------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 177
mkarlsso 0:8dbd6bd9167f 178
mkarlsso 0:8dbd6bd9167f 179
mkarlsso 0:8dbd6bd9167f 180 MBEDSystem::MBEDSystem():
mkarlsso 0:8dbd6bd9167f 181 clockResetInt(p5),
mkarlsso 0:8dbd6bd9167f 182 clockExternalIncrement(p8) {
mkarlsso 0:8dbd6bd9167f 183
mkarlsso 0:8dbd6bd9167f 184 clockResetInt.rise(this, &MBEDSystem::externalClockReset);
mkarlsso 3:d7b0a0890d96 185 clockResetInt.fall(&externalResetDown);
mkarlsso 0:8dbd6bd9167f 186 clockResetInt.mode(PullDown);
mkarlsso 0:8dbd6bd9167f 187
mkarlsso 0:8dbd6bd9167f 188 clockExternalIncrement.mode(PullDown);
mkarlsso 0:8dbd6bd9167f 189
mkarlsso 0:8dbd6bd9167f 190 #ifdef MBED_RF
mkarlsso 0:8dbd6bd9167f 191 //Karpova version-------------
mkarlsso 0:8dbd6bd9167f 192 //Set up callbacks for RF clock signal
mkarlsso 0:8dbd6bd9167f 193 RFClock.rise(&callback_RFClock);
mkarlsso 0:8dbd6bd9167f 194 RFClock.mode(PullDown);
mkarlsso 0:8dbd6bd9167f 195 #endif
mkarlsso 0:8dbd6bd9167f 196
mkarlsso 0:8dbd6bd9167f 197 //-------------------------------
mkarlsso 0:8dbd6bd9167f 198
mkarlsso 0:8dbd6bd9167f 199 for (int i=0; i < NUMPORTS; i++) {
mkarlsso 0:8dbd6bd9167f 200 dIn[i].init(i);
mkarlsso 0:8dbd6bd9167f 201 dOut[i].init(i);
mkarlsso 0:8dbd6bd9167f 202 }
mkarlsso 0:8dbd6bd9167f 203
mkarlsso 0:8dbd6bd9167f 204
mkarlsso 0:8dbd6bd9167f 205 sWav.reset();
mkarlsso 0:8dbd6bd9167f 206
mkarlsso 0:8dbd6bd9167f 207 }
mkarlsso 0:8dbd6bd9167f 208
mkarlsso 0:8dbd6bd9167f 209 void MBEDSystem::timerinit() {
mkarlsso 0:8dbd6bd9167f 210 //intiatiation of timer (specific to the LPC17xx chip). This is used in
mkarlsso 0:8dbd6bd9167f 211 //standalone mode to increment the clock every ms.
mkarlsso 0:8dbd6bd9167f 212 //----------------------------------------------------
mkarlsso 0:8dbd6bd9167f 213 //LPC_SC->PCLKSEL1 &= (3 << 12); //mask
mkarlsso 0:8dbd6bd9167f 214 //LPC_SC->PCLKSEL1 |= (1 << 12); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual)
mkarlsso 0:8dbd6bd9167f 215 //LPC_SC->PCLKSEL0 &= (3 << 3); //mask
mkarlsso 0:8dbd6bd9167f 216 //LPC_SC->PCLKSEL0 |= (1 << 3); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual)
mkarlsso 3:d7b0a0890d96 217 LPC_SC->PCONP |=1<1; //timer0 power on
mkarlsso 5:abee20c0bf2a 218
mkarlsso 5:abee20c0bf2a 219
mkarlsso 0:8dbd6bd9167f 220 LPC_TIM0->MR0 = 23980; //1 msec
mkarlsso 3:d7b0a0890d96 221
mkarlsso 5:abee20c0bf2a 222
mkarlsso 0:8dbd6bd9167f 223 //LPC_TIM0->PR = (SystemCoreClock / 1000000); //microsecond steps
mkarlsso 0:8dbd6bd9167f 224 //LPC_TIM0->MR0 = 1000; //100 msec
mkarlsso 0:8dbd6bd9167f 225 //LPC_TIM0->MR0 = (SystemCoreClock / 1000000); //microsecond steps
mkarlsso 0:8dbd6bd9167f 226 LPC_TIM0->MCR = 3; //interrupt and reset control
mkarlsso 0:8dbd6bd9167f 227 //3 = Interrupt & reset timer0 on match
mkarlsso 0:8dbd6bd9167f 228 //1 = Interrupt only, no reset of timer0
mkarlsso 0:8dbd6bd9167f 229 NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt
mkarlsso 0:8dbd6bd9167f 230 LPC_TIM0->TCR = 1; //enable Timer0
mkarlsso 0:8dbd6bd9167f 231 //--------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 232 }
mkarlsso 0:8dbd6bd9167f 233
mkarlsso 3:d7b0a0890d96 234 void MBEDSystem::pauseInterrupts() {
mkarlsso 3:d7b0a0890d96 235 __disable_irq();
mkarlsso 3:d7b0a0890d96 236 }
mkarlsso 3:d7b0a0890d96 237
mkarlsso 3:d7b0a0890d96 238 void MBEDSystem::resumeInterrupts() {
mkarlsso 3:d7b0a0890d96 239 __enable_irq();
mkarlsso 3:d7b0a0890d96 240 }
mkarlsso 3:d7b0a0890d96 241
mkarlsso 0:8dbd6bd9167f 242 void MBEDSystem::mainLoopToDo() {
mkarlsso 0:8dbd6bd9167f 243 #ifdef MBED_RF
mkarlsso 0:8dbd6bd9167f 244 //Karpova version--------------------------
mkarlsso 0:8dbd6bd9167f 245 //Karpova lab RF addition. Print out time sync.
mkarlsso 0:8dbd6bd9167f 246 if (RFSyncReadable) {
mkarlsso 0:8dbd6bd9167f 247 /*
mkarlsso 0:8dbd6bd9167f 248 ostringstream RFTimeConvert;
mkarlsso 0:8dbd6bd9167f 249 RFTimeConvert << timeKeeperAtLastRFTime << " " << "RFsync " << lastRFTime << " "; //broadcast the earliest timestamp when a change occured
mkarlsso 0:8dbd6bd9167f 250
mkarlsso 0:8dbd6bd9167f 251 textDisplay.send(RFTimeConvert.str() + "\r\n");
mkarlsso 0:8dbd6bd9167f 252 RFTimeConvert.clear();
mkarlsso 0:8dbd6bd9167f 253 RFTimeConvert.seekp(0);
mkarlsso 0:8dbd6bd9167f 254 */
mkarlsso 0:8dbd6bd9167f 255
mkarlsso 0:8dbd6bd9167f 256 textDisplay << timeKeeperAtLastRFTime << " RFsync " << lastRFTime << "\r\n";
mkarlsso 0:8dbd6bd9167f 257
mkarlsso 0:8dbd6bd9167f 258 RFSyncReadable = false;
mkarlsso 0:8dbd6bd9167f 259 RFSyncWritable = true;
mkarlsso 0:8dbd6bd9167f 260 }
mkarlsso 0:8dbd6bd9167f 261 //------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 262 #endif
mkarlsso 0:8dbd6bd9167f 263 }
mkarlsso 0:8dbd6bd9167f 264
mkarlsso 0:8dbd6bd9167f 265 sDigitalOut* MBEDSystem::getDigitalOutPtr(int portNum){
mkarlsso 0:8dbd6bd9167f 266 if (portNum < NUMPORTS) {
mkarlsso 0:8dbd6bd9167f 267 return &dOut[portNum];
mkarlsso 0:8dbd6bd9167f 268 } else {
mkarlsso 0:8dbd6bd9167f 269 return NULL;
mkarlsso 0:8dbd6bd9167f 270 }
mkarlsso 0:8dbd6bd9167f 271 }
mkarlsso 0:8dbd6bd9167f 272
mkarlsso 0:8dbd6bd9167f 273 sDigitalIn* MBEDSystem::getDigitalInPtr(int portNum) {
mkarlsso 0:8dbd6bd9167f 274 if (portNum < NUMPORTS) {
mkarlsso 0:8dbd6bd9167f 275 return &dIn[portNum];
mkarlsso 0:8dbd6bd9167f 276 } else {
mkarlsso 0:8dbd6bd9167f 277 return NULL;
mkarlsso 0:8dbd6bd9167f 278 }
mkarlsso 0:8dbd6bd9167f 279 }
mkarlsso 0:8dbd6bd9167f 280
mkarlsso 0:8dbd6bd9167f 281 sSound* MBEDSystem::createNewSoundAction() {
mkarlsso 0:8dbd6bd9167f 282 MBEDSound *tmpSound = new MBEDSound();
mkarlsso 0:8dbd6bd9167f 283 return tmpSound;
mkarlsso 0:8dbd6bd9167f 284 }
mkarlsso 0:8dbd6bd9167f 285
mkarlsso 0:8dbd6bd9167f 286
mkarlsso 0:8dbd6bd9167f 287 void MBEDSystem::externalClockReset() {
mkarlsso 0:8dbd6bd9167f 288
mkarlsso 3:d7b0a0890d96 289 //The pulse has gone high. When the pulse comes down we will check to see if it was long enough to be valid.
mkarlsso 3:d7b0a0890d96 290 uSec_SinceLastReset = 0;
mkarlsso 3:d7b0a0890d96 291
mkarlsso 3:d7b0a0890d96 292
mkarlsso 5:abee20c0bf2a 293
mkarlsso 0:8dbd6bd9167f 294 if (clockSlave) {
mkarlsso 0:8dbd6bd9167f 295 LPC_TIM0->TCR = 0x02; // reset timer
mkarlsso 0:8dbd6bd9167f 296 externalIncrementCounter = 0;
mkarlsso 5:abee20c0bf2a 297
mkarlsso 0:8dbd6bd9167f 298 immediateClockReset();
mkarlsso 5:abee20c0bf2a 299 }
mkarlsso 0:8dbd6bd9167f 300
mkarlsso 0:8dbd6bd9167f 301 }
mkarlsso 0:8dbd6bd9167f 302
mkarlsso 0:8dbd6bd9167f 303 void MBEDSystem::setStandAloneClock() {
mkarlsso 3:d7b0a0890d96 304 clockSlave = false;
mkarlsso 3:d7b0a0890d96 305 NVIC_DisableIRQ(TIMER0_IRQn); // Disable the interrupt
mkarlsso 3:d7b0a0890d96 306 timerinit(); //set up and enable interrupt
mkarlsso 0:8dbd6bd9167f 307 clockExternalIncrement.rise(NULL); //remove the callback to the external interrupt
mkarlsso 3:d7b0a0890d96 308 //clockExternalIncrement.fall(NULL);
mkarlsso 0:8dbd6bd9167f 309 changeToSlave = false;
mkarlsso 0:8dbd6bd9167f 310 changeToStandAlone = false;
mkarlsso 0:8dbd6bd9167f 311 }
mkarlsso 0:8dbd6bd9167f 312
mkarlsso 0:8dbd6bd9167f 313 void MBEDSystem::setSlaveClock() {
mkarlsso 3:d7b0a0890d96 314 clockSlave = true;
mkarlsso 0:8dbd6bd9167f 315 NVIC_DisableIRQ(TIMER0_IRQn); // Disable the interrupt
mkarlsso 3:d7b0a0890d96 316 //timerinit(); //set up and enable interrupt
mkarlsso 0:8dbd6bd9167f 317 clockExternalIncrement.rise(this, &MBEDSystem::incrementClock);
mkarlsso 3:d7b0a0890d96 318 //clockExternalIncrement.fall(&externalClockIncDown);
mkarlsso 0:8dbd6bd9167f 319 clockSlave = true;
mkarlsso 0:8dbd6bd9167f 320 changeToSlave = false;
mkarlsso 0:8dbd6bd9167f 321 changeToStandAlone = false;
mkarlsso 0:8dbd6bd9167f 322 }
mkarlsso 0:8dbd6bd9167f 323
mkarlsso 0:8dbd6bd9167f 324 void MBEDSystem::incrementClock() {
mkarlsso 0:8dbd6bd9167f 325
mkarlsso 0:8dbd6bd9167f 326 if (clockSlave) {
mkarlsso 3:d7b0a0890d96 327 //The pulse has gone high. When the pulse comes down we will check to see if it was long enough to be valid.
mkarlsso 3:d7b0a0890d96 328 //uSec_SinceLastClockInc = 0;
mkarlsso 3:d7b0a0890d96 329
mkarlsso 3:d7b0a0890d96 330
mkarlsso 3:d7b0a0890d96 331 //The clock is incremented
mkarlsso 3:d7b0a0890d96 332
mkarlsso 0:8dbd6bd9167f 333 externalIncrementCounter = (externalIncrementCounter+1) % externalIncrementMod;
mkarlsso 0:8dbd6bd9167f 334 if (externalIncrementCounter==0) {
mkarlsso 0:8dbd6bd9167f 335 timeKeeper++;
mkarlsso 0:8dbd6bd9167f 336 }
mkarlsso 0:8dbd6bd9167f 337
mkarlsso 0:8dbd6bd9167f 338 } else {
mkarlsso 0:8dbd6bd9167f 339 timeKeeper++;
mkarlsso 0:8dbd6bd9167f 340 }
mkarlsso 0:8dbd6bd9167f 341 //Clock resets happen upon update so we dont get a partial first ms
mkarlsso 3:d7b0a0890d96 342 /*
mkarlsso 0:8dbd6bd9167f 343 if (resetTimer) {
mkarlsso 3:d7b0a0890d96 344 uSec_SinceLastReset = 0;
mkarlsso 0:8dbd6bd9167f 345 timeKeeper = 0;
mkarlsso 0:8dbd6bd9167f 346 resetTimer = false;
mkarlsso 3:d7b0a0890d96 347 }*/
mkarlsso 0:8dbd6bd9167f 348 }
mkarlsso 0:8dbd6bd9167f 349
mkarlsso 0:8dbd6bd9167f 350 //-----------------------------------------------------
mkarlsso 0:8dbd6bd9167f 351
mkarlsso 0:8dbd6bd9167f 352 MBEDSound::MBEDSound() {
mkarlsso 0:8dbd6bd9167f 353
mkarlsso 0:8dbd6bd9167f 354 }
mkarlsso 0:8dbd6bd9167f 355
mkarlsso 0:8dbd6bd9167f 356 void MBEDSound::execute() {
mkarlsso 0:8dbd6bd9167f 357 if (reset) {
mkarlsso 0:8dbd6bd9167f 358 sWav.reset();
mkarlsso 0:8dbd6bd9167f 359 } else if (!play) {
mkarlsso 0:8dbd6bd9167f 360 sWav.stopTrack();
mkarlsso 0:8dbd6bd9167f 361 } else {
mkarlsso 0:8dbd6bd9167f 362 if (volume > -1) {
mkarlsso 0:8dbd6bd9167f 363 sWav.volume(volume);
mkarlsso 0:8dbd6bd9167f 364 } else if (volumePtr != NULL) {
mkarlsso 0:8dbd6bd9167f 365 sWav.volume(*volumePtr);
mkarlsso 0:8dbd6bd9167f 366 }
mkarlsso 0:8dbd6bd9167f 367
mkarlsso 0:8dbd6bd9167f 368 if (fileNameExists) {
mkarlsso 0:8dbd6bd9167f 369 //sWav.playTracks();
mkarlsso 0:8dbd6bd9167f 370 sWav.stopTrack();
mkarlsso 0:8dbd6bd9167f 371 sWav.playTrackName(fileName);
mkarlsso 0:8dbd6bd9167f 372 }
mkarlsso 0:8dbd6bd9167f 373 }
mkarlsso 0:8dbd6bd9167f 374 }
mkarlsso 0:8dbd6bd9167f 375
mkarlsso 0:8dbd6bd9167f 376 //-----------------------------------------------------
mkarlsso 0:8dbd6bd9167f 377 MBEDDigitalOut::MBEDDigitalOut() {
mkarlsso 5:abee20c0bf2a 378 pinExists = false;
mkarlsso 0:8dbd6bd9167f 379 }
mkarlsso 0:8dbd6bd9167f 380
mkarlsso 0:8dbd6bd9167f 381 void MBEDDigitalOut::init(int pin) {
mkarlsso 5:abee20c0bf2a 382 if (pin < NUMOUTPORTS) {
mkarlsso 5:abee20c0bf2a 383 outpin = new DigitalOut(outPins[pin]);
mkarlsso 5:abee20c0bf2a 384 pinExists = true;
mkarlsso 5:abee20c0bf2a 385 }
mkarlsso 0:8dbd6bd9167f 386 }
mkarlsso 0:8dbd6bd9167f 387
mkarlsso 0:8dbd6bd9167f 388 int MBEDDigitalOut::read() {
mkarlsso 5:abee20c0bf2a 389 if (pinExists) {
mkarlsso 5:abee20c0bf2a 390 return outpin->read();
mkarlsso 5:abee20c0bf2a 391 } else {
mkarlsso 5:abee20c0bf2a 392 return 0;
mkarlsso 5:abee20c0bf2a 393 }
mkarlsso 0:8dbd6bd9167f 394 }
mkarlsso 0:8dbd6bd9167f 395
mkarlsso 0:8dbd6bd9167f 396 void MBEDDigitalOut::write(int value) {
mkarlsso 5:abee20c0bf2a 397 if (pinExists) {
mkarlsso 5:abee20c0bf2a 398
mkarlsso 5:abee20c0bf2a 399 outpin->write(value);
mkarlsso 5:abee20c0bf2a 400 }
mkarlsso 0:8dbd6bd9167f 401 }
mkarlsso 0:8dbd6bd9167f 402 //--------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 403
mkarlsso 0:8dbd6bd9167f 404 MBEDDigitalIn::MBEDDigitalIn() {
mkarlsso 5:abee20c0bf2a 405 pinExists = false;
mkarlsso 0:8dbd6bd9167f 406 }
mkarlsso 0:8dbd6bd9167f 407
mkarlsso 0:8dbd6bd9167f 408 void MBEDDigitalIn::init(int pin) {
mkarlsso 5:abee20c0bf2a 409
mkarlsso 5:abee20c0bf2a 410 if (pin < NUMINPORTS) {
mkarlsso 5:abee20c0bf2a 411 inpin = new DigitalIn(inPins[pin]);
mkarlsso 5:abee20c0bf2a 412 inpin_interrupt = new InterruptIn(inPins[pin]);
mkarlsso 5:abee20c0bf2a 413 inpin->mode(PullDown);
mkarlsso 5:abee20c0bf2a 414 //Set up callbacks for the port interrupts
mkarlsso 5:abee20c0bf2a 415 inpin_interrupt->rise(this, &MBEDDigitalIn::interrupt_up_callback);
mkarlsso 5:abee20c0bf2a 416 inpin_interrupt->fall(this, &MBEDDigitalIn::interrupt_down_callback);
mkarlsso 5:abee20c0bf2a 417 pinExists = true;
mkarlsso 5:abee20c0bf2a 418 }
mkarlsso 0:8dbd6bd9167f 419 }
mkarlsso 0:8dbd6bd9167f 420
mkarlsso 0:8dbd6bd9167f 421 int MBEDDigitalIn::read() {
mkarlsso 5:abee20c0bf2a 422 if (pinExists) {
mkarlsso 5:abee20c0bf2a 423 return inpin->read();
mkarlsso 5:abee20c0bf2a 424 } else {
mkarlsso 5:abee20c0bf2a 425 return 0;
mkarlsso 5:abee20c0bf2a 426 }
mkarlsso 0:8dbd6bd9167f 427 }
mkarlsso 0:8dbd6bd9167f 428
mkarlsso 0:8dbd6bd9167f 429 void MBEDDigitalIn::interrupt_up_callback() {
mkarlsso 0:8dbd6bd9167f 430 addStateChange(1, timeKeeper);
mkarlsso 0:8dbd6bd9167f 431 }
mkarlsso 0:8dbd6bd9167f 432
mkarlsso 0:8dbd6bd9167f 433 void MBEDDigitalIn::interrupt_down_callback() {
mkarlsso 0:8dbd6bd9167f 434 addStateChange(0, timeKeeper);
mkarlsso 0:8dbd6bd9167f 435 }
mkarlsso 0:8dbd6bd9167f 436
mkarlsso 0:8dbd6bd9167f 437 //----------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 438 MBEDSerialPort::MBEDSerialPort() {
mkarlsso 0:8dbd6bd9167f 439
mkarlsso 0:8dbd6bd9167f 440 }
mkarlsso 0:8dbd6bd9167f 441
mkarlsso 0:8dbd6bd9167f 442 void MBEDSerialPort::init() {
mkarlsso 0:8dbd6bd9167f 443 //Initialize serial communication
mkarlsso 0:8dbd6bd9167f 444 serialToPC = new Serial(USBTX, USBRX); // tx, rx
mkarlsso 0:8dbd6bd9167f 445 serialToPC->baud(115200);
mkarlsso 0:8dbd6bd9167f 446
mkarlsso 0:8dbd6bd9167f 447
mkarlsso 0:8dbd6bd9167f 448 }
mkarlsso 0:8dbd6bd9167f 449
mkarlsso 0:8dbd6bd9167f 450 bool MBEDSerialPort::readable() {
mkarlsso 0:8dbd6bd9167f 451 return serialToPC->readable();
mkarlsso 0:8dbd6bd9167f 452 }
mkarlsso 0:8dbd6bd9167f 453
mkarlsso 0:8dbd6bd9167f 454 char MBEDSerialPort::readChar() {
mkarlsso 0:8dbd6bd9167f 455 return serialToPC->getc();
mkarlsso 0:8dbd6bd9167f 456 }
mkarlsso 0:8dbd6bd9167f 457
mkarlsso 0:8dbd6bd9167f 458 void MBEDSerialPort::writeChar(char s) {
mkarlsso 0:8dbd6bd9167f 459 serialToPC->printf("%c", s);
mkarlsso 0:8dbd6bd9167f 460 }
mkarlsso 3:d7b0a0890d96 461
mkarlsso 3:d7b0a0890d96 462 int MBEDSerialPort::requestToWriteString(char *s, int numBytesRequested) {
mkarlsso 3:d7b0a0890d96 463 //request to print a string to the serial output buffer
mkarlsso 3:d7b0a0890d96 464 //function returns the number of chars actually accepted for output
mkarlsso 3:d7b0a0890d96 465 int numBytesAccepted = 0;
mkarlsso 3:d7b0a0890d96 466 while (numBytesAccepted < numBytesRequested) {
mkarlsso 3:d7b0a0890d96 467
mkarlsso 3:d7b0a0890d96 468 writeChar(*(s+numBytesAccepted));
mkarlsso 3:d7b0a0890d96 469 numBytesAccepted++;
mkarlsso 3:d7b0a0890d96 470 }
mkarlsso 3:d7b0a0890d96 471
mkarlsso 3:d7b0a0890d96 472 return numBytesAccepted;
mkarlsso 3:d7b0a0890d96 473 }