Water-like simulation for miniblip

Dependencies:   PixelArray mbed

Committer:
erpheus
Date:
Sat Jan 23 00:57:14 2016 +0000
Revision:
0:ba73ace939cd
Only one wave front per button. Random leds on [bug]

Who changed what in which revision?

UserRevisionLine numberNew contents of line
erpheus 0:ba73ace939cd 1 // miniblip led matrix demo
erpheus 0:ba73ace939cd 2
erpheus 0:ba73ace939cd 3 #include "mbed.h"
erpheus 0:ba73ace939cd 4 #include "neopixel.h"
erpheus 0:ba73ace939cd 5
erpheus 0:ba73ace939cd 6 // Matrix led output pin
erpheus 0:ba73ace939cd 7 #define DATA_PIN P0_9
erpheus 0:ba73ace939cd 8 #define NLEDS 25
erpheus 0:ba73ace939cd 9
erpheus 0:ba73ace939cd 10 AnalogIn b1(P0_11);
erpheus 0:ba73ace939cd 11 AnalogIn b2(P0_12);
erpheus 0:ba73ace939cd 12 AnalogIn b3(P0_13);
erpheus 0:ba73ace939cd 13 AnalogIn b4(P0_14);
erpheus 0:ba73ace939cd 14 AnalogIn b5(P0_15);
erpheus 0:ba73ace939cd 15
erpheus 0:ba73ace939cd 16 PwmOut speaker(P0_8);
erpheus 0:ba73ace939cd 17
erpheus 0:ba73ace939cd 18 #define WIDTH 1.2
erpheus 0:ba73ace939cd 19 #define SPEED 8.0 // depends on tick_freq
erpheus 0:ba73ace939cd 20 #define POWER 50
erpheus 0:ba73ace939cd 21
erpheus 0:ba73ace939cd 22 #define tick_freq 20.0
erpheus 0:ba73ace939cd 23 #define maxdistance 8 // ceil(sqrt(5^2+5^2)) 5 = width = height
erpheus 0:ba73ace939cd 24
erpheus 0:ba73ace939cd 25 neopixel::PixelArray array(DATA_PIN);
erpheus 0:ba73ace939cd 26 neopixel::Pixel buffer[NLEDS];
erpheus 0:ba73ace939cd 27
erpheus 0:ba73ace939cd 28 AnalogIn buttons[5] = {b1,b2,b3,b4,b5};
erpheus 0:ba73ace939cd 29
erpheus 0:ba73ace939cd 30 void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue) {
erpheus 0:ba73ace939cd 31 if(x < 0 || x > 4 || y < 0 || y > 4) return;
erpheus 0:ba73ace939cd 32 int posicion=x+y*5;
erpheus 0:ba73ace939cd 33 buffer[posicion].red=red;
erpheus 0:ba73ace939cd 34 buffer[posicion].green=green;
erpheus 0:ba73ace939cd 35 buffer[posicion].blue=blue;
erpheus 0:ba73ace939cd 36 }
erpheus 0:ba73ace939cd 37
erpheus 0:ba73ace939cd 38 unsigned int counter = 0;
erpheus 0:ba73ace939cd 39
erpheus 0:ba73ace939cd 40 unsigned int waves[5] = {0,0,0,0,0};
erpheus 0:ba73ace939cd 41
erpheus 0:ba73ace939cd 42 void checkButton(){
erpheus 0:ba73ace939cd 43 int i;
erpheus 0:ba73ace939cd 44 for (i = 0; i < 5; i++){
erpheus 0:ba73ace939cd 45 if (waves[i] && (counter - waves[i]) > (maxdistance * tick_freq / SPEED)){
erpheus 0:ba73ace939cd 46 waves[i]=0;
erpheus 0:ba73ace939cd 47 }else if(!waves[i]){
erpheus 0:ba73ace939cd 48 if (buttons[i].read() == 0.00){
erpheus 0:ba73ace939cd 49 waves[i] = counter;
erpheus 0:ba73ace939cd 50 }
erpheus 0:ba73ace939cd 51 }
erpheus 0:ba73ace939cd 52 }
erpheus 0:ba73ace939cd 53 }
erpheus 0:ba73ace939cd 54
erpheus 0:ba73ace939cd 55 void count(void){
erpheus 0:ba73ace939cd 56 counter++;
erpheus 0:ba73ace939cd 57 checkButton();
erpheus 0:ba73ace939cd 58 }
erpheus 0:ba73ace939cd 59
erpheus 0:ba73ace939cd 60
erpheus 0:ba73ace939cd 61 struct position {
erpheus 0:ba73ace939cd 62 int x, y;
erpheus 0:ba73ace939cd 63 };
erpheus 0:ba73ace939cd 64
erpheus 0:ba73ace939cd 65 struct color {
erpheus 0:ba73ace939cd 66 uint8_t red, green, blue;
erpheus 0:ba73ace939cd 67 };
erpheus 0:ba73ace939cd 68
erpheus 0:ba73ace939cd 69 float distance(position pos1, position pos2){
erpheus 0:ba73ace939cd 70 return sqrt(pow((pos1.x - pos2.x)*1.0,2.0)+pow((pos1.y - pos2.y)*1.0,2.0));
erpheus 0:ba73ace939cd 71 }
erpheus 0:ba73ace939cd 72
erpheus 0:ba73ace939cd 73
erpheus 0:ba73ace939cd 74
erpheus 0:ba73ace939cd 75 position startpositions[5] = {{3,-1},{-1,0},{-2,2},{-1,4},{0,5}};
erpheus 0:ba73ace939cd 76
erpheus 0:ba73ace939cd 77 color colorForPixel(unsigned int* waves, position p){
erpheus 0:ba73ace939cd 78 int colors[3] = {0,0,0};
erpheus 0:ba73ace939cd 79 int i;
erpheus 0:ba73ace939cd 80 float d, wave_spread, delta;
erpheus 0:ba73ace939cd 81 for(i = 0; i < 5; i++){
erpheus 0:ba73ace939cd 82 if (waves[i]==0){ continue; }
erpheus 0:ba73ace939cd 83 d = distance(startpositions[i],p);
erpheus 0:ba73ace939cd 84 wave_spread = ((counter - waves[i])*SPEED)/tick_freq;
erpheus 0:ba73ace939cd 85 delta = d - wave_spread;
erpheus 0:ba73ace939cd 86 if (delta > WIDTH || delta < -WIDTH) {
erpheus 0:ba73ace939cd 87 continue;
erpheus 0:ba73ace939cd 88 }
erpheus 0:ba73ace939cd 89 colors[i%3] = colors[i%3] + sinf((WIDTH-fabs(delta))/WIDTH * 3.141592/2) * POWER ;
erpheus 0:ba73ace939cd 90 /*if (colors[i%3] > 255) {
erpheus 0:ba73ace939cd 91 colors[i%3] = 255;
erpheus 0:ba73ace939cd 92 }*/
erpheus 0:ba73ace939cd 93
erpheus 0:ba73ace939cd 94 }
erpheus 0:ba73ace939cd 95 color res = {0,0,0};
erpheus 0:ba73ace939cd 96 res.red = colors[0];
erpheus 0:ba73ace939cd 97 res.green = colors[1];
erpheus 0:ba73ace939cd 98 res.blue = colors[2];
erpheus 0:ba73ace939cd 99 return res;
erpheus 0:ba73ace939cd 100 }
erpheus 0:ba73ace939cd 101
erpheus 0:ba73ace939cd 102 Ticker tick;
erpheus 0:ba73ace939cd 103
erpheus 0:ba73ace939cd 104 int main() {
erpheus 0:ba73ace939cd 105
erpheus 0:ba73ace939cd 106 speaker = 0.0;
erpheus 0:ba73ace939cd 107
erpheus 0:ba73ace939cd 108 tick.attach(count, 1.0 / tick_freq);
erpheus 0:ba73ace939cd 109
erpheus 0:ba73ace939cd 110
erpheus 0:ba73ace939cd 111
erpheus 0:ba73ace939cd 112
erpheus 0:ba73ace939cd 113 int i,j;
erpheus 0:ba73ace939cd 114 position p;
erpheus 0:ba73ace939cd 115
erpheus 0:ba73ace939cd 116 while(true){
erpheus 0:ba73ace939cd 117
erpheus 0:ba73ace939cd 118 for(i=0; i<5; i++){
erpheus 0:ba73ace939cd 119 for(j=0; j<5; j++){
erpheus 0:ba73ace939cd 120 p.x = i;
erpheus 0:ba73ace939cd 121 p.y = j;
erpheus 0:ba73ace939cd 122 color c = colorForPixel(waves,p);
erpheus 0:ba73ace939cd 123 setPixel(i, j, c.red, c.green, c.blue);
erpheus 0:ba73ace939cd 124 }
erpheus 0:ba73ace939cd 125 }
erpheus 0:ba73ace939cd 126 array.update(buffer, NLEDS);
erpheus 0:ba73ace939cd 127 wait_ms(1);
erpheus 0:ba73ace939cd 128 }
erpheus 0:ba73ace939cd 129 }