/
lcd.c
executable file
·378 lines (335 loc) · 11.4 KB
/
lcd.c
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
Sample task that initialises the EA QVGA LCD display
with touch screen controller and processes touch screen
interrupt events.
Jonathan Dukes (jdukes@scss.tcd.ie)
*/
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "lcd.h"
#include "lcd_hw.h"
#include "lcd_grph.h"
#include <string.h>
#include <stdio.h>
#include "globals.h"
/* Maximum task stack size */
#define lcdSTACK_SIZE ( ( unsigned portBASE_TYPE ) 256 )
/* The LCD task. */
static void vLcdTask( void *pvParameters );
/* Interrupt handlers */
extern void vLCD_ISREntry( void );
void vLCD_ISRHandler( void );
/* Semaphore for ISR/task synchronisation */
xSemaphoreHandle xLcdSemphr;
int k, l;
void vStartLcd( unsigned portBASE_TYPE uxPriority )
{
vSemaphoreCreateBinary(xLcdSemphr);
/* Spawn the console task. */
xTaskCreate( vLcdTask, ( signed char * ) "Lcd", lcdSTACK_SIZE, NULL,
uxPriority, ( xTaskHandle * ) NULL );
}
/*** EDIT SETUP **/
// Renders the screen to the correct layout for edit mode using the current sequence[] and final_timing[] arrays.
void setupScreen(int step_width, int left, int bottom, int right, int top)
{
right = step_width;
step_width = 320/sequence_steps;
// Filling the full screen will cause a flicker if this function is over used. Therefore, used as a setup function for changing modes.
lcd_fillScreen(LIGHT_GRAY);
// Iterate one row at a time and set cell colours and grid lines.
for(k=0; k<numNotes; k++){
for(l=0; l<sequence_steps; l++) {
lcd_drawRect(bottom, left, top, right, DARK_GRAY);
if(k == sequence[l]){
// If a note is found in this row. Setup the relative cell with the corresponding note size colour value.
if(final_timing[l] == 1){
lcd_fillRect(bottom + 4, left + 4, top - 4, right - 4, MAROON);
}
else if(final_timing[l] == 2){
lcd_fillRect(bottom + 4, left + 4, top - 4, right - 4, OLIVE);
}
else if(final_timing[l] == 4){
lcd_fillRect(bottom + 4, left + 4, top - 4, right - 4, PURPLE);
}
}
left = left + step_width;
right = right + step_width;
}
left = 0;
right = step_width;
top = top + (240/12);
bottom = bottom + (240/12);
}
}
/*** PLAYBACK SETUP **/
// Renders the screen to the correct layout for playback mode using the current sequence[], currentlySelectedSequence variable and final_timing[] array.
void setupPlaybackMode () {
// Filling the screen as this function is used as a setup function when changing modes.
lcd_fillScreen(LIGHT_GRAY);
lcd_fillRect(180, 0, 240, 320, DARK_GRAY);
// Visually showing the sequence currently in use.
for(k=0; k<9; k++) {
if(currentlySelectedSequence == k) {
lcd_fillRect(185, (35*k) + 5, 235, (35*k) + 35, MAGENTA);
}
else {
lcd_fillRect(185, (35*k) + 5, 235, (35*k) + 35, LIGHT_GRAY);
}
}
// Setting up the graphic bars for the sequence in use.
displayNoteBars();
// Setup BPM visual display.
updateBPMVisuals();
// Setup octave visual display.
updateOctaveVisuals();
}
void displayNoteBars () {
// Setting up the graphic bars for the sequence in use.
for(k=0; k<sequence_steps; k++) {
if(sequence[k] < 12){
if(final_timing[k]==1){
lcd_fillRect(0, (k*31)+5, ((sequence[k]+1)*11), (k*31)+31, MAROON);
}
else if(final_timing[k]==2){
lcd_fillRect(0, (k*31)+5, ((sequence[k]+1)*11), (k*31)+31, OLIVE);
}
else if(final_timing[k]==4){
lcd_fillRect(0, (k*31)+5, ((sequence[k]+1)*11), (k*31)+31, PURPLE);
}
}
}
}
void updateBPMVisuals () {
// Setup BPM visual display.
if(t3speed == 12000000) {
lcd_fillRect(160, 5, 170, 30, DARK_CYAN);
lcd_fillRect(160, 31, 170, 136, LIGHT_GRAY);
}
else if(t3speed == 6000000) {
lcd_fillRect(160, 5, 170, 30, DARK_CYAN);
lcd_fillRect(160, 35, 170, 65, DARK_CYAN);
lcd_fillRect(160, 66, 170, 136, LIGHT_GRAY);
}
else if(t3speed == 3000000) {
lcd_fillRect(160, 5, 170, 30, DARK_CYAN);
lcd_fillRect(160, 35, 170, 65, DARK_CYAN);
lcd_fillRect(160, 70, 170, 100, DARK_CYAN);
lcd_fillRect(160, 101, 170, 136, LIGHT_GRAY);
}
else if(t3speed == 1500000) {
lcd_fillRect(160, 5, 170, 30, DARK_CYAN);
lcd_fillRect(160, 35, 170, 65, DARK_CYAN);
lcd_fillRect(160, 70, 170, 100, DARK_CYAN);
lcd_fillRect(160, 105, 170, 135, DARK_CYAN);
}
}
void updateOctaveVisuals () {
// Setup BPM visual display.
if(octive == 1) {
lcd_fillRect(140, 5, 150, 30, CYAN);
lcd_fillRect(140, 35, 150, 65, DARK_GRAY);
}
else if(octive == 2) {
lcd_fillRect(140, 5, 150, 30, DARK_GRAY);
lcd_fillRect(140, 35, 150, 65, CYAN);
}
}
static portTASK_FUNCTION( vLcdTask, pvParameters )
{
unsigned int pressure;
unsigned int xPos;
unsigned int yPos;
//portTickType xLastWakeTime;
int step_width = 320/sequence_steps;
int step_height = 240/numNotes;
int left, bottom = 0;
int right = step_width;
int top = step_height;
int i, n, x;
int waiting = 0;
int newlySelectedSequence = 0;
int prevMode = 0;
int prevSeq = 0;
/* Just to stop compiler warnings. */
( void ) pvParameters;
/* Initialise LCD display */
/* NOTE: We needed to delay calling lcd_init() until here because it uses
* xTaskDelay to implement a delay and, as a result, can only be called from
* a task */
lcd_init();
lcd_fillScreen(LIGHT_GRAY);
/* Initial LCD display */
setupScreen(step_width,left, bottom,right,top);
/* Infinite loop blocks waiting for a touch screen interrupt event from
* the queue. */
while(1)
{
/* Clear TS interrupts (EINT3) */
/* Reset and (re-)enable TS interrupts on EINT3 */
EXTINT = 8; /* Reset EINT3 */
/* Enable TS interrupt vector (VIC) (vector 17) */
VICIntEnable = 1 << 17; /* Enable interrupts on vector 17 */
/* Block on a queue waiting for an event from the TS interrupt handler */
xSemaphoreTake(xLcdSemphr, portMAX_DELAY);
/* Disable TS interrupt vector (VIC) (vector 17) */
VICIntEnClr = 1 << 17;
/* Measure next sleep interval from this point */
//xLastWakeTime = xTaskGetTickCount();
// When switching sequences in edit mode.
if(mode == 2){
setupScreen(step_width,0, 0,step_width,step_height);
mode = 0;
}
// Switching from playback mode to edit mode, screen must be setup.
if((prevMode == 1) && (mode == 0)){
setupScreen(step_width,0, 0,step_width,step_height);
}
// Switching from edit mode to playback mode.
else if((prevMode == 0) && (mode == 1)) {
setupPlaybackMode();
}
prevMode = mode;
/*** EDIT MODE ***/
/*****************/
if(mode != 1) {
/* Start polling the touchscreen pressure and position ( getTouch(...) ) */
/* Keep polling until pressure == 0 */
getTouch(&xPos, &yPos, &pressure);
while (pressure > 0)
{
// Button debounce on screen touch.
waiting = 0;
while(waiting<100000) {
waiting++;
}
// Iterating through each row.
for(i=0; i<numNotes; i++) {
if((xPos < top) && (xPos > bottom)) {
// If touch within bounds of row i, check for the corresponding column.
for(n=0; n<sequence_steps; n++) {
if((yPos < right) && (yPos > left)){
// Render column cells to clear previously selected cell and set up newly selected or change previously selected note time.
for(x=0; x<numNotes;x++){
lcd_fillRect((x*step_height), (n*step_width), (x*step_height)+step_height, ((n*step_width)+step_width), LIGHT_GRAY);
lcd_drawRect((x*step_height), (n*step_width), (x*step_height)+step_height, ((n*step_width)+step_width), DARK_GRAY);
if(x != i){
// Majority of cells will have no note.
timing[x][n] = 0;
}
}
if(timing[i][n]==0 || timing[i][n]==1){
timing[i][n]++;
}
else {
timing[i][n] = timing[i][n] + 2;
}
if (timing[i][n] > 4){
timing[i][n] = 0;
}
final_timing[n] = timing[i][n];
if(timing[i][n] == 1){
// Full note.
lcd_fillRect(bottom + 4, left + 4, top - 4, right - 4, MAROON);
}
else if(timing[i][n] == 2){
// Half note.
lcd_fillRect(bottom + 4, left + 4, top - 4, right - 4, OLIVE);
}
else if(timing[i][n] == 4){
// Quarter note.
lcd_fillRect(bottom + 4, left + 4, top - 4, right - 4, PURPLE);
}
sequence[n] = i;
}
left = left + step_width;
right = right + step_width;
}
}
left = 0;
right = step_width;
top = top + step_height;
bottom = bottom + step_height;
}
step_width = 320/sequence_steps;
bottom = 0;
left = 0;
right = step_width;
top = step_height;
getTouch(&xPos, &yPos, &pressure);
}
}
/*** PLAYBACK MODE ***/
/*********************/
else {
/* Start polling the touchscreen pressure and position ( getTouch(...) ) */
/* Keep polling until pressure == 0 */
getTouch(&xPos, &yPos, &pressure);
while (pressure > 0)
{
// Bound touch area for more efficiency.
if((xPos < 235) && (xPos > 185)){
// Select current sequence.
for(i=0; i<9; i++) {
if(((xPos < 235) && (xPos > 185)) && ((yPos < (35*i) + 35) && (yPos > (35*i) + 5))){
// If a new sequence selected, must save the one that's currently in use to the listOfSequences[][] and listOfTimingSequences[][]
// and then reinitialise the current sequence[] to the relative one in listOfSequences[][] and listOfTimingSequences[][].
newlySelectedSequence = i;
if(newlySelectedSequence != currentlySelectedSequence) {
lcd_fillRect(185, (35*currentlySelectedSequence) + 5, 235, (35*currentlySelectedSequence) + 35, LIGHT_GRAY);
lcd_fillRect(185, (35*newlySelectedSequence) + 5, 235, (35*newlySelectedSequence) + 35, MAGENTA);
for(n=0;n<sequence_steps;n++){
listOfSequences[currentlySelectedSequence][n] = sequence[n];
listOfTimingSequences[currentlySelectedSequence][n] = final_timing[n];
}
currentlySelectedSequence = newlySelectedSequence;
for(n=0;n<sequence_steps;n++){
sequence[n] = listOfSequences[currentlySelectedSequence][n];
final_timing[n] = listOfTimingSequences[currentlySelectedSequence][n];
}
}
}
}
}
getTouch(&xPos, &yPos, &pressure);
}
// Prevent flicker.
if(prevSeq != currentlySelectedSequence){
// Only fill part of screen to reduce flicker.
lcd_fillRect(0, 0, 175, 320, LIGHT_GRAY);
// Setup note bars for current sequence[] and final_timing[].
for(i=0; i<sequence_steps; i++) {
if(sequence[i] != numNotes) {
if(final_timing[i]==1){
lcd_fillRect(0, (i*31)+5, ((sequence[i]+1)*11), (i*31)+31, MAROON);
}
else if(final_timing[i]==2){
lcd_fillRect(0, (i*31)+5, ((sequence[i]+1)*11), (i*31)+31, OLIVE);
}
else if(final_timing[i]==4){
lcd_fillRect(0, (i*31)+5, ((sequence[i]+1)*11), (i*31)+31, PURPLE);
}
}
}
// Setup BPM visual display.
updateBPMVisuals();
// Setup octave visual display.
updateOctaveVisuals();
}
prevSeq = currentlySelectedSequence;
}
}
}
void vLCD_ISRHandler( void )
{
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
/* Process the touchscreen interrupt */
/* We would want to indicate to the task above that an event has occurred */
xSemaphoreGiveFromISR(xLcdSemphr, &xHigherPriorityTaskWoken);
EXTINT = 8; /* Reset EINT3 */
VICVectAddr = 0; /* Clear VIC interrupt */
/* Exit the ISR. If a task was woken by either a character being received
or transmitted then a context switch will occur. */
portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );
}