ff0493c339b59e3439f93c0fe55d82e489fbd565
[Arduino] / c2_ulx3s_test / c2_ulx3s_test.ino
1 #include <Compositing.h>
2 #include "font.h"
3 #include "rtc.h"
4 #include "adc.h"
5 #include "dac.h"
6 #include "edid.h"
7 #include "btn.h"
8 #include "flash.h"
9 #include "sd.h"
10 #include "oled.h"
11 #include "ram.h"
12 #include "rds.h"
13 #include "pcm.h"
14
15 #define N_LETTERS (sizeof(Font)/sizeof(Font[0]))
16
17 Compositing c2;
18
19 const int Cols = 40;
20 const int Rows = 30;
21 int Cursor_col = 0, Cursor_row = 0;
22
23 void cls()
24 {
25   for(int row = 0; row < Rows; row++)
26     for(int col = 0; col < Cols; col++)
27     {
28       int i = N_LETTERS + row*Cols + col;
29       c2.sprite_link_content('@'-'@', i);
30     }
31   Cursor_col = Cursor_row = 0;
32 }
33
34 void prints(char *a)
35 {
36   for(;*a != '\0'; a++)
37   {
38     int i = N_LETTERS + Cursor_row*Cols + Cursor_col;
39     uint8_t letter = 0; // space is default
40     char c = *a;
41     if(c == '\n')
42     {
43       Cursor_col = 0;
44       Cursor_row = (Cursor_row + 1) % Rows;
45       continue;
46     }
47     c = toupper(c);
48     if(c >= ' ' && c <= '_')
49       letter = c - ' ';
50     c2.sprite_link_content(letter, i);
51     if(++Cursor_col >= Cols)
52     {
53       Cursor_col = 0;
54       Cursor_row = (Cursor_row + 1) % Rows;
55     }
56   }  
57 }
58
59 // initialize compositing2 text using sprites
60 void video_init()
61 {
62   int i;
63   int unique_sprites;
64   c2.init();
65   c2.alloc_sprites(N_LETTERS+Rows*Cols); // it sets c2.sprite_max
66   *c2.videobase_reg = NULL; // disable video during update
67   *c2.cntrl_reg = 0;
68   #if 1
69     for(i = 0; i < c2.sprite_max && i < N_LETTERS; i++)
70       c2.shape_to_sprite(&(Font[i]));
71     unique_sprites = c2.n_sprites;
72     // unique sprites containing alphabet are not displayed, set them off-screen
73     for(i = 0; i < unique_sprites; i++)
74     {
75       c2.Sprite[i]->x = -100;
76       c2.Sprite[i]->y = -100;
77     }
78     // position cloned sprites on display
79     // i = N_LETTERS;
80     for(int row = 0; row < Rows; row++)
81       for(int col = 0; col < Cols; col++)
82     {
83       c2.sprite_clone(0); // by default clone 0-sprite which is space
84       int n = N_LETTERS + row*Cols + col;
85       c2.Sprite[n]->x = col*16;
86       c2.Sprite[n]->y = row*16;
87       i++;
88     }
89   #endif
90   // cls();
91   c2.sprite_refresh();
92   // this is needed for vgatext
93   // to disable textmode and enable bitmap
94   *c2.cntrl_reg = 0b11000000; // enable video, yes bitmap, no text mode, no cursor
95 }
96
97 void setup()
98 {
99   video_init();
100   rtc_init();
101   adc_init();
102   dac_init();
103   btn_init();
104   oled_init();
105   rds_init();
106   pcm_init();
107 }
108
109 void loop()
110 {
111   static uint8_t counter = 0;
112   const int nlines = 6;
113   char line[nlines][256];
114   if(1 & ++counter)
115     pcm_tone();
116   else
117     pcm_mute();
118   rtc_read(line[0]);
119   edid_read(line[1]);
120   adc_read(line[2]);
121   dac_read(line[3]);
122   btn_read(line[4]); // buttons. DIP switches and blink LEDs
123   if(line[4][5] == '1') // BTN1 pressed
124   {
125     rtc_set_clock();
126     rtc_set_alarm();
127     oled_init();
128   }
129   if(line[4][6] == '2') // BTN2 pressed - shutdown
130   {
131     volatile uint32_t *simple_out = (uint32_t *)0xFFFFFF10;
132     simple_out[0] |= (1<<13); // bit 13 of simple_out is shutdown
133   }
134   char flash_str[64], sd_str[64], oled_str[64];
135   flash_read(flash_str);
136   sd_read(sd_str); // esp32 must be flashed not to access SD card
137   oled_read(oled_str);
138   sprintf(line[5], "%s %s   %s\n", flash_str, oled_str, sd_str);
139   line[6][0]='\0';
140   //ram_test(line[5]); // works but too slow, need speedup
141   Serial.println("");
142   for(int i = 0; i < nlines; i++)
143     Serial.print(line[i]);
144   cls();
145   for(int i = 0; i < nlines; i++)
146     prints(line[i]);
147   while((*c2.vblank_reg & 0x80) == 0);
148   c2.sprite_refresh();
149   // delay(1000);
150 }