Etch a Sketch on the uLCD screen with wheel encoders as the knobs.
Dependencies: 4DGL-uLCD-SE mbed
Diff: main.cpp
- Revision:
- 1:876e9cc9088b
- Parent:
- 0:c1245c18d07e
- Child:
- 2:8752f5af08a4
--- a/main.cpp Mon Mar 13 21:51:12 2017 +0000 +++ b/main.cpp Mon Mar 13 22:27:09 2017 +0000 @@ -4,18 +4,26 @@ Serial pc(USBTX, USBRX); -InterruptIn chA(p26); -InterruptIn chB(p25); -int chanA; -int chanB; -int currState; -int prevState; -int change; +InterruptIn leftchA(p26); +InterruptIn leftchB(p25); +int leftChanA; +int leftChanB; +int leftCurrState; +int leftPrevState; +int leftChange; +InterruptIn rightchA(p13); +InterruptIn rightchB(p14); +int rightChanA; +int rightChanB; +int rightCurrState; +int rightPrevState; +int rightChange; + +volatile int count1 = 0; +volatile int count2 = 0; //uLCD_4GDL uLCD(p9,p10,p11); -volatile int count1 = 0; -//volatile int count2 = 0; InterruptIn pb(p21); DigitalOut led(LED1); @@ -27,59 +35,91 @@ 4 different states */ -void encode() { - change = 0; - chanA = chA.read(); - chanB = chB.read(); - currState = (chanA << 1) | (chanB); - if (((currState ^ prevState) != 0x3) && (currState != prevState)) { +void encode1() { + leftChange = 0; + leftChanA = leftchA.read(); + leftChanB = leftchB.read(); + leftCurrState = (leftChanA << 1) | (leftChanB); + if (((leftCurrState ^ leftPrevState) != 0x3) && (leftCurrState != leftPrevState)) { + // example 01 <- 11 + // change = (11&01 = 01) ^ (01&10 = 00>>1) = 1 + // example 10 -> 00 + // change = (10&01 = 00) ^ (00&10 = 00>>1) = 0 + // change = (if(left) ^ + + leftChange = (leftPrevState & 0x1) ^ ((leftCurrState & 0x2) >> 1); + if (leftChange == 0) { + leftChange = -1; + } + count1 += leftChange; + } + leftPrevState = leftCurrState; +} + +void encode2() { + rightChange = 0; + rightChanA = rightchA.read(); + rightChanB = rightchB.read(); + rightCurrState = (rightChanA << 1) | (rightChanB); + if (((rightCurrState ^ rightPrevState) != 0x3) && (rightCurrState != rightPrevState)) { // example 01 <- 11 // change = (11&01 = 01) ^ (01&10 = 00>>1) = 1 // example 10 -> 00 // change = (10&01 = 00) ^ (00&10 = 00>>1) = 0 // change = (if(left) ^ - change = (prevState & 0x1) ^ ((currState & 0x2) >> 1); - if (change == 0) { - change = -1; + rightChange = (rightPrevState & 0x1) ^ ((rightCurrState & 0x2) >> 1); + if (rightChange == 0) { + rightChange = -1; } - count1 += change; + count2 += rightChange; } - prevState = currState; + rightPrevState = rightCurrState; } + + void clear() { //uLCD.cls(); led = !led; } -int main() { - chA.mode(PullUp); - chB.mode(PullUp); +void setupEncoder() { + leftchA.mode(PullUp); + leftchB.mode(PullUp); + rightchA.mode(PullUp); + rightchB.mode(PullUp); + + leftChanA = leftchA.read(); + leftChanB = leftchB.read(); + rightChanA = rightchA.read(); + rightChanB = rightchB.read(); - chanA = chA.read(); - chanB = chB.read(); - currState = (chanA << 1) | (chanB); - prevState = currState; - chA.rise(&encode); - chA.fall(&encode); - chB.rise(&encode); - chB.fall(&encode); + leftCurrState = (leftChanA << 1) | (leftChanB); + leftPrevState = leftCurrState; + rightCurrState = (rightChanA << 1) | (rightChanB); + rightPrevState = rightCurrState; + leftchA.rise(&encode1); + leftchA.fall(&encode1); + leftchB.rise(&encode1); + leftchB.fall(&encode1); + rightchA.rise(&encode2); + rightchA.fall(&encode2); + rightchB.rise(&encode2); + rightchB.fall(&encode2); +} + +int main() { + setupEncoder(); pb.mode(PullUp); pb.fall(&clear); //pb to clear screen //add a pb to turn on and off pen //add a pot to go through colors //have something display current color and RGB val while(1) { - /*if(count1 < 0) { - pc.printf("CW \n\r"); - } else { - pc.printf("CCW \n\r"); - }*/ - pc.printf("count1: %d\r\n", count1); - pc.printf("change: %d\r\n", change); + pc.printf("count2: %d\r\n", count2); pc.printf("---------\r\n"); wait(.1); }