2021年度のキャンパスプロジェクトで使用する予定のプログラムです。

Dependencies:   TLC59116

Fork of led-panel by Suzu Tomo

Revision:
2:417ed8ce01b5
Parent:
1:c3f2c02fa679
Child:
5:f7b4ba4aa6af
--- a/main.cpp	Sat Oct 26 12:37:13 2019 +0000
+++ b/main.cpp	Tue Nov 09 07:29:21 2021 +0000
@@ -1,6 +1,190 @@
 #include "mbed.h"
 #include "TLC59116.h"
 
+#define PI 3.1415926535897934238
+
+RawSerial pc(USBTX,USBRX,115200);
+RawSerial dev(PA_9,PA_10,115200);
+DigitalIn user(PC_13);
+I2C i2c(PB_9,PB_8);
+I2C arI2c(PB_4,PA_8);
+TLC59116 ledUnit[] = {TLC59116(&i2c,0x60 << 1),
+                      TLC59116(&i2c,0x61 << 1),
+                      TLC59116(&i2c,0x62 << 1),
+                      TLC59116(&i2c,0x63 << 1)
+                     };
+
+char data = 0;
+
+void re()
+{
+    while (1) {
+        arI2c.read(0x10<<1,&data,1);
+        //printf("%x\r\n",data);
+        wait_us(0.05 * 1e3);
+    }
+}
+
+void Init();
+
+void I2cDeviceChecker();
+
+void LightPanel(int i,uint32_t color);
+void LightPanel(int i,uint8_t color_r,uint8_t color_g,uint8_t color_b);
+void Gradation(int i,uint32_t start,uint32_t end,float per);
+void Send();
+
+uint32_t HSVtoRGB(float h,float s,float v);
+
+
+int main()
+{
+    printf("Start Program\r\n");
+
+    Thread i2cRead;
+    i2cRead.start(re);
+
+    float count = 0;
+    int color = 0;
+
+    Init();
+
+    while(1) {
+        count += 0.01;
+        for (int i = 0; i < 16; i++) {
+            LightPanel(i,HSVtoRGB(fmod((i / 16.0) + count,1),1,1));
+        }
+        //printf("%f",fmod((0 / 16.0) + count,1));
+        //float tri =abs(fmod(count / 30.0, 2.0) - 1);
+        for (int i = 0; i < 4; i++) {
+            if ((data >> i) & 0x01)
+                for(int j = 0; j < 4; j++) LightPanel(i + 4*j,0xFFFFFF);
+            //printf("%d ",(data >> i)&0x01);
+        }
+
+        //printf("%f\r\n",sin(count));
+        //printf("%f\r\n",tri);
+        Send();
+
+        wait_us(1*1e6 / 60);
+
+        //printf("\r\n");
+    }
+}
+
+void Init()
+{
+    for (int i = 0; i <= 16; i++) {
+        LightPanel(i - 1,0x0);
+        LightPanel(i,0xFFFFFF);
+        Send();
+        wait_us(100 * 1e3);
+    }
+}
+
+void Gradation(int i,uint32_t start,uint32_t end,float per)
+{
+    uint8_t s_r = (start & 0xFF0000) >> 16;
+    uint8_t s_g = (start & 0xFF00) >> 8;
+    uint8_t s_b = (start & 0xFF);
+
+    uint8_t e_r = (end & 0xFF0000) >> 16;
+    uint8_t e_g = (end & 0xFF00) >> 8;
+    uint8_t e_b = (end & 0xFF);
+
+    uint8_t c_r = (e_r - s_r) * per + s_r;
+    uint8_t c_g = (e_g - s_g) * per + s_g;
+    uint8_t c_b = (e_b - s_b) * per + s_b;
+    ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3,c_g);
+    ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 1,c_r);
+    ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 2,c_b);
+
+}
+
+
+
+void LightPanel(int i,uint32_t color)
+{
+    if (i >= 0 && i < 16) {
+        uint8_t c_r = (color & 0xFF0000) >> 16;
+        uint8_t c_g = (color & 0xFF00) >> 8;
+        uint8_t c_b = (color & 0xFF);
+        ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3,c_g);
+        ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 1,c_r);
+        ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 2,c_b);
+    }
+}
+
+void LightPanel(int i,uint8_t c_r,uint8_t c_g,uint8_t c_b)
+{
+    ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3,c_g);
+    ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 1,c_r);
+    ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 2,c_b);
+}
+
+void Send()
+{
+    for (int i = 0; i < 4; i++) ledUnit[i].Send(2,12);
+}
+
+void I2cDeviceChecker()
+{
+    char data[1] = {0x00};
+    for (int i = 0; i < 127; i++) {
+        printf("%2x : %d\r\n",i,i2c.write(i<<1,data,1));
+    }
+}
+
+uint32_t HSVtoRGB(float h,float s,float v)
+{
+    float r = v;
+    float g = v;
+    float b = v;
+    if (s > 0.0f) {
+        h *= 6.0f;
+        int i = (int) h;
+        float f = h - (float) i;
+        switch (i) {
+            default:
+            case 0:
+                g *= 1 - s * (1 - f);
+                b *= 1 - s;
+                break;
+            case 1:
+                r *= 1 - s * f;
+                b *= 1 - s;
+                break;
+            case 2:
+                r *= 1 - s;
+                b *= 1 - s * (1 - f);
+                break;
+            case 3:
+                r *= 1 - s;
+                g *= 1 - s * f;
+                break;
+            case 4:
+                r *= 1 - s * (1 - f);
+                g *= 1 - s;
+                break;
+            case 5:
+                g *= 1 - s;
+                b *= 1 - s * f;
+                break;
+        }
+    }
+    r *= 255;
+    g *= 255;
+    b *= 255;
+    return ((int)r << 16) | ((int)g << 8) | (int)b;
+
+}
+
+
+/*
+SPI spi(PB_15,PB_14,PB_13);
+
+DigitalOut cs(PC_0);
+
 DigitalIn sv2[8] = {
     PC_4,
     PA_10,
@@ -17,78 +201,137 @@
 //PwmOut intb(PA_12);
 PwmOut sdb(PA_11);
 Thread te;
-I2C i2c(PB_9,PB_8);
+
+volatile bool touch[8] = {0};
+
+
+const int generallyWait = 200;
+
+void GetData(char com,char *data,int length)
+{
+    cs = 0;
+    char enable = spi.write(com);
+    wait_us(generallyWait);
+    if (enable == 0x55) {
+        for (int i = 0; i < length; i++) {
+            data[i] = spi.write(0x00);
+            wait_us(generallyWait);
+        }
+    } else printf("error\r\n");
+    cs = 1;
+}
+
+void SetData(char com,char *data,int length)
+{
+    cs = 0;
+    char enable = spi.write(com);
+    wait_us(generallyWait);
+    if (enable == 0x55) {
+        for (int i = 0; i < length; i++) {
+            spi.write(data[i]);
+            wait_us(generallyWait);
+        }
+    } else printf("error\r\n");
+    cs = 1;
+    wait_us(150*1e3);
+}
+
+int Reset()
+{
+    cs = 0;
+    char enable = spi.write(0x04);
+    cs = 1;
+    wait_us(180 * 1e3);
+    return enable == 0x55;
+}
+
+int EraseEeprom()
+{
+    cs = 0;
+    char enable = spi.write(0x0C);
+    cs = 1;
+    wait_us(60 * 1e3);
+    return enable == 0x55;
+}
+
+int Calibrate()
+{
+    cs = 0;
+    char enable = spi.write(0x03);
+    cs = 1;
+    wait_us(180 * 1e3);
+    return enable == 0x55;
+}
+
 
 void teTh()
 {
-    //oe = 0;
-    clk = 0;
+    int place[8] = {0,1,8,9,6,7,4,5};
+    char *data;
+    data = new char[2];
     while(1) {
-        clk = 1;
-        wait_ms(2);
+        GetData(0xC1,data,2);
+        int d = (data[0] << 8)| data[1];
         for (int i = 0; i < 8; i++) {
-            printf("%d",sv2[i].read());
+            touch[i] = (d >> place[i]) & 0x1;
         }
-        clk = 0;
+
+        for (int i = 7; i >= 0; i--) {
+        printf("%x",touch[i]);
+        }
         printf("\r\n");
-        wait_ms(10);
+
+        //wait_us(100);
+
     }
-}
-
-int Write(char regAddr,char data)
-{
-    char data_[2] = {regAddr,data};    
-    return i2c.write(0x60<<1,data_,2); 
+    delete data;
 }
 
-int main()
-{
-    printf("Start Program\r\n");
-    //te.start(teTh);
-    char data[1] = {0x00};
+
+void main() {
+        cs = 1;
+
+    spi.format(8,3);
+    spi.frequency(1500000);
+
+    char* data;
+    printf("Reset : %s\r\n",Reset() ? "OK" : "FALSE");
+    printf("Erase EEPROM : %s\r\n",EraseEeprom() ? "OK" : "FALSE");
+
+
+
 
-    //sdb.mode(PullUp);
-    sdb = 0.3;
-    //intb = ;
+
+
+    data = new char[1];
+    data[0] = 0b11110010;
 
-    for (int i = 0; i < 127; i++) {
-        /*i2c.start();
-        printf("%x : %d\r\n",i,i2c.write(i << 1));
-        i2c.stop();*/
-        printf("%x : %d\r\n",i,i2c.write(i<<1,data,1));
+    SetData(0x90,data,1);
+    data[0] = 0b0;
+    SetData(0x97,data,1);
+    SetData(0x98,data,1);
+
+    data[0] = 0b00101000;
+    for (int i = 0; i < 11; i++) {
+        SetData(0xA3 + i,data,1);
     }
 
