see https://developer.mbed.org/users/okini3939/notebook/graphic-poi/

Dependencies:   IAP RAM_DISK USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Suga-koubou Graphic Poi Kit
00003  *   LPC11U35 (select EA LPC11U35 QuickStart Board)
00004  *   APA102 (Adafruit DotStar LED Strip)
00005  */
00006 #include "mbed.h"
00007 #include "file.h"
00008 
00009 #define LED_NUM     32
00010 #define LED_GLOBAL  31 // brightness 0-31
00011 #define LED_FREQ    500000 // spi
00012 #define LED_WAIT    1 // *10ms
00013 
00014 #if defined(TARGET_LPC11U24)
00015 Serial pc(USBTX, USBRX);
00016 DigitalIn usb_vbus(p30);
00017 #else
00018 DigitalIn usb_vbus(P0_3);
00019 #endif
00020 DigitalOut led(LED1);
00021 DigitalIn button(P0_1);
00022 SPI spi(P0_21, P0_22, P1_15);
00023 
00024 void dotStar (int *buf, int num) {
00025     int i;
00026 
00027     // start frame
00028     for (i = 0; i < 4; i ++) {
00029         spi.write(0);
00030     }
00031     // led frame
00032     for (i = 0; i < num; i ++) {
00033         spi.write((7<<5) | LED_GLOBAL);
00034         spi.write((buf[i] >> 16) & 0xff); // B
00035         spi.write((buf[i] >> 8) & 0xff); // G
00036         spi.write(buf[i] & 0xff); // R
00037     }
00038     // end frame
00039     for (i = 0; i < 4; i ++) {
00040         spi.write(1);
00041     }
00042 }
00043 
00044 void dotStar_off () {
00045     int i;
00046 
00047     for (i = 0; i < 4; i ++) {
00048         spi.write(0);
00049     }
00050     for (i = 0; i < LED_NUM; i ++) {
00051         spi.write((7<<5) | LED_GLOBAL);
00052         spi.write(0);
00053         spi.write(0);
00054         spi.write(0);
00055     }
00056     for (i = 0; i < 4; i ++) {
00057         spi.write(1);
00058     }
00059 }
00060 
00061 void work () {
00062     int i, a, x, y;
00063     int led_buf[LED_NUM];
00064     int num = 0;
00065     char *buf = getPicture(num);
00066     struct BmpHeader *header = (struct BmpHeader *)buf;
00067 
00068     if (buf == NULL) return;
00069     DBG(" %d: %08x\r\n", num, buf);
00070     y = 0;
00071     a = 0;
00072     for (;;) {
00073         for (x = 0; x < header->width; x ++) {
00074           if (x < LED_NUM) {
00075             led_buf[x] = (header->data[a] << 16) | (header->data[a + 1] << 8) | header->data[a + 2];
00076           }
00077           a += 3;
00078         }
00079         for (; x < LED_NUM; x ++) {
00080             led_buf[x] = 0;
00081         }
00082         a = ((a + 3) / 4) * 4; // padding
00083         DBG("%d %d %d/%d\r\n", a, y, header->width, header->height);
00084         dotStar(led_buf, LED_NUM);
00085         y ++;
00086         if (y >= header->height) {
00087             y = 0;
00088             a = 0;
00089             led = 0;
00090         }
00091 
00092         for (i = 0; i < LED_WAIT; i ++) {
00093             if (button == 0) {
00094                 led = 1;
00095 next:
00096                 num ++;
00097                 if (num >= MAX_SECTOR) {
00098                     num = 0;
00099                 }
00100                 buf = getPicture(num);
00101                 if (buf == NULL) goto next;
00102                 header = (struct BmpHeader *)buf;
00103                 DBG(" %d: %08x\r\n", num, buf);
00104                 while (button == 0);
00105             }
00106             wait_ms(1);
00107         }
00108     }
00109 }
00110 
00111 void demo () {
00112     int i, c;
00113     int color = 7;
00114     int led_buf[LED_NUM];
00115 
00116     for (;;) {
00117         for (i = 0; i < LED_NUM; i ++) {
00118             c = ((i + color) % 7) + 1;
00119             led_buf[i] = (c & 4 ? 0xff0000 : 0) | (c & 2 ? 0xff00 : 0) | (c & 1 ? 0xff : 0);
00120         }
00121         dotStar(led_buf, LED_NUM);
00122         led = !led;
00123 
00124         wait_ms(500);
00125         color --;
00126         if (color <= 0) color = 7;
00127     }
00128 }
00129 
00130 int main() {
00131 
00132     LPC_SYSCON->BODCTRL = 0x12; // BOD Reset 2.4V
00133 
00134     button.mode(PullUp);
00135     usb_vbus.mode(PullDown);
00136 #if defined(TARGET_LPC11U24)
00137     pc.baud(115200);
00138 #endif
00139     DBG("*** MSD\r\n");
00140 
00141     wait_ms(10);
00142     spi.frequency(LED_FREQ);
00143     dotStar_off();
00144 
00145     if (usb_vbus) {
00146         DBG("usb\r\n");
00147         workMsd();
00148     }
00149 
00150     DBG("work\r\n");
00151     work();
00152 
00153     // demo
00154     DBG("demo\r\n");
00155     demo();
00156 }
00157 
00158 extern "C" void HardFault_Handler() {
00159     for (;;) {
00160         led = !led;
00161         for (volatile int w = 0; w < 1000000; w ++);
00162     }
00163 }