Karpova Lab fork of stateScript

Dependencies:   SMARTWAV mbed

Fork of stateScript_v2 by Mattias Karlsson

Committer:
mkarlsso
Date:
Sat Oct 10 22:37:17 2015 +0000
Revision:
3:d7b0a0890d96
Parent:
0:8dbd6bd9167f
Changes ports for new PCB board

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 0:8dbd6bd9167f 21 int externalIncrementMod = 30;
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 3:d7b0a0890d96 121 if ((clockSlave)&&(uSec_SinceLastReset >= 700)) {
mkarlsso 3:d7b0a0890d96 122 uSec_SinceLastReset = 0;
mkarlsso 3:d7b0a0890d96 123 timeKeeper = 1; //It has been 1ms since the reset pulse went up
mkarlsso 3:d7b0a0890d96 124 textDisplay << timeKeeper << " Clock reset\r\n";
mkarlsso 3:d7b0a0890d96 125 }
mkarlsso 3:d7b0a0890d96 126 }
mkarlsso 0:8dbd6bd9167f 127
mkarlsso 0:8dbd6bd9167f 128 //------------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 129 //------------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 130
mkarlsso 0:8dbd6bd9167f 131 //MBED-specific stuff
mkarlsso 0:8dbd6bd9167f 132 //---------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 133
mkarlsso 0:8dbd6bd9167f 134 //translate pin numbers to hardware pins
mkarlsso 3:d7b0a0890d96 135 //PinName outPins[NUMPORTS] = {p11,p13,p15,p18,p21,p23,p25,p29,p20};
mkarlsso 3:d7b0a0890d96 136 PinName outPins[NUMPORTS] = {p18,p15,p13,p11,p29,p25,p23,p21,p20};
mkarlsso 0:8dbd6bd9167f 137 PinName inPins[NUMPORTS] = {p12,p14,p16,p17,p22,p24,p26,p30,p7};
mkarlsso 0:8dbd6bd9167f 138
mkarlsso 0:8dbd6bd9167f 139
mkarlsso 0:8dbd6bd9167f 140
mkarlsso 3:d7b0a0890d96 141
mkarlsso 0:8dbd6bd9167f 142 //The sound output uses a SmartWav device and their simple serial library
mkarlsso 0:8dbd6bd9167f 143 SMARTWAV sWav(p9,p10,p19); //(TX,RX,Reset);
mkarlsso 0:8dbd6bd9167f 144
mkarlsso 0:8dbd6bd9167f 145 //This is the callback for the MBED timer
mkarlsso 0:8dbd6bd9167f 146 extern "C" void TIMER0_IRQHandler (void) {
mkarlsso 0:8dbd6bd9167f 147
mkarlsso 3:d7b0a0890d96 148 if (clockSlave) {
mkarlsso 3:d7b0a0890d96 149 //The function is called every 100 us
mkarlsso 3:d7b0a0890d96 150 uSec_SinceLastClockInc = uSec_SinceLastClockInc+100;
mkarlsso 3:d7b0a0890d96 151 uSec_SinceLastReset = uSec_SinceLastReset+100;
mkarlsso 3:d7b0a0890d96 152 } else {
mkarlsso 3:d7b0a0890d96 153 //The function is called every 1 ms
mkarlsso 3:d7b0a0890d96 154 if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed
mkarlsso 0:8dbd6bd9167f 155
mkarlsso 3:d7b0a0890d96 156 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag
mkarlsso 3:d7b0a0890d96 157 timeKeeper++;
mkarlsso 0:8dbd6bd9167f 158
mkarlsso 3:d7b0a0890d96 159 if (resetTimer) {
mkarlsso 3:d7b0a0890d96 160 timeKeeper = 0;
mkarlsso 3:d7b0a0890d96 161 resetTimer = false;
mkarlsso 3:d7b0a0890d96 162 }
mkarlsso 0:8dbd6bd9167f 163 }
mkarlsso 0:8dbd6bd9167f 164 }
mkarlsso 3:d7b0a0890d96 165
mkarlsso 0:8dbd6bd9167f 166 }
mkarlsso 0:8dbd6bd9167f 167 //-----------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 168
mkarlsso 0:8dbd6bd9167f 169
mkarlsso 0:8dbd6bd9167f 170
mkarlsso 0:8dbd6bd9167f 171 MBEDSystem::MBEDSystem():
mkarlsso 0:8dbd6bd9167f 172 clockResetInt(p5),
mkarlsso 0:8dbd6bd9167f 173 clockExternalIncrement(p8) {
mkarlsso 0:8dbd6bd9167f 174
mkarlsso 0:8dbd6bd9167f 175 clockResetInt.rise(this, &MBEDSystem::externalClockReset);
mkarlsso 3:d7b0a0890d96 176 clockResetInt.fall(&externalResetDown);
mkarlsso 0:8dbd6bd9167f 177 clockResetInt.mode(PullDown);
mkarlsso 0:8dbd6bd9167f 178
mkarlsso 0:8dbd6bd9167f 179 clockExternalIncrement.mode(PullDown);
mkarlsso 0:8dbd6bd9167f 180
mkarlsso 0:8dbd6bd9167f 181 #ifdef MBED_RF
mkarlsso 0:8dbd6bd9167f 182 //Karpova version-------------
mkarlsso 0:8dbd6bd9167f 183 //Set up callbacks for RF clock signal
mkarlsso 0:8dbd6bd9167f 184 RFClock.rise(&callback_RFClock);
mkarlsso 0:8dbd6bd9167f 185 RFClock.mode(PullDown);
mkarlsso 0:8dbd6bd9167f 186 #endif
mkarlsso 0:8dbd6bd9167f 187
mkarlsso 0:8dbd6bd9167f 188 //-------------------------------
mkarlsso 0:8dbd6bd9167f 189
mkarlsso 0:8dbd6bd9167f 190 for (int i=0; i < NUMPORTS; i++) {
mkarlsso 0:8dbd6bd9167f 191 dIn[i].init(i);
mkarlsso 0:8dbd6bd9167f 192 dOut[i].init(i);
mkarlsso 0:8dbd6bd9167f 193 }
mkarlsso 0:8dbd6bd9167f 194
mkarlsso 0:8dbd6bd9167f 195
mkarlsso 0:8dbd6bd9167f 196
mkarlsso 0:8dbd6bd9167f 197 sWav.reset();
mkarlsso 0:8dbd6bd9167f 198
mkarlsso 0:8dbd6bd9167f 199 }
mkarlsso 0:8dbd6bd9167f 200
mkarlsso 0:8dbd6bd9167f 201 void MBEDSystem::timerinit() {
mkarlsso 0:8dbd6bd9167f 202 //intiatiation of timer (specific to the LPC17xx chip). This is used in
mkarlsso 0:8dbd6bd9167f 203 //standalone mode to increment the clock every ms.
mkarlsso 0:8dbd6bd9167f 204 //----------------------------------------------------
mkarlsso 0:8dbd6bd9167f 205 //LPC_SC->PCLKSEL1 &= (3 << 12); //mask
mkarlsso 0:8dbd6bd9167f 206 //LPC_SC->PCLKSEL1 |= (1 << 12); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual)
mkarlsso 0:8dbd6bd9167f 207 //LPC_SC->PCLKSEL0 &= (3 << 3); //mask
mkarlsso 0:8dbd6bd9167f 208 //LPC_SC->PCLKSEL0 |= (1 << 3); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual)
mkarlsso 3:d7b0a0890d96 209 LPC_SC->PCONP |=1<1; //timer0 power on
mkarlsso 0:8dbd6bd9167f 210 LPC_TIM0->MR0 = 23980; //1 msec
mkarlsso 3:d7b0a0890d96 211
mkarlsso 0:8dbd6bd9167f 212 //LPC_TIM0->PR = (SystemCoreClock / 1000000); //microsecond steps
mkarlsso 0:8dbd6bd9167f 213 //LPC_TIM0->MR0 = 1000; //100 msec
mkarlsso 0:8dbd6bd9167f 214 //LPC_TIM0->MR0 = (SystemCoreClock / 1000000); //microsecond steps
mkarlsso 0:8dbd6bd9167f 215 LPC_TIM0->MCR = 3; //interrupt and reset control
mkarlsso 0:8dbd6bd9167f 216 //3 = Interrupt & reset timer0 on match
mkarlsso 0:8dbd6bd9167f 217 //1 = Interrupt only, no reset of timer0
mkarlsso 0:8dbd6bd9167f 218 NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt
mkarlsso 0:8dbd6bd9167f 219 LPC_TIM0->TCR = 1; //enable Timer0
mkarlsso 0:8dbd6bd9167f 220 //--------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 221 }
mkarlsso 0:8dbd6bd9167f 222
mkarlsso 3:d7b0a0890d96 223 void MBEDSystem::pauseInterrupts() {
mkarlsso 3:d7b0a0890d96 224 __disable_irq();
mkarlsso 3:d7b0a0890d96 225 }
mkarlsso 3:d7b0a0890d96 226
mkarlsso 3:d7b0a0890d96 227 void MBEDSystem::resumeInterrupts() {
mkarlsso 3:d7b0a0890d96 228 __enable_irq();
mkarlsso 3:d7b0a0890d96 229 }
mkarlsso 3:d7b0a0890d96 230
mkarlsso 0:8dbd6bd9167f 231 void MBEDSystem::mainLoopToDo() {
mkarlsso 0:8dbd6bd9167f 232 #ifdef MBED_RF
mkarlsso 0:8dbd6bd9167f 233 //Karpova version--------------------------
mkarlsso 0:8dbd6bd9167f 234 //Karpova lab RF addition. Print out time sync.
mkarlsso 0:8dbd6bd9167f 235 if (RFSyncReadable) {
mkarlsso 0:8dbd6bd9167f 236 /*
mkarlsso 0:8dbd6bd9167f 237 ostringstream RFTimeConvert;
mkarlsso 0:8dbd6bd9167f 238 RFTimeConvert << timeKeeperAtLastRFTime << " " << "RFsync " << lastRFTime << " "; //broadcast the earliest timestamp when a change occured
mkarlsso 0:8dbd6bd9167f 239
mkarlsso 0:8dbd6bd9167f 240 textDisplay.send(RFTimeConvert.str() + "\r\n");
mkarlsso 0:8dbd6bd9167f 241 RFTimeConvert.clear();
mkarlsso 0:8dbd6bd9167f 242 RFTimeConvert.seekp(0);
mkarlsso 0:8dbd6bd9167f 243 */
mkarlsso 0:8dbd6bd9167f 244
mkarlsso 0:8dbd6bd9167f 245 textDisplay << timeKeeperAtLastRFTime << " RFsync " << lastRFTime << "\r\n";
mkarlsso 0:8dbd6bd9167f 246
mkarlsso 0:8dbd6bd9167f 247 RFSyncReadable = false;
mkarlsso 0:8dbd6bd9167f 248 RFSyncWritable = true;
mkarlsso 0:8dbd6bd9167f 249 }
mkarlsso 0:8dbd6bd9167f 250 //------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 251 #endif
mkarlsso 0:8dbd6bd9167f 252 }
mkarlsso 0:8dbd6bd9167f 253
mkarlsso 0:8dbd6bd9167f 254 sDigitalOut* MBEDSystem::getDigitalOutPtr(int portNum){
mkarlsso 0:8dbd6bd9167f 255 if (portNum < NUMPORTS) {
mkarlsso 0:8dbd6bd9167f 256 return &dOut[portNum];
mkarlsso 0:8dbd6bd9167f 257 } else {
mkarlsso 0:8dbd6bd9167f 258 return NULL;
mkarlsso 0:8dbd6bd9167f 259 }
mkarlsso 0:8dbd6bd9167f 260 }
mkarlsso 0:8dbd6bd9167f 261
mkarlsso 0:8dbd6bd9167f 262 sDigitalIn* MBEDSystem::getDigitalInPtr(int portNum) {
mkarlsso 0:8dbd6bd9167f 263 if (portNum < NUMPORTS) {
mkarlsso 0:8dbd6bd9167f 264 return &dIn[portNum];
mkarlsso 0:8dbd6bd9167f 265 } else {
mkarlsso 0:8dbd6bd9167f 266 return NULL;
mkarlsso 0:8dbd6bd9167f 267 }
mkarlsso 0:8dbd6bd9167f 268 }
mkarlsso 0:8dbd6bd9167f 269
mkarlsso 0:8dbd6bd9167f 270 sSound* MBEDSystem::createNewSoundAction() {
mkarlsso 0:8dbd6bd9167f 271 MBEDSound *tmpSound = new MBEDSound();
mkarlsso 0:8dbd6bd9167f 272 return tmpSound;
mkarlsso 0:8dbd6bd9167f 273 }
mkarlsso 0:8dbd6bd9167f 274
mkarlsso 0:8dbd6bd9167f 275
mkarlsso 0:8dbd6bd9167f 276 void MBEDSystem::externalClockReset() {
mkarlsso 0:8dbd6bd9167f 277
mkarlsso 3:d7b0a0890d96 278 //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 279 uSec_SinceLastReset = 0;
mkarlsso 3:d7b0a0890d96 280
mkarlsso 3:d7b0a0890d96 281
mkarlsso 3:d7b0a0890d96 282 /*
mkarlsso 0:8dbd6bd9167f 283 if (clockSlave) {
mkarlsso 0:8dbd6bd9167f 284 LPC_TIM0->TCR = 0x02; // reset timer
mkarlsso 0:8dbd6bd9167f 285 externalIncrementCounter = 0;
mkarlsso 0:8dbd6bd9167f 286 immediateClockReset();
mkarlsso 3:d7b0a0890d96 287 }*/
mkarlsso 0:8dbd6bd9167f 288
mkarlsso 0:8dbd6bd9167f 289 }
mkarlsso 0:8dbd6bd9167f 290
mkarlsso 0:8dbd6bd9167f 291 void MBEDSystem::setStandAloneClock() {
mkarlsso 3:d7b0a0890d96 292 clockSlave = false;
mkarlsso 3:d7b0a0890d96 293 NVIC_DisableIRQ(TIMER0_IRQn); // Disable the interrupt
mkarlsso 3:d7b0a0890d96 294 timerinit(); //set up and enable interrupt
mkarlsso 0:8dbd6bd9167f 295 clockExternalIncrement.rise(NULL); //remove the callback to the external interrupt
mkarlsso 3:d7b0a0890d96 296 //clockExternalIncrement.fall(NULL);
mkarlsso 0:8dbd6bd9167f 297 changeToSlave = false;
mkarlsso 0:8dbd6bd9167f 298 changeToStandAlone = false;
mkarlsso 0:8dbd6bd9167f 299 }
mkarlsso 0:8dbd6bd9167f 300
mkarlsso 0:8dbd6bd9167f 301 void MBEDSystem::setSlaveClock() {
mkarlsso 3:d7b0a0890d96 302 clockSlave = true;
mkarlsso 0:8dbd6bd9167f 303 NVIC_DisableIRQ(TIMER0_IRQn); // Disable the interrupt
mkarlsso 3:d7b0a0890d96 304 //timerinit(); //set up and enable interrupt
mkarlsso 0:8dbd6bd9167f 305 clockExternalIncrement.rise(this, &MBEDSystem::incrementClock);
mkarlsso 3:d7b0a0890d96 306 //clockExternalIncrement.fall(&externalClockIncDown);
mkarlsso 0:8dbd6bd9167f 307 clockSlave = true;
mkarlsso 0:8dbd6bd9167f 308 changeToSlave = false;
mkarlsso 0:8dbd6bd9167f 309 changeToStandAlone = false;
mkarlsso 0:8dbd6bd9167f 310 }
mkarlsso 0:8dbd6bd9167f 311
mkarlsso 0:8dbd6bd9167f 312 void MBEDSystem::incrementClock() {
mkarlsso 0:8dbd6bd9167f 313
mkarlsso 0:8dbd6bd9167f 314 if (clockSlave) {
mkarlsso 3:d7b0a0890d96 315 //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 316 //uSec_SinceLastClockInc = 0;
mkarlsso 3:d7b0a0890d96 317
mkarlsso 3:d7b0a0890d96 318
mkarlsso 3:d7b0a0890d96 319 //The clock is incremented
mkarlsso 3:d7b0a0890d96 320
mkarlsso 0:8dbd6bd9167f 321 externalIncrementCounter = (externalIncrementCounter+1) % externalIncrementMod;
mkarlsso 0:8dbd6bd9167f 322 if (externalIncrementCounter==0) {
mkarlsso 0:8dbd6bd9167f 323 timeKeeper++;
mkarlsso 0:8dbd6bd9167f 324 }
mkarlsso 0:8dbd6bd9167f 325
mkarlsso 0:8dbd6bd9167f 326 } else {
mkarlsso 0:8dbd6bd9167f 327 timeKeeper++;
mkarlsso 0:8dbd6bd9167f 328 }
mkarlsso 0:8dbd6bd9167f 329 //Clock resets happen upon update so we dont get a partial first ms
mkarlsso 3:d7b0a0890d96 330 /*
mkarlsso 0:8dbd6bd9167f 331 if (resetTimer) {
mkarlsso 3:d7b0a0890d96 332 uSec_SinceLastReset = 0;
mkarlsso 0:8dbd6bd9167f 333 timeKeeper = 0;
mkarlsso 0:8dbd6bd9167f 334 resetTimer = false;
mkarlsso 3:d7b0a0890d96 335 }*/
mkarlsso 0:8dbd6bd9167f 336 }
mkarlsso 0:8dbd6bd9167f 337
mkarlsso 0:8dbd6bd9167f 338 //-----------------------------------------------------
mkarlsso 0:8dbd6bd9167f 339
mkarlsso 0:8dbd6bd9167f 340 MBEDSound::MBEDSound() {
mkarlsso 0:8dbd6bd9167f 341
mkarlsso 0:8dbd6bd9167f 342 }
mkarlsso 0:8dbd6bd9167f 343
mkarlsso 0:8dbd6bd9167f 344 void MBEDSound::execute() {
mkarlsso 0:8dbd6bd9167f 345 if (reset) {
mkarlsso 0:8dbd6bd9167f 346 sWav.reset();
mkarlsso 0:8dbd6bd9167f 347 } else if (!play) {
mkarlsso 0:8dbd6bd9167f 348 sWav.stopTrack();
mkarlsso 0:8dbd6bd9167f 349 } else {
mkarlsso 0:8dbd6bd9167f 350 if (volume > -1) {
mkarlsso 0:8dbd6bd9167f 351 sWav.volume(volume);
mkarlsso 0:8dbd6bd9167f 352 } else if (volumePtr != NULL) {
mkarlsso 0:8dbd6bd9167f 353 sWav.volume(*volumePtr);
mkarlsso 0:8dbd6bd9167f 354 }
mkarlsso 0:8dbd6bd9167f 355
mkarlsso 0:8dbd6bd9167f 356 if (fileNameExists) {
mkarlsso 0:8dbd6bd9167f 357 //sWav.playTracks();
mkarlsso 0:8dbd6bd9167f 358 sWav.stopTrack();
mkarlsso 0:8dbd6bd9167f 359 sWav.playTrackName(fileName);
mkarlsso 0:8dbd6bd9167f 360 }
mkarlsso 0:8dbd6bd9167f 361 }
mkarlsso 0:8dbd6bd9167f 362 }
mkarlsso 0:8dbd6bd9167f 363
mkarlsso 0:8dbd6bd9167f 364 //-----------------------------------------------------
mkarlsso 0:8dbd6bd9167f 365 MBEDDigitalOut::MBEDDigitalOut() {
mkarlsso 0:8dbd6bd9167f 366
mkarlsso 0:8dbd6bd9167f 367 }
mkarlsso 0:8dbd6bd9167f 368
mkarlsso 0:8dbd6bd9167f 369 void MBEDDigitalOut::init(int pin) {
mkarlsso 0:8dbd6bd9167f 370 outpin = new DigitalOut(outPins[pin]);
mkarlsso 0:8dbd6bd9167f 371 }
mkarlsso 0:8dbd6bd9167f 372
mkarlsso 0:8dbd6bd9167f 373 int MBEDDigitalOut::read() {
mkarlsso 0:8dbd6bd9167f 374 return outpin->read();
mkarlsso 0:8dbd6bd9167f 375 }
mkarlsso 0:8dbd6bd9167f 376
mkarlsso 0:8dbd6bd9167f 377 void MBEDDigitalOut::write(int value) {
mkarlsso 0:8dbd6bd9167f 378 outpin->write(value);
mkarlsso 0:8dbd6bd9167f 379 }
mkarlsso 0:8dbd6bd9167f 380 //--------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 381
mkarlsso 0:8dbd6bd9167f 382 MBEDDigitalIn::MBEDDigitalIn() {
mkarlsso 0:8dbd6bd9167f 383
mkarlsso 0:8dbd6bd9167f 384 }
mkarlsso 0:8dbd6bd9167f 385
mkarlsso 0:8dbd6bd9167f 386 void MBEDDigitalIn::init(int pin) {
mkarlsso 0:8dbd6bd9167f 387 inpin = new DigitalIn(inPins[pin]);
mkarlsso 0:8dbd6bd9167f 388 inpin_interrupt = new InterruptIn(inPins[pin]);
mkarlsso 0:8dbd6bd9167f 389 inpin->mode(PullDown);
mkarlsso 0:8dbd6bd9167f 390 //Set up callbacks for the port interrupts
mkarlsso 0:8dbd6bd9167f 391 inpin_interrupt->rise(this, &MBEDDigitalIn::interrupt_up_callback);
mkarlsso 0:8dbd6bd9167f 392 inpin_interrupt->fall(this, &MBEDDigitalIn::interrupt_down_callback);
mkarlsso 0:8dbd6bd9167f 393 }
mkarlsso 0:8dbd6bd9167f 394
mkarlsso 0:8dbd6bd9167f 395 int MBEDDigitalIn::read() {
mkarlsso 0:8dbd6bd9167f 396 return inpin->read();
mkarlsso 0:8dbd6bd9167f 397 }
mkarlsso 0:8dbd6bd9167f 398
mkarlsso 0:8dbd6bd9167f 399 void MBEDDigitalIn::interrupt_up_callback() {
mkarlsso 0:8dbd6bd9167f 400 addStateChange(1, timeKeeper);
mkarlsso 0:8dbd6bd9167f 401 }
mkarlsso 0:8dbd6bd9167f 402
mkarlsso 0:8dbd6bd9167f 403 void MBEDDigitalIn::interrupt_down_callback() {
mkarlsso 0:8dbd6bd9167f 404 addStateChange(0, timeKeeper);
mkarlsso 0:8dbd6bd9167f 405 }
mkarlsso 0:8dbd6bd9167f 406
mkarlsso 0:8dbd6bd9167f 407 //----------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 408 MBEDSerialPort::MBEDSerialPort() {
mkarlsso 0:8dbd6bd9167f 409
mkarlsso 0:8dbd6bd9167f 410 }
mkarlsso 0:8dbd6bd9167f 411
mkarlsso 0:8dbd6bd9167f 412 void MBEDSerialPort::init() {
mkarlsso 0:8dbd6bd9167f 413 //Initialize serial communication
mkarlsso 0:8dbd6bd9167f 414 serialToPC = new Serial(USBTX, USBRX); // tx, rx
mkarlsso 0:8dbd6bd9167f 415 serialToPC->baud(115200);
mkarlsso 0:8dbd6bd9167f 416
mkarlsso 0:8dbd6bd9167f 417
mkarlsso 0:8dbd6bd9167f 418 }
mkarlsso 0:8dbd6bd9167f 419
mkarlsso 0:8dbd6bd9167f 420 bool MBEDSerialPort::readable() {
mkarlsso 0:8dbd6bd9167f 421 return serialToPC->readable();
mkarlsso 0:8dbd6bd9167f 422 }
mkarlsso 0:8dbd6bd9167f 423
mkarlsso 0:8dbd6bd9167f 424 char MBEDSerialPort::readChar() {
mkarlsso 0:8dbd6bd9167f 425 return serialToPC->getc();
mkarlsso 0:8dbd6bd9167f 426 }
mkarlsso 0:8dbd6bd9167f 427
mkarlsso 0:8dbd6bd9167f 428 void MBEDSerialPort::writeChar(char s) {
mkarlsso 0:8dbd6bd9167f 429 serialToPC->printf("%c", s);
mkarlsso 0:8dbd6bd9167f 430 }
mkarlsso 3:d7b0a0890d96 431
mkarlsso 3:d7b0a0890d96 432 int MBEDSerialPort::requestToWriteString(char *s, int numBytesRequested) {
mkarlsso 3:d7b0a0890d96 433 //request to print a string to the serial output buffer
mkarlsso 3:d7b0a0890d96 434 //function returns the number of chars actually accepted for output
mkarlsso 3:d7b0a0890d96 435 int numBytesAccepted = 0;
mkarlsso 3:d7b0a0890d96 436 while (numBytesAccepted < numBytesRequested) {
mkarlsso 3:d7b0a0890d96 437
mkarlsso 3:d7b0a0890d96 438 writeChar(*(s+numBytesAccepted));
mkarlsso 3:d7b0a0890d96 439 numBytesAccepted++;
mkarlsso 3:d7b0a0890d96 440 }
mkarlsso 3:d7b0a0890d96 441
mkarlsso 3:d7b0a0890d96 442 return numBytesAccepted;
mkarlsso 3:d7b0a0890d96 443 }