Karpova Lab fork of stateScript

Dependencies:   SMARTWAV SOMO_II mbed

Fork of stateScript_v2_karpova by Andy Lustig

Committer:
mkarlsso
Date:
Tue May 19 15:45:42 2015 +0000
Revision:
0:8dbd6bd9167f
Child:
1:3a050d26d4f6
Child:
3:d7b0a0890d96
initial commit

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 0:8dbd6bd9167f 12 extern bool resetTimer;
mkarlsso 0:8dbd6bd9167f 13 extern bool clockSlave;
mkarlsso 0:8dbd6bd9167f 14 extern bool changeToSlave;
mkarlsso 0:8dbd6bd9167f 15 extern bool changeToStandAlone;
mkarlsso 0:8dbd6bd9167f 16 extern outputStream textDisplay;
mkarlsso 0:8dbd6bd9167f 17
mkarlsso 0:8dbd6bd9167f 18 int externalIncrementMod = 30;
mkarlsso 0:8dbd6bd9167f 19 int externalIncrementCounter = 0;
mkarlsso 0:8dbd6bd9167f 20
mkarlsso 0:8dbd6bd9167f 21
mkarlsso 0:8dbd6bd9167f 22 #ifdef MBED_RF
mkarlsso 0:8dbd6bd9167f 23 //Karpova version----------------------------------------
mkarlsso 0:8dbd6bd9167f 24 //Uses DB9->RJ45 connector to map the following channels:
mkarlsso 0:8dbd6bd9167f 25 //1: P28 Clock Signal In
mkarlsso 0:8dbd6bd9167f 26 //2: P27 Data Signal In
mkarlsso 0:8dbd6bd9167f 27 //3: P20 FiberLED Out Signal
mkarlsso 0:8dbd6bd9167f 28 //4: 5V
mkarlsso 0:8dbd6bd9167f 29 //5: GND
mkarlsso 0:8dbd6bd9167f 30 //6: P6 NC
mkarlsso 0:8dbd6bd9167f 31 //7: P7 NC
mkarlsso 0:8dbd6bd9167f 32 //8: P8 NC
mkarlsso 0:8dbd6bd9167f 33
mkarlsso 0:8dbd6bd9167f 34 DigitalOut mainLED(LED1);
mkarlsso 0:8dbd6bd9167f 35 DigitalOut secondaryLED(LED2);
mkarlsso 0:8dbd6bd9167f 36
mkarlsso 0:8dbd6bd9167f 37 bool lightOn = false;
mkarlsso 0:8dbd6bd9167f 38 bool lightOn2 = false;
mkarlsso 0:8dbd6bd9167f 39 int pulseCounter = 0;
mkarlsso 0:8dbd6bd9167f 40 uint32_t lastPulse = 0;
mkarlsso 0:8dbd6bd9167f 41 uint32_t currentRFTime = 0;
mkarlsso 0:8dbd6bd9167f 42 uint32_t lastRFTime = 0;
mkarlsso 0:8dbd6bd9167f 43 uint32_t timeKeeperAtCurrentRFTime = 0;
mkarlsso 0:8dbd6bd9167f 44 uint32_t timeKeeperAtLastRFTime = 0;
mkarlsso 0:8dbd6bd9167f 45 int RFSyncCounter = 0;
mkarlsso 0:8dbd6bd9167f 46 bool RFSyncReadable = false;
mkarlsso 0:8dbd6bd9167f 47 bool RFSyncWritable = true;
mkarlsso 0:8dbd6bd9167f 48
mkarlsso 0:8dbd6bd9167f 49 //Recieve clock signal from RF system
mkarlsso 0:8dbd6bd9167f 50 InterruptIn RFClock(p28);
mkarlsso 0:8dbd6bd9167f 51 DigitalIn RFData(p27);
mkarlsso 0:8dbd6bd9167f 52
mkarlsso 0:8dbd6bd9167f 53
mkarlsso 0:8dbd6bd9167f 54 void callback_RFClock(void) {
mkarlsso 0:8dbd6bd9167f 55
mkarlsso 0:8dbd6bd9167f 56 //if this amount of time has passed since the last pulse, we have a new timestamp
mkarlsso 0:8dbd6bd9167f 57 if ((timeKeeper-lastPulse) > 4) {
mkarlsso 0:8dbd6bd9167f 58
mkarlsso 0:8dbd6bd9167f 59 //make sure the previous timestamp was 32 pulses
mkarlsso 0:8dbd6bd9167f 60 //if so, update lastRFTime
mkarlsso 0:8dbd6bd9167f 61 //we only process every 100th stamp (once every 10 seconds with 10 Hz timestamps)
mkarlsso 0:8dbd6bd9167f 62 if ((pulseCounter == 31) && (RFSyncCounter == 99)){
mkarlsso 0:8dbd6bd9167f 63 if (RFSyncWritable) {
mkarlsso 0:8dbd6bd9167f 64 lastRFTime = currentRFTime;
mkarlsso 0:8dbd6bd9167f 65 timeKeeperAtLastRFTime = timeKeeperAtCurrentRFTime;
mkarlsso 0:8dbd6bd9167f 66 RFSyncReadable = true;
mkarlsso 0:8dbd6bd9167f 67 RFSyncWritable = false;
mkarlsso 0:8dbd6bd9167f 68 }
mkarlsso 0:8dbd6bd9167f 69 }
mkarlsso 0:8dbd6bd9167f 70
mkarlsso 0:8dbd6bd9167f 71 pulseCounter = 0;
mkarlsso 0:8dbd6bd9167f 72 currentRFTime = 0;
mkarlsso 0:8dbd6bd9167f 73 timeKeeperAtCurrentRFTime = timeKeeper;
mkarlsso 0:8dbd6bd9167f 74 RFSyncCounter = (RFSyncCounter+1)%100;
mkarlsso 0:8dbd6bd9167f 75
mkarlsso 0:8dbd6bd9167f 76
mkarlsso 0:8dbd6bd9167f 77 if (lightOn) {
mkarlsso 0:8dbd6bd9167f 78 mainLED = 0;
mkarlsso 0:8dbd6bd9167f 79 lightOn = false;
mkarlsso 0:8dbd6bd9167f 80 } else {
mkarlsso 0:8dbd6bd9167f 81 mainLED = 1;
mkarlsso 0:8dbd6bd9167f 82 lightOn = true;
mkarlsso 0:8dbd6bd9167f 83 }
mkarlsso 0:8dbd6bd9167f 84 } else {
mkarlsso 0:8dbd6bd9167f 85 if (pulseCounter < 32) {
mkarlsso 0:8dbd6bd9167f 86 currentRFTime = (currentRFTime | ( RFData.read() << pulseCounter));
mkarlsso 0:8dbd6bd9167f 87 pulseCounter++;
mkarlsso 0:8dbd6bd9167f 88 }
mkarlsso 0:8dbd6bd9167f 89 }
mkarlsso 0:8dbd6bd9167f 90 lastPulse = timeKeeper;
mkarlsso 0:8dbd6bd9167f 91 }
mkarlsso 0:8dbd6bd9167f 92
mkarlsso 0:8dbd6bd9167f 93 //------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 94 #endif
mkarlsso 0:8dbd6bd9167f 95
mkarlsso 0:8dbd6bd9167f 96
mkarlsso 0:8dbd6bd9167f 97
mkarlsso 0:8dbd6bd9167f 98 //------------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 99 //------------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 100
mkarlsso 0:8dbd6bd9167f 101 //MBED-specific stuff
mkarlsso 0:8dbd6bd9167f 102 //---------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 103
mkarlsso 0:8dbd6bd9167f 104 //translate pin numbers to hardware pins
mkarlsso 0:8dbd6bd9167f 105 PinName outPins[NUMPORTS] = {p11,p13,p15,p18,p21,p23,p25,p29,p20};
mkarlsso 0:8dbd6bd9167f 106 PinName inPins[NUMPORTS] = {p12,p14,p16,p17,p22,p24,p26,p30,p7};
mkarlsso 0:8dbd6bd9167f 107
mkarlsso 0:8dbd6bd9167f 108
mkarlsso 0:8dbd6bd9167f 109
mkarlsso 0:8dbd6bd9167f 110 //The sound output uses a SmartWav device and their simple serial library
mkarlsso 0:8dbd6bd9167f 111 SMARTWAV sWav(p9,p10,p19); //(TX,RX,Reset);
mkarlsso 0:8dbd6bd9167f 112
mkarlsso 0:8dbd6bd9167f 113 //This is the callback for the MBED timer
mkarlsso 0:8dbd6bd9167f 114 extern "C" void TIMER0_IRQHandler (void) {
mkarlsso 0:8dbd6bd9167f 115
mkarlsso 0:8dbd6bd9167f 116 if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed
mkarlsso 0:8dbd6bd9167f 117
mkarlsso 0:8dbd6bd9167f 118 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag
mkarlsso 0:8dbd6bd9167f 119 timeKeeper++;
mkarlsso 0:8dbd6bd9167f 120
mkarlsso 0:8dbd6bd9167f 121 if (resetTimer) {
mkarlsso 0:8dbd6bd9167f 122 timeKeeper = 0;
mkarlsso 0:8dbd6bd9167f 123 resetTimer = false;
mkarlsso 0:8dbd6bd9167f 124 }
mkarlsso 0:8dbd6bd9167f 125 }
mkarlsso 0:8dbd6bd9167f 126 }
mkarlsso 0:8dbd6bd9167f 127 //-----------------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 128
mkarlsso 0:8dbd6bd9167f 129
mkarlsso 0:8dbd6bd9167f 130
mkarlsso 0:8dbd6bd9167f 131 MBEDSystem::MBEDSystem():
mkarlsso 0:8dbd6bd9167f 132 clockResetInt(p5),
mkarlsso 0:8dbd6bd9167f 133 clockExternalIncrement(p8) {
mkarlsso 0:8dbd6bd9167f 134
mkarlsso 0:8dbd6bd9167f 135 clockResetInt.rise(this, &MBEDSystem::externalClockReset);
mkarlsso 0:8dbd6bd9167f 136 clockResetInt.mode(PullDown);
mkarlsso 0:8dbd6bd9167f 137
mkarlsso 0:8dbd6bd9167f 138 clockExternalIncrement.mode(PullDown);
mkarlsso 0:8dbd6bd9167f 139
mkarlsso 0:8dbd6bd9167f 140 #ifdef MBED_RF
mkarlsso 0:8dbd6bd9167f 141 //Karpova version-------------
mkarlsso 0:8dbd6bd9167f 142 //Set up callbacks for RF clock signal
mkarlsso 0:8dbd6bd9167f 143 RFClock.rise(&callback_RFClock);
mkarlsso 0:8dbd6bd9167f 144 RFClock.mode(PullDown);
mkarlsso 0:8dbd6bd9167f 145 #endif
mkarlsso 0:8dbd6bd9167f 146
mkarlsso 0:8dbd6bd9167f 147 //-------------------------------
mkarlsso 0:8dbd6bd9167f 148
mkarlsso 0:8dbd6bd9167f 149 for (int i=0; i < NUMPORTS; i++) {
mkarlsso 0:8dbd6bd9167f 150 dIn[i].init(i);
mkarlsso 0:8dbd6bd9167f 151 dOut[i].init(i);
mkarlsso 0:8dbd6bd9167f 152 }
mkarlsso 0:8dbd6bd9167f 153
mkarlsso 0:8dbd6bd9167f 154
mkarlsso 0:8dbd6bd9167f 155
mkarlsso 0:8dbd6bd9167f 156 sWav.reset();
mkarlsso 0:8dbd6bd9167f 157
mkarlsso 0:8dbd6bd9167f 158 }
mkarlsso 0:8dbd6bd9167f 159
mkarlsso 0:8dbd6bd9167f 160 void MBEDSystem::timerinit() {
mkarlsso 0:8dbd6bd9167f 161 //intiatiation of timer (specific to the LPC17xx chip). This is used in
mkarlsso 0:8dbd6bd9167f 162 //standalone mode to increment the clock every ms.
mkarlsso 0:8dbd6bd9167f 163 //----------------------------------------------------
mkarlsso 0:8dbd6bd9167f 164 //LPC_SC->PCLKSEL1 &= (3 << 12); //mask
mkarlsso 0:8dbd6bd9167f 165 //LPC_SC->PCLKSEL1 |= (1 << 12); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual)
mkarlsso 0:8dbd6bd9167f 166 //LPC_SC->PCLKSEL0 &= (3 << 3); //mask
mkarlsso 0:8dbd6bd9167f 167 //LPC_SC->PCLKSEL0 |= (1 << 3); //sets it to 1*SystemCoreClock - table 42 (page 57 in user manual)
mkarlsso 0:8dbd6bd9167f 168 LPC_SC->PCONP |=1<1; //timer0 power on
mkarlsso 0:8dbd6bd9167f 169 LPC_TIM0->MR0 = 23980; //1 msec
mkarlsso 0:8dbd6bd9167f 170 //LPC_TIM0->PR = (SystemCoreClock / 1000000); //microsecond steps
mkarlsso 0:8dbd6bd9167f 171 //LPC_TIM0->MR0 = 1000; //100 msec
mkarlsso 0:8dbd6bd9167f 172 //LPC_TIM0->MR0 = (SystemCoreClock / 1000000); //microsecond steps
mkarlsso 0:8dbd6bd9167f 173 LPC_TIM0->MCR = 3; //interrupt and reset control
mkarlsso 0:8dbd6bd9167f 174 //3 = Interrupt & reset timer0 on match
mkarlsso 0:8dbd6bd9167f 175 //1 = Interrupt only, no reset of timer0
mkarlsso 0:8dbd6bd9167f 176 NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt
mkarlsso 0:8dbd6bd9167f 177 LPC_TIM0->TCR = 1; //enable Timer0
mkarlsso 0:8dbd6bd9167f 178 //--------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 179 }
mkarlsso 0:8dbd6bd9167f 180
mkarlsso 0:8dbd6bd9167f 181 void MBEDSystem::mainLoopToDo() {
mkarlsso 0:8dbd6bd9167f 182 #ifdef MBED_RF
mkarlsso 0:8dbd6bd9167f 183 //Karpova version--------------------------
mkarlsso 0:8dbd6bd9167f 184 //Karpova lab RF addition. Print out time sync.
mkarlsso 0:8dbd6bd9167f 185 if (RFSyncReadable) {
mkarlsso 0:8dbd6bd9167f 186 /*
mkarlsso 0:8dbd6bd9167f 187 ostringstream RFTimeConvert;
mkarlsso 0:8dbd6bd9167f 188 RFTimeConvert << timeKeeperAtLastRFTime << " " << "RFsync " << lastRFTime << " "; //broadcast the earliest timestamp when a change occured
mkarlsso 0:8dbd6bd9167f 189
mkarlsso 0:8dbd6bd9167f 190 textDisplay.send(RFTimeConvert.str() + "\r\n");
mkarlsso 0:8dbd6bd9167f 191 RFTimeConvert.clear();
mkarlsso 0:8dbd6bd9167f 192 RFTimeConvert.seekp(0);
mkarlsso 0:8dbd6bd9167f 193 */
mkarlsso 0:8dbd6bd9167f 194
mkarlsso 0:8dbd6bd9167f 195 textDisplay << timeKeeperAtLastRFTime << " RFsync " << lastRFTime << "\r\n";
mkarlsso 0:8dbd6bd9167f 196
mkarlsso 0:8dbd6bd9167f 197 RFSyncReadable = false;
mkarlsso 0:8dbd6bd9167f 198 RFSyncWritable = true;
mkarlsso 0:8dbd6bd9167f 199 }
mkarlsso 0:8dbd6bd9167f 200 //------------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 201 #endif
mkarlsso 0:8dbd6bd9167f 202 }
mkarlsso 0:8dbd6bd9167f 203
mkarlsso 0:8dbd6bd9167f 204 sDigitalOut* MBEDSystem::getDigitalOutPtr(int portNum){
mkarlsso 0:8dbd6bd9167f 205 if (portNum < NUMPORTS) {
mkarlsso 0:8dbd6bd9167f 206 return &dOut[portNum];
mkarlsso 0:8dbd6bd9167f 207 } else {
mkarlsso 0:8dbd6bd9167f 208 return NULL;
mkarlsso 0:8dbd6bd9167f 209 }
mkarlsso 0:8dbd6bd9167f 210 }
mkarlsso 0:8dbd6bd9167f 211
mkarlsso 0:8dbd6bd9167f 212 sDigitalIn* MBEDSystem::getDigitalInPtr(int portNum) {
mkarlsso 0:8dbd6bd9167f 213 if (portNum < NUMPORTS) {
mkarlsso 0:8dbd6bd9167f 214 return &dIn[portNum];
mkarlsso 0:8dbd6bd9167f 215 } else {
mkarlsso 0:8dbd6bd9167f 216 return NULL;
mkarlsso 0:8dbd6bd9167f 217 }
mkarlsso 0:8dbd6bd9167f 218 }
mkarlsso 0:8dbd6bd9167f 219
mkarlsso 0:8dbd6bd9167f 220 sSound* MBEDSystem::createNewSoundAction() {
mkarlsso 0:8dbd6bd9167f 221 MBEDSound *tmpSound = new MBEDSound();
mkarlsso 0:8dbd6bd9167f 222 return tmpSound;
mkarlsso 0:8dbd6bd9167f 223 }
mkarlsso 0:8dbd6bd9167f 224
mkarlsso 0:8dbd6bd9167f 225
mkarlsso 0:8dbd6bd9167f 226 void MBEDSystem::externalClockReset() {
mkarlsso 0:8dbd6bd9167f 227
mkarlsso 0:8dbd6bd9167f 228 if (clockSlave) {
mkarlsso 0:8dbd6bd9167f 229 LPC_TIM0->TCR = 0x02; // reset timer
mkarlsso 0:8dbd6bd9167f 230 externalIncrementCounter = 0;
mkarlsso 0:8dbd6bd9167f 231 immediateClockReset();
mkarlsso 0:8dbd6bd9167f 232 }
mkarlsso 0:8dbd6bd9167f 233
mkarlsso 0:8dbd6bd9167f 234 }
mkarlsso 0:8dbd6bd9167f 235
mkarlsso 0:8dbd6bd9167f 236 void MBEDSystem::setStandAloneClock() {
mkarlsso 0:8dbd6bd9167f 237 timerinit();
mkarlsso 0:8dbd6bd9167f 238 clockExternalIncrement.rise(NULL); //remove the callback to the external interrupt
mkarlsso 0:8dbd6bd9167f 239 clockSlave = false;
mkarlsso 0:8dbd6bd9167f 240 changeToSlave = false;
mkarlsso 0:8dbd6bd9167f 241 changeToStandAlone = false;
mkarlsso 0:8dbd6bd9167f 242 }
mkarlsso 0:8dbd6bd9167f 243
mkarlsso 0:8dbd6bd9167f 244 void MBEDSystem::setSlaveClock() {
mkarlsso 0:8dbd6bd9167f 245 NVIC_DisableIRQ(TIMER0_IRQn); // Disable the interrupt
mkarlsso 0:8dbd6bd9167f 246 clockExternalIncrement.rise(this, &MBEDSystem::incrementClock);
mkarlsso 0:8dbd6bd9167f 247 clockSlave = true;
mkarlsso 0:8dbd6bd9167f 248 changeToSlave = false;
mkarlsso 0:8dbd6bd9167f 249 changeToStandAlone = false;
mkarlsso 0:8dbd6bd9167f 250 }
mkarlsso 0:8dbd6bd9167f 251
mkarlsso 0:8dbd6bd9167f 252 void MBEDSystem::incrementClock() {
mkarlsso 0:8dbd6bd9167f 253
mkarlsso 0:8dbd6bd9167f 254 if (clockSlave) {
mkarlsso 0:8dbd6bd9167f 255 externalIncrementCounter = (externalIncrementCounter+1) % externalIncrementMod;
mkarlsso 0:8dbd6bd9167f 256 if (externalIncrementCounter==0) {
mkarlsso 0:8dbd6bd9167f 257 timeKeeper++;
mkarlsso 0:8dbd6bd9167f 258 }
mkarlsso 0:8dbd6bd9167f 259
mkarlsso 0:8dbd6bd9167f 260 } else {
mkarlsso 0:8dbd6bd9167f 261 timeKeeper++;
mkarlsso 0:8dbd6bd9167f 262 }
mkarlsso 0:8dbd6bd9167f 263 //Clock resets happen upon update so we dont get a partial first ms
mkarlsso 0:8dbd6bd9167f 264 if (resetTimer) {
mkarlsso 0:8dbd6bd9167f 265 timeKeeper = 0;
mkarlsso 0:8dbd6bd9167f 266 resetTimer = false;
mkarlsso 0:8dbd6bd9167f 267 }
mkarlsso 0:8dbd6bd9167f 268 }
mkarlsso 0:8dbd6bd9167f 269
mkarlsso 0:8dbd6bd9167f 270 //-----------------------------------------------------
mkarlsso 0:8dbd6bd9167f 271
mkarlsso 0:8dbd6bd9167f 272 MBEDSound::MBEDSound() {
mkarlsso 0:8dbd6bd9167f 273
mkarlsso 0:8dbd6bd9167f 274 }
mkarlsso 0:8dbd6bd9167f 275
mkarlsso 0:8dbd6bd9167f 276 void MBEDSound::execute() {
mkarlsso 0:8dbd6bd9167f 277 if (reset) {
mkarlsso 0:8dbd6bd9167f 278 sWav.reset();
mkarlsso 0:8dbd6bd9167f 279 } else if (!play) {
mkarlsso 0:8dbd6bd9167f 280 sWav.stopTrack();
mkarlsso 0:8dbd6bd9167f 281 } else {
mkarlsso 0:8dbd6bd9167f 282 if (volume > -1) {
mkarlsso 0:8dbd6bd9167f 283 sWav.volume(volume);
mkarlsso 0:8dbd6bd9167f 284 } else if (volumePtr != NULL) {
mkarlsso 0:8dbd6bd9167f 285 sWav.volume(*volumePtr);
mkarlsso 0:8dbd6bd9167f 286 }
mkarlsso 0:8dbd6bd9167f 287
mkarlsso 0:8dbd6bd9167f 288 if (fileNameExists) {
mkarlsso 0:8dbd6bd9167f 289 //sWav.playTracks();
mkarlsso 0:8dbd6bd9167f 290 sWav.stopTrack();
mkarlsso 0:8dbd6bd9167f 291 sWav.playTrackName(fileName);
mkarlsso 0:8dbd6bd9167f 292 }
mkarlsso 0:8dbd6bd9167f 293 }
mkarlsso 0:8dbd6bd9167f 294 }
mkarlsso 0:8dbd6bd9167f 295
mkarlsso 0:8dbd6bd9167f 296 //-----------------------------------------------------
mkarlsso 0:8dbd6bd9167f 297 MBEDDigitalOut::MBEDDigitalOut() {
mkarlsso 0:8dbd6bd9167f 298
mkarlsso 0:8dbd6bd9167f 299 }
mkarlsso 0:8dbd6bd9167f 300
mkarlsso 0:8dbd6bd9167f 301 void MBEDDigitalOut::init(int pin) {
mkarlsso 0:8dbd6bd9167f 302 outpin = new DigitalOut(outPins[pin]);
mkarlsso 0:8dbd6bd9167f 303 }
mkarlsso 0:8dbd6bd9167f 304
mkarlsso 0:8dbd6bd9167f 305 int MBEDDigitalOut::read() {
mkarlsso 0:8dbd6bd9167f 306 return outpin->read();
mkarlsso 0:8dbd6bd9167f 307 }
mkarlsso 0:8dbd6bd9167f 308
mkarlsso 0:8dbd6bd9167f 309 void MBEDDigitalOut::write(int value) {
mkarlsso 0:8dbd6bd9167f 310 outpin->write(value);
mkarlsso 0:8dbd6bd9167f 311 }
mkarlsso 0:8dbd6bd9167f 312 //--------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 313
mkarlsso 0:8dbd6bd9167f 314 MBEDDigitalIn::MBEDDigitalIn() {
mkarlsso 0:8dbd6bd9167f 315
mkarlsso 0:8dbd6bd9167f 316 }
mkarlsso 0:8dbd6bd9167f 317
mkarlsso 0:8dbd6bd9167f 318 void MBEDDigitalIn::init(int pin) {
mkarlsso 0:8dbd6bd9167f 319 inpin = new DigitalIn(inPins[pin]);
mkarlsso 0:8dbd6bd9167f 320 inpin_interrupt = new InterruptIn(inPins[pin]);
mkarlsso 0:8dbd6bd9167f 321 inpin->mode(PullDown);
mkarlsso 0:8dbd6bd9167f 322 //Set up callbacks for the port interrupts
mkarlsso 0:8dbd6bd9167f 323 inpin_interrupt->rise(this, &MBEDDigitalIn::interrupt_up_callback);
mkarlsso 0:8dbd6bd9167f 324 inpin_interrupt->fall(this, &MBEDDigitalIn::interrupt_down_callback);
mkarlsso 0:8dbd6bd9167f 325 }
mkarlsso 0:8dbd6bd9167f 326
mkarlsso 0:8dbd6bd9167f 327 int MBEDDigitalIn::read() {
mkarlsso 0:8dbd6bd9167f 328 return inpin->read();
mkarlsso 0:8dbd6bd9167f 329 }
mkarlsso 0:8dbd6bd9167f 330
mkarlsso 0:8dbd6bd9167f 331 void MBEDDigitalIn::interrupt_up_callback() {
mkarlsso 0:8dbd6bd9167f 332 addStateChange(1, timeKeeper);
mkarlsso 0:8dbd6bd9167f 333 }
mkarlsso 0:8dbd6bd9167f 334
mkarlsso 0:8dbd6bd9167f 335 void MBEDDigitalIn::interrupt_down_callback() {
mkarlsso 0:8dbd6bd9167f 336 addStateChange(0, timeKeeper);
mkarlsso 0:8dbd6bd9167f 337 }
mkarlsso 0:8dbd6bd9167f 338
mkarlsso 0:8dbd6bd9167f 339 //----------------------------------------------------------
mkarlsso 0:8dbd6bd9167f 340 MBEDSerialPort::MBEDSerialPort() {
mkarlsso 0:8dbd6bd9167f 341
mkarlsso 0:8dbd6bd9167f 342 }
mkarlsso 0:8dbd6bd9167f 343
mkarlsso 0:8dbd6bd9167f 344 void MBEDSerialPort::init() {
mkarlsso 0:8dbd6bd9167f 345 //Initialize serial communication
mkarlsso 0:8dbd6bd9167f 346 serialToPC = new Serial(USBTX, USBRX); // tx, rx
mkarlsso 0:8dbd6bd9167f 347 serialToPC->baud(115200);
mkarlsso 0:8dbd6bd9167f 348
mkarlsso 0:8dbd6bd9167f 349
mkarlsso 0:8dbd6bd9167f 350 }
mkarlsso 0:8dbd6bd9167f 351
mkarlsso 0:8dbd6bd9167f 352 bool MBEDSerialPort::readable() {
mkarlsso 0:8dbd6bd9167f 353 return serialToPC->readable();
mkarlsso 0:8dbd6bd9167f 354 }
mkarlsso 0:8dbd6bd9167f 355
mkarlsso 0:8dbd6bd9167f 356 char MBEDSerialPort::readChar() {
mkarlsso 0:8dbd6bd9167f 357 return serialToPC->getc();
mkarlsso 0:8dbd6bd9167f 358 }
mkarlsso 0:8dbd6bd9167f 359
mkarlsso 0:8dbd6bd9167f 360 void MBEDSerialPort::writeChar(char s) {
mkarlsso 0:8dbd6bd9167f 361 serialToPC->printf("%c", s);
mkarlsso 0:8dbd6bd9167f 362 }