Etch a Sketch on the uLCD screen with wheel encoders as the knobs.
Dependencies: 4DGL-uLCD-SE mbed
main.cpp@0:c1245c18d07e, 2017-03-13 (annotated)
- Committer:
- simplyellow
- Date:
- Mon Mar 13 21:51:12 2017 +0000
- Revision:
- 0:c1245c18d07e
- Child:
- 1:876e9cc9088b
directional encoder works;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simplyellow | 0:c1245c18d07e | 1 | #include "mbed.h" |
simplyellow | 0:c1245c18d07e | 2 | |
simplyellow | 0:c1245c18d07e | 3 | //#include "uLCD_4DGL.h" |
simplyellow | 0:c1245c18d07e | 4 | |
simplyellow | 0:c1245c18d07e | 5 | Serial pc(USBTX, USBRX); |
simplyellow | 0:c1245c18d07e | 6 | |
simplyellow | 0:c1245c18d07e | 7 | InterruptIn chA(p26); |
simplyellow | 0:c1245c18d07e | 8 | InterruptIn chB(p25); |
simplyellow | 0:c1245c18d07e | 9 | int chanA; |
simplyellow | 0:c1245c18d07e | 10 | int chanB; |
simplyellow | 0:c1245c18d07e | 11 | int currState; |
simplyellow | 0:c1245c18d07e | 12 | int prevState; |
simplyellow | 0:c1245c18d07e | 13 | int change; |
simplyellow | 0:c1245c18d07e | 14 | |
simplyellow | 0:c1245c18d07e | 15 | |
simplyellow | 0:c1245c18d07e | 16 | //uLCD_4GDL uLCD(p9,p10,p11); |
simplyellow | 0:c1245c18d07e | 17 | volatile int count1 = 0; |
simplyellow | 0:c1245c18d07e | 18 | //volatile int count2 = 0; |
simplyellow | 0:c1245c18d07e | 19 | InterruptIn pb(p21); |
simplyellow | 0:c1245c18d07e | 20 | DigitalOut led(LED1); |
simplyellow | 0:c1245c18d07e | 21 | |
simplyellow | 0:c1245c18d07e | 22 | |
simplyellow | 0:c1245c18d07e | 23 | /* |
simplyellow | 0:c1245c18d07e | 24 | Implemented barebones version of "QEI.h" by Aaron Berk for the sake of |
simplyellow | 0:c1245c18d07e | 25 | understanding the concept of X4 encoding |
simplyellow | 0:c1245c18d07e | 26 | 00 01 11 10 00 |
simplyellow | 0:c1245c18d07e | 27 | 4 different states |
simplyellow | 0:c1245c18d07e | 28 | */ |
simplyellow | 0:c1245c18d07e | 29 | |
simplyellow | 0:c1245c18d07e | 30 | void encode() { |
simplyellow | 0:c1245c18d07e | 31 | change = 0; |
simplyellow | 0:c1245c18d07e | 32 | chanA = chA.read(); |
simplyellow | 0:c1245c18d07e | 33 | chanB = chB.read(); |
simplyellow | 0:c1245c18d07e | 34 | currState = (chanA << 1) | (chanB); |
simplyellow | 0:c1245c18d07e | 35 | if (((currState ^ prevState) != 0x3) && (currState != prevState)) { |
simplyellow | 0:c1245c18d07e | 36 | // example 01 <- 11 |
simplyellow | 0:c1245c18d07e | 37 | // change = (11&01 = 01) ^ (01&10 = 00>>1) = 1 |
simplyellow | 0:c1245c18d07e | 38 | // example 10 -> 00 |
simplyellow | 0:c1245c18d07e | 39 | // change = (10&01 = 00) ^ (00&10 = 00>>1) = 0 |
simplyellow | 0:c1245c18d07e | 40 | // change = (if(left) ^ |
simplyellow | 0:c1245c18d07e | 41 | |
simplyellow | 0:c1245c18d07e | 42 | change = (prevState & 0x1) ^ ((currState & 0x2) >> 1); |
simplyellow | 0:c1245c18d07e | 43 | if (change == 0) { |
simplyellow | 0:c1245c18d07e | 44 | change = -1; |
simplyellow | 0:c1245c18d07e | 45 | } |
simplyellow | 0:c1245c18d07e | 46 | count1 += change; |
simplyellow | 0:c1245c18d07e | 47 | } |
simplyellow | 0:c1245c18d07e | 48 | prevState = currState; |
simplyellow | 0:c1245c18d07e | 49 | } |
simplyellow | 0:c1245c18d07e | 50 | void clear() { |
simplyellow | 0:c1245c18d07e | 51 | //uLCD.cls(); |
simplyellow | 0:c1245c18d07e | 52 | led = !led; |
simplyellow | 0:c1245c18d07e | 53 | } |
simplyellow | 0:c1245c18d07e | 54 | |
simplyellow | 0:c1245c18d07e | 55 | int main() { |
simplyellow | 0:c1245c18d07e | 56 | chA.mode(PullUp); |
simplyellow | 0:c1245c18d07e | 57 | chB.mode(PullUp); |
simplyellow | 0:c1245c18d07e | 58 | |
simplyellow | 0:c1245c18d07e | 59 | chanA = chA.read(); |
simplyellow | 0:c1245c18d07e | 60 | chanB = chB.read(); |
simplyellow | 0:c1245c18d07e | 61 | currState = (chanA << 1) | (chanB); |
simplyellow | 0:c1245c18d07e | 62 | prevState = currState; |
simplyellow | 0:c1245c18d07e | 63 | chA.rise(&encode); |
simplyellow | 0:c1245c18d07e | 64 | chA.fall(&encode); |
simplyellow | 0:c1245c18d07e | 65 | chB.rise(&encode); |
simplyellow | 0:c1245c18d07e | 66 | chB.fall(&encode); |
simplyellow | 0:c1245c18d07e | 67 | |
simplyellow | 0:c1245c18d07e | 68 | |
simplyellow | 0:c1245c18d07e | 69 | pb.mode(PullUp); |
simplyellow | 0:c1245c18d07e | 70 | pb.fall(&clear); //pb to clear screen |
simplyellow | 0:c1245c18d07e | 71 | //add a pb to turn on and off pen |
simplyellow | 0:c1245c18d07e | 72 | //add a pot to go through colors |
simplyellow | 0:c1245c18d07e | 73 | //have something display current color and RGB val |
simplyellow | 0:c1245c18d07e | 74 | while(1) { |
simplyellow | 0:c1245c18d07e | 75 | /*if(count1 < 0) { |
simplyellow | 0:c1245c18d07e | 76 | pc.printf("CW \n\r"); |
simplyellow | 0:c1245c18d07e | 77 | } else { |
simplyellow | 0:c1245c18d07e | 78 | pc.printf("CCW \n\r"); |
simplyellow | 0:c1245c18d07e | 79 | }*/ |
simplyellow | 0:c1245c18d07e | 80 | |
simplyellow | 0:c1245c18d07e | 81 | pc.printf("count1: %d\r\n", count1); |
simplyellow | 0:c1245c18d07e | 82 | pc.printf("change: %d\r\n", change); |
simplyellow | 0:c1245c18d07e | 83 | pc.printf("---------\r\n"); |
simplyellow | 0:c1245c18d07e | 84 | wait(.1); |
simplyellow | 0:c1245c18d07e | 85 | } |
simplyellow | 0:c1245c18d07e | 86 | } |