-    /*//printf("shutdown  : %d\r\n",Write(0x00,0x00));
-    printf("shutdown  : %d\r\n",Write(0x00,0b00000000));
-    //printf("shutdown  : %d\r\n",Write(0x02,0x01));
-    //printf("shutdown  : %d\r\n",Write(0x09,0b11111111));
+
+    printf("Calibrate : %s\r\n",Calibrate() ? "OK" : "FALSE");
 
-    printf("shutdown  : %d\r\n",Write(0x10,0x80));
-    printf("shutdown  : %d\r\n",Write(0xB0,0x00));
-    printf("shutdown  : %d\r\n",Write(0x11,0x00));
-    printf("shutdown  : %d\r\n",Write(0xB0,0x00));
-    printf("shutdown  : %d\r\n",Write(0x12,0x00));
-    printf("shutdown  : %d\r\n",Write(0xB0,0x00));
+    data = new char[42];
+    GetData(0xC8,data,42);
 
-    //printf("shutdown  : %d\r\n",Write(0x01,0x80));
-    //printf("shutdown  : %d\r\n",Write(0x4A,0x1));
-    */
-    printf("Mode1 Set  : %d\r\n",Write(0x00,0b00000000)); 
-    //printf("Mode2 Set  : %d\r\n",Write(0x01,0x0b00100000)); 
+    //spi.write(0xC9);
+//wait_us(150);
+//printf("%x\r\n",spi.write(0x00));
+//cs = 1;
+//wait_us(50 * 1e3);
 
-    printf("LED State  : %d\r\n",Write(0x14,0b11111111)); 
-    printf("LED State  : %d\r\n",Write(0x15,0b11111111)); 
-    printf("LED State  : %d\r\n",Write(0x16,0b11111111)); 
-    printf("LED State  : %d\r\n",Write(0x17,0b11111111)); 
 
-    while(1) {
-        printf("LED0  : %d\r\n",Write(0x02,0x00));
-        printf("LED1  : %d\r\n",Write(0x03,0x00));
-        wait(0.5);
-        printf("LED0  : %d\r\n",Write(0x02,0b11111111));
-        printf("LED1  : %d\r\n",Write(0x03,0b11111111));
-        wait(0.5);
-        //printf("LED1  : %d\r\n",Write(0x13,0x0b0));
-        
-    }
+    //while (user.read());
+    //te.start(teTh);
+
+
 }
+*/
\ No newline at end of file