Etch a Sketch on the uLCD screen with wheel encoders as the knobs.

Dependencies:   4DGL-uLCD-SE mbed

main.cpp

Committer:
simplyellow
Date:
2017-03-13
Revision:
1:876e9cc9088b
Parent:
0:c1245c18d07e
Child:
2:8752f5af08a4

File content as of revision 1:876e9cc9088b:

#include "mbed.h"

//#include "uLCD_4DGL.h"

Serial pc(USBTX, USBRX);

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);
InterruptIn pb(p21);
DigitalOut led(LED1);


/*
Implemented barebones version of "QEI.h" by Aaron Berk for the sake of
understanding the concept of X4 encoding
00 01 11 10 00
4 different states
*/

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)    ^

        rightChange = (rightPrevState & 0x1) ^ ((rightCurrState & 0x2) >> 1);
        if (rightChange == 0) {
            rightChange = -1;
        }
        count2 += rightChange;
    }
    rightPrevState = rightCurrState;
}


void clear() {
    //uLCD.cls();
    led = !led;
}

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();

    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) {
        pc.printf("count1: %d\r\n", count1);
        pc.printf("count2: %d\r\n", count2);
        pc.printf("---------\r\n");
        wait(.1);
    }
}