Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SD600A by
SD600A.cpp@17:a625ffe26993, 2012-10-11 (annotated)
- Committer:
- heroic
- Date:
- Thu Oct 11 04:30:47 2012 +0000
- Revision:
- 17:a625ffe26993
- Parent:
- 16:910bf46f2ce4
- Child:
- 18:a61600110235
Better comment.
Who changed what in which revision?
User | Revision | Line number | New 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 | 17:a625ffe26993 | 12 | /* |
heroic | 17:a625ffe26993 | 13 | * Soft SPI clockout routine. Triggered every ten microseconds, |
heroic | 17:a625ffe26993 | 14 | * repeatedly clocks out the strip contents. Since each update function |
heroic | 17:a625ffe26993 | 15 | * actually updates a byte at a time, and the C-m3 peeks the write buffer, |
heroic | 17:a625ffe26993 | 16 | * there is no need to lock the buffer. |
heroic | 17:a625ffe26993 | 17 | */ |
heroic | 16:910bf46f2ce4 | 18 | |
heroic | 12:08d02c0aaa67 | 19 | void SD600A::idle_function(void) { |
heroic | 16:910bf46f2ce4 | 20 | clk = 1; |
heroic | 16:910bf46f2ce4 | 21 | dat = pixels[byte_index] & (0x80 >> bit_index); |
heroic | 16:910bf46f2ce4 | 22 | clk = 0; |
heroic | 16:910bf46f2ce4 | 23 | bit_index++; |
heroic | 16:910bf46f2ce4 | 24 | if (bit_index == 8) { |
heroic | 16:910bf46f2ce4 | 25 | byte_index++; |
heroic | 16:910bf46f2ce4 | 26 | bit_index = 0; |
heroic | 16:910bf46f2ce4 | 27 | } |
heroic | 16:910bf46f2ce4 | 28 | if (byte_index == data_length) |
heroic | 16:910bf46f2ce4 | 29 | byte_index = 0; |
heroic | 12:08d02c0aaa67 | 30 | } |
heroic | 12:08d02c0aaa67 | 31 | |
heroic | 12:08d02c0aaa67 | 32 | SD600A::SD600A(PinName dataPin, PinName clockPin, int n) : |
heroic | 12:08d02c0aaa67 | 33 | dat(dataPin), |
heroic | 12:08d02c0aaa67 | 34 | clk(clockPin) { |
heroic | 12:08d02c0aaa67 | 35 | // Allocate 3 bytes per pixel: |
heroic | 12:08d02c0aaa67 | 36 | numLEDs = n; |
heroic | 16:910bf46f2ce4 | 37 | if ((pixels = (uint8_t *)malloc(4+ numLEDs * 3))) { |
heroic | 12:08d02c0aaa67 | 38 | memset(pixels, 0, numLEDs * 3); // Init to RGB 'off' state |
heroic | 16:910bf46f2ce4 | 39 | pixels[numLEDs*3] = 0x7f; |
heroic | 16:910bf46f2ce4 | 40 | pixels[numLEDs*3+1] = 0xff; |
heroic | 16:910bf46f2ce4 | 41 | pixels[numLEDs*3+2] = 0xff; |
heroic | 16:910bf46f2ce4 | 42 | pixels[numLEDs*3+3] = 0x80; |
heroic | 16:910bf46f2ce4 | 43 | data_length = numLEDs*3 +4; |
heroic | 16:910bf46f2ce4 | 44 | byte_index = 0; |
heroic | 16:910bf46f2ce4 | 45 | bit_index = 0; |
heroic | 16:910bf46f2ce4 | 46 | } else error("SD600A could not allocate memory!\r\n"); |
heroic | 16:910bf46f2ce4 | 47 | NVIC_SetPriority(TIMER3_IRQn, 0); |
heroic | 12:08d02c0aaa67 | 48 | idletoggle.attach_us(this, &SD600A::idle_function, IDLE_INTERVAL); |
heroic | 12:08d02c0aaa67 | 49 | } |
heroic | 12:08d02c0aaa67 | 50 | |
heroic | 12:08d02c0aaa67 | 51 | void SD600A::begin(void) { |
heroic | 16:910bf46f2ce4 | 52 | // Null stub. |
heroic | 12:08d02c0aaa67 | 53 | } |
heroic | 12:08d02c0aaa67 | 54 | |
heroic | 12:08d02c0aaa67 | 55 | uint16_t SD600A::numPixels(void) { |
heroic | 12:08d02c0aaa67 | 56 | return numLEDs; |
heroic | 12:08d02c0aaa67 | 57 | } |
heroic | 12:08d02c0aaa67 | 58 | |
heroic | 12:08d02c0aaa67 | 59 | void SD600A::blank(void) { |
heroic | 12:08d02c0aaa67 | 60 | memset(pixels, 0x00, numLEDs * 3); |
heroic | 12:08d02c0aaa67 | 61 | } |
heroic | 12:08d02c0aaa67 | 62 | |
heroic | 12:08d02c0aaa67 | 63 | void SD600A::show(void) { |
heroic | 16:910bf46f2ce4 | 64 | // Null stub, since shows continuously. |
heroic | 12:08d02c0aaa67 | 65 | } |
heroic | 12:08d02c0aaa67 | 66 | |
heroic | 12:08d02c0aaa67 | 67 | // Convert R,G,B to combined 32-bit color |
heroic | 12:08d02c0aaa67 | 68 | uint32_t SD600A::Color(uint8_t r, uint8_t g, uint8_t b) { |
heroic | 12:08d02c0aaa67 | 69 | // Take 23 bits of the value and append them end to end |
heroic | 12:08d02c0aaa67 | 70 | // We cannot drive all ones or it will make the part latch if the previous word ended in one! |
heroic | 12:08d02c0aaa67 | 71 | return 0xfefefe & ((uint32_t)g << 16) | ((uint32_t)r << 8) | (uint32_t)b; |
heroic | 12:08d02c0aaa67 | 72 | } |
heroic | 12:08d02c0aaa67 | 73 | |
heroic | 12:08d02c0aaa67 | 74 | // store the rgb component in our array |
heroic | 12:08d02c0aaa67 | 75 | void SD600A::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) { |
heroic | 12:08d02c0aaa67 | 76 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
heroic | 12:08d02c0aaa67 | 77 | |
heroic | 12:08d02c0aaa67 | 78 | pixels[n*3 ] = b & 0xfe; |
heroic | 12:08d02c0aaa67 | 79 | pixels[n*3+1] = g & 0xfe; |
heroic | 12:08d02c0aaa67 | 80 | pixels[n*3+2] = r & 0xfe; |
heroic | 12:08d02c0aaa67 | 81 | } |
heroic | 12:08d02c0aaa67 | 82 | |
heroic | 12:08d02c0aaa67 | 83 | void SD600A::setPixelR(uint16_t n, uint8_t r) { |
heroic | 12:08d02c0aaa67 | 84 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
heroic | 12:08d02c0aaa67 | 85 | |
heroic | 12:08d02c0aaa67 | 86 | pixels[n*3+2] = r & 0xfe; |
heroic | 12:08d02c0aaa67 | 87 | } |
heroic | 12:08d02c0aaa67 | 88 | |
heroic | 12:08d02c0aaa67 | 89 | void SD600A::setPixelG(uint16_t n, uint8_t g) { |
heroic | 12:08d02c0aaa67 | 90 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
heroic | 12:08d02c0aaa67 | 91 | |
heroic | 12:08d02c0aaa67 | 92 | pixels[n*3+1] = g & 0xfe; |
heroic | 12:08d02c0aaa67 | 93 | } |
heroic | 12:08d02c0aaa67 | 94 | |
heroic | 12:08d02c0aaa67 | 95 | void SD600A::setPixelB(uint16_t n, uint8_t b) { |
heroic | 12:08d02c0aaa67 | 96 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
heroic | 12:08d02c0aaa67 | 97 | |
heroic | 12:08d02c0aaa67 | 98 | pixels[n*3] = b & 0xfe; |
heroic | 12:08d02c0aaa67 | 99 | } |
heroic | 12:08d02c0aaa67 | 100 | |
heroic | 12:08d02c0aaa67 | 101 | void SD600A::setPixelColor(uint16_t n, uint32_t c) { |
heroic | 12:08d02c0aaa67 | 102 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
heroic | 12:08d02c0aaa67 | 103 | |
heroic | 12:08d02c0aaa67 | 104 | pixels[n*3 ] = (c >> 16) & 0xfe; |
heroic | 12:08d02c0aaa67 | 105 | pixels[n*3+1] = (c >> 8) & 0xfe; |
heroic | 12:08d02c0aaa67 | 106 | pixels[n*3+2] = c & 0xfe; |
heroic | 12:08d02c0aaa67 | 107 | } |