Suzu Tomo / Mbed OS 2020_ChristmasLED

Dependencies:   WS2812B_SPI

Committer:
Suzutomo
Date:
Sat Nov 07 13:40:03 2020 +0000
Revision:
0:943dfa56009a
first?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Suzutomo 0:943dfa56009a 1 #include "WS2812B.h"
Suzutomo 0:943dfa56009a 2
Suzutomo 0:943dfa56009a 3 #define BUF 60
Suzutomo 0:943dfa56009a 4 #define BUF2 42
Suzutomo 0:943dfa56009a 5
Suzutomo 0:943dfa56009a 6 double beatTime = 60/60;
Suzutomo 0:943dfa56009a 7 bool LR = false;
Suzutomo 0:943dfa56009a 8
Suzutomo 0:943dfa56009a 9
Suzutomo 0:943dfa56009a 10 int buf[3] = {BUF,BUF,BUF2};
Suzutomo 0:943dfa56009a 11 WS2812B tape[2] = {
Suzutomo 0:943dfa56009a 12 WS2812B(PB_0,BUF),
Suzutomo 0:943dfa56009a 13 WS2812B(PC_1,BUF)/*,
Suzutomo 0:943dfa56009a 14 WS2812B(PC_0,BUF2)*/
Suzutomo 0:943dfa56009a 15 };
Suzutomo 0:943dfa56009a 16
Suzutomo 0:943dfa56009a 17 Timer ledT;
Suzutomo 0:943dfa56009a 18
Suzutomo 0:943dfa56009a 19 volatile double calcBar = 0;
Suzutomo 0:943dfa56009a 20
Suzutomo 0:943dfa56009a 21 void TempoWait(double beat,bool useF8 = 0)
Suzutomo 0:943dfa56009a 22 {
Suzutomo 0:943dfa56009a 23 calcBar += beat;
Suzutomo 0:943dfa56009a 24 if (useF8) while (barClock < calcBar * 24);
Suzutomo 0:943dfa56009a 25 else while (ledT.read() < calcBar * beatTime);
Suzutomo 0:943dfa56009a 26 }
Suzutomo 0:943dfa56009a 27
Suzutomo 0:943dfa56009a 28 void FlowLED(double beat,int type,uint32_t color,bool useF8 = 0)
Suzutomo 0:943dfa56009a 29 {
Suzutomo 0:943dfa56009a 30 for (int t = 0; t < 2; t++) tape[t].Clear(buf[t]);
Suzutomo 0:943dfa56009a 31 switch (type) {
Suzutomo 0:943dfa56009a 32 case 0 : // 端から端
Suzutomo 0:943dfa56009a 33 for (int i = 0; i<=BUF; i++) {
Suzutomo 0:943dfa56009a 34 for (int t = 0; t < 2; t++) tape[t].Write(i-1,0x0);
Suzutomo 0:943dfa56009a 35 for (int j = 0; j < 3; j++) for (int t = 0; t < 2; t++) tape[t].Write(i + j,color);
Suzutomo 0:943dfa56009a 36
Suzutomo 0:943dfa56009a 37 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 / BUF);
Suzutomo 0:943dfa56009a 38 else while (ledT.read() < calcBar * beatTime + i * beat * beatTime / BUF);
Suzutomo 0:943dfa56009a 39 }
Suzutomo 0:943dfa56009a 40 break;
Suzutomo 0:943dfa56009a 41
Suzutomo 0:943dfa56009a 42 case 1 : // 端から端
Suzutomo 0:943dfa56009a 43 for (int i = 0; i<=BUF; i++) {
Suzutomo 0:943dfa56009a 44 for (int t = 0; t < 2; t++) tape[t].Write(BUF - i + 1,0x0);
Suzutomo 0:943dfa56009a 45 for (int j = 0; j > -3; j--) for (int t = 0; t < 2; t++) tape[t].Write(BUF - i + j,color);
Suzutomo 0:943dfa56009a 46
Suzutomo 0:943dfa56009a 47 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 / BUF);
Suzutomo 0:943dfa56009a 48 else while (ledT.read() < calcBar * beatTime + i * beat * beatTime / BUF);
Suzutomo 0:943dfa56009a 49 }
Suzutomo 0:943dfa56009a 50 break;
Suzutomo 0:943dfa56009a 51
Suzutomo 0:943dfa56009a 52 case 2 : // 外から中央に
Suzutomo 0:943dfa56009a 53 for (int i = 0; i<=(BUF / 2); i++) {
Suzutomo 0:943dfa56009a 54 for (int t = 0; t < 2; t++) tape[t].Write(i - 1,0x0);
Suzutomo 0:943dfa56009a 55 for (int t = 0; t < 2; t++) tape[t].Write(BUF - i + 1,0x0);
Suzutomo 0:943dfa56009a 56 for (int j = 0; j < 3; j++) {
Suzutomo 0:943dfa56009a 57 for (int t = 0; t < 2; t++) tape[t].Write(i + j,color);
Suzutomo 0:943dfa56009a 58 for (int t = 0; t < 2; t++) tape[t].Write(BUF - i - j,color);
Suzutomo 0:943dfa56009a 59 }
Suzutomo 0:943dfa56009a 60
Suzutomo 0:943dfa56009a 61 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 * 2 / BUF);
Suzutomo 0:943dfa56009a 62 else while (ledT.read() < calcBar * beatTime + i * beat * beatTime * 2 / BUF);
Suzutomo 0:943dfa56009a 63 }
Suzutomo 0:943dfa56009a 64 break;
Suzutomo 0:943dfa56009a 65
Suzutomo 0:943dfa56009a 66 case 3 : // 中央から外に
Suzutomo 0:943dfa56009a 67 for (int i = 0; i<=(BUF / 2); i++) {
Suzutomo 0:943dfa56009a 68 for (int t = 0; t < 2; t++) tape[t].Write(BUF / 2 - i + 1,0x0);
Suzutomo 0:943dfa56009a 69 for (int t = 0; t < 2; t++) tape[t].Write(BUF / 2 + i - 1,0x0);
Suzutomo 0:943dfa56009a 70 for (int j = 0; j < 3; j++) {
Suzutomo 0:943dfa56009a 71 for (int t = 0; t < 2; t++) tape[t].Write(BUF / 2 - i - j,color);
Suzutomo 0:943dfa56009a 72 for (int t = 0; t < 2; t++) tape[t].Write(BUF / 2 + i + j,color);
Suzutomo 0:943dfa56009a 73 }
Suzutomo 0:943dfa56009a 74
Suzutomo 0:943dfa56009a 75 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 * 2 / BUF);
Suzutomo 0:943dfa56009a 76 else while (ledT.read() < calcBar * beatTime + i * beat * beatTime * 2 / BUF);
Suzutomo 0:943dfa56009a 77 }
Suzutomo 0:943dfa56009a 78 break;
Suzutomo 0:943dfa56009a 79
Suzutomo 0:943dfa56009a 80 case 4 : // まとまって流れる
Suzutomo 0:943dfa56009a 81 for (int i = 0; i<=BUF + 40; i++) {
Suzutomo 0:943dfa56009a 82 //printf("%d\r\n",i);
Suzutomo 0:943dfa56009a 83 for (int j = 0; j<20; j++) {
Suzutomo 0:943dfa56009a 84 for (int t = 0; t < 2; t++) tape[t].Write(i - 20 + j,tape[t].BrightAdjust(color,(double)j * 1/20/*sin(j * (PI / 10))*/));
Suzutomo 0:943dfa56009a 85 }
Suzutomo 0:943dfa56009a 86 for (int j = 0; j < 20; j++) for (int t = 0; t < 2; t++) tape[t].Write(i - 20 + j,0);
Suzutomo 0:943dfa56009a 87 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 / (BUF + 40));
Suzutomo 0:943dfa56009a 88 else while (barClock < calcBar * 24 + i * beat * 24 / (BUF + 40));
Suzutomo 0:943dfa56009a 89
Suzutomo 0:943dfa56009a 90 }
Suzutomo 0:943dfa56009a 91 break;
Suzutomo 0:943dfa56009a 92 }
Suzutomo 0:943dfa56009a 93 for (int t = 0; t < 2; t++) tape[t].Clear(BUF);
Suzutomo 0:943dfa56009a 94 calcBar += beat;
Suzutomo 0:943dfa56009a 95
Suzutomo 0:943dfa56009a 96 }
Suzutomo 0:943dfa56009a 97
Suzutomo 0:943dfa56009a 98 void FlashLED(double beat,int type,uint32_t color,bool useF8 = 0)
Suzutomo 0:943dfa56009a 99 {
Suzutomo 0:943dfa56009a 100 calcBar += beat;
Suzutomo 0:943dfa56009a 101 for (int t = 0; t < 2; t++) tape[t].Clear(BUF);
Suzutomo 0:943dfa56009a 102 switch (type) {
Suzutomo 0:943dfa56009a 103 case 0 :
Suzutomo 0:943dfa56009a 104 int rw = (rand() % 5) + 2;
Suzutomo 0:943dfa56009a 105 int rb = (rand() % 5) + 2;
Suzutomo 0:943dfa56009a 106 for (int i = 0; i<=BUF;) {
Suzutomo 0:943dfa56009a 107 for (int j = 0; j < rw; j++) {
Suzutomo 0:943dfa56009a 108 if (rw % 2) for (int t = 0; t < 2; t++) tape[t].Write(i + j,color);
Suzutomo 0:943dfa56009a 109 else for (int t = 0; t < 2; t++) tape[t].Write(i + j,0x0);
Suzutomo 0:943dfa56009a 110 }
Suzutomo 0:943dfa56009a 111 for (int j = 0; j < rb; j++) {
Suzutomo 0:943dfa56009a 112 if (rw % 2)for (int t = 0; t < 2; t++) tape[t].Write(i + rw + j,0x0);
Suzutomo 0:943dfa56009a 113 else for (int t = 0; t < 2; t++) tape[t].Write(i + rw + j,color);
Suzutomo 0:943dfa56009a 114 }
Suzutomo 0:943dfa56009a 115 i += rw + rb;
Suzutomo 0:943dfa56009a 116 }
Suzutomo 0:943dfa56009a 117 break;
Suzutomo 0:943dfa56009a 118 case 1 :
Suzutomo 0:943dfa56009a 119 int r = (rand() % (BUF / 2 - BUF / 4));
Suzutomo 0:943dfa56009a 120 for (int i = 0; i<=BUF; i++) {
Suzutomo 0:943dfa56009a 121 if (LR) {
Suzutomo 0:943dfa56009a 122 if ((i - r > 0) && (i - r < (BUF / 4)))for (int t = 0; t < 2; t++) tape[t].Write(i,color);
Suzutomo 0:943dfa56009a 123 else for (int t = 0; t < 2; t++) tape[t].Write(i,0x0);
Suzutomo 0:943dfa56009a 124 } else {
Suzutomo 0:943dfa56009a 125 if ((i - (r + BUF / 2) > 0) && (i - (r + BUF / 2) < (BUF / 4))) for (int t = 0; t < 2; t++) tape[t].Write(i,color);
Suzutomo 0:943dfa56009a 126 else for (int t = 0; t < 2; t++) tape[t].Write(i,0x0);
Suzutomo 0:943dfa56009a 127 }
Suzutomo 0:943dfa56009a 128 }
Suzutomo 0:943dfa56009a 129 LR = !LR;
Suzutomo 0:943dfa56009a 130 break;
Suzutomo 0:943dfa56009a 131 }
Suzutomo 0:943dfa56009a 132
Suzutomo 0:943dfa56009a 133 if (useF8) while (barClock < calcBar * 24);
Suzutomo 0:943dfa56009a 134 else while (ledT.read() < calcBar * beatTime);
Suzutomo 0:943dfa56009a 135 //ws.Clear(BUF);
Suzutomo 0:943dfa56009a 136
Suzutomo 0:943dfa56009a 137 }
Suzutomo 0:943dfa56009a 138
Suzutomo 0:943dfa56009a 139 void LightLED(double beat,uint32_t color,bool useF8 = 0)
Suzutomo 0:943dfa56009a 140 {
Suzutomo 0:943dfa56009a 141 calcBar += beat;
Suzutomo 0:943dfa56009a 142 for (int t = 0; t < 2; t++) tape[t].Write(color);
Suzutomo 0:943dfa56009a 143 if (useF8) while (barClock < calcBar * 24);
Suzutomo 0:943dfa56009a 144 else while (ledT.read() < calcBar * beatTime);
Suzutomo 0:943dfa56009a 145 }
Suzutomo 0:943dfa56009a 146
Suzutomo 0:943dfa56009a 147 void SendLED(double beat,bool useF8 = 0)
Suzutomo 0:943dfa56009a 148 {
Suzutomo 0:943dfa56009a 149 calcBar += beat;
Suzutomo 0:943dfa56009a 150 if (useF8) while (calcBar < calcBar * 24);
Suzutomo 0:943dfa56009a 151 else while (ledT.read() < calcBar * beatTime);
Suzutomo 0:943dfa56009a 152 }
Suzutomo 0:943dfa56009a 153
Suzutomo 0:943dfa56009a 154 void FadeLED(double beat,int mode,uint32_t color,bool useF8 = 0)
Suzutomo 0:943dfa56009a 155 {
Suzutomo 0:943dfa56009a 156 for (int i = 1; i <= 48; i++) {
Suzutomo 0:943dfa56009a 157 if (mode == 0) for (int t = 0; t < 2; t++) tape[t].Write(color,i / 48.0);
Suzutomo 0:943dfa56009a 158 if (mode == 1) for (int t = 0; t < 2; t++) tape[t].Write(color,(1.0 - i / 48.0));
Suzutomo 0:943dfa56009a 159 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 / 48);
Suzutomo 0:943dfa56009a 160 else while (ledT.read() < calcBar * beatTime + i * beat * beatTime / 48);
Suzutomo 0:943dfa56009a 161 }
Suzutomo 0:943dfa56009a 162 calcBar += beat;
Suzutomo 0:943dfa56009a 163 }
Suzutomo 0:943dfa56009a 164
Suzutomo 0:943dfa56009a 165 void SendingLED()
Suzutomo 0:943dfa56009a 166 {
Suzutomo 0:943dfa56009a 167 while (1) {
Suzutomo 0:943dfa56009a 168 for (int t = 0; t < 2; t++) tape[t].Send();
Suzutomo 0:943dfa56009a 169 wait_us((1000 / 60)*1e3);
Suzutomo 0:943dfa56009a 170 }
Suzutomo 0:943dfa56009a 171 }
Suzutomo 0:943dfa56009a 172
Suzutomo 0:943dfa56009a 173
Suzutomo 0:943dfa56009a 174 void FlowLED2(double beat,int type,uint32_t color,int num,bool useF8 = 0)
Suzutomo 0:943dfa56009a 175 {
Suzutomo 0:943dfa56009a 176 tape[num].Clear(buf[num]);
Suzutomo 0:943dfa56009a 177 switch (type) {
Suzutomo 0:943dfa56009a 178 case 0 : // 端から端
Suzutomo 0:943dfa56009a 179 for (int i = 0; i<=BUF; i++) {
Suzutomo 0:943dfa56009a 180 tape[num].Write(i-1,0x0);
Suzutomo 0:943dfa56009a 181 for (int j = 0; j < 3; j++) tape[num].Write(i + j,color);
Suzutomo 0:943dfa56009a 182
Suzutomo 0:943dfa56009a 183 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 / BUF);
Suzutomo 0:943dfa56009a 184 else while (ledT.read() < calcBar * beatTime + i * beat * beatTime / BUF);
Suzutomo 0:943dfa56009a 185 }
Suzutomo 0:943dfa56009a 186 break;
Suzutomo 0:943dfa56009a 187
Suzutomo 0:943dfa56009a 188 case 1 : // 端から端
Suzutomo 0:943dfa56009a 189 for (int i = 0; i<=BUF; i++) {
Suzutomo 0:943dfa56009a 190 tape[num].Write(BUF - i + 1,0x0);
Suzutomo 0:943dfa56009a 191 for (int j = 0; j > -3; j--) tape[num].Write(BUF - i + j,color);
Suzutomo 0:943dfa56009a 192
Suzutomo 0:943dfa56009a 193 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 / BUF);
Suzutomo 0:943dfa56009a 194 else while (ledT.read() < calcBar * beatTime + i * beat * beatTime / BUF);
Suzutomo 0:943dfa56009a 195 }
Suzutomo 0:943dfa56009a 196 break;
Suzutomo 0:943dfa56009a 197
Suzutomo 0:943dfa56009a 198 case 2 : // 外から中央に
Suzutomo 0:943dfa56009a 199 for (int i = 0; i<=(BUF / 2); i++) {
Suzutomo 0:943dfa56009a 200 tape[num].Write(i - 1,0x0);
Suzutomo 0:943dfa56009a 201 tape[num].Write(BUF - i + 1,0x0);
Suzutomo 0:943dfa56009a 202 for (int j = 0; j < 3; j++) {
Suzutomo 0:943dfa56009a 203 tape[num].Write(i + j,color);
Suzutomo 0:943dfa56009a 204 tape[num].Write(BUF - i - j,color);
Suzutomo 0:943dfa56009a 205 }
Suzutomo 0:943dfa56009a 206
Suzutomo 0:943dfa56009a 207 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 * 2 / BUF);
Suzutomo 0:943dfa56009a 208 else while (ledT.read() < calcBar * beatTime + i * beat * beatTime * 2 / BUF);
Suzutomo 0:943dfa56009a 209 }
Suzutomo 0:943dfa56009a 210 break;
Suzutomo 0:943dfa56009a 211
Suzutomo 0:943dfa56009a 212 case 3 : // 中央から外に
Suzutomo 0:943dfa56009a 213 for (int i = 0; i<=(BUF / 2); i++) {
Suzutomo 0:943dfa56009a 214 tape[num].Write(BUF / 2 - i + 1,0x0);
Suzutomo 0:943dfa56009a 215 tape[num].Write(BUF / 2 + i - 1,0x0);
Suzutomo 0:943dfa56009a 216 for (int j = 0; j < 3; j++) {
Suzutomo 0:943dfa56009a 217 tape[num].Write(BUF / 2 - i - j,color);
Suzutomo 0:943dfa56009a 218 tape[num].Write(BUF / 2 + i + j,color);
Suzutomo 0:943dfa56009a 219 }
Suzutomo 0:943dfa56009a 220
Suzutomo 0:943dfa56009a 221 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 * 2 / BUF);
Suzutomo 0:943dfa56009a 222 else while (ledT.read() < calcBar * beatTime + i * beat * beatTime * 2 / BUF);
Suzutomo 0:943dfa56009a 223 }
Suzutomo 0:943dfa56009a 224 break;
Suzutomo 0:943dfa56009a 225
Suzutomo 0:943dfa56009a 226 case 4 : // まとまって流れる
Suzutomo 0:943dfa56009a 227 for (int i = 0; i<=BUF + 40; i++) {
Suzutomo 0:943dfa56009a 228 //printf("%d\r\n",i);
Suzutomo 0:943dfa56009a 229 for (int j = 0; j<20; j++) {
Suzutomo 0:943dfa56009a 230 tape[num].Write(i - 20 + j,tape[num].BrightAdjust(color,(double)j * 1/20));
Suzutomo 0:943dfa56009a 231 }
Suzutomo 0:943dfa56009a 232 for (int j = 0; j < 20; j++) tape[num].Write(i - 20 + j,0);
Suzutomo 0:943dfa56009a 233 if (useF8) while (barClock < calcBar * 24 + i * beat * 24 / (BUF + 40));
Suzutomo 0:943dfa56009a 234 else while (barClock < calcBar * 24 + i * beat * 24 / (BUF + 40));
Suzutomo 0:943dfa56009a 235
Suzutomo 0:943dfa56009a 236 }
Suzutomo 0:943dfa56009a 237 break;
Suzutomo 0:943dfa56009a 238 }
Suzutomo 0:943dfa56009a 239 tape[num].Clear(BUF);
Suzutomo 0:943dfa56009a 240 calcBar += beat;
Suzutomo 0:943dfa56009a 241
Suzutomo 0:943dfa56009a 242 }