Lab_interaccio/2011/Mediatic/display5x7_mod5/display5x7_mod5.pde
2025-02-25 21:29:42 +01:00

324 lines
11 KiB
Plaintext

#define BAUD 115200
//TLC5940NT pin definitions
#define VPRG 2
#define SIN 11
#define SCLK 13
#define XLAT 4
#define BLANK 5
#define DCPRG 6
#define GSCLK 7
#define intensidad 500 //0 - 4095
#define MISO 12 //No se usan
#define SS 10 //No se usan
const byte remap[7][5] = { //Mapeo de los leds
{6,17,24,35,42},
{5,16,23,34,41},
{4,11,22,33,40},
{3,10,21,32,39},
{2,9,20,27,38},
{1,8,19,26,37},
{0,7,18,25,36},
};
int modo= 26;
byte numero[10][7] = { { B01110, B10001, B10011, B10101, B11001, B10001, B01110}, //48 = '0'
{ B00100, B01100, B10100, B00100, B00100, B00100, B11111}, //49 = '1'
{ B01110, B10001, B00001, B00110, B01000, B10000, B11111}, //50 = '2'
{ B01110, B10001, B00001, B01110, B00001, B10001, B01110}, //51 = '3'
{ B00010, B00110, B01010, B10010, B11111, B00010, B00010}, //52 = '4'
{ B11111, B10000, B11110, B00001, B00001, B10001, B01110}, //53 = '5'
{ B01110, B10001, B10000, B11110, B10001, B10001, B01110}, //54 = '6'
{ B11111, B00001, B00010, B00100, B01000, B10000, B10000}, //55 = '7'
{ B01110, B10001, B10001, B01110, B10001, B10001, B01110}, //56 = '8'
{ B01110, B10001, B10001, B01111, B00001, B10001, B01110}, //57 = '9'
};
byte caracterA[13][7] = {{ B01110, B10001, B10001, B11111, B10001, B10001, B10001}, //65 = 'A'
{ B11110, B10001, B10001, B11110, B10001, B10001, B11110}, //66 = 'B'
{ B01110, B10001, B10000, B10000, B10000, B10001, B01110}, //67 = 'C'
{ B11110, B10001, B10001, B10001, B10001, B10001, B11110}, //68 = 'D'
{ B11111, B10000, B10000, B11110, B10000, B10000, B11111}, //69 = 'E'
{ B11111, B10000, B10000, B11110, B10000, B10000, B10000}, //70 = 'F'
{ B01110, B10001, B10000, B10111, B10001, B10001, B01110}, //71 = 'G'
{ B10001, B10001, B10001, B11111, B10001, B10001, B10001}, //72 = 'H'
{ B11111, B00100, B00100, B00100, B00100, B00100, B11111}, //73 = 'I'
{ B00001, B00001, B00001, B00001, B10001, B10001, B01110}, //74 = 'J'
{ B10001, B10010, B10100, B11000, B10100, B10010, B10001}, //75 = 'K'
{ B10000, B10000, B10000, B10000, B10000, B10000, B11111}, //76 = 'L'
{ B10001, B11011, B10101, B10001, B10001, B10001, B10001}, //77 = 'M'
};
byte caracterB[14][7] = {{ B10001, B11001, B10101, B10011, B10001, B10001, B10001}, //78 = 'N'
{ B01110, B10001, B10001, B10001, B10001, B10001, B01110}, //79 = 'O'
{ B11110, B10001, B10001, B11110, B10000, B10000, B10000}, //80 = 'P'
{ B01110, B10001, B10001, B10001, B10101, B10101, B01110}, //81 = 'Q'
{ B11110, B10001, B10001, B11110, B10100, B10010, B10001}, //82 = 'R'
{ B01110, B10001, B10000, B01110, B00001, B10001, B01110}, //83 = 'S'
{ B11111, B00100, B00100, B00100, B00100, B00100, B00100}, //84 = 'T'
{ B10001, B10001, B10001, B10001, B10001, B10001, B01110}, //85 = 'U'
{ B10001, B10001, B10001, B10001, B10001, B01010, B00100}, //86 = 'V'
{ B10001, B10001, B10001, B10001, B10101, B10101, B01010}, //87 = 'W'
{ B10001, B10001, B01010, B00100, B01010, B10001, B10001}, //88 = 'X'
{ B10001, B10001, B01010, B00100, B00100, B00100, B00100}, //89 = 'Y'
{ B11111, B00001, B00010, B00100, B01000, B10000, B11111}, //90 = 'Z'
{ B10000, B10000, B10000, B10010, B01010, B00110, B11110}, //flecha = 35
};
byte espacio[7] = { B00000, B00000, B00000, B00000, B00000, B00000, B00000}; //32 = ' '
byte sonrisa[7] = { B00000, B01110, B10001, B00000, B01010, B01010, B00000}; //32 = ' '
// Holds the current colour level for each of the buttons
int levelW[64] = {
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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Variables for interpreting the serial commands
int command = 32;
byte command_ant = 32;
byte finish = 1;
byte count = 0;
// For interrupt timing; needed only to do intermediate clock speeds
/* Divide interrupt frequency by a factor of FREQ. It is preferable to keep
FREQ as small as possible, and control the frequency of the interrupts
using the hardware clock. Setting it to 1 disables this entirely, which,
if it works, is ideal; this should be the same as commenting out the
"#define FREQ" statement entirely.
*/
#define FREQ 100 // How many interrupts occur before the serial commands are read
#if FREQ > 1
byte int_counter = 0;
#endif
// Transfer a character out over hardware SPI
char spi_transfer(volatile byte data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte
}
// Run this animation once at startup. Currently unfinished.
void startup(){
for(byte x = 0; x < 7; ++x){
for(byte y = 0; y <5; ++y){
//levelR[remap[x][y]] = 4095;
levelW[remap[x][y]] = intensidad;
for(int i = 0; i <50; ++i) makemagic();
}
}
}
ISR(TIMER2_OVF_vect) {
sei(); //Reenable global interrupts, otherwise serial commands will get dropped
#if FREQ > 1
if(++int_counter == FREQ){ // Only do this once every FREQ-th interrupt
int_counter = 0;
#endif //FREQ
if(Serial.available()){
int val = Serial.read();
if ((val==13)||(val==10)) //INTRO
{
if(val!=10)
{
Serial.print(val,BYTE);
count=0;
finish=0;
modo=8;
}
}
else if (count==1) Serial.print(val,BYTE); //tunel
else
{
command=val;
if (command==32) modo=1; //Espacio
else if ((command<58)&&(command>=48)) modo=2; //Numeros
else if ((command<78)&&(command>=65)) modo=3; //Letras de la 'A' a la 'M'
else if ((command<92)&&(command>=78)) modo=5; //Letras de la 'N' a la 'Z' y la flecha
else if ((command<110)&&(command>=97)) modo=4; //Letras de la 'a' a 'm'
else if ((command<123)&&(command>=110)) modo=6; //Letras de la 'n' a la 'z'
else modo=7;
count=1;
}
}
// If the serial buffer is getting too close to full, keep executing the parsing until it falls below a given level
// This might cause flicker, or even dropped messages, but it should prevent a crash.
#if FREQ > 1
}
#endif //FREQ
}
void setup() {
//Setup data directions, and set everything to the correct initial levels,
// For TLC5940
pinMode(VPRG, OUTPUT);
pinMode(SIN, OUTPUT);
pinMode(SCLK, OUTPUT);
pinMode(XLAT, OUTPUT);
pinMode(BLANK, OUTPUT);
pinMode(DCPRG, OUTPUT);
pinMode(GSCLK, OUTPUT);
pinMode(MISO, INPUT);
pinMode(SS,OUTPUT);
digitalWrite(SS,HIGH); //disable device
digitalWrite(SIN, LOW);
digitalWrite(SCLK, LOW);
digitalWrite(XLAT, LOW);
digitalWrite(VPRG, LOW);
digitalWrite(BLANK, HIGH);
digitalWrite(GSCLK, HIGH);
digitalWrite(DCPRG, LOW); // USE EEPROM DC register if LOW
byte gira_prov[7] = { B00000, B00000, B00000, B00000, B00000, B00000, B00000};
for(int j = 0; j<10; j++){
for(int i = 0; i<7; i++){
gira_prov[i]=numero[j][6-i];
}
for(int i = 0; i<7; i++){
numero[j][i]=gira_prov[i];
}
}
for(int j = 0; j<13; j++){
for(int i = 0; i<7; i++){
gira_prov[i]=caracterA[j][6-i];
}
for(int i = 0; i<7; i++){
caracterA[j][i]=gira_prov[i];
}
}
for(int j = 0; j<14; j++){
for(int i = 0; i<7; i++){
gira_prov[i]=caracterB[j][6-i];
}
for(int i = 0; i<7; i++){
caracterB[j][i]=gira_prov[i];
}
}
//Setup the Hardware SPI registers
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 (fastest)
byte clr;
SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
delay(10);
// Start the serial port
Serial.begin(BAUD);
delay(10);
TCCR2A = 0;
TCCR2B = 0<<CS22 | 1<<CS21 | 1<<CS20;
//Timer2 Overflow Interrupt Enable
TIMSK2 = 1<<TOIE2;
delay(10);
Serial.print(13,BYTE);
// Run the startup animation
//startup();
}
void loop () {
switch (modo) {
case 1: if (finish) {letra(espacio,espacio); modo=0;} break;
case 2: if (finish) {letra(numero[command-48],espacio); modo=0;} break;
case 3: if (finish) {letra(caracterA[command-65],espacio); modo=0;} break;
case 4: if (finish) {letra(caracterA[command-97],espacio); modo=0;} break;
case 5: if (finish) {letra(caracterB[command-78],espacio); modo=0;} break;
case 6: if (finish) {letra(caracterB[command-110],espacio); modo=0;} break;
case 7: if (finish) {letra(sonrisa,espacio); modo=0;} break;
case 8: letra(espacio,espacio); if (modo==8) modo=0; finish=1; break;
}
makemagic();
}
void makemagic(){
setGreysW();
feedPorts();
}
void setGreysW() {
digitalWrite(BLANK, HIGH);
digitalWrite(XLAT,LOW);
for(int i = 21; i>=0; i--){
spi_transfer( (levelW[2*i+1] & 0x0FF0) >> 4 );
spi_transfer( ((levelW[2*i+1] & 0xF) << 4) | ((levelW[2*i] & 0x0F00) >> 8) );
spi_transfer( levelW[2*i] & 0xFF);
}
digitalWrite(XLAT,HIGH);
digitalWrite(XLAT,LOW);
digitalWrite(BLANK, LOW);
}
void feedPorts() {
// Clock for TLC5940's PWM
digitalWrite(BLANK, HIGH);
digitalWrite(BLANK, LOW); //=all outputs ON, start PWM cycle
for (int i=0; i<4096; i++) {
pulseGSCLK();
}
}
void pulseGSCLK() {
//ultra fast pulse trick, using digitalWrite caused flickering
PORTD |= 0x80 ; // bring pin 7 high, but don't touch any of the other pins in PORTB
//16 nanosecs is the min pulse width for the 5940, but no pause seems needed here
PORTD &= 0x7F; // bring pin 7 low without touching the other pins in PORTB
}
byte letra(byte let[7], byte let_ant[7]) {
byte letbool=0;
byte let_new[7] = { B00000, B00000, B00000, B00000, B00000, B00000, B00000};
for(byte j = 1; j <6; ++j){
//let_new[1]=((let_ant[1]<<j)|(let[1]>>(5-j)));
for(byte h = 0; h <7; ++h){
let_new[h]=((0X1F&(let_ant[h]<<j))|(let[h]>>(5-j)));
/*Serial.print("Display actual ");
Serial.print(let[h]+0x20,BIN);
Serial.print(' ');
Serial.print("Display anterior ");
Serial.print(let_ant[h]+0x20,BIN);
Serial.print(' ');
Serial.print("Visualiza ");
Serial.println(let_new[h]+0x20,BIN);*/
}
letbool=0;
for(byte x = 0; x < 7; ++x){
for(byte y = 0; y <5; ++y){
//if (0x10&(let_new[letbool]<<y)) levelW[remap[x][y]] = intensidad;
if (0x01&let_new[letbool]>>y) levelW[remap[x][y]] = intensidad;
else levelW[remap[x][y]] = 0;
}
++letbool;
}
for(int i = 0; i <20; ++i) makemagic();
}
}