Carlos GS
/
blip_textoLEDs
Muestra texto en la matriz de LEDs
main.cpp@0:ecf0f187117c, 2016-01-18 (annotated)
- Committer:
- carlosgs
- Date:
- Mon Jan 18 15:10:52 2016 +0000
- Revision:
- 0:ecf0f187117c
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
carlosgs | 0:ecf0f187117c | 1 | // Display de texto con scrolling |
carlosgs | 0:ecf0f187117c | 2 | // Basado en: https://github.com/hack-miniblip/hack-miniblip.github.io/tree/master/ejemplos/iluminandoLetras |
carlosgs | 0:ecf0f187117c | 3 | // Autores: @javacasm, @neon520, @juanAFernandez, @carlosgs |
carlosgs | 0:ecf0f187117c | 4 | |
carlosgs | 0:ecf0f187117c | 5 | #include "mbed.h" |
carlosgs | 0:ecf0f187117c | 6 | #include "neopixel.h" |
carlosgs | 0:ecf0f187117c | 7 | |
carlosgs | 0:ecf0f187117c | 8 | // Matrix led output pin |
carlosgs | 0:ecf0f187117c | 9 | #define DATA_PIN P0_9 |
carlosgs | 0:ecf0f187117c | 10 | |
carlosgs | 0:ecf0f187117c | 11 | void fill_pixel(neopixel::Pixel buffer[25], int x, int y, int red, int green, int blue){ |
carlosgs | 0:ecf0f187117c | 12 | |
carlosgs | 0:ecf0f187117c | 13 | if(x<0) x=0; |
carlosgs | 0:ecf0f187117c | 14 | if(x>4) x=4; |
carlosgs | 0:ecf0f187117c | 15 | if(y<0) y=0; |
carlosgs | 0:ecf0f187117c | 16 | if(y>4) y=4; |
carlosgs | 0:ecf0f187117c | 17 | |
carlosgs | 0:ecf0f187117c | 18 | |
carlosgs | 0:ecf0f187117c | 19 | int posicion=x+y*5; |
carlosgs | 0:ecf0f187117c | 20 | buffer[posicion].red=red; |
carlosgs | 0:ecf0f187117c | 21 | buffer[posicion].green=green; |
carlosgs | 0:ecf0f187117c | 22 | buffer[posicion].blue=blue; |
carlosgs | 0:ecf0f187117c | 23 | |
carlosgs | 0:ecf0f187117c | 24 | } |
carlosgs | 0:ecf0f187117c | 25 | |
carlosgs | 0:ecf0f187117c | 26 | void void_matrix(neopixel::Pixel aux[25], int tam=25){ |
carlosgs | 0:ecf0f187117c | 27 | |
carlosgs | 0:ecf0f187117c | 28 | for(int i=0;i<tam;i++){ |
carlosgs | 0:ecf0f187117c | 29 | aux[i].red=0; |
carlosgs | 0:ecf0f187117c | 30 | aux[i].green=0; |
carlosgs | 0:ecf0f187117c | 31 | aux[i].blue=0; |
carlosgs | 0:ecf0f187117c | 32 | } |
carlosgs | 0:ecf0f187117c | 33 | } |
carlosgs | 0:ecf0f187117c | 34 | |
carlosgs | 0:ecf0f187117c | 35 | //r: intensidadRojo |
carlosgs | 0:ecf0f187117c | 36 | //g: intensidadVerde |
carlosgs | 0:ecf0f187117c | 37 | //b: intensidadAzul |
carlosgs | 0:ecf0f187117c | 38 | void generaLetra(neopixel::Pixel vector[], char letra, int red, int green, int blue, int scroll){ |
carlosgs | 0:ecf0f187117c | 39 | |
carlosgs | 0:ecf0f187117c | 40 | /* |
carlosgs | 0:ecf0f187117c | 41 | * Cada letra tiene 5 valores en hexadecimal que representan en binario cada fila de la matrix 5x5 (aunque sobren 3 bits) |
carlosgs | 0:ecf0f187117c | 42 | */ |
carlosgs | 0:ecf0f187117c | 43 | unsigned char espacio[5]={0x0, 0x0, 0x0, 0x0, 0x0}; |
carlosgs | 0:ecf0f187117c | 44 | unsigned char a[5]={0x88, 0x88, 0xF8, 0x88, 0x70}; |
carlosgs | 0:ecf0f187117c | 45 | unsigned char b[5]={0xF0, 0x88, 0xF0, 0x88, 0xF0}; |
carlosgs | 0:ecf0f187117c | 46 | unsigned char c[5]={0x78, 0x80, 0x80, 0x80, 0x78}; |
carlosgs | 0:ecf0f187117c | 47 | unsigned char d[5]={0xF0, 0x88, 0x88, 0x88, 0xF0}; |
carlosgs | 0:ecf0f187117c | 48 | unsigned char e[5]={0xF8, 0x80, 0xF8, 0x80, 0xF8}; |
carlosgs | 0:ecf0f187117c | 49 | unsigned char f[5]={0x80, 0x80, 0xE0, 0x80, 0xF8}; |
carlosgs | 0:ecf0f187117c | 50 | unsigned char g[5]={0x78, 0x88, 0xB8, 0x80, 0x78}; |
carlosgs | 0:ecf0f187117c | 51 | unsigned char h[5]={0x88, 0x88, 0xF8, 0x88, 0x88}; |
carlosgs | 0:ecf0f187117c | 52 | unsigned char i[5]={0x70, 0x20, 0x20, 0x20, 0x70}; |
carlosgs | 0:ecf0f187117c | 53 | unsigned char j[5]={0x70, 0x88, 0x8, 0x8, 0x38}; |
carlosgs | 0:ecf0f187117c | 54 | unsigned char k[5]={0x88, 0x88, 0xF0, 0xA0, 0x90}; |
carlosgs | 0:ecf0f187117c | 55 | unsigned char l[5]={0xF8, 0x80, 0x80, 0x80, 0x80}; |
carlosgs | 0:ecf0f187117c | 56 | unsigned char m[5]={0x88, 0x88, 0xA8, 0xD8, 0x88}; |
carlosgs | 0:ecf0f187117c | 57 | unsigned char n[5]={0x98, 0xA8, 0xA8, 0xA8, 0xC8}; |
carlosgs | 0:ecf0f187117c | 58 | unsigned char gn[5]={0x98, 0xA8, 0xA8, 0xA8, 0xC8}; // es la ñ |
carlosgs | 0:ecf0f187117c | 59 | unsigned char o[5]={0x70, 0x88, 0x88, 0x88, 0x70}; |
carlosgs | 0:ecf0f187117c | 60 | unsigned char p[5]={0x80, 0x80, 0x78, 0x88, 0x78}; |
carlosgs | 0:ecf0f187117c | 61 | unsigned char q[5]={0x78, 0x90, 0x90, 0x90, 0x60}; |
carlosgs | 0:ecf0f187117c | 62 | unsigned char r[5]={0x88, 0x90, 0xF0, 0x88, 0xF0}; |
carlosgs | 0:ecf0f187117c | 63 | unsigned char s[5]={0xF0, 0x8, 0x70, 0x80, 0x78}; |
carlosgs | 0:ecf0f187117c | 64 | unsigned char t[5]={0x20, 0x20, 0x20, 0x20, 0xF8}; |
carlosgs | 0:ecf0f187117c | 65 | unsigned char u[5]={0x70,0x88,0x88,0x88,0x88}; |
carlosgs | 0:ecf0f187117c | 66 | unsigned char v[5]={0x20,0x50,0x50,0x88,0x88}; |
carlosgs | 0:ecf0f187117c | 67 | unsigned char w[5]={0x88,0xD8,0xA8,0x88,0x88}; |
carlosgs | 0:ecf0f187117c | 68 | unsigned char x[5]={0x88,0x50,0x20,0x50,0x88}; |
carlosgs | 0:ecf0f187117c | 69 | unsigned char y[5]={0x20,0x20,0x70,0x88,0x88}; |
carlosgs | 0:ecf0f187117c | 70 | unsigned char z[5]={0xF8,0x80,0x70,0x8,0xF8}; |
carlosgs | 0:ecf0f187117c | 71 | |
carlosgs | 0:ecf0f187117c | 72 | |
carlosgs | 0:ecf0f187117c | 73 | //unsigned char l0[0]= |
carlosgs | 0:ecf0f187117c | 74 | |
carlosgs | 0:ecf0f187117c | 75 | |
carlosgs | 0:ecf0f187117c | 76 | //Montamos un vector de vectores: |
carlosgs | 0:ecf0f187117c | 77 | unsigned char *vectorPunteros []={a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z}; |
carlosgs | 0:ecf0f187117c | 78 | |
carlosgs | 0:ecf0f187117c | 79 | //Con la letra recibida le restamos 26 y tenemos la posicion del vector. |
carlosgs | 0:ecf0f187117c | 80 | //Para moverlo por los vectores. |
carlosgs | 0:ecf0f187117c | 81 | unsigned char *puntero; |
carlosgs | 0:ecf0f187117c | 82 | if (letra==32) //Se trata de un espacio |
carlosgs | 0:ecf0f187117c | 83 | puntero=espacio; |
carlosgs | 0:ecf0f187117c | 84 | else |
carlosgs | 0:ecf0f187117c | 85 | //a= al valor 97 |
carlosgs | 0:ecf0f187117c | 86 | puntero=vectorPunteros[(letra-'a')]; |
carlosgs | 0:ecf0f187117c | 87 | |
carlosgs | 0:ecf0f187117c | 88 | |
carlosgs | 0:ecf0f187117c | 89 | //Vamos a recorrer todo el vector de bytes |
carlosgs | 0:ecf0f187117c | 90 | for(int i=0; i<5; i++){ |
carlosgs | 0:ecf0f187117c | 91 | |
carlosgs | 0:ecf0f187117c | 92 | unsigned char elemento = puntero[i]; |
carlosgs | 0:ecf0f187117c | 93 | unsigned int mask=0x80; |
carlosgs | 0:ecf0f187117c | 94 | //Nos movemos por 5 bits de los 8 |
carlosgs | 0:ecf0f187117c | 95 | for(int x=0; x<5; x++){ |
carlosgs | 0:ecf0f187117c | 96 | if (elemento & mask) |
carlosgs | 0:ecf0f187117c | 97 | if(x+scroll >= 0 && x+scroll < 5) |
carlosgs | 0:ecf0f187117c | 98 | fill_pixel(vector, x+scroll,i, red, green, blue); |
carlosgs | 0:ecf0f187117c | 99 | //Desplazamos 1 bit |
carlosgs | 0:ecf0f187117c | 100 | mask >>=1; |
carlosgs | 0:ecf0f187117c | 101 | } |
carlosgs | 0:ecf0f187117c | 102 | } |
carlosgs | 0:ecf0f187117c | 103 | } |
carlosgs | 0:ecf0f187117c | 104 | |
carlosgs | 0:ecf0f187117c | 105 | void iluminaTexto(char cadena[], neopixel::PixelArray array, int r, int g, int b){ |
carlosgs | 0:ecf0f187117c | 106 | |
carlosgs | 0:ecf0f187117c | 107 | //Creamos un vector de pixeles. |
carlosgs | 0:ecf0f187117c | 108 | neopixel::Pixel letra[25]; |
carlosgs | 0:ecf0f187117c | 109 | //Inicializamos el vector a 0 |
carlosgs | 0:ecf0f187117c | 110 | void_matrix(letra); |
carlosgs | 0:ecf0f187117c | 111 | |
carlosgs | 0:ecf0f187117c | 112 | char letraAnterior = ' '; |
carlosgs | 0:ecf0f187117c | 113 | |
carlosgs | 0:ecf0f187117c | 114 | for(int i=0; i<strlen(cadena); i++){ |
carlosgs | 0:ecf0f187117c | 115 | |
carlosgs | 0:ecf0f187117c | 116 | for(int scroll=0; scroll<7; scroll++){ |
carlosgs | 0:ecf0f187117c | 117 | //Generamos la letra en el vector letra |
carlosgs | 0:ecf0f187117c | 118 | generaLetra(letra, letraAnterior, r, g, b, -scroll-2); |
carlosgs | 0:ecf0f187117c | 119 | generaLetra(letra, cadena[i], r, g, b, 5-scroll); |
carlosgs | 0:ecf0f187117c | 120 | //Iluminamos la matriz |
carlosgs | 0:ecf0f187117c | 121 | array.update(letra, 25); |
carlosgs | 0:ecf0f187117c | 122 | //Esperamos |
carlosgs | 0:ecf0f187117c | 123 | wait_ms(60); |
carlosgs | 0:ecf0f187117c | 124 | //Reseteamos la matriz |
carlosgs | 0:ecf0f187117c | 125 | void_matrix(letra); |
carlosgs | 0:ecf0f187117c | 126 | } |
carlosgs | 0:ecf0f187117c | 127 | letraAnterior = cadena[i]; |
carlosgs | 0:ecf0f187117c | 128 | } |
carlosgs | 0:ecf0f187117c | 129 | |
carlosgs | 0:ecf0f187117c | 130 | |
carlosgs | 0:ecf0f187117c | 131 | } |
carlosgs | 0:ecf0f187117c | 132 | |
carlosgs | 0:ecf0f187117c | 133 | PwmOut speaker(P0_8); |
carlosgs | 0:ecf0f187117c | 134 | |
carlosgs | 0:ecf0f187117c | 135 | int main() |
carlosgs | 0:ecf0f187117c | 136 | { |
carlosgs | 0:ecf0f187117c | 137 | // Turn off miniblip buzzer |
carlosgs | 0:ecf0f187117c | 138 | speaker=0.0; |
carlosgs | 0:ecf0f187117c | 139 | |
carlosgs | 0:ecf0f187117c | 140 | // Create a temporary DigitalIn so we can configure the pull-down resistor. |
carlosgs | 0:ecf0f187117c | 141 | DigitalIn(DATA_PIN, PullDown); |
carlosgs | 0:ecf0f187117c | 142 | |
carlosgs | 0:ecf0f187117c | 143 | // The pixel array control class. |
carlosgs | 0:ecf0f187117c | 144 | //Se está creando un objeto de tipo PixelArray y se está nombrando como array a la que se le está pasando DATA_PIN |
carlosgs | 0:ecf0f187117c | 145 | neopixel::PixelArray array(DATA_PIN); |
carlosgs | 0:ecf0f187117c | 146 | |
carlosgs | 0:ecf0f187117c | 147 | char greeting[] = "hola mundo "; |
carlosgs | 0:ecf0f187117c | 148 | |
carlosgs | 0:ecf0f187117c | 149 | while (1) { |
carlosgs | 0:ecf0f187117c | 150 | int r = 0; |
carlosgs | 0:ecf0f187117c | 151 | int g = rand()%30; |
carlosgs | 0:ecf0f187117c | 152 | int b = rand()%30; |
carlosgs | 0:ecf0f187117c | 153 | |
carlosgs | 0:ecf0f187117c | 154 | if (rand()%10 < 3){ |
carlosgs | 0:ecf0f187117c | 155 | r = rand()%30; |
carlosgs | 0:ecf0f187117c | 156 | g = 0; |
carlosgs | 0:ecf0f187117c | 157 | b = 0; |
carlosgs | 0:ecf0f187117c | 158 | } |
carlosgs | 0:ecf0f187117c | 159 | |
carlosgs | 0:ecf0f187117c | 160 | iluminaTexto(greeting, array, r,g,b); |
carlosgs | 0:ecf0f187117c | 161 | wait_ms(500); |
carlosgs | 0:ecf0f187117c | 162 | } |
carlosgs | 0:ecf0f187117c | 163 | } |