Patrick H.
/
ledmatrix
errors
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "animations.h" 00003 00004 DigitalOut ledy0(PC_0); 00005 DigitalOut ledy1(PC_1); 00006 DigitalOut ledy2(PC_2); 00007 DigitalOut ledx0(PC_13); 00008 DigitalOut ledx1(PC_14); 00009 DigitalOut ledx2(PC_15); 00010 00011 AnalogIn poti(A0); 00012 00013 void turnOn(short x, short y) 00014 { 00015 switch(x) { 00016 case 0: 00017 ledx0 = 0; 00018 break; 00019 case 1: 00020 ledx1 = 0; 00021 break; 00022 case 2: 00023 ledx2 = 0; 00024 break; 00025 } 00026 00027 switch(y) { 00028 case 0: 00029 ledy0 = 1; 00030 break; 00031 case 1: 00032 ledy1 = 1; 00033 break; 00034 case 2: 00035 ledy2 = 1; 00036 break; 00037 } 00038 } 00039 00040 void turnOff(short x, short y) 00041 { 00042 switch(x) { 00043 case 0: 00044 ledx0 = 1; 00045 break; 00046 case 1: 00047 ledx1 = 1; 00048 break; 00049 case 2: 00050 ledx2 = 1; 00051 break; 00052 } 00053 00054 switch(y) { 00055 case 0: 00056 ledy0 = 0; 00057 break; 00058 case 1: 00059 ledy1 = 0; 00060 break; 00061 case 2: 00062 ledy2 = 0; 00063 break; 00064 } 00065 } 00066 00067 void setPixel(int x, int y, bool on) 00068 { 00069 if (on) 00070 turnOn(x,y); 00071 else 00072 turnOff(x,y); 00073 } 00074 00075 int delay = 100; 00076 00077 void updateDelay() 00078 { 00079 delay = 1000 + pow(poti * 4, 4) * 1000; 00080 } 00081 00082 int leds[9][2] = { 00083 { 0, 0 }, 00084 { 1, 0 }, 00085 { 2, 0 }, 00086 { 0, 1 }, 00087 { 1, 1 }, 00088 { 2, 1 }, 00089 { 0, 2 }, 00090 { 1, 2 }, 00091 { 2, 2 }, 00092 }; 00093 00094 void animate(int animation[][10] animation, int cycles, bool inverted = false) 00095 { 00096 int frames = sizeof(animation) / 320; 00097 00098 for (int frame = 0; frame < frames; frame++) { 00099 int frameTime = animation[frame][9]; 00100 00101 Timer timer; 00102 00103 while (timer.read_ms() < frameTime) { 00104 for (int pixel = 0; pixel < 9; pixel++) { 00105 bool state = (bool)animation[frame][pixel] ^ inverted; 00106 setPixel(leds[pixel][0], leds[pixel][1], state); 00107 wait_us(delay); 00108 setPixel(leds[pixel][0], leds[pixel][1], !state); 00109 } 00110 } 00111 } 00112 } 00113 00114 void init() 00115 { 00116 for (int x = 0; x<3; x++) 00117 for (int y = 0; y<3; y++) 00118 turnOff(x,y); 00119 } 00120 00121 int main() 00122 { 00123 init(); 00124 Ticker ticker; 00125 ticker.attach(&updateDelay, 0.05); 00126 00127 while(true) { 00128 animate(animation1, 8); 00129 animate(animation2, 2); 00130 animate(animation3, 1); 00131 animate(animation4, 2); 00132 animate(animation5, 2); 00133 animate(transition6, 1); 00134 animate(animation6, 3); 00135 animate(end6, 1); 00136 } 00137 }
Generated on Fri Jul 15 2022 11:42:32 by 1.7.2