work in progress

Dependencies:   FastAnalogIn FastIO USBDevice mbed FastPWM SimpleDMA

Fork of Pinscape_Controller by Mike R

Committer:
mjr
Date:
Fri Feb 27 04:14:04 2015 +0000
Revision:
17:ab3cec0c8bf4
Parent:
14:df700b22ca08
Child:
18:5e890ebd0023
FastIO and FastAnalogIn; better firing event sensing; potentiometer plunger sensor option; new key debouncing; ZB Launch Ball feature

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjr 2:c174f9ee414a 1 /*
mjr 2:c174f9ee414a 2 * TSL1410R interface class.
mjr 2:c174f9ee414a 3 *
mjr 2:c174f9ee414a 4 * This provides a high-level interface for the Taos TSL1410R linear CCD array sensor.
mjr 2:c174f9ee414a 5 */
mjr 2:c174f9ee414a 6
mjr 2:c174f9ee414a 7 #include "mbed.h"
mjr 17:ab3cec0c8bf4 8 #include "FastIO.h"
mjr 17:ab3cec0c8bf4 9 #include "FastAnalogIn.h"
mjr 2:c174f9ee414a 10
mjr 2:c174f9ee414a 11 #ifndef TSL1410R_H
mjr 2:c174f9ee414a 12 #define TSL1410R_H
mjr 2:c174f9ee414a 13
mjr 17:ab3cec0c8bf4 14 template <PinName siPin, PinName clockPin> class TSL1410R
mjr 2:c174f9ee414a 15 {
mjr 2:c174f9ee414a 16 public:
mjr 17:ab3cec0c8bf4 17 // set up the analog in port for reading the currently selected
mjr 17:ab3cec0c8bf4 18 // pixel value
mjr 17:ab3cec0c8bf4 19 TSL1410R(PinName aoPin) : ao(aoPin)
mjr 17:ab3cec0c8bf4 20 {
mjr 17:ab3cec0c8bf4 21 // disable continuous conversion mode in FastAnalogIn - since we're
mjr 17:ab3cec0c8bf4 22 // reading discrete pixel values, we want to control when the samples
mjr 17:ab3cec0c8bf4 23 // are taken rather than continuously averaging over time
mjr 17:ab3cec0c8bf4 24 ao.disable();
mjr 17:ab3cec0c8bf4 25
mjr 17:ab3cec0c8bf4 26 // clear out power-on noise by clocking through all pixels twice
mjr 17:ab3cec0c8bf4 27 clear();
mjr 17:ab3cec0c8bf4 28 clear();
mjr 17:ab3cec0c8bf4 29 }
mjr 2:c174f9ee414a 30
mjr 17:ab3cec0c8bf4 31 // Read the pixels.
mjr 17:ab3cec0c8bf4 32 //
mjr 17:ab3cec0c8bf4 33 // 'n' specifies the number of pixels to sample, and is the size of
mjr 17:ab3cec0c8bf4 34 // the output array 'pix'. This can be less than the full number
mjr 17:ab3cec0c8bf4 35 // of pixels on the physical device; if it is, we'll spread the
mjr 17:ab3cec0c8bf4 36 // sample evenly across the full length of the device by skipping
mjr 17:ab3cec0c8bf4 37 // one or more pixels between each sampled pixel to pad out the
mjr 17:ab3cec0c8bf4 38 // difference between the sample size and the physical CCD size.
mjr 17:ab3cec0c8bf4 39 // For example, if the physical sensor has 1280 pixels, and 'n' is
mjr 17:ab3cec0c8bf4 40 // 640, we'll read every other pixel and skip every other pixel.
mjr 17:ab3cec0c8bf4 41 // If 'n' is 160, we'll read every 8th pixel and skip 7 between
mjr 17:ab3cec0c8bf4 42 // each sample.
mjr 17:ab3cec0c8bf4 43 //
mjr 17:ab3cec0c8bf4 44 // The reason that we provide this subset mode (where 'n' is less
mjr 17:ab3cec0c8bf4 45 // than the physical pixel count) is that reading a pixel is the most
mjr 17:ab3cec0c8bf4 46 // time-consuming part of the scan. For each pixel we read, we have
mjr 17:ab3cec0c8bf4 47 // to wait for the pixel's charge to transfer from its internal smapling
mjr 17:ab3cec0c8bf4 48 // capacitor to the CCD's output pin, for that charge to transfer to
mjr 17:ab3cec0c8bf4 49 // the KL25Z input pin, and for the KL25Z ADC to get a stable reading.
mjr 17:ab3cec0c8bf4 50 // This all takes on the order of 20us per pixel. Skipping a pixel
mjr 17:ab3cec0c8bf4 51 // only requires a clock pulse, which takes about 350ns. So we can
mjr 17:ab3cec0c8bf4 52 // skip 60 pixels in the time it takes to sample 1 pixel.
mjr 2:c174f9ee414a 53 //
mjr 2:c174f9ee414a 54 // We clock an SI pulse at the beginning of the read. This starts the
mjr 2:c174f9ee414a 55 // next integration cycle: the pixel array will reset on the SI, and
mjr 17:ab3cec0c8bf4 56 // the integration starts 18 clocks later. So by the time this method
mjr 17:ab3cec0c8bf4 57 // returns, the next sample will have been integrating for npix-18 clocks.
mjr 17:ab3cec0c8bf4 58 // That's usually enough time to allow immediately reading the next
mjr 17:ab3cec0c8bf4 59 // sample. If more integration time is required, the caller can simply
mjr 2:c174f9ee414a 60 // sleep/spin for the desired additional time, or can do other work that
mjr 17:ab3cec0c8bf4 61 // takes the desired additional time.
mjr 2:c174f9ee414a 62 //
mjr 2:c174f9ee414a 63 // If the caller has other work to tend to that takes longer than the
mjr 2:c174f9ee414a 64 // desired maximum integration time, it can call clear() to clock out
mjr 2:c174f9ee414a 65 // the current pixels and start a fresh integration cycle.
mjr 14:df700b22ca08 66 void read(uint16_t *pix, int n) { read(pix, n, 0, 0, 0); }
mjr 14:df700b22ca08 67
mjr 14:df700b22ca08 68 // Read with interval callback. We'll call the callback the given
mjr 14:df700b22ca08 69 // number of times per read cycle.
mjr 17:ab3cec0c8bf4 70 void read(uint16_t *pix, int n, void (*cb)(void *ctx), void *cbctx, int cbcnt)
mjr 17:ab3cec0c8bf4 71 {
mjr 17:ab3cec0c8bf4 72 // start the next integration cycle by pulsing SI and one clock
mjr 17:ab3cec0c8bf4 73 si = 1;
mjr 17:ab3cec0c8bf4 74 clock = 1;
mjr 17:ab3cec0c8bf4 75 si = 0;
mjr 17:ab3cec0c8bf4 76 clock = 0;
mjr 17:ab3cec0c8bf4 77
mjr 17:ab3cec0c8bf4 78 // figure how many pixels to skip on each read
mjr 17:ab3cec0c8bf4 79 int skip = nPix/n - 1;
mjr 17:ab3cec0c8bf4 80
mjr 17:ab3cec0c8bf4 81 // figure the callback interval
mjr 17:ab3cec0c8bf4 82 int cbInterval = nPix;
mjr 17:ab3cec0c8bf4 83 if (cb != 0)
mjr 17:ab3cec0c8bf4 84 cbInterval = nPix/(cbcnt+1);
mjr 17:ab3cec0c8bf4 85
mjr 17:ab3cec0c8bf4 86 // read all of the pixels
mjr 17:ab3cec0c8bf4 87 for (int src = 0, dst = 0 ; src < nPix ; )
mjr 17:ab3cec0c8bf4 88 {
mjr 17:ab3cec0c8bf4 89 // figure the end of this callback interval
mjr 17:ab3cec0c8bf4 90 int srcEnd = src + cbInterval;
mjr 17:ab3cec0c8bf4 91 if (srcEnd > nPix)
mjr 17:ab3cec0c8bf4 92 srcEnd = nPix;
mjr 17:ab3cec0c8bf4 93
mjr 17:ab3cec0c8bf4 94 // read one callback chunk of pixels
mjr 17:ab3cec0c8bf4 95 for ( ; src < srcEnd ; ++src)
mjr 17:ab3cec0c8bf4 96 {
mjr 17:ab3cec0c8bf4 97 // clock in and read the next pixel
mjr 17:ab3cec0c8bf4 98 clock = 1;
mjr 17:ab3cec0c8bf4 99 ao.enable();
mjr 17:ab3cec0c8bf4 100 wait_us(1);
mjr 17:ab3cec0c8bf4 101 clock = 0;
mjr 17:ab3cec0c8bf4 102 wait_us(11);
mjr 17:ab3cec0c8bf4 103 pix[dst++] = ao.read_u16();
mjr 17:ab3cec0c8bf4 104 ao.disable();
mjr 17:ab3cec0c8bf4 105
mjr 17:ab3cec0c8bf4 106 // clock skipped pixels
mjr 17:ab3cec0c8bf4 107 for (int i = 0 ; i < skip ; ++i, ++src)
mjr 17:ab3cec0c8bf4 108 {
mjr 17:ab3cec0c8bf4 109 clock = 1;
mjr 17:ab3cec0c8bf4 110 clock = 0;
mjr 17:ab3cec0c8bf4 111 }
mjr 17:ab3cec0c8bf4 112 }
mjr 17:ab3cec0c8bf4 113
mjr 17:ab3cec0c8bf4 114 // call the callback, if we're not at the last pixel
mjr 17:ab3cec0c8bf4 115 if (cb != 0 && src < nPix)
mjr 17:ab3cec0c8bf4 116 (*cb)(cbctx);
mjr 17:ab3cec0c8bf4 117 }
mjr 17:ab3cec0c8bf4 118
mjr 17:ab3cec0c8bf4 119 // clock out one extra pixel to leave A1 in the high-Z state
mjr 17:ab3cec0c8bf4 120 clock = 1;
mjr 17:ab3cec0c8bf4 121 clock = 0;
mjr 17:ab3cec0c8bf4 122 }
mjr 2:c174f9ee414a 123
mjr 2:c174f9ee414a 124 // Clock through all pixels to clear the array. Pulses SI at the
mjr 2:c174f9ee414a 125 // beginning of the operation, which starts a new integration cycle.
mjr 2:c174f9ee414a 126 // The caller can thus immediately call read() to read the pixels
mjr 2:c174f9ee414a 127 // integrated while the clear() was taking place.
mjr 17:ab3cec0c8bf4 128 void clear()
mjr 17:ab3cec0c8bf4 129 {
mjr 17:ab3cec0c8bf4 130 // clock in an SI pulse
mjr 17:ab3cec0c8bf4 131 si = 1;
mjr 17:ab3cec0c8bf4 132 clock = 1;
mjr 17:ab3cec0c8bf4 133 clock = 0;
mjr 17:ab3cec0c8bf4 134 si = 0;
mjr 17:ab3cec0c8bf4 135
mjr 17:ab3cec0c8bf4 136 // clock out all pixels
mjr 17:ab3cec0c8bf4 137 for (int i = 0 ; i < nPix + 1 ; ++i) {
mjr 17:ab3cec0c8bf4 138 clock = 1;
mjr 17:ab3cec0c8bf4 139 clock = 0;
mjr 17:ab3cec0c8bf4 140 }
mjr 17:ab3cec0c8bf4 141 }
mjr 2:c174f9ee414a 142
mjr 2:c174f9ee414a 143 // number of pixels in the array
mjr 2:c174f9ee414a 144 static const int nPix = 1280;
mjr 2:c174f9ee414a 145
mjr 2:c174f9ee414a 146
mjr 2:c174f9ee414a 147 private:
mjr 17:ab3cec0c8bf4 148 FastOut<siPin> si;
mjr 17:ab3cec0c8bf4 149 FastOut<clockPin> clock;
mjr 17:ab3cec0c8bf4 150 FastAnalogIn ao;
mjr 2:c174f9ee414a 151 };
mjr 2:c174f9ee414a 152
mjr 2:c174f9ee414a 153 #endif /* TSL1410R_H */