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

Dependencies:   PixelArray USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Programa de Hola mundo
00002 // Animación en la matriz de LEDs, recibe input del pulsador, y escribe por el puerto serie
00003 
00004 // Enlaces útiles:
00005 // http://hack-miniblip.github.io/programar.html
00006 // https://raw.githubusercontent.com/hack-miniblip/hack-miniblip.github.io/master/Scripts/miniblip_loader.sh
00007 // https://github.com/hack-miniblip/hack-miniblip.github.io/blob/master/cookbook.md
00008 // http://hack-miniblip.github.io/esquema_minilip_pinout.png
00009 
00010 
00011 #include "mbed.h"
00012 #include "neopixel.h"
00013 #include "USBSerial.h"
00014 
00015 // Matrix led output pin
00016 #define MATRIX_PIN P0_9
00017 #define NLEDS 25
00018 
00019 unsigned int counter = 0;   
00020 USBSerial serial;
00021 
00022 DigitalIn pushbutton(P0_23); //Define pushbutton
00023 
00024 neopixel::Pixel buffer[NLEDS];
00025 
00026 neopixel::PixelArray array(MATRIX_PIN);
00027 
00028 void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue) {
00029   if(x < 0 || x > 4 || y < 0 || y > 4) return;
00030   int posicion=x+y*5;
00031   buffer[posicion].red=red;
00032   buffer[posicion].green=green;
00033   buffer[posicion].blue=blue;
00034 }
00035 
00036 void updateLEDs() {
00037     array.update(buffer, NLEDS);
00038 }
00039 
00040 void clearPixels() {
00041     for(int x=0; x<5; x++)
00042         for(int y=0; y<5; y++)
00043             setPixel(x,y, 0,0,0);
00044 }
00045 
00046 
00047 PwmOut speaker(P0_8);
00048 
00049 int main()
00050 {
00051     // Apagar el zumbador
00052     speaker=0.0;
00053     
00054     // setPixel(x,y, r,g,b); // valor máximo RGB: 255
00055     
00056     // Poner todos los LED en verde gradualmente
00057     for(int x=0; x<5; x++) {
00058         for(int y=0; y<5; y++) {
00059             setPixel(x,y, 0,10,0);
00060             updateLEDs();
00061             wait_ms(100);
00062         }
00063     }
00064     
00065     clearPixels(); // Poner a 0 la matriz actual
00066     
00067     // Dibujar un smiley
00068     setPixel(4,1, 0,0,20); // ojos
00069     setPixel(4,3, 0,0,20);
00070     
00071     setPixel(1,0, 0,0,20); // boca
00072     setPixel(0,1, 0,0,20);
00073     setPixel(0,2, 0,0,20);
00074     setPixel(0,3, 0,0,20);
00075     setPixel(1,4, 0,0,20);
00076     
00077     while(1) {
00078         if (pushbutton) {
00079             updateLEDs(); // Se muestra el smiley
00080             serial.printf("Hola Mundo! Texto enviado por el puerto serie, a 9600 baudios\r\n");
00081             wait(1);  // Espera 1 segundo  
00082         }
00083         
00084     }
00085     
00086 }