work in progress

Dependencies:   FastAnalogIn FastIO USBDevice mbed FastPWM SimpleDMA

Fork of Pinscape_Controller by Mike R

Committer:
mjr
Date:
Fri Feb 27 07:41:29 2015 +0000
Revision:
18:5e890ebd0023
Parent:
17:ab3cec0c8bf4
Child:
25:e22b88bd783a
Old debounce about to be removed

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 18:5e890ebd0023 66 void read(uint16_t *pix, int n)
mjr 17:ab3cec0c8bf4 67 {
mjr 17:ab3cec0c8bf4 68 // start the next integration cycle by pulsing SI and one clock
mjr 17:ab3cec0c8bf4 69 si = 1;
mjr 17:ab3cec0c8bf4 70 clock = 1;
mjr 17:ab3cec0c8bf4 71 si = 0;
mjr 17:ab3cec0c8bf4 72 clock = 0;
mjr 17:ab3cec0c8bf4 73
mjr 17:ab3cec0c8bf4 74 // figure how many pixels to skip on each read
mjr 17:ab3cec0c8bf4 75 int skip = nPix/n - 1;
mjr 17:ab3cec0c8bf4 76
mjr 17:ab3cec0c8bf4 77 // read all of the pixels
mjr 18:5e890ebd0023 78 for (int src = 0, dst = 0 ; src < nPix ; ++src)
mjr 17:ab3cec0c8bf4 79 {
mjr 18:5e890ebd0023 80 // clock in and read the next pixel
mjr 18:5e890ebd0023 81 clock = 1;
mjr 18:5e890ebd0023 82 ao.enable();
mjr 18:5e890ebd0023 83 wait_us(1);
mjr 18:5e890ebd0023 84 clock = 0;
mjr 18:5e890ebd0023 85 wait_us(11);
mjr 18:5e890ebd0023 86 pix[dst++] = ao.read_u16();
mjr 18:5e890ebd0023 87 ao.disable();
mjr 18:5e890ebd0023 88
mjr 18:5e890ebd0023 89 // clock skipped pixels
mjr 18:5e890ebd0023 90 for (int i = 0 ; i < skip ; ++i, ++src)
mjr 17:ab3cec0c8bf4 91 {
mjr 17:ab3cec0c8bf4 92 clock = 1;
mjr 17:ab3cec0c8bf4 93 clock = 0;
mjr 17:ab3cec0c8bf4 94 }
mjr 17:ab3cec0c8bf4 95 }
mjr 17:ab3cec0c8bf4 96
mjr 17:ab3cec0c8bf4 97 // clock out one extra pixel to leave A1 in the high-Z state
mjr 17:ab3cec0c8bf4 98 clock = 1;
mjr 17:ab3cec0c8bf4 99 clock = 0;
mjr 17:ab3cec0c8bf4 100 }
mjr 2:c174f9ee414a 101
mjr 2:c174f9ee414a 102 // Clock through all pixels to clear the array. Pulses SI at the
mjr 2:c174f9ee414a 103 // beginning of the operation, which starts a new integration cycle.
mjr 2:c174f9ee414a 104 // The caller can thus immediately call read() to read the pixels
mjr 2:c174f9ee414a 105 // integrated while the clear() was taking place.
mjr 17:ab3cec0c8bf4 106 void clear()
mjr 17:ab3cec0c8bf4 107 {
mjr 17:ab3cec0c8bf4 108 // clock in an SI pulse
mjr 17:ab3cec0c8bf4 109 si = 1;
mjr 17:ab3cec0c8bf4 110 clock = 1;
mjr 17:ab3cec0c8bf4 111 clock = 0;
mjr 17:ab3cec0c8bf4 112 si = 0;
mjr 17:ab3cec0c8bf4 113
mjr 17:ab3cec0c8bf4 114 // clock out all pixels
mjr 17:ab3cec0c8bf4 115 for (int i = 0 ; i < nPix + 1 ; ++i) {
mjr 17:ab3cec0c8bf4 116 clock = 1;
mjr 17:ab3cec0c8bf4 117 clock = 0;
mjr 17:ab3cec0c8bf4 118 }
mjr 17:ab3cec0c8bf4 119 }
mjr 2:c174f9ee414a 120
mjr 2:c174f9ee414a 121 // number of pixels in the array
mjr 2:c174f9ee414a 122 static const int nPix = 1280;
mjr 2:c174f9ee414a 123
mjr 2:c174f9ee414a 124
mjr 2:c174f9ee414a 125 private:
mjr 17:ab3cec0c8bf4 126 FastOut<siPin> si;
mjr 17:ab3cec0c8bf4 127 FastOut<clockPin> clock;
mjr 17:ab3cec0c8bf4 128 FastAnalogIn ao;
mjr 2:c174f9ee414a 129 };
mjr 2:c174f9ee414a 130
mjr 2:c174f9ee414a 131 #endif /* TSL1410R_H */