Skip to content

Commit e4a609f

Browse files
committed
Merge pull request #74 from JCristobal/patch-1
Versión del ejemplo de "blip_dado"
2 parents c477062 + 5d34816 commit e4a609f

1 file changed

Lines changed: 177 additions & 0 deletions

File tree

ejemplos/dado/main.cpp

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// miniblip led dado
2+
// JCristobal version, original Alberto Pigante from https://developer.mbed.org/users/pighixxx/code/blip_dado/
3+
// Now all faces can win (not ones always win) and wait sequence reduced
4+
5+
#include "mbed.h"
6+
#include "neopixel.h"
7+
8+
// Matrix led output pin
9+
#define DATA_PIN P0_9
10+
11+
#define ANALOG_PHOTO P0_16
12+
#define ANALOG_POTENTIOMETER P0_22
13+
#define ANALOG_BUZZER P0_8
14+
#define DIGITAL_CIRCLE P0_12
15+
16+
AnalogIn ain(ANALOG_POTENTIOMETER);
17+
18+
void fill_pixel(neopixel::Pixel buffer[25], int x, int y, int red, int green, int blue){
19+
20+
if(x<0) x=0;
21+
if(x>4) x=4;
22+
if(y<0) y=0;
23+
if(y>4) y=4;
24+
25+
26+
int posicion=x+y*5;
27+
buffer[posicion].red=red;
28+
buffer[posicion].green=green;
29+
buffer[posicion].blue=blue;
30+
}
31+
32+
void void_matrix(neopixel::Pixel aux[25], int tam=25){
33+
34+
for(int i=0;i<tam;i++){
35+
aux[i].red=0;
36+
aux[i].green=0;
37+
aux[i].blue=0;
38+
}
39+
}
40+
41+
int dado_1[5][5] = {
42+
{0,0,0,0,0},
43+
{0,0,0,0,0},
44+
{0,0,1,0,0},
45+
{0,0,0,0,0},
46+
{0,0,0,0,0}
47+
};
48+
49+
int dado_2[5][5] = {
50+
{0,0,0,0,1},
51+
{0,0,0,0,0},
52+
{0,0,0,0,0},
53+
{0,0,0,0,0},
54+
{1,0,0,0,0}
55+
};
56+
57+
int dado_3[5][5] = {
58+
{0,0,0,0,1},
59+
{0,0,0,0,0},
60+
{0,0,1,0,0},
61+
{0,0,0,0,0},
62+
{1,0,0,0,0}
63+
};
64+
65+
int dado_4[5][5] = {
66+
{1,0,0,0,1},
67+
{0,0,0,0,0},
68+
{0,0,0,0,0},
69+
{0,0,0,0,0},
70+
{1,0,0,0,1}
71+
};
72+
73+
int dado_5[5][5] = {
74+
{1,0,0,0,1},
75+
{0,0,0,0,0},
76+
{0,0,1,0,0},
77+
{0,0,0,0,0},
78+
{1,0,0,0,1}
79+
};
80+
81+
int dado_6[5][5] = {
82+
{1,0,0,0,1},
83+
{0,0,0,0,0},
84+
{1,0,0,0,1},
85+
{0,0,0,0,0},
86+
{1,0,0,0,1}
87+
};
88+
89+
void drawVector(int theArray[5][5], neopixel::Pixel * vectorPixel, int r, int g, int b){
90+
for(int i = 0;i<5;i++){
91+
for(int j = 0; j<5;j++){
92+
if(theArray[i][j] == 1)
93+
fill_pixel(vectorPixel,i,j,r,g,b);
94+
else
95+
fill_pixel(vectorPixel,i,j,0,0,0);
96+
}
97+
}
98+
}
99+
int main()
100+
{
101+
// Turn off miniblip buzzer
102+
PwmOut speaker(P0_8);
103+
speaker=0.0;
104+
// Create a temporary DigitalIn so we can configure the pull-down resistor.
105+
DigitalIn(DATA_PIN, PullDown);
106+
107+
// Pushbutton
108+
DigitalIn pushbutton(P0_23);
109+
110+
neopixel::Pixel vector[25];
111+
void_matrix(vector);
112+
113+
// The pixel array control class.
114+
neopixel::PixelArray array(DATA_PIN);
115+
int posLed=0;
116+
int cred=10;
117+
int cblue=10;
118+
int cgreen=10;
119+
while (1) {
120+
// Wait screen
121+
int posx=posLed%5;
122+
int posy=int(posLed/5);
123+
fill_pixel(vector,posx,posy,cred,cblue,cgreen);
124+
array.update(vector, 64);
125+
wait_ms(30);
126+
posLed++;
127+
if (posLed>24) {
128+
cred=int(rand()%50);
129+
cblue=int(rand()%50);
130+
cgreen=int(rand()%50);
131+
posLed=0;
132+
}
133+
// Wait button
134+
if(pushbutton){
135+
posLed=0;
136+
float pot = ain.read() * 100.0f;
137+
int startdelay=10;
138+
int face=1; // ones always win
139+
face = rand() % 6 + 1; // Let's give a chance to others
140+
int red=50;
141+
while (1) {
142+
if (startdelay>300+(pot*3)) red=200;
143+
switch(face) {
144+
case 1:
145+
drawVector(dado_1,vector,red,200-red,200-red);
146+
break;
147+
case 2:
148+
drawVector(dado_2,vector,red,200-red,200-red);
149+
break;
150+
case 3:
151+
drawVector(dado_3,vector,red,200-red,200-red);
152+
break;
153+
case 4:
154+
drawVector(dado_4,vector,red,200-red,200-red);
155+
break;
156+
case 5:
157+
drawVector(dado_5,vector,red,200-red,200-red);
158+
break;
159+
case 6:
160+
drawVector(dado_6,vector,red,200-red,200-red);
161+
break;
162+
}
163+
array.update(vector, 64);
164+
if (startdelay>300+(pot*3)) break;
165+
face++;
166+
startdelay=startdelay+50;
167+
if (face>6) face=1;
168+
wait_ms(startdelay);
169+
170+
}
171+
speaker=5.0;
172+
wait_ms(1000);
173+
speaker=0.0;
174+
wait_ms(2500);
175+
}
176+
}
177+
}

0 commit comments

Comments
 (0)