Akhil Nair
/
Lab3p1
Lab 3 the ADC part with the leds
main.cpp@5:beff003ed7e0, 2019-02-07 (annotated)
- Committer:
- anair12345
- Date:
- Thu Feb 07 13:43:32 2019 +0000
- Revision:
- 5:beff003ed7e0
- Parent:
- 4:ebd00f94455a
lab 3 part 1 full
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:ad3cdc525b99 | 1 | // LAB 3 SAMPLE PROGRAM 1 |
WilliamMarshQMUL | 3:f5d7fddeef39 | 2 | // Revised for mbed 5 |
WilliamMarshQMUL | 4:ebd00f94455a | 3 | // Revised to replace Ticker with event queue and thread |
WilliamMarshQMUL | 0:ad3cdc525b99 | 4 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 5 | #include "mbed.h" |
WilliamMarshQMUL | 1:126dd2f5fc2d | 6 | |
anair12345 | 5:beff003ed7e0 | 7 | AnalogIn ain(A0) ; // Analog input |
WilliamMarshQMUL | 0:ad3cdc525b99 | 8 | |
anair12345 | 5:beff003ed7e0 | 9 | DigitalOut led0(PTA13); |
anair12345 | 5:beff003ed7e0 | 10 | DigitalOut led1(PTD5); |
anair12345 | 5:beff003ed7e0 | 11 | DigitalOut led2(PTD0); |
anair12345 | 5:beff003ed7e0 | 12 | DigitalOut led3(PTD2); |
anair12345 | 5:beff003ed7e0 | 13 | DigitalOut led4(PTD3); |
anair12345 | 5:beff003ed7e0 | 14 | |
anair12345 | 5:beff003ed7e0 | 15 | DigitalIn b1(PTA1, PullUp); |
anair12345 | 5:beff003ed7e0 | 16 | |
anair12345 | 5:beff003ed7e0 | 17 | //DigitalOut led1(REDLED); // Red LED |
WilliamMarshQMUL | 4:ebd00f94455a | 18 | EventQueue queue; // creates an event queue, to call read ADC |
WilliamMarshQMUL | 0:ad3cdc525b99 | 19 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 20 | Serial pc(USBTX, USBRX); // tx, rx, for debugging |
WilliamMarshQMUL | 0:ad3cdc525b99 | 21 | |
WilliamMarshQMUL | 4:ebd00f94455a | 22 | // This thread runs the event queue |
WilliamMarshQMUL | 4:ebd00f94455a | 23 | Thread eventThread ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 24 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 25 | // Message type |
WilliamMarshQMUL | 0:ad3cdc525b99 | 26 | typedef struct { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 27 | uint16_t analog; /* Analog input value */ |
anair12345 | 5:beff003ed7e0 | 28 | int button; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 29 | } message_t; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 30 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 31 | // Mail box |
WilliamMarshQMUL | 0:ad3cdc525b99 | 32 | Mail<message_t, 2> mailbox; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 33 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 34 | // Function called every 10ms to read ADC |
WilliamMarshQMUL | 4:ebd00f94455a | 35 | // Average using a low pass filter |
WilliamMarshQMUL | 0:ad3cdc525b99 | 36 | // Every 10th value is sent to mailbox |
WilliamMarshQMUL | 4:ebd00f94455a | 37 | volatile int samples = 0 ; |
WilliamMarshQMUL | 4:ebd00f94455a | 38 | volatile uint16_t smoothed = 0 ; |
WilliamMarshQMUL | 4:ebd00f94455a | 39 | |
anair12345 | 5:beff003ed7e0 | 40 | //Thread pollT ; |
anair12345 | 5:beff003ed7e0 | 41 | volatile int pressEvent = 0 ; |
anair12345 | 5:beff003ed7e0 | 42 | |
anair12345 | 5:beff003ed7e0 | 43 | enum buttonPos { up, down, bounce }; // Button positions |
anair12345 | 5:beff003ed7e0 | 44 | |
anair12345 | 5:beff003ed7e0 | 45 | buttonPos pos = up ; |
anair12345 | 5:beff003ed7e0 | 46 | int bcounter = 0 ; |
anair12345 | 5:beff003ed7e0 | 47 | |
anair12345 | 5:beff003ed7e0 | 48 | void readA0() { |
WilliamMarshQMUL | 4:ebd00f94455a | 49 | smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; |
WilliamMarshQMUL | 4:ebd00f94455a | 50 | samples++ ; |
anair12345 | 5:beff003ed7e0 | 51 | |
anair12345 | 5:beff003ed7e0 | 52 | switch (pos) { |
anair12345 | 5:beff003ed7e0 | 53 | case up : |
anair12345 | 5:beff003ed7e0 | 54 | if (!b1.read()) { // now down |
anair12345 | 5:beff003ed7e0 | 55 | pressEvent = 1 ; // transition occurred |
anair12345 | 5:beff003ed7e0 | 56 | pos = down ; |
anair12345 | 5:beff003ed7e0 | 57 | } |
anair12345 | 5:beff003ed7e0 | 58 | break ; |
anair12345 | 5:beff003ed7e0 | 59 | case down : |
anair12345 | 5:beff003ed7e0 | 60 | if (b1 == 1) { // no longer down |
anair12345 | 5:beff003ed7e0 | 61 | bcounter = 3 ; // wait four cycles |
anair12345 | 5:beff003ed7e0 | 62 | pos = bounce ; |
anair12345 | 5:beff003ed7e0 | 63 | } |
anair12345 | 5:beff003ed7e0 | 64 | break ; |
anair12345 | 5:beff003ed7e0 | 65 | case bounce : |
anair12345 | 5:beff003ed7e0 | 66 | if (b1 == 0) { // down again - button has bounced |
anair12345 | 5:beff003ed7e0 | 67 | pos = down ; // no event |
anair12345 | 5:beff003ed7e0 | 68 | } else if (bcounter == 0) { |
anair12345 | 5:beff003ed7e0 | 69 | pos = up ; // delay passed - reset to up |
anair12345 | 5:beff003ed7e0 | 70 | } else { |
anair12345 | 5:beff003ed7e0 | 71 | bcounter-- ; // continue waiting |
anair12345 | 5:beff003ed7e0 | 72 | } |
anair12345 | 5:beff003ed7e0 | 73 | break ; |
anair12345 | 5:beff003ed7e0 | 74 | } |
anair12345 | 5:beff003ed7e0 | 75 | wait(0.03); |
anair12345 | 5:beff003ed7e0 | 76 | |
WilliamMarshQMUL | 4:ebd00f94455a | 77 | if (samples == 10) { |
WilliamMarshQMUL | 4:ebd00f94455a | 78 | // send to thread |
WilliamMarshQMUL | 4:ebd00f94455a | 79 | message_t *mess = mailbox.alloc() ; // may fail but does not block |
WilliamMarshQMUL | 4:ebd00f94455a | 80 | if (mess) { |
WilliamMarshQMUL | 4:ebd00f94455a | 81 | mess->analog = smoothed ; |
anair12345 | 5:beff003ed7e0 | 82 | mess->button = pressEvent ; |
WilliamMarshQMUL | 4:ebd00f94455a | 83 | mailbox.put(mess); // fails but does not block if full |
anair12345 | 5:beff003ed7e0 | 84 | pressEvent = 0;// clear press |
WilliamMarshQMUL | 0:ad3cdc525b99 | 85 | } |
WilliamMarshQMUL | 4:ebd00f94455a | 86 | samples = 0; |
anair12345 | 5:beff003ed7e0 | 87 | |
WilliamMarshQMUL | 4:ebd00f94455a | 88 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 89 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 90 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 91 | // Write voltage digits |
WilliamMarshQMUL | 0:ad3cdc525b99 | 92 | // v Voltage as scale int, e.g. 3.30 is 330 |
WilliamMarshQMUL | 0:ad3cdc525b99 | 93 | void vToString(int v, char* s) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 94 | s[3] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 95 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 96 | s[2] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 97 | v = v / 10 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 98 | s[0] = '0' + (v % 10) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 99 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 100 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 101 | // Main program |
WilliamMarshQMUL | 0:ad3cdc525b99 | 102 | // Initialise variables |
WilliamMarshQMUL | 0:ad3cdc525b99 | 103 | // Attach ISR for ticker |
WilliamMarshQMUL | 0:ad3cdc525b99 | 104 | // Procss messages from mailbox |
WilliamMarshQMUL | 0:ad3cdc525b99 | 105 | int main() { |
anair12345 | 5:beff003ed7e0 | 106 | led0 = 0 ; // turn off |
anair12345 | 5:beff003ed7e0 | 107 | led1 = 0 ; |
anair12345 | 5:beff003ed7e0 | 108 | led2 = 0 ; |
anair12345 | 5:beff003ed7e0 | 109 | led3 = 0 ; |
anair12345 | 5:beff003ed7e0 | 110 | led4 = 0 ; |
anair12345 | 5:beff003ed7e0 | 111 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 112 | int volts = 0 ; |
anair12345 | 5:beff003ed7e0 | 113 | int threshold[] = {55,110,165,220,275,330} ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 114 | int counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 115 | char vstring[] = "X.XX\r\n" ; |
WilliamMarshQMUL | 3:f5d7fddeef39 | 116 | |
anair12345 | 5:beff003ed7e0 | 117 | //pollT.start(callback(polling)); |
anair12345 | 5:beff003ed7e0 | 118 | |
WilliamMarshQMUL | 4:ebd00f94455a | 119 | // Start the event queue |
WilliamMarshQMUL | 4:ebd00f94455a | 120 | eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); |
WilliamMarshQMUL | 4:ebd00f94455a | 121 | |
WilliamMarshQMUL | 4:ebd00f94455a | 122 | // call the readA0 function every 10ms |
WilliamMarshQMUL | 4:ebd00f94455a | 123 | queue.call_every(10, readA0) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 124 | |
WilliamMarshQMUL | 0:ad3cdc525b99 | 125 | while (true) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 126 | osEvent evt = mailbox.get(); // wait for mail |
WilliamMarshQMUL | 0:ad3cdc525b99 | 127 | if (evt.status == osEventMail) { |
WilliamMarshQMUL | 0:ad3cdc525b99 | 128 | message_t* mess = (message_t*)evt.value.p ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 129 | volts = (mess->analog * 330) / 0xffff ; |
anair12345 | 5:beff003ed7e0 | 130 | int mypressEvent = mess->button; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 131 | mailbox.free(mess) ; // free the message space |
anair12345 | 5:beff003ed7e0 | 132 | if(mypressEvent) { |
anair12345 | 5:beff003ed7e0 | 133 | for(int i = 0; i<5;i++){ |
anair12345 | 5:beff003ed7e0 | 134 | threshold[i] = (i*volts)/6; |
anair12345 | 5:beff003ed7e0 | 135 | }// use volts to reset thresholds |
anair12345 | 5:beff003ed7e0 | 136 | } |
anair12345 | 5:beff003ed7e0 | 137 | if (volts > threshold[0] ) led0 = 1 ; else led0 = 0 ; |
anair12345 | 5:beff003ed7e0 | 138 | if (volts > threshold[1]) led1 = 1 ; else led1 = 0 ; |
anair12345 | 5:beff003ed7e0 | 139 | if (volts > threshold[2]) led2 = 1 ; else led2 = 0 ; |
anair12345 | 5:beff003ed7e0 | 140 | if (volts > threshold[3]) led3 = 1 ; else led3 = 0 ; |
anair12345 | 5:beff003ed7e0 | 141 | if (volts > threshold[4]) led4 = 1 ; else led4 = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 142 | vToString(volts, vstring) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 143 | counter++ ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 144 | if (counter == 10) { // limit bandwidth of serial |
WilliamMarshQMUL | 0:ad3cdc525b99 | 145 | pc.printf(vstring) ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 146 | counter = 0 ; |
WilliamMarshQMUL | 0:ad3cdc525b99 | 147 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 148 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 149 | } |
WilliamMarshQMUL | 0:ad3cdc525b99 | 150 | } |