Karpova Lab fork of stateScript

Dependencies:   SMARTWAV SOMO_II mbed

Fork of stateScript_v2_karpova by Andy Lustig

Committer:
alustig3
Date:
Wed Jan 27 21:31:45 2016 +0000
Revision:
8:436cf2ab961c
Parent:
5:abee20c0bf2a
Child:
9:f15ad8d18881
added pin mapping for ephys module 1.2 and max module 1.1

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