Heroic Robotics / SD600A

Fork of SD600A by Heroic Robotics

Committer:
heroic
Date:
Sat Oct 13 03:10:29 2012 +0000
Revision:
25:14e5984d3aae
Parent:
23:2e60470dd134
Child:
26:98163818a82e
Diddle interrupt frequency... again.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
heroic 23:2e60470dd134 1 // Mbed library to control SD600A-based RGB LED Strips
heroic 23:2e60470dd134 2 // Partially based on work (c) 2011 Jelmer Tiete
heroic 23:2e60470dd134 3 //
heroic 23:2e60470dd134 4 // Ported from Arduino by
heroic 23:2e60470dd134 5 // Jas Strong <jasmine@electronpusher.org>
heroic 23:2e60470dd134 6 /*****************************************************************************/
heroic 23:2e60470dd134 7
heroic 23:2e60470dd134 8 #include "rtos.h"
heroic 23:2e60470dd134 9 #include "LedStrip.h"
heroic 23:2e60470dd134 10 #include "SD600A.h"
heroic 23:2e60470dd134 11
heroic 23:2e60470dd134 12 /*
heroic 25:14e5984d3aae 13 * Soft SPI clockout routine. Triggered every few microseconds,
heroic 23:2e60470dd134 14 * repeatedly clocks out the strip contents. Since each update function
heroic 23:2e60470dd134 15 * actually updates a byte at a time, and the C-m3 peeks the write buffer,
heroic 23:2e60470dd134 16 * there is no need to lock the buffer.
heroic 23:2e60470dd134 17 */
heroic 23:2e60470dd134 18
heroic 23:2e60470dd134 19 void SD600A::spi_isr(void) {
heroic 23:2e60470dd134 20 clk = 1;
heroic 23:2e60470dd134 21 dat = pixels[byte_index] & (0x80 >> bit_index);
heroic 23:2e60470dd134 22 clk = 0;
heroic 23:2e60470dd134 23 bit_index++;
heroic 23:2e60470dd134 24 clk = 1;
heroic 23:2e60470dd134 25 dat = pixels[byte_index] & (0x80 >> bit_index);
heroic 23:2e60470dd134 26 clk = 0;
heroic 23:2e60470dd134 27 bit_index++;
heroic 23:2e60470dd134 28 if (bit_index == 8) {
heroic 23:2e60470dd134 29 byte_index++;
heroic 23:2e60470dd134 30 bit_index = 0;
heroic 23:2e60470dd134 31 }
heroic 23:2e60470dd134 32 if (byte_index == data_length)
heroic 23:2e60470dd134 33 byte_index = 0;
heroic 23:2e60470dd134 34 }
heroic 23:2e60470dd134 35
heroic 23:2e60470dd134 36 SD600A::SD600A(PinName dataPin, PinName clockPin, int n) :
heroic 23:2e60470dd134 37 dat(dataPin),
heroic 23:2e60470dd134 38 clk(clockPin) {
heroic 23:2e60470dd134 39 // Allocate 3 bytes per pixel:
heroic 23:2e60470dd134 40 numLEDs = n;
heroic 23:2e60470dd134 41 if ((pixels = (uint8_t *)malloc(4+ numLEDs * 3))) {
heroic 23:2e60470dd134 42 memset(pixels, 0, numLEDs * 3); // Init to RGB 'off' state
heroic 23:2e60470dd134 43 pixels[numLEDs*3] = 0x7f;
heroic 23:2e60470dd134 44 pixels[numLEDs*3+1] = 0xff;
heroic 23:2e60470dd134 45 pixels[numLEDs*3+2] = 0xff;
heroic 23:2e60470dd134 46 pixels[numLEDs*3+3] = 0x80;
heroic 23:2e60470dd134 47 data_length = numLEDs*3 +4;
heroic 23:2e60470dd134 48 byte_index = 0;
heroic 23:2e60470dd134 49 bit_index = 0;
heroic 23:2e60470dd134 50 } else error("SD600A could not allocate memory!\r\n");
heroic 23:2e60470dd134 51 NVIC_SetPriority(TIMER3_IRQn, 0);
heroic 23:2e60470dd134 52 idletoggle.attach_us(this, &SD600A::spi_isr, INTERRUPT_INTERVAL);
heroic 23:2e60470dd134 53 }
heroic 23:2e60470dd134 54
heroic 23:2e60470dd134 55 void SD600A::begin(void) {
heroic 23:2e60470dd134 56 // Null stub.
heroic 23:2e60470dd134 57 }
heroic 23:2e60470dd134 58
heroic 23:2e60470dd134 59 uint16_t SD600A::numPixels(void) {
heroic 23:2e60470dd134 60 return numLEDs;
heroic 23:2e60470dd134 61 }
heroic 23:2e60470dd134 62
heroic 23:2e60470dd134 63 void SD600A::blank(void) {
heroic 23:2e60470dd134 64 memset(pixels, 0x00, numLEDs * 3);
heroic 23:2e60470dd134 65 }
heroic 23:2e60470dd134 66
heroic 23:2e60470dd134 67 void SD600A::show(void) {
heroic 23:2e60470dd134 68 // Null stub, since shows continuously.
heroic 23:2e60470dd134 69 }
heroic 23:2e60470dd134 70
heroic 23:2e60470dd134 71 uint32_t SD600A::total_luminance(void) {
heroic 23:2e60470dd134 72 uint32_t running_total;
heroic 23:2e60470dd134 73 running_total = 0;
heroic 23:2e60470dd134 74 for (int i=0; i<numLEDs*3; i++)
heroic 23:2e60470dd134 75 running_total += pixels[i];
heroic 23:2e60470dd134 76 return running_total;
heroic 23:2e60470dd134 77 }
heroic 23:2e60470dd134 78
heroic 23:2e60470dd134 79 // Convert R,G,B to combined 32-bit color
heroic 23:2e60470dd134 80 uint32_t SD600A::Color(uint8_t r, uint8_t g, uint8_t b) {
heroic 23:2e60470dd134 81 // Take 23 bits of the value and append them end to end
heroic 23:2e60470dd134 82 // We cannot drive all ones or it will make the part latch if the previous word ended in one!
heroic 23:2e60470dd134 83 return 0xfefefe & ((uint32_t)b << 16) | ((uint32_t)r << 8) | (uint32_t)g;
heroic 23:2e60470dd134 84 }
heroic 23:2e60470dd134 85
heroic 23:2e60470dd134 86 // store the rgb component in our array
heroic 23:2e60470dd134 87 void SD600A::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
heroic 23:2e60470dd134 88 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
heroic 23:2e60470dd134 89 n=numLEDs-(n+1);
heroic 23:2e60470dd134 90
heroic 23:2e60470dd134 91 pixels[n*3 ] = g & 0xfe;
heroic 23:2e60470dd134 92 pixels[n*3+1] = b & 0xfe;
heroic 23:2e60470dd134 93 pixels[n*3+2] = r & 0xfe;
heroic 23:2e60470dd134 94 }
heroic 23:2e60470dd134 95
heroic 23:2e60470dd134 96 void SD600A::setPixelR(uint16_t n, uint8_t r) {
heroic 23:2e60470dd134 97 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
heroic 23:2e60470dd134 98 n=numLEDs-(n+1);
heroic 23:2e60470dd134 99 pixels[n*3+2] = r & 0xfe;
heroic 23:2e60470dd134 100 }
heroic 23:2e60470dd134 101
heroic 23:2e60470dd134 102 void SD600A::setPixelG(uint16_t n, uint8_t g) {
heroic 23:2e60470dd134 103 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
heroic 23:2e60470dd134 104 n=numLEDs-(n+1);
heroic 23:2e60470dd134 105 pixels[n*3+1] = g & 0xfe;
heroic 23:2e60470dd134 106 }
heroic 23:2e60470dd134 107
heroic 23:2e60470dd134 108 void SD600A::setPixelB(uint16_t n, uint8_t b) {
heroic 23:2e60470dd134 109 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
heroic 23:2e60470dd134 110 n=numLEDs-(n+1);
heroic 23:2e60470dd134 111 pixels[n*3] = b & 0xfe;
heroic 23:2e60470dd134 112 }
heroic 23:2e60470dd134 113
heroic 23:2e60470dd134 114 void SD600A::setPixelColor(uint16_t n, uint32_t c) {
heroic 23:2e60470dd134 115 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
heroic 23:2e60470dd134 116 n=numLEDs-(n+1);
heroic 23:2e60470dd134 117 pixels[n*3 ] = (c >> 16) & 0xfe;
heroic 23:2e60470dd134 118 pixels[n*3+1] = (c >> 8) & 0xfe;
heroic 23:2e60470dd134 119 pixels[n*3+2] = c & 0xfe;
heroic 23:2e60470dd134 120 }