#include "WProgram.h"
void SPI_MasterInit(void);
void clearScreen(boolean mode);
void blinkStatus(void);
void resetCursor(void);
#define PIN_SYNC 8
#define PIN_VIDEO 9
#define PIN_LED 12
#define PIN_AUDIO 11
#define _SYNC 0x00
#define _BLACK 0x01
#define _GRAY 0x02
#define _WHITE 0x03
#define _LONG_SYNC 19
#define _SHORT_SYNC 2
#define _LONG_SYNC_DELAI 2
#define _SHORT_SYNC_DELAI 30
#define _NB_PIXELS 29
#define _NB_LIGNES 19
//#define _NB_TV_LIGNES 525
#define _NB_TV_LIGNES 262
#define _COMPENS_BOUCLE 7
#define _COMPENS_IF_SERIAL 17
#define _BAUD_RATE 19200
byte memVideo[_NB_PIXELS][_NB_LIGNES];
byte index, index2;
int ligne;
char c;
int cx=(_NB_PIXELS / 2), cy=(_NB_LIGNES / 2);
byte couleur = _BLACK;
void SPI_MasterInit(void)
{
/* Set MOSI and SCK output, all others input */
DDRD = (1<<DDB0)|(1<<DDB1);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
void clearScreen(boolean mode)
{
for (index2 = 0; index2 < _NB_LIGNES; index2++)
for (index = 0; index < _NB_PIXELS; index++)
if (!mode) {
memVideo[index][index2] = _BLACK;
} else {
memVideo[index][index2] = (index + index2) % 3 + 1;
}
memVideo[0][0] = _WHITE;
resetCursor();
}
void resetCursor(void)
{
cx=(_NB_PIXELS / 2);
cy=(_NB_LIGNES / 2);
}
void blinkStatus(void)
{
int ii;
pinMode(PIN_LED, OUTPUT);
for(ii=0;ii<3;ii++)
{
digitalWrite(PIN_LED, HIGH);
delay(75);
digitalWrite(PIN_LED, LOW);
delay(75);
digitalWrite(PIN_LED, HIGH);
if (ii < 2) delay(75);
}
}
void setup()
{
pinMode(PIN_SYNC, OUTPUT);
pinMode(PIN_VIDEO, OUTPUT);
pinMode(PIN_AUDIO, OUTPUT);
digitalWrite(PIN_SYNC, HIGH);
digitalWrite(PIN_VIDEO, HIGH);
//SPI_MasterInit();
Serial.begin(_BAUD_RATE);
Serial.println("GO");
clearScreen(true);
blinkStatus();
}
void loop()
{
// SYNC VERT A
// ligne 1 LONG SYNC
PORTB = _SYNC; delayMicroseconds(_LONG_SYNC);
PORTB = _BLACK; delayMicroseconds(_LONG_SYNC_DELAI);
PORTB = _SYNC; delayMicroseconds(_LONG_SYNC);
PORTB = _BLACK; delayMicroseconds(_LONG_SYNC_DELAI);
// ligne 2 LONG SYNC
PORTB = _SYNC; delayMicroseconds(_LONG_SYNC);
PORTB = _BLACK; delayMicroseconds(_LONG_SYNC_DELAI);
PORTB = _SYNC; delayMicroseconds(_LONG_SYNC);
PORTB = _BLACK; delayMicroseconds(_LONG_SYNC_DELAI);
// ligne 3 MIXTE SYNC
PORTB = _SYNC; delayMicroseconds(_LONG_SYNC);
PORTB = _BLACK; delayMicroseconds(_LONG_SYNC_DELAI);
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI);
// ligne 4 SHORT SYNC
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI);
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI);
// ligne 5 SHORT SYNC
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI);
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI-_COMPENS_BOUCLE);
// IMAGE
for (ligne = 0; ligne < _NB_TV_LIGNES; ligne++)
{
//** synchro
// HSync
PORTB = _SYNC;
delayMicroseconds(4.7);
// Black
PORTB = _BLACK;
delayMicroseconds(4.7);
//** image ligne 51.5 uS
for (index = 0; index < _NB_PIXELS; index++)
{
PORTB = memVideo[index][ligne>>4];
PORTB = PORTB;
PORTB = PORTB;
}
delayMicroseconds(1.4); // 4 uS
PORTB = _BLACK;
PORTB = _BLACK;
PORTB = _BLACK;
PORTB = _BLACK;
PORTB = _BLACK;
PORTB = _BLACK;
PORTB = _BLACK;
PORTB = _BLACK;
PORTB = _BLACK;
}
// SYNC VERT B
// ligne 310 LONG SYNC
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI);
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI);
// ligne 311 SHORT SYNC
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI);
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI);
// ligne 312 SHORT SYNC
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI);
PORTB = _SYNC; delayMicroseconds(_SHORT_SYNC);
PORTB = _BLACK; delayMicroseconds(_SHORT_SYNC_DELAI-_COMPENS_BOUCLE-_COMPENS_IF_SERIAL);
if (Serial.available())
{
c = Serial.read();
switch (c)
{
case '4' : if (cx>0) cx--; break;
case '6' : if (cx<29) cx++; break;
case '8' : if (cy>0) cy--; break;
case '2' : if (cy<18) cy++; break;
case '5' : memVideo[cx][cy] = couleur; break;
case 'n' : couleur = _BLACK; break;
case 'g' : couleur = _GRAY; break;
case 'b' : couleur = _WHITE; break;
case 'c' : clearScreen(false); break;
case 'f' : clearScreen(true); break;
}
}
}