Akhil Nair
/
Lab3p2
Lab3 part2 with DAC
Revision 5:1ab4ad65d5cf, committed 2019-02-14
- Comitter:
- anair12345
- Date:
- Thu Feb 14 23:23:10 2019 +0000
- Parent:
- 4:75ad475aff41
- Commit message:
- lab3p2;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Feb 04 16:12:23 2019 +0000 +++ b/main.cpp Thu Feb 14 23:23:10 2019 +0000 @@ -30,6 +30,83 @@ Ticker tick ; // Creates periodic interrupt AnalogOut_unsafe ao(PTE30) ; // Analog output +AnalogIn ain(A0) ; // Analog input + +DigitalOut led0(PTA13); +DigitalOut led1(PTD5); +DigitalOut led2(PTD0); +DigitalOut led3(PTD2); +DigitalOut led4(PTD3); + +DigitalIn b1(PTA1, PullUp); + +EventQueue queue; // creates an event queue, to call read ADC + +Serial pc(USBTX, USBRX); // tx, rx, for debugging + +// This thread runs the event queue +Thread eventThread ; + +typedef struct { + uint16_t analog; /* Analog input value */ + int button; +} message_t; + +Mail<message_t, 2> mailbox; + +volatile int samples = 0 ; +volatile uint16_t smoothed = 0 ; + +volatile int pressEvent = 0 ; + +enum buttonPos { up, down, bounce }; // Button positions + +buttonPos pos = up ; +int bcounter = 0 ; + +void readA0() { + smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; + samples++ ; + + switch (pos) { + case up : + if (!b1.read()) { // now down + pressEvent = 1 ; // transition occurred + pos = down ; + } + break ; + case down : + if (b1 == 1) { // no longer down + bcounter = 3 ; // wait four cycles + pos = bounce ; + } + break ; + case bounce : + if (b1 == 0) { // down again - button has bounced + pos = down ; // no event + } else if (bcounter == 0) { + pos = up ; // delay passed - reset to up + } else { + bcounter-- ; // continue waiting + } + break ; + } + wait(0.03); + + if (samples == 10) { + // send to thread + message_t *mess = mailbox.alloc() ; // may fail but does not block + if (mess) { + mess->analog = smoothed ; + mess->button = pressEvent ; + mailbox.put(mess); // fails but does not block if full + pressEvent = 0;// clear press + } + samples = 0; + + } +} + // Function called periodically // Write new value to AnalogOut volatile int index = 0 ; // index into array of sin values @@ -41,13 +118,51 @@ // Control the frequency of updates // Alternative between two frequencies int main() { + led0 = 0 ; // turn off + led1 = 0 ; + led2 = 0 ; + led3 = 0 ; + led4 = 0 ; + + int volts = 0 ; + int threshold[] = {55,110,165,220,275,330} ; + int counter = 0 ; + + eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); + + // call the readA0 function every 10ms + queue.call_every(10, readA0) ; + + int update_us = 1000 ; // 1ms + int freq = 1; while (true) { + osEvent evt = mailbox.get(); // wait for mail + if (evt.status == osEventMail) { + message_t* mess = (message_t*)evt.value.p ; + volts = (mess->analog * 330) / 0xffff ; + int mypressEvent = mess->button; + mailbox.free(mess) ; // free the message space + if(mypressEvent) { + for(int i = 0; i<5;i++){ + threshold[i] = (i*volts)/6; + }// use volts to reset thresholds + } + if (volts > threshold[0] ) led0 = 1 ; else led0 = 0 ; + if (volts > threshold[1]) led1 = 1 ; else led1 = 0 ; + if (volts > threshold[2]) led2 = 1 ; else led2 = 0 ; + if (volts > threshold[3]) led3 = 1 ; else led3 = 0 ; + if (volts > threshold[4]) led4 = 1 ; else led4 = 0 ; + //vToString(volts, vstring) ; + counter++ ; + } + //tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut + //wait(30.0) ; // wait 30 sec + freq = 1+((49*volts)/330); + update_us = 1000000/(freq*64); + //update_us = 2000 ; // 2ms tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut - wait(30.0) ; // wait 30 sec - update_us = 2000 ; // 2ms - tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut - wait(30.0) ; // wait 30 sec - update_us = 1000 ; // 1ms + //wait(30.0) ; // wait 30 sec + //update_us = 1000 ; // 1ms } }