fade86c024d2b398f97242fcec1545fa9d7785d4
[des2015.git] / mart / ex11 / ex11.c
1 #include <stdio.h>
2 #include <signal.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5 #include <sys/io.h>
6
7 #include <native/task.h>
8 #include <native/timer.h>
9 #include <native/intr.h>
10
11 #include <rtdk.h>
12
13 #define LPT1IRQ 7
14 #define TICKS 5
15 #define BANNERWIDTH 10000
16 #define MSGMAX 1000
17 #define MAXX 20
18
19 RT_TASK intrtask, drawtask;
20 RTIME ticks[TICKS+1];
21 RTIME tocks[TICKS+1];
22
23 unsigned char data[BANNERWIDTH];
24 char message[MSGMAX];
25 char inputmsg[MSGMAX];
26 unsigned int pointer = 0;
27 unsigned int msglen;
28
29 void add_tick(RTIME *t, RTIME tnew)
30 {
31 unsigned int i = 0;
32 t[TICKS] = 0;
33 for(i=TICKS-1; i>0; i--){
34 t[i] = t[i-1];
35 t[TICKS] += t[i];
36 }
37 t[0] = tnew;
38 t[TICKS] = (t[TICKS]+t[0])/TICKS;
39 }
40
41 void taskd(void *arg)
42 {
43 RTIME time = *(RTIME *)arg;
44 RTIME step = time/(2*MAXX+2);
45 unsigned int i;
46
47 //To the right
48 outb(0x00, 0x378);
49 rt_task_sleep(step*2);
50 for(i=0; i<MAXX; i++){
51 outb(data[i+pointer], 0x378);
52 rt_task_sleep(step);
53 outb(0x00, 0x378);
54 }
55 rt_task_sleep(step);
56 outb(0x00, 0x378);
57
58 pointer = pointer > msglen ? 0 : pointer+1;
59 }
60
61 void taski(void *arg)
62 {
63 //Register as interrupt handler
64 RT_INTR intr;
65 rt_intr_create(&intr, NULL, LPT1IRQ, 0);
66 //rt_printf("Interrupt created\n");
67
68 //Initialize and declare the timer variables
69 RTIME tick = rt_timer_read();
70 RTIME tock;
71 RTIME period;
72 while(1){
73 tock = rt_timer_read();
74 add_tick(ticks, tock-tick);
75
76 // Tocks waiting period
77 if(tocks[TICKS] < ticks[TICKS]){
78 // sleep until the arm is most left
79 rt_task_sleep(tocks[TICKS]/2);
80 rt_task_create(&drawtask, NULL, 0, 50, 0);
81 period = ticks[TICKS]+tocks[TICKS];
82 rt_task_start(&drawtask, &taskd, &period);
83 //rt_printf("tocks is the short stretch\n");
84 }
85 rt_intr_wait(&intr, TM_INFINITE);
86
87 //Tick waiting period
88 tick = rt_timer_read();
89 add_tick(tocks, tick-tock);
90
91 if(ticks[TICKS] < tocks[TICKS]){
92 // sleep until the arm is most left
93 rt_task_sleep(ticks[TICKS]/2);
94 rt_task_create(&drawtask, NULL, 0, 50, 0);
95 period = ticks[TICKS]+tocks[TICKS];
96 rt_task_start(&drawtask, &taskd, &period);
97 //rt_printf("ticks is the short stretch\n");
98 }
99 rt_intr_wait(&intr, TM_INFINITE);
100 //Print average
101 //rt_printf("avgtick: %lluns, avgtock: %lluns, avgticktock: %lluns\n",
102 // ticks[TICKS], tocks[TICKS], (ticks[TICKS]+tocks[TICKS])/2);
103 }
104 }
105
106 void make_message(char *msg)
107 {
108 unsigned int i=20;
109 unsigned char s = 0;
110 while(i<BANNERWIDTH && s < strlen(msg)){
111 char c = msg[s];
112 switch(c){
113 case 'A':
114 case 'a':
115 data[i++] = 0x1e; data[i++] = 0x5; data[i++] = 0x1e; break;
116 case 'B':
117 case 'b':
118 data[i++] = 0x1f; data[i++] = 0x15; data[i++] = 0xe; break;
119 case 'C':
120 case 'c':
121 data[i++] = 0xe; data[i++] = 0x11; break;
122 case 'D':
123 case 'd':
124 data[i++] = 0x1f; data[i++] = 0x11; data[i++] = 0xe; break;
125 case 'E':
126 case 'e':
127 data[i++] = 0x1f; data[i++] = 0x15; break;
128 case 'F':
129 case 'f':
130 data[i++] = 0x1f; data[i++] = 0x05; break;
131 case 'G':
132 case 'g':
133 data[i++] = 0xe; data[i++] = 0x11, data[i++] = 0x1d; break;
134 case 'H':
135 case 'h':
136 data[i++] = 0x1f; data[i++] = 0x4, data[i++] = 0x1f; break;
137 case 'I':
138 case 'i':
139 data[i++] = 0x1f; break;
140 case 'J':
141 case 'j':
142 data[i++] = 0x10; data[i++] = 0x1f; break;
143 case 'K':
144 case 'k':
145 data[i++] = 0x1f; data[i++] = 0x04; data[i++] = 0x1b; break;
146 case 'L':
147 case 'l':
148 data[i++] = 0x1f; data[i++] = 0x10; break;
149 case 'M':
150 case 'm':
151 data[i++] = 0x1f; data[i++] = 0x2; data[i++] = 0x7; data[i++] = 0x2; data[i++] = 0x1f; break;
152 case 'N':
153 case 'n':
154 data[i++] = 0x1f; data[i++] = 0x2; data[i++] = 0x4; data[i++] = 0x1f; break;
155 case 'O':
156 case 'o':
157 data[i++] = 0xe; data[i++] = 0x11; data[i++] = 0xe; break;
158 case 'P':
159 case 'p':
160 data[i++] = 0x1f; data[i++] = 0x5; data[i++] = 0x2; break;
161 case 'Q':
162 case 'q':
163 data[i++] = 0xe; data[i++] = 0x11; data[i++] = 0x19; data[i++] = 0x1e; break;
164 case 'R':
165 case 'r':
166 data[i++] = 0x1f; data[i++] = 0x5; data[i++] = 0x1a; break;
167 case 'S':
168 case 's':
169 data[i++] = 0x12; data[i++] = 0x15; data[i++] = 0xd; break;
170 case 'T':
171 case 't':
172 data[i++] = 0x1; data[i++] = 0x1f; data[i++] = 0x1; break;
173 case 'U':
174 case 'u':
175 data[i++] = 0x1f; data[i++] = 0x10; data[i++] = 0x1f; break;
176 case 'V':
177 case 'v':
178 data[i++] = 0xf; data[i++] = 0x10; data[i++] = 0xf; break;
179 case 'W':
180 case 'w':
181 data[i++] = 0xf; data[i++] = 0x10; data[i++] = 0xc; data[i++] = 0x10; data[i++] = 0xf; break;
182 case 'X':
183 case 'x':
184 data[i++] = 0x11; data[i++] = 0xe; data[i++] = 0x11; break;
185 case 'Y':
186 case 'y':
187 data[i++] = 0x17; data[i++] = 0x14; data[i++] = 0xf; break;
188 case 'Z':
189 case 'z':
190 data[i++] = 0x19; data[i++] = 0x15; data[i++] = 0x13; break;
191 case '0':
192 data[i++] = 0xe; data[i++] = 0x11; data[i++] = 0xe; break;
193 case '1':
194 data[i++] = 0x12; data[i++] = 0x1f; data[i++] = 0x10; break;
195 case '2':
196 data[i++] = 0x12; data[i++] = 0x19; data[i++] = 0x16; break;
197 case '3':
198 data[i++] = 0x15; data[i++] = 0xa; break;
199 case '4':
200 data[i++] = 0x7; data[i++] = 0x4; data[i++] = 0x1f; break;
201 case '5':
202 data[i++] = 0x17; data[i++] = 0x15; data[i++] = 0x9; break;
203 case '6':
204 data[i++] = 0xe; data[i++] = 0x15; data[i++] = 0x9; break;
205 case '7':
206 data[i++] = 0x1; data[i++] = 0x1f; break;
207 case '8':
208 data[i++] = 0x1f; data[i++] = 0x15; data[i++] = 0x1f; break;
209 case '9':
210 data[i++] = 0x12; data[i++] = 0x15; data[i++] = 0x1e; break;
211 case '.':
212 data[i++] = 0x10; break;
213 case ',':
214 data[i++] = 0x10; data[i++] = 0x8; break;
215 case '\'':
216 case '"':
217 data[i++] = 0x1; break;
218 case ':':
219 data[i++] = 0xa; break;
220 case '(':
221 data[i++] = 0xe; data[i++] = 0x11; break;
222 case ')':
223 data[i++] = 0x11; data[i++] = 0xe; break;
224 case '-':
225 data[i++] = 0x4; data[i++] = 0x4; break;
226 case ' ':
227 i++;
228 break;
229 case '!':
230 data[i++] = 0x17; break;
231 default:
232 break;
233 }
234 i++;
235 s++;
236 }
237 msglen = i;
238 pointer = 0;
239 }
240
241 int main(int argc, char* argv[])
242 {
243 rt_print_auto_init(1);
244 mlockall(MCL_CURRENT | MCL_FUTURE);
245
246 unsigned int i;
247 for(i=0; i<BANNERWIDTH; i++){
248 data[i] = 0;
249 }
250
251 // Get permission to write to parallel port
252 ioperm(0x37A, 1, 1);
253 ioperm(0x378, 1, 1);
254 outb(inb(0x37A) | 0x10, 0x37A);
255
256 make_message("abcdefghijklmnopqrstuvwxyz1234567890,.\"()");
257
258 rt_task_create(&intrtask, NULL, 0, 50, 0);
259 rt_task_start(&intrtask, &taski, 0);
260
261 printf("CRTL+C to exit\n");
262 printf("legal characters: abcdefghijklmnopqrstuvwxyz1234567890,.\"()\n");
263 while(1){
264 printf("Type your message(max %d chars): ", MSGMAX);
265 scanf("%[^\n]", inputmsg);
266 getchar();
267 make_message(inputmsg);
268 sleep(0.1);
269 }
270 }