Takumi Okamoto
/
FullColorLEDControl
デジットで入手したフルカラーLEDの制御プログラムです。
デジットで入手したフルカラーLEDの制御プログラム。
Diff: main.cpp
- Revision:
- 0:dc9726268dc6
- Child:
- 1:00a40243c245
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jul 15 08:16:02 2013 +0000 @@ -0,0 +1,91 @@ +/** + * This program is test for p9813 IC + **/ + + +#include "mbed.h" + +//DigitalOut led(LED1); +BusOut leds(LED1,LED2,LED3,LED4); +AnalogIn analog(p15); + + +#define H 1 +#define L 0 + +DigitalOut di(p10);//signal +DigitalOut ci(p11);//clock + +void sendBit(char bit){ + ci =L; + di = (bit==0 ? L:H ); + ci=H;//read DI at rising edge +} +char toGrayCode(char byte){ + char shift = byte>>1; + return shift^byte; +} + +void sendByte(char byte){ + char mask =0x80; + for( ; mask != 0x00 ; mask>>=1){// 1000 0000 + sendBit(byte & mask); + } +} + +void sendStartFrame(){ + for(int i = 0; i<4 ;i++){ + sendByte(0x00); + } +} + +void sendStopFrame(){ + sendStartFrame(); +} + +void sendRGB(char rb,char gb ,char bb){ + printf("******\n"); + printf("r is %X \n",rb); + printf("g is %X \n",gb ); + printf("b is %X \n",bb ); + printf("******\n\n"); + + char r = toGrayCode(rb); + char g = toGrayCode(gb); + char b = toGrayCode(bb); + + sendStartFrame(); + sendBit(H); + sendBit(H); + // error check + sendBit( (~b) & 0x80 );//mask by 1000 0000 + sendBit( (~b) & 0x40 );//mask by 0100 0000 + sendBit( (~g) & 0x80 );//mask by 1000 0000 + sendBit( (~g) & 0x40 );//mask by 0100 0000 + sendBit( (~r) & 0x80 );//mask by 1000 0000 + sendBit( (~r) & 0x40 );//mask by 0100 0000 + //send RGB data + sendByte(b); + sendByte(g); + sendByte(r); + sendStopFrame(); +} + + + +int main() { + leds =0x0; + while(1) { + + leds=leds+1; + sendRGB(0x00,0x00,0xFF); + wait(2.0*analog.read()); + leds=leds+1; + sendRGB(0xFF,0x00,0x00); + wait(2.0*analog.read()); + leds=leds+1; + sendRGB(0x00,0xFF,0x00); + wait(2.0*analog.read()); + leds=leds+1; + } +}