EE445M RTOS
Taken at the University of Texas Spring 2015
console-fb.c
Go to the documentation of this file.
1 /* This file is not intended to be flashed on the board */
2 
3 /*
4  * Hershal Bhave and Eric Crosson
5  * 2014-02-08
6  * Function: display a frame buffer in console exactly as it would
7  * look on the OLED
8  * Lab 3
9  * Last Revision: LOOK IN GIT FGT
10  * *nix
11  */
12 
13 #include <time.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include "shape.h"
17 #include "clock.h"
18 #include "framebuffer.h"
19 #include "graphics2d.h"
20 #include "timekit.h"
21 /* #include "../src/lmsmenu.h" */
22 
23 int usleep(int usec);
24 
25 void mockCheckout(void);
27 void demonstrateClockMenu(void);
28 /* void demonstrateMenu(void); */
29 void demonstrateClock(void);
30 void demonstrateDrawString(void);
31 void demonstrateDrawShapes(void);
32 
33 void printFramebuffer(unsigned char* fb);
34 
35 void pseudointerrupt(clocktime* tm);
36 
37 int main(void) {
38  /* mockCheckout(); */
39  /* demonstrateVoidPointerMenus(); */
40  /* demonstrateClockMenu(); */
41  /* demonstrateMenu(); */
43  /* demonstrateDrawString(); */
44  /* demonstrateDrawShapes(); */
45 }
46 
47 void mockCheckout() {
48 
49 }
50 
51 /* void demonstrateVoidPointerMenus() { */
52 /* framebuffer fb = FBInit(); */
53 /* */
54 /* clocktime* time = TKCreateTimeHandle(TK_PRINT_MODE_HOURS_MINUTES_SECONDS_MS); */
55 /* mu_menu* main = MUCreateMenu("I'm a MU_MENU", 2); */
56 /* mu_clock_menu* clock = */
57 /* MUCreateClockSettingMenu("I'm a MU_CLOCK_MENU", 1, time); */
58 /* */
59 /* printf("Main is: %x\n", MUMenuDecoder(main)); */
60 /* printf("Clock is: %x\n", MUMenuDecoder(clock)); */
61 /* */
62 /* MUCDestroyClockMenu(clock); */
63 /* MUDestroyMenu(main); */
64 /* TKDestroy(time); */
65 /* FBDestroy(fb); */
66 /* } */
67 
68 /* void demonstrateClockMenu() { */
69 /* framebuffer fb = FBInit(); */
70 /* */
71 /* mu_menu* main = MUCreateMenu("Set:", 2); */
72 /* */
73 /* clocktime* time = TKCreateTimeHandle(TK_PRINT_MODE_HOURS_MINUTES_SECONDS_MS); */
74 /* mu_clock_menu* clock = MUCreateClockSettingMenu("Set clock time", 1, time); */
75 /* mu_clock_menu* alarm = MUCreateClockSettingMenu("Set alarm time", 1, time); */
76 /* */
77 /* MUSetOptionStrings(main, "clock time", "alarm time"); */
78 /* MUSetClockMenuItems(main, clock, alarm); */
79 /* */
80 /* MUCPrevScreen(clock, main); */
81 /* MUCPrevScreen(alarm, main); */
82 /* */
83 /* FBDrawMUMenu(fb, main); */
84 /* printFramebuffer( G2ConvertFBToRITFormat(fb) ); */
85 /* usleep(1000000); */
86 /* */
87 /* mu_clock_menu* next = MUCControllerHandleInputEvent(main, MU_IO_SELECT); */
88 /* FBDestroy(fb); /\* clear the framebuffer *\/ */
89 /* fb = FBInit(); /\* reallocate *\/ */
90 /* FBDrawMUClockMenu(fb, next); */
91 /* printFramebuffer( G2ConvertFBToRITFormat(fb) ); */
92 /* usleep(1000000); */
93 /* */
94 /* MUCHandleInputEvent(next, MU_IO_UP); /\* inc hours *\/ */
95 /* MUCHandleInputEvent(next, MU_IO_UP); /\* inc hours *\/ */
96 /* MUCHandleInputEvent(next, MU_IO_RIGHT); /\* sel min *\/ */
97 /* MUCHandleInputEvent(next, MU_IO_DOWN); /\* dec min *\/ */
98 /* FBDestroy(fb); /\* clear the framebuffer *\/ */
99 /* fb = FBInit(); /\* reallocate *\/ */
100 /* FBDrawMUClockMenu(fb, next); */
101 /* printFramebuffer( G2ConvertFBToRITFormat(fb) ); */
102 /* FBDestroy(fb); /\* clear the framebuffer *\/ */
103 /* */
104 /* MUDestroyMenu(main); */
105 /* MUCDestroyClockMenu(alarm); */
106 /* MUCDestroyClockMenu(clock); */
107 /* } */
108 
109 /* Note that the relationship between menua and clock_menu is
110  * suboptimal at present because c doesn't have any sort of reasonable
111  * inheritance. */
112 
113 /* void demonstrateMenu() { */
114 /* framebuffer fb = FBInit(); */
115 /* */
116 /* mu_menu* m = MUCreateMenu("Suicide booth", 2); */
117 /* mu_menu* q = MUCreateMenu("You have selected quick and painless", 1); */
118 /* mu_menu* p = MUCreateMenu("You have selected painful and horrible", 1); */
119 /* mu_menu* d = MUCreateMenu("You are dead.", 1); */
120 /* */
121 /* MUSetOptionStrings(m, "Quick and painless", "Painful and horrible"); */
122 /* MUSetMenuItems(m, q, p); */
123 /* */
124 /* MUSetMenuItems(p, "Return to start", m); */
125 /* MUSetMenuItems(q, "Return to start", m); */
126 /* MUSetMenuItems(d, "Return to start", m); */
127 /* */
128 /* MUPrevScreen(d, m); */
129 /* MUPrevScreen(p, m); */
130 /* MUPrevScreen(q, m); */
131 /* */
132 /* mu_menu* current = m; */
133 /* mu_menu* next = (mu_menu*) 0x1; */
134 /* */
135 /* /\* Display main menu *\/ */
136 /* FBDrawMUMenu(fb, current); */
137 /* printFramebuffer( G2ConvertFBToRITFormat(fb) ); */
138 /* printf("-----------\n"); */
139 /* usleep(500000); */
140 /* */
141 /* /\* Select the first option and redisplay *\/ */
142 /* next = MUHandleInputEvent(m, MU_IO_SELECT); */
143 /* FBDrawMUMenu(fb, next); */
144 /* printFramebuffer( G2ConvertFBToRITFormat(fb) ); */
145 /* printf("-----------\n"); */
146 /* */
147 /* MUDestroyMenu(m); */
148 /* MUDestroyMenu(p); */
149 /* MUDestroyMenu(q); */
150 /* MUDestroyMenu(d); */
151 /* FBDestroy(fb); */
152 /* } */
153 
154 void demonstrateClock(void) {
155 
156 
157  point* pt_center = SHCreatePoint(OLED_WIDTH/2, OLED_HEIGHT/2, 15);
158  point* pt_offset_ms = SHCreatePoint(OLED_WIDTH/2, 3*OLED_HEIGHT/4, 15);
159  circle* circle_cir = SHCreateCircle(46, pt_center);
160  circle* circle_ms = SHCreateCircle(12, pt_offset_ms);
161 
163 
164  clockhand* ch_hour = CHCreateClockHand(pt_center, 25, CH_HOURS); // px
165  clockhand* ch_min = CHCreateClockHand(pt_center, 32, CH_MINUTES); // px
166  clockhand* ch_sec = CHCreateClockHand(pt_center, 36, CH_SECONDS); // px
167  clockhand* ch_ms = CHCreateClockHand(pt_offset_ms, 10, CH_MS); // px
168 
169  ushort numhands = 4;
170  clockhand** hands_arr = (clockhand**)malloc(numhands*sizeof(clockhand*));
171  hands_arr[0] = ch_hour;
172  hands_arr[1] = ch_min;
173  hands_arr[2] = ch_sec;
174  hands_arr[3] = ch_ms;
175  clockface* cf = CFCreateClockFace(numhands, hands_arr, tm);
176 
177  shape** shapeptr;
178 
179  TKIncrementHours(tm, 10);
180  TKIncrementMinutes(tm, 40);
181 
182  framebuffer fb = FBInit();
183 
184  /* Begin clock face layout */
185  point* twelve_corner = SHCreatePoint(58, 4, FB_COLOR_MUCH);
186  point* six_corner = SHCreatePoint(62, 86, FB_COLOR_MUCH);
187  point* three_corner = SHCreatePoint(21, 45, FB_COLOR_MUCH);
188  point* nine_corner = SHCreatePoint(103, 45, FB_COLOR_MUCH);
189  FBDrawCircle(fb, circle_cir);
190  FBDrawString(fb, twelve_corner, "12");
191  FBDrawString(fb, six_corner, "6");
192  FBDrawString(fb, three_corner, "3");
193  FBDrawString(fb, nine_corner, "9");
194  free(twelve_corner);
195  free(six_corner);
196  free(three_corner);
197  free(nine_corner);
198  /* End clock face layout */
199 
200  while(1) {
201  /* printf("----- %s -----\n", TKToString(tm)); */
202  pseudointerrupt(tm);
203  CFUpdateTime(cf);
204  shapeptr = CFToShapes(cf);
205  FBDrawCircle(fb, circle_ms);
206  FBDrawShapeArr(fb, numhands, shapeptr);
208  FBEraseShapeArr(fb, numhands, shapeptr);
209  CFDestroyShapes(cf);
210  usleep(500000); // .5 second
211  }
212 
213  /* cleanup */
214  /* CFDestroyClockFace(cf); */
215  free(circle_cir);
216  free(circle_ms);
217  free(pt_offset_ms);
218  free(pt_center);
219  TKDestroy(tm);
220 
221  CHDestroyClockHand(ch_hour);
222  CHDestroyClockHand(ch_min);
223  CHDestroyClockHand(ch_sec);
224  CHDestroyClockHand(ch_ms);
225  CFDestroyClockFace(cf);
226  free(hands_arr);
227 
228  /* free(fb); */
229  FBDestroy(fb);
230 }
231 
233  TKIncrementMS(tm,100);
234  /* TKIncrementHours(tm,1); */
235 }
236 
238  point* p0 = SHCreatePoint(10, 10, 15);
239  point* p1 = SHCreatePoint(10, 20, 15);
240  point* p2 = SHCreatePoint(30, 30, 15);
241  shape* tri = SHCreateTriangle(p0, p1, p2);
242 
243  point* p2_0 = SHCreatePoint(50, 50, 11);
244  shape* square = SHCreateQuad(p2_0, 10, 10);
245 
246  point* round_center =SHCreatePoint(70, 35, 7);
247  /* circle* circle = SHCreateCircle(15, round_center); */
248 
249  shape** shapeArr = (shape**)malloc(2*sizeof(shape*));
250  shapeArr[0] = tri; shapeArr[1] = square;
251 
252  framebuffer fb = FBInit();
253 
254  FBDrawLine(fb, SHCreatePoint(0, 0, 15), SHCreatePoint(0, 10, 15), 15);
255 
256  /* FBDrawShape(fb, tri); */
257  /* FBDrawShape(fb, square); */
258  /* FBDrawMultipleShapes(fb, 2, square, tri); */
259  FBDrawShapeArr(fb, 2, shapeArr);
260  free(shapeArr);
261 
262  FBDrawEllipseFill(fb, round_center, 30, 5, round_center->shade);
264  FBDestroy(fb);
265 }
266 
268  framebuffer fb = FBInit();
269  point* pt = SHCreatePoint(20, 35, 15);
270 
271  FBDrawString(fb, pt, "A B C D E F G");
273 
274  FBDestroy(fb);
275  free(pt);
276 }
277 
278 // Print framebuffer contents to console.
279 // Inputs: framebuffer framebuffer to display
280 // Outputs: void
281 /* TODO: Fix the bit-strapping!! */
282 void printFramebuffer(unsigned char* fb) {
283  unsigned char i, j, a, b;
284 
285  for(j=0; j<OLED_HEIGHT; ++j) {
286  for(i=0; i<OLED_WIDTH; i+=2) {
287  a = (fb[(i/2)+j*RIT_FB_WIDTH] >> 4);
288  b = (fb[(i/2)+j*RIT_FB_WIDTH] & 0x0F);
289 
290  switch(a/4) {
291  case 0: a=SHADE_0; break;
292  case 1: a=SHADE_1; break;
293  case 2: a=SHADE_2; break;
294  case 3: a=SHADE_3; break;
295  }
296 
297  switch(b/4) {
298  case 0: b=SHADE_0; break;
299  case 1: b=SHADE_1; break;
300  case 2: b=SHADE_2; break;
301  case 3: b=SHADE_3; break;
302  }
303 
304  printf("%c%c", a, b);
305  }
306  printf("\n");
307  }
308 }
unsigned char * G2ConvertFBToRITFormat(framebuffer fb)
Definition: graphics2d.c:23
int usleep(int usec)
#define OLED_HEIGHT
Definition: defines.h:106
void TKIncrementHours(clocktime *tm, short hours)
Definition: timekit.c:55
Representation of a circle.
Definition: shape.h:37
#define RIT_FB_WIDTH
Definition: defines.h:110
Representation of a shape.
Definition: shape.h:26
void mockCheckout(void)
Definition: console-fb.c:47
void TKDestroy(clocktime *tm)
Definition: timekit.c:24
shade_t shade
Definition: shape.h:18
void CFDestroyClockFace(clockface *cf)
Definition: clock.c:112
#define CH_SECONDS
Definition: clock.h:18
#define SHADE_3
Definition: defines.h:92
#define OLED_WIDTH
Definition: defines.h:107
int main(void)
Definition: console-fb.c:37
#define SHADE_2
Definition: defines.h:91
void TKIncrementMS(clocktime *tm, short ms)
Definition: timekit.c:109
unsigned short ushort
Definition: defines.h:35
char ** framebuffer
Definition: framebuffer.h:12
void printFramebuffer(unsigned char *fb)
Definition: console-fb.c:282
void CFUpdateTime(clockface *cf)
Definition: clock.c:128
shape ** CFToShapes(clockface *cf)
Definition: clock.c:135
clockhand * CHCreateClockHand(point *center, ushort radius, uchar type)
Definition: clock.c:32
#define FB_COLOR_MUCH
Definition: defines.h:85
void CHDestroyClockHand(clockhand *ch)
Definition: clock.c:71
void demonstrateDrawString(void)
Definition: console-fb.c:267
#define CH_MINUTES
Definition: clock.h:17
#define CH_MS
Definition: clock.h:19
void CFDestroyShapes(clockface *cf)
Definition: clock.c:146
void demonstrateClock(void)
Definition: console-fb.c:154
#define SHADE_0
Definition: defines.h:89
Representation of an ordered pair with a shade.
Definition: shape.h:13
clockface * CFCreateClockFace(ushort numHands, clockhand **chs, clocktime *th)
Definition: clock.c:91
void pseudointerrupt(clocktime *tm)
Definition: console-fb.c:232
void demonstrateClockMenu(void)
#define CH_HOURS
Definition: clock.h:16
void demonstrateDrawShapes(void)
Definition: console-fb.c:237
void TKIncrementMinutes(clocktime *tm, short minutes)
Definition: timekit.c:69
#define SHADE_1
Definition: defines.h:90
#define TK_PRINT_MODE_HOURS_MINUTES_SECONDS_MS
Definition: timekit.h:8
void demonstrateVoidPointerMenus(void)
clocktime * TKCreateTimeHandle(char mode)
Definition: timekit.c:14