kosen-music-game / Mbed OS led-panel

Dependencies:   TLC59116

Fork of led-panel by Suzu Tomo

Committer:
Suzutomo
Date:
Tue Nov 09 08:03:55 2021 +0000
Revision:
6:d813296f9f41
Parent:
5:f7b4ba4aa6af
Delete: comment code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Suzutomo 0:0ba9df5f8571 1 #include "mbed.h"
Suzutomo 1:c3f2c02fa679 2 #include "TLC59116.h"
Suzutomo 0:0ba9df5f8571 3
Suzutomo 2:417ed8ce01b5 4 RawSerial pc(USBTX,USBRX,115200);
Suzutomo 2:417ed8ce01b5 5 RawSerial dev(PA_9,PA_10,115200);
Suzutomo 2:417ed8ce01b5 6 DigitalIn user(PC_13);
Suzutomo 2:417ed8ce01b5 7 I2C i2c(PB_9,PB_8);
Suzutomo 5:f7b4ba4aa6af 8
Suzutomo 2:417ed8ce01b5 9 TLC59116 ledUnit[] = {TLC59116(&i2c,0x60 << 1),
Suzutomo 2:417ed8ce01b5 10 TLC59116(&i2c,0x61 << 1),
Suzutomo 2:417ed8ce01b5 11 TLC59116(&i2c,0x62 << 1),
Suzutomo 2:417ed8ce01b5 12 TLC59116(&i2c,0x63 << 1)
Suzutomo 2:417ed8ce01b5 13 };
Suzutomo 2:417ed8ce01b5 14
Suzutomo 2:417ed8ce01b5 15 void Init();
Suzutomo 2:417ed8ce01b5 16
Suzutomo 2:417ed8ce01b5 17 void I2cDeviceChecker();
Suzutomo 2:417ed8ce01b5 18
Suzutomo 2:417ed8ce01b5 19 void LightPanel(int i,uint32_t color);
Suzutomo 2:417ed8ce01b5 20 void LightPanel(int i,uint8_t color_r,uint8_t color_g,uint8_t color_b);
Suzutomo 2:417ed8ce01b5 21 void Gradation(int i,uint32_t start,uint32_t end,float per);
Suzutomo 2:417ed8ce01b5 22 void Send();
Suzutomo 2:417ed8ce01b5 23
Suzutomo 2:417ed8ce01b5 24 uint32_t HSVtoRGB(float h,float s,float v);
Suzutomo 2:417ed8ce01b5 25
Suzutomo 2:417ed8ce01b5 26
Suzutomo 2:417ed8ce01b5 27 int main()
Suzutomo 2:417ed8ce01b5 28 {
Suzutomo 2:417ed8ce01b5 29 printf("Start Program\r\n");
Suzutomo 2:417ed8ce01b5 30
Suzutomo 2:417ed8ce01b5 31 float count = 0;
Suzutomo 5:f7b4ba4aa6af 32 //int color = 0;
Suzutomo 2:417ed8ce01b5 33
Suzutomo 2:417ed8ce01b5 34 Init();
Suzutomo 2:417ed8ce01b5 35
Suzutomo 2:417ed8ce01b5 36 while(1) {
Suzutomo 2:417ed8ce01b5 37 count += 0.01;
Suzutomo 2:417ed8ce01b5 38 for (int i = 0; i < 16; i++) {
Suzutomo 2:417ed8ce01b5 39 LightPanel(i,HSVtoRGB(fmod((i / 16.0) + count,1),1,1));
Suzutomo 2:417ed8ce01b5 40 }
Suzutomo 2:417ed8ce01b5 41
Suzutomo 2:417ed8ce01b5 42 Send();
Suzutomo 2:417ed8ce01b5 43
Suzutomo 2:417ed8ce01b5 44 wait_us(1*1e6 / 60);
Suzutomo 2:417ed8ce01b5 45 }
Suzutomo 2:417ed8ce01b5 46 }
Suzutomo 2:417ed8ce01b5 47
Suzutomo 2:417ed8ce01b5 48 void Init()
Suzutomo 2:417ed8ce01b5 49 {
Suzutomo 2:417ed8ce01b5 50 for (int i = 0; i <= 16; i++) {
Suzutomo 2:417ed8ce01b5 51 LightPanel(i - 1,0x0);
Suzutomo 2:417ed8ce01b5 52 LightPanel(i,0xFFFFFF);
Suzutomo 2:417ed8ce01b5 53 Send();
Suzutomo 2:417ed8ce01b5 54 wait_us(100 * 1e3);
Suzutomo 2:417ed8ce01b5 55 }
Suzutomo 2:417ed8ce01b5 56 }
Suzutomo 2:417ed8ce01b5 57
Suzutomo 2:417ed8ce01b5 58 void Gradation(int i,uint32_t start,uint32_t end,float per)
Suzutomo 2:417ed8ce01b5 59 {
Suzutomo 2:417ed8ce01b5 60 uint8_t s_r = (start & 0xFF0000) >> 16;
Suzutomo 2:417ed8ce01b5 61 uint8_t s_g = (start & 0xFF00) >> 8;
Suzutomo 2:417ed8ce01b5 62 uint8_t s_b = (start & 0xFF);
Suzutomo 2:417ed8ce01b5 63
Suzutomo 2:417ed8ce01b5 64 uint8_t e_r = (end & 0xFF0000) >> 16;
Suzutomo 2:417ed8ce01b5 65 uint8_t e_g = (end & 0xFF00) >> 8;
Suzutomo 2:417ed8ce01b5 66 uint8_t e_b = (end & 0xFF);
Suzutomo 2:417ed8ce01b5 67
Suzutomo 2:417ed8ce01b5 68 uint8_t c_r = (e_r - s_r) * per + s_r;
Suzutomo 2:417ed8ce01b5 69 uint8_t c_g = (e_g - s_g) * per + s_g;
Suzutomo 2:417ed8ce01b5 70 uint8_t c_b = (e_b - s_b) * per + s_b;
Suzutomo 2:417ed8ce01b5 71 ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3,c_g);
Suzutomo 2:417ed8ce01b5 72 ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 1,c_r);
Suzutomo 2:417ed8ce01b5 73 ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 2,c_b);
Suzutomo 2:417ed8ce01b5 74
Suzutomo 2:417ed8ce01b5 75 }
Suzutomo 2:417ed8ce01b5 76
Suzutomo 2:417ed8ce01b5 77
Suzutomo 2:417ed8ce01b5 78
Suzutomo 2:417ed8ce01b5 79 void LightPanel(int i,uint32_t color)
Suzutomo 2:417ed8ce01b5 80 {
Suzutomo 2:417ed8ce01b5 81 if (i >= 0 && i < 16) {
Suzutomo 2:417ed8ce01b5 82 uint8_t c_r = (color & 0xFF0000) >> 16;
Suzutomo 2:417ed8ce01b5 83 uint8_t c_g = (color & 0xFF00) >> 8;
Suzutomo 2:417ed8ce01b5 84 uint8_t c_b = (color & 0xFF);
Suzutomo 2:417ed8ce01b5 85 ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3,c_g);
Suzutomo 2:417ed8ce01b5 86 ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 1,c_r);
Suzutomo 2:417ed8ce01b5 87 ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 2,c_b);
Suzutomo 2:417ed8ce01b5 88 }
Suzutomo 2:417ed8ce01b5 89 }
Suzutomo 2:417ed8ce01b5 90
Suzutomo 2:417ed8ce01b5 91 void LightPanel(int i,uint8_t c_r,uint8_t c_g,uint8_t c_b)
Suzutomo 2:417ed8ce01b5 92 {
Suzutomo 2:417ed8ce01b5 93 ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3,c_g);
Suzutomo 2:417ed8ce01b5 94 ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 1,c_r);
Suzutomo 2:417ed8ce01b5 95 ledUnit[(int)(i / 4)].SetChannel((i % 4) * 3 + 2,c_b);
Suzutomo 2:417ed8ce01b5 96 }
Suzutomo 2:417ed8ce01b5 97
Suzutomo 2:417ed8ce01b5 98 void Send()
Suzutomo 2:417ed8ce01b5 99 {
Suzutomo 2:417ed8ce01b5 100 for (int i = 0; i < 4; i++) ledUnit[i].Send(2,12);
Suzutomo 2:417ed8ce01b5 101 }
Suzutomo 2:417ed8ce01b5 102
Suzutomo 2:417ed8ce01b5 103 void I2cDeviceChecker()
Suzutomo 2:417ed8ce01b5 104 {
Suzutomo 2:417ed8ce01b5 105 char data[1] = {0x00};
Suzutomo 2:417ed8ce01b5 106 for (int i = 0; i < 127; i++) {
Suzutomo 2:417ed8ce01b5 107 printf("%2x : %d\r\n",i,i2c.write(i<<1,data,1));
Suzutomo 2:417ed8ce01b5 108 }
Suzutomo 2:417ed8ce01b5 109 }
Suzutomo 2:417ed8ce01b5 110
Suzutomo 2:417ed8ce01b5 111 uint32_t HSVtoRGB(float h,float s,float v)
Suzutomo 2:417ed8ce01b5 112 {
Suzutomo 2:417ed8ce01b5 113 float r = v;
Suzutomo 2:417ed8ce01b5 114 float g = v;
Suzutomo 2:417ed8ce01b5 115 float b = v;
Suzutomo 2:417ed8ce01b5 116 if (s > 0.0f) {
Suzutomo 2:417ed8ce01b5 117 h *= 6.0f;
Suzutomo 2:417ed8ce01b5 118 int i = (int) h;
Suzutomo 2:417ed8ce01b5 119 float f = h - (float) i;
Suzutomo 2:417ed8ce01b5 120 switch (i) {
Suzutomo 2:417ed8ce01b5 121 default:
Suzutomo 2:417ed8ce01b5 122 case 0:
Suzutomo 2:417ed8ce01b5 123 g *= 1 - s * (1 - f);
Suzutomo 2:417ed8ce01b5 124 b *= 1 - s;
Suzutomo 2:417ed8ce01b5 125 break;
Suzutomo 2:417ed8ce01b5 126 case 1:
Suzutomo 2:417ed8ce01b5 127 r *= 1 - s * f;
Suzutomo 2:417ed8ce01b5 128 b *= 1 - s;
Suzutomo 2:417ed8ce01b5 129 break;
Suzutomo 2:417ed8ce01b5 130 case 2:
Suzutomo 2:417ed8ce01b5 131 r *= 1 - s;
Suzutomo 2:417ed8ce01b5 132 b *= 1 - s * (1 - f);
Suzutomo 2:417ed8ce01b5 133 break;
Suzutomo 2:417ed8ce01b5 134 case 3:
Suzutomo 2:417ed8ce01b5 135 r *= 1 - s;
Suzutomo 2:417ed8ce01b5 136 g *= 1 - s * f;
Suzutomo 2:417ed8ce01b5 137 break;
Suzutomo 2:417ed8ce01b5 138 case 4:
Suzutomo 2:417ed8ce01b5 139 r *= 1 - s * (1 - f);
Suzutomo 2:417ed8ce01b5 140 g *= 1 - s;
Suzutomo 2:417ed8ce01b5 141 break;
Suzutomo 2:417ed8ce01b5 142 case 5:
Suzutomo 2:417ed8ce01b5 143 g *= 1 - s;
Suzutomo 2:417ed8ce01b5 144 b *= 1 - s * f;
Suzutomo 2:417ed8ce01b5 145 break;
Suzutomo 2:417ed8ce01b5 146 }
Suzutomo 2:417ed8ce01b5 147 }
Suzutomo 2:417ed8ce01b5 148 r *= 255;
Suzutomo 2:417ed8ce01b5 149 g *= 255;
Suzutomo 2:417ed8ce01b5 150 b *= 255;
Suzutomo 2:417ed8ce01b5 151 return ((int)r << 16) | ((int)g << 8) | (int)b;
Suzutomo 2:417ed8ce01b5 152
Suzutomo 6:d813296f9f41 153 }