Adjusts the great pinscape controller to work with a cheap linear potentiometer instead of the expensive CCD array
Fork of Pinscape_Controller by
TSL1410R/tsl410r.cpp@14:df700b22ca08, 2014-09-26 (annotated)
- Committer:
- mjr
- Date:
- Fri Sep 26 20:51:02 2014 +0000
- Revision:
- 14:df700b22ca08
- Parent:
- 6:cc35eb643e8f
Reduce button input latency by reducing debounce time and polling during CCD read cycle
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mjr | 2:c174f9ee414a | 1 | #include "mbed.h" |
mjr | 2:c174f9ee414a | 2 | #include "tsl1410r.h" |
mjr | 2:c174f9ee414a | 3 | |
mjr | 2:c174f9ee414a | 4 | TSL1410R::TSL1410R(PinName siPort, PinName clockPort, PinName aoPort) |
mjr | 2:c174f9ee414a | 5 | : si(siPort), clock(clockPort), ao(aoPort) |
mjr | 2:c174f9ee414a | 6 | { |
mjr | 2:c174f9ee414a | 7 | // clear out power-on noise by clocking through all pixels twice |
mjr | 2:c174f9ee414a | 8 | clear(); |
mjr | 2:c174f9ee414a | 9 | clear(); |
mjr | 2:c174f9ee414a | 10 | } |
mjr | 2:c174f9ee414a | 11 | |
mjr | 2:c174f9ee414a | 12 | void TSL1410R::clear() |
mjr | 2:c174f9ee414a | 13 | { |
mjr | 2:c174f9ee414a | 14 | // clock in an SI pulse |
mjr | 2:c174f9ee414a | 15 | si = 1; |
mjr | 2:c174f9ee414a | 16 | clock = 1; |
mjr | 2:c174f9ee414a | 17 | clock = 0; |
mjr | 2:c174f9ee414a | 18 | si = 0; |
mjr | 2:c174f9ee414a | 19 | |
mjr | 2:c174f9ee414a | 20 | // clock out all pixels |
mjr | 2:c174f9ee414a | 21 | for (int i = 0 ; i < nPix + 1 ; ++i) { |
mjr | 2:c174f9ee414a | 22 | clock = 1; |
mjr | 2:c174f9ee414a | 23 | clock = 0; |
mjr | 2:c174f9ee414a | 24 | } |
mjr | 2:c174f9ee414a | 25 | } |
mjr | 2:c174f9ee414a | 26 | |
mjr | 14:df700b22ca08 | 27 | void TSL1410R::read(uint16_t *pix, int n, void (*cb)(void *ctx), void *cbctx, int cbcnt) |
mjr | 2:c174f9ee414a | 28 | { |
mjr | 6:cc35eb643e8f | 29 | // start the next integration cycle by pulsing SI and one clock |
mjr | 2:c174f9ee414a | 30 | si = 1; |
mjr | 2:c174f9ee414a | 31 | clock = 1; |
mjr | 2:c174f9ee414a | 32 | clock = 0; |
mjr | 2:c174f9ee414a | 33 | si = 0; |
mjr | 2:c174f9ee414a | 34 | |
mjr | 2:c174f9ee414a | 35 | // figure how many pixels to skip on each read |
mjr | 2:c174f9ee414a | 36 | int skip = nPix/n - 1; |
mjr | 14:df700b22ca08 | 37 | |
mjr | 14:df700b22ca08 | 38 | // figure the callback interval |
mjr | 14:df700b22ca08 | 39 | int cbInterval = nPix; |
mjr | 14:df700b22ca08 | 40 | if (cb != 0) |
mjr | 14:df700b22ca08 | 41 | cbInterval = nPix/(cbcnt+1); |
mjr | 2:c174f9ee414a | 42 | |
mjr | 14:df700b22ca08 | 43 | // read all of the pixels |
mjr | 14:df700b22ca08 | 44 | for (int src = 0, dst = 0 ; src < nPix ; ) |
mjr | 2:c174f9ee414a | 45 | { |
mjr | 14:df700b22ca08 | 46 | // figure the end of this callback interval |
mjr | 14:df700b22ca08 | 47 | int srcEnd = src + cbInterval; |
mjr | 14:df700b22ca08 | 48 | if (srcEnd > nPix) |
mjr | 14:df700b22ca08 | 49 | srcEnd = nPix; |
mjr | 2:c174f9ee414a | 50 | |
mjr | 14:df700b22ca08 | 51 | // read one callback chunk of pixels |
mjr | 14:df700b22ca08 | 52 | for ( ; src < srcEnd ; ++src) |
mjr | 14:df700b22ca08 | 53 | { |
mjr | 14:df700b22ca08 | 54 | // read this pixel |
mjr | 14:df700b22ca08 | 55 | pix[dst++] = ao.read_u16(); |
mjr | 2:c174f9ee414a | 56 | |
mjr | 14:df700b22ca08 | 57 | // clock in the next pixel |
mjr | 2:c174f9ee414a | 58 | clock = 1; |
mjr | 2:c174f9ee414a | 59 | clock = 0; |
mjr | 14:df700b22ca08 | 60 | |
mjr | 14:df700b22ca08 | 61 | // clock skipped pixels |
mjr | 14:df700b22ca08 | 62 | for (int i = 0 ; i < skip ; ++i, ++src) |
mjr | 14:df700b22ca08 | 63 | { |
mjr | 14:df700b22ca08 | 64 | clock = 1; |
mjr | 14:df700b22ca08 | 65 | clock = 0; |
mjr | 14:df700b22ca08 | 66 | } |
mjr | 2:c174f9ee414a | 67 | } |
mjr | 14:df700b22ca08 | 68 | |
mjr | 14:df700b22ca08 | 69 | // call the callback, if we're not at the last pixel |
mjr | 14:df700b22ca08 | 70 | if (cb != 0 && src < nPix) |
mjr | 14:df700b22ca08 | 71 | (*cb)(cbctx); |
mjr | 2:c174f9ee414a | 72 | } |
mjr | 2:c174f9ee414a | 73 | |
mjr | 2:c174f9ee414a | 74 | // clock out one extra pixel to leave A1 in the high-Z state |
mjr | 2:c174f9ee414a | 75 | clock = 1; |
mjr | 2:c174f9ee414a | 76 | clock = 0; |
mjr | 2:c174f9ee414a | 77 | } |