Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "WProgram.h"

#define DEBUG               1
//#define   USE_SPI             1

// Définition des pins
#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

// Non modifié, voir mon post!
#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

// Modifié pour NTSC
#define _NB_TV_LIGNES       262

#define _COMPENS_BOUCLE     7
#define _COMPENS_IF_SERIAL  17

#define _BAUD_RATE          19200

// Prototypes
void SPI_MasterInit(void);
void clearScreen(boolean mode);
#ifdef DEBUG
    void blinkStatus(void);
#endif
void resetCursor(void);

// Variables definition
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;

#ifdef USE_SPI
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);
}
#endif

// Fonction d'effacement de l'écran (avec possibilité de faire un "cadriage" pour les tests)
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();
}

// Remet le curseur au milieux
void resetCursor(void)
{
    cx=(_NB_PIXELS / 2);
    cy=(_NB_LIGNES / 2);
}

#ifdef DEBUG
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);
    }
}
#endif

void setup()
{
    pinMode(PIN_SYNC, OUTPUT);
    pinMode(PIN_VIDEO, OUTPUT);
    pinMode(PIN_AUDIO, OUTPUT);
    
    digitalWrite(PIN_SYNC, HIGH);
    digitalWrite(PIN_VIDEO, HIGH);
    
#ifdef USE_SPI
    SPI_MasterInit();
#endif
    Serial.begin(_BAUD_RATE);
    Serial.println("GO");
    
    clearScreen(true);
#ifdef DEBUG
    blinkStatus();
#endif
}

void loop()
{
    // SYNC VERT A
    
    /**
     * Cette section je suis pas certain de comprendre
    **/
    
    // 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); // Modifier NTSC

        // Black
        PORTB = _BLACK;
        delayMicroseconds(4.7); // Modifier NTSC
        
        //** image ligne 51.5 uS
        
        for (index = 0; index < _NB_PIXELS; index++)
        {
            PORTB = memVideo[index][ligne>>4];
            PORTB = PORTB;  // Pas sur de comprendre ???
            PORTB = PORTB;  // Pas sur de comprendre ???
        }
        
        delayMicroseconds(1.4); // Modifier NTSC
        
        /**
         * Cette section je suis pas certain de comprendre
        **/
        PORTB = _BLACK;
        PORTB = _BLACK;
        PORTB = _BLACK;
        PORTB = _BLACK;
        PORTB = _BLACK;

        PORTB = _BLACK;
        PORTB = _BLACK;
        PORTB = _BLACK;
        PORTB = _BLACK;
    }

    // SYNC VERT B
    
    /**
     * Cette section je suis pas certain de comprendre
    **/
    
    // 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;       // Clear screen
            case 'f' : clearScreen(true); break;        // Fill screen
        }
    }
}