Código de iniciación para MiniBlip: LEDs+pulsador+puertoSerie

Dependencies:   PixelArray USBDevice mbed

Committer:
carlosgs
Date:
Mon Jan 18 16:16:09 2016 +0000
Revision:
0:49f15045d34a
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
carlosgs 0:49f15045d34a 1 // Programa de Hola mundo
carlosgs 0:49f15045d34a 2 // Animación en la matriz de LEDs, recibe input del pulsador, y escribe por el puerto serie
carlosgs 0:49f15045d34a 3
carlosgs 0:49f15045d34a 4 // Enlaces útiles:
carlosgs 0:49f15045d34a 5 // http://hack-miniblip.github.io/programar.html
carlosgs 0:49f15045d34a 6 // https://raw.githubusercontent.com/hack-miniblip/hack-miniblip.github.io/master/Scripts/miniblip_loader.sh
carlosgs 0:49f15045d34a 7 // https://github.com/hack-miniblip/hack-miniblip.github.io/blob/master/cookbook.md
carlosgs 0:49f15045d34a 8 // http://hack-miniblip.github.io/esquema_minilip_pinout.png
carlosgs 0:49f15045d34a 9
carlosgs 0:49f15045d34a 10
carlosgs 0:49f15045d34a 11 #include "mbed.h"
carlosgs 0:49f15045d34a 12 #include "neopixel.h"
carlosgs 0:49f15045d34a 13 #include "USBSerial.h"
carlosgs 0:49f15045d34a 14
carlosgs 0:49f15045d34a 15 // Matrix led output pin
carlosgs 0:49f15045d34a 16 #define MATRIX_PIN P0_9
carlosgs 0:49f15045d34a 17 #define NLEDS 25
carlosgs 0:49f15045d34a 18
carlosgs 0:49f15045d34a 19 unsigned int counter = 0;
carlosgs 0:49f15045d34a 20 USBSerial serial;
carlosgs 0:49f15045d34a 21
carlosgs 0:49f15045d34a 22 DigitalIn pushbutton(P0_23); //Define pushbutton
carlosgs 0:49f15045d34a 23
carlosgs 0:49f15045d34a 24 neopixel::Pixel buffer[NLEDS];
carlosgs 0:49f15045d34a 25
carlosgs 0:49f15045d34a 26 neopixel::PixelArray array(MATRIX_PIN);
carlosgs 0:49f15045d34a 27
carlosgs 0:49f15045d34a 28 void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue) {
carlosgs 0:49f15045d34a 29 if(x < 0 || x > 4 || y < 0 || y > 4) return;
carlosgs 0:49f15045d34a 30 int posicion=x+y*5;
carlosgs 0:49f15045d34a 31 buffer[posicion].red=red;
carlosgs 0:49f15045d34a 32 buffer[posicion].green=green;
carlosgs 0:49f15045d34a 33 buffer[posicion].blue=blue;
carlosgs 0:49f15045d34a 34 }
carlosgs 0:49f15045d34a 35
carlosgs 0:49f15045d34a 36 void updateLEDs() {
carlosgs 0:49f15045d34a 37 array.update(buffer, NLEDS);
carlosgs 0:49f15045d34a 38 }
carlosgs 0:49f15045d34a 39
carlosgs 0:49f15045d34a 40 void clearPixels() {
carlosgs 0:49f15045d34a 41 for(int x=0; x<5; x++)
carlosgs 0:49f15045d34a 42 for(int y=0; y<5; y++)
carlosgs 0:49f15045d34a 43 setPixel(x,y, 0,0,0);
carlosgs 0:49f15045d34a 44 }
carlosgs 0:49f15045d34a 45
carlosgs 0:49f15045d34a 46
carlosgs 0:49f15045d34a 47 PwmOut speaker(P0_8);
carlosgs 0:49f15045d34a 48
carlosgs 0:49f15045d34a 49 int main()
carlosgs 0:49f15045d34a 50 {
carlosgs 0:49f15045d34a 51 // Apagar el zumbador
carlosgs 0:49f15045d34a 52 speaker=0.0;
carlosgs 0:49f15045d34a 53
carlosgs 0:49f15045d34a 54 // setPixel(x,y, r,g,b); // valor máximo RGB: 255
carlosgs 0:49f15045d34a 55
carlosgs 0:49f15045d34a 56 // Poner todos los LED en verde gradualmente
carlosgs 0:49f15045d34a 57 for(int x=0; x<5; x++) {
carlosgs 0:49f15045d34a 58 for(int y=0; y<5; y++) {
carlosgs 0:49f15045d34a 59 setPixel(x,y, 0,10,0);
carlosgs 0:49f15045d34a 60 updateLEDs();
carlosgs 0:49f15045d34a 61 wait_ms(100);
carlosgs 0:49f15045d34a 62 }
carlosgs 0:49f15045d34a 63 }
carlosgs 0:49f15045d34a 64
carlosgs 0:49f15045d34a 65 clearPixels(); // Poner a 0 la matriz actual
carlosgs 0:49f15045d34a 66
carlosgs 0:49f15045d34a 67 // Dibujar un smiley
carlosgs 0:49f15045d34a 68 setPixel(4,1, 0,0,20); // ojos
carlosgs 0:49f15045d34a 69 setPixel(4,3, 0,0,20);
carlosgs 0:49f15045d34a 70
carlosgs 0:49f15045d34a 71 setPixel(1,0, 0,0,20); // boca
carlosgs 0:49f15045d34a 72 setPixel(0,1, 0,0,20);
carlosgs 0:49f15045d34a 73 setPixel(0,2, 0,0,20);
carlosgs 0:49f15045d34a 74 setPixel(0,3, 0,0,20);
carlosgs 0:49f15045d34a 75 setPixel(1,4, 0,0,20);
carlosgs 0:49f15045d34a 76
carlosgs 0:49f15045d34a 77 while(1) {
carlosgs 0:49f15045d34a 78 if (pushbutton) {
carlosgs 0:49f15045d34a 79 updateLEDs(); // Se muestra el smiley
carlosgs 0:49f15045d34a 80 serial.printf("Hola Mundo! Texto enviado por el puerto serie, a 9600 baudios\r\n");
carlosgs 0:49f15045d34a 81 wait(1); // Espera 1 segundo
carlosgs 0:49f15045d34a 82 }
carlosgs 0:49f15045d34a 83
carlosgs 0:49f15045d34a 84 }
carlosgs 0:49f15045d34a 85
carlosgs 0:49f15045d34a 86 }