Heroic Robotics / SD600A

Fork of SD600A by Heroic Robotics

Committer:
heroic
Date:
Thu Oct 11 04:05:02 2012 +0000
Revision:
16:910bf46f2ce4
Parent:
15:2733cd5f34e4
Child:
17:a625ffe26993
Move soft SPI task to interrupt routine.  Remove idle mode, since we now clock out continuously.

Who changed what in which revision?

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