miniBLIP - paint on matrix

Dependencies:   USBDevice mbed

Fork of blip_dado by Alberto Piganti

main.cpp

Committer:
pighixxx
Date:
2015-12-01
Revision:
6:cacc8514502c
Parent:
5:03859160e3a9
Child:
7:5b8708227c46

File content as of revision 6:cacc8514502c:

// miniblip led dado demo

#include "mbed.h"
#include "neopixel.h"

// Matrix led output pin
#define DATA_PIN P0_9

#define ANALOG_PHOTO P0_16
#define ANALOG_POTENTIOMETER P0_22
#define ANALOG_BUZZER P0_8
#define DIGITAL_CIRCLE P0_12

AnalogIn   ain(ANALOG_POTENTIOMETER);

void fill_pixel(neopixel::Pixel buffer[25], int x, int y, int red, int green, int blue){
    
    if(x<0) x=0;
    if(x>4) x=4;
    if(y<0) y=0;
    if(y>4) y=4;
       

    int posicion=x+y*5;
    buffer[posicion].red=red;
    buffer[posicion].green=green;
    buffer[posicion].blue=blue;
}

void void_matrix(neopixel::Pixel aux[25], int tam=25){
    
    for(int i=0;i<tam;i++){
        aux[i].red=0;
        aux[i].green=0;
        aux[i].blue=0;
    }
}

int pant[5][5] = {
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
};

int pantR[5][5] = {
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
};

int pantG[5][5] = {
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
};

int pantB[5][5] = {
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}
};



void drawVector(int theArray[5][5], neopixel::Pixel * vectorPixel, int r, int g, int b){
    for(int i = 0;i<5;i++){
        for(int j = 0; j<5;j++){
            if(theArray[i][j] == 1)
                fill_pixel(vectorPixel,i,j,r,g,b);
            else
                fill_pixel(vectorPixel,i,j,0,0,0); 
        }
    }
}
int main()
{
    // Turn off miniblip buzzer
    PwmOut speaker(P0_8);
    speaker=0.0;
    // Create a temporary DigitalIn so we can configure the pull-down resistor.
    DigitalIn(DATA_PIN, PullDown);
    
    // Pushbutton
    DigitalIn pushbutton(P0_23);
    
    neopixel::Pixel vector[25];
    void_matrix(vector);

    // The pixel array control class.
    neopixel::PixelArray array(DATA_PIN);
    
    bool pixState=0;
    int pixCount=0;
    int OldPos = ain.read() * 26.0f;
    
    int color=0;
    
    while (1) {
        // Read Pot
        int posLed = ain.read() * 26.0f;
        
        int posx=posLed%5;
        int posy=int(posLed/5);
        if (pixState) fill_pixel(vector,posx,posy,30,30,30); else {
            int posx=posLed%5;
            int posy=int(posLed/5);
            int cred=pantR[posx][posy];
            int cblu=pantG[posx][posy];
            int cgreen=pantB[posx][posy];
            fill_pixel(vector,posx,posy,cred,cblu,cgreen);
        }
        if (posLed!=OldPos) {
            // Delete old pos
            int posx=OldPos%5;
            int posy=int(OldPos/5);
            int cred=pantR[posx][posy];
            int cblu=pantG[posx][posy];
            int cgreen=pantB[posx][posy];
            fill_pixel(vector,posx,posy,cred,cblu,cgreen);
            OldPos=posLed;
        }
            
        pixCount++;
        if (pixCount>8) {
            pixCount=0;
            pixState^= 1;
        }
        array.update(vector, 64);
        wait_ms(30); 
       
        
        // Wait button
        if(pushbutton){
            //Check
            int posx=posLed%5;
            int posy=int(posLed/5);
            int cred=pantR[posx][posy];
            int cblu=pantG[posx][posy];
            int cgreen=pantB[posx][posy];
            if (cred==0&&cblu==0&cgreen==0) color=30; else color=0;
            pantR[posx][posy]=color;
            pantG[posx][posy]=0;
            pantB[posx][posy]=0;
            //fill_pixel(vector,posx,posy,cred,cblu,cgreen);
            wait_ms(50);
            while (pushbutton) {wait_ms(10);}
        }
    }
}