-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholed.c
355 lines (325 loc) · 12.8 KB
/
oled.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
/*
* oled.c
*
* Created on: Jul 6, 2015
* Author: George
*/
/*
* OLED (OLED_0.96)
* (write on rise, change on fall,
* CS active low, MSB first)
* eUSCI_A1
* ste, miso, clk
* DC P2.6
* RES P2.7
*/
#include <oled.h>
#include <grlib.h>
#include <qc12_oled.h>
#include <qc12.h>
#include <string.h>
#include <stdlib.h>
uint8_t oled_anim_state = OLED_ANIM_DONE;
uint8_t anim_index = 0;
uint8_t anim_loops = 0;
uint8_t anim_frame_skip = 0;
const qc12_anim_t *anim_data;
uint8_t oled_overhead_type = OLED_OVERHEAD_OFF;
uint8_t oled_overhead_loops = 0;
uint8_t oled_overhead_half_width = 0;
const tImage *oled_overhead_image;
char oled_overhead_text[NAME_MAX_LEN+1] = "";
int8_t char_pos_x = 0;
int8_t char_pos_y = 0;
void init_oled() {
qc12_oledInit(0);
GrContextInit(&g_sContext, &g_sqc12_oled);
GrContextBackgroundSet(&g_sContext, ClrBlack);
GrContextForegroundSet(&g_sContext, ClrWhite);
GrContextFontSet(&g_sContext, &SYS_FONT);
GrClearDisplay(&g_sContext);
GrFlush(&g_sContext);
}
void oled_draw_pane_and_flush(uint8_t softkey_sel) {
static tRectangle erase_rect_top = {0, 0, 64, 2*NAME_FONT_HEIGHT+1};
static tRectangle erase_rect_btm = {0, SPRITE_Y + 64, 64, 127};
static uint8_t need_light_bulb;
need_light_bulb = 0;
GrContextForegroundSet(&g_sContext, ClrBlack);
GrRectFill(&g_sContext, &erase_rect_btm);
GrRectFill(&g_sContext, &erase_rect_top);
GrContextForegroundSet(&g_sContext, ClrWhite);
GrContextFontSet(&g_sContext, &NAME_FONT);
GrStringDraw(&g_sContext, my_conf.handle, -1, 0, 0, 0);
uint8_t title_width = GrStringWidthGet(&g_sContext, titles[my_conf.title_index], -1);
uint8_t the_width = GrStringWidthGet(&g_sContext, "the", -1);
GrStringDraw(&g_sContext, "the", -1, 65 - title_width - the_width - 3, NAME_FONT_HEIGHT, 0);
GrStringDraw(&g_sContext, titles[my_conf.title_index], -1, 65 - title_width, NAME_FONT_HEIGHT, 0);
GrLineDrawH(&g_sContext, 0, 64, 2*NAME_FONT_HEIGHT+1);
GrContextFontSet(&g_sContext, &SOFTKEY_LABEL_FONT);
if (!idle_mode_softkey_dis) {
if (softkey_sel == SK_SEL_SLEEP) {
if (!my_conf.seen_sleep && (softkey_sel == SK_SEL_SLEEP)) {
need_light_bulb = 1;
} else if (my_conf.seen_sleep == 1) { // 1: seen sleep but not "sleep".
for (uint8_t i=0; i<FAVORITE_COUNT; i++) {
if (neighbor_badges[fav_badges_ids[i]]) {
// favorite nearby.
if ((BADGE_FRIEND_BIT & badges_seen[fav_badges_ids[i]]) && !(BADGE_SEX_BIT & badges_seen[fav_badges_ids[i]])) {
// Favorite friend nearby:
need_light_bulb = 1;
break;
}
}
}
}
} else if (softkey_sel == SK_SEL_HATCH) {
need_light_bulb = 1;
} else if (!my_conf.seen_flags && (softkey_sel == SK_SEL_FLAG || softkey_sel == SK_SEL_SETFLAG)) {
need_light_bulb = 1;
} else if (my_conf.adult && !my_conf.seen_titles && (softkey_sel == SK_SEL_ASL)) {
need_light_bulb = 1;
} else if (!my_conf.seen_befriend && (softkey_sel == SK_SEL_FRIEND)) {
need_light_bulb = 1;
}
if (need_light_bulb) {
GrImageDraw(&g_sContext, &idea, -5, 105); // To mark a NEW IDEA!
// GrStringDraw(&g_sContext, sk_labels[softkey_sel], -2, 64 - sk_width, SPRITE_Y + 64, 0);
GrStringDrawCentered(&g_sContext, sk_labels[softkey_sel], -2, 39, SPRITE_Y + 64 + SOFTKEY_FONT_HEIGHT/2, 0);
} else {
GrStringDrawCentered(&g_sContext, sk_labels[softkey_sel], -2, 32, SPRITE_Y + 64 + SOFTKEY_FONT_HEIGHT/2, 0);
}
} else if (befriend_mode){
GrStringDrawCentered(&g_sContext, "LOOKING", -2, 32, SPRITE_Y + 64 + SOFTKEY_FONT_HEIGHT/2, 0);
} else if (play_mode) {
GrStringDrawCentered(&g_sContext, "PLAYING!", -2, 32, SPRITE_Y + 64 + SOFTKEY_FONT_HEIGHT/2, 0);
} else if (my_conf.time_to_hatch) {
GrStringDrawCentered(&g_sContext, "GROWING!", -2, 32, SPRITE_Y + 64 + SOFTKEY_FONT_HEIGHT/2, 0);
}
GrLineDrawH(&g_sContext, 0, 64, SPRITE_Y + 64);
GrFlush(&g_sContext);
}
void draw_overhead() {
if (oled_overhead_type == OLED_OVERHEAD_TXT) {
GrContextFontSet(&g_sContext, &SYS_FONT);
int8_t text_x = char_pos_x + 32;
if (text_x + oled_overhead_half_width > 63) {
text_x = 63 - oled_overhead_half_width;
} else if (text_x - oled_overhead_half_width < 0) {
text_x = oled_overhead_half_width;
}
GrStringDrawCentered(&g_sContext, oled_overhead_text, -1, text_x, SPRITE_Y-SYS_FONT_HEIGHT/2, 0);
} else if (oled_overhead_type == OLED_OVERHEAD_IMG) {
GrImageDraw(&g_sContext, oled_overhead_image, char_pos_x + (32 - oled_overhead_image->XSize/2), SPRITE_Y - oled_overhead_image->YSize - char_pos_y);
}
}
void oled_set_overhead_image(const tImage *image, uint8_t loop_len) {
oled_overhead_loops = loop_len;
oled_overhead_type = OLED_OVERHEAD_IMG;
oled_overhead_image = image;
if (oled_anim_state == OLED_ANIM_DONE) {
s_oled_needs_redrawn_idle = 1;
}
}
void oled_set_overhead_text(char *text, uint8_t loop_len) {
oled_overhead_loops = loop_len;
oled_overhead_type = OLED_OVERHEAD_TXT;
oled_overhead_half_width = GrStringWidthGet(&g_sContext, text, -1)/2;
if (strlen(text) > NAME_MAX_LEN) {
return; // PROBLEM
}
strcpy(oled_overhead_text, text);
if (oled_anim_state == OLED_ANIM_DONE) {
s_oled_needs_redrawn_idle = 1;
}
}
void oled_set_overhead_off() {
s_overhead_done = 1;
oled_overhead_loops = 0;
oled_overhead_type = OLED_OVERHEAD_OFF;
if (oled_anim_state == OLED_ANIM_DONE) {
s_oled_needs_redrawn_idle = 1;
}
}
void do_move(uint8_t move_signal) {
if (!move_signal)
return;
uint8_t move_flag = move_signal >> 6;
uint8_t move_amt = move_signal & 0b00111111;
switch (move_flag) {
case 0: // up
char_pos_y += move_amt;
break;
case 1: // down
char_pos_y -= move_amt;
break;
case 2: // left
char_pos_x -= move_amt;
if (char_pos_x < -64) {
char_pos_x+=128;
}
break;
case 3: // right
char_pos_x += move_amt;
if (char_pos_x > 64) {
char_pos_x-=128;
}
break;
default:
__never_executed();
}
}
void oled_anim_disp_frame(const qc12_anim_t *animation_data, uint8_t frame_no) {
GrClearDisplay(&g_sContext);
GrImageDraw(&g_sContext, &legs[animation_data->legs_indices[frame_no]], char_pos_x, legs_clip_offset + animation_data->legs_tops[frame_no] + SPRITE_Y - char_pos_y);
GrImageDraw(&g_sContext, &bodies[animation_data->bodies_indices[frame_no]], char_pos_x, animation_data->body_tops[frame_no] + SPRITE_Y - char_pos_y);
GrImageDraw(&g_sContext, &heads[animation_data->heads_indices[frame_no]], char_pos_x, animation_data->head_tops[frame_no] + SPRITE_Y - char_pos_y);
if (my_conf.adult && !my_conf.time_to_hatch)
draw_overhead();
oled_draw_pane_and_flush(idle_mode_softkey_sel); // This flushes.
}
void oled_consider_walking_back() {
if (char_pos_y != 0) {
char_pos_x = 64;
char_pos_y = 0;
}
// Determine whether we need to walk back onto the screen after anim.
if (!my_conf.adult) {
if (char_pos_x < -12) {
oled_play_animation(&infant_walk, (uint8_t) (-char_pos_x/4) - 1);
} else if (char_pos_x > 12) {
oled_play_animation(&infant_walk_left, (uint8_t) (char_pos_x/4) - 1);
}
} else if (my_conf.mood < MOOD_THRESH_SAD) {
// zombie walk back
if (char_pos_x < -12) {
oled_play_animation(&zombie, (uint8_t) (-char_pos_x/12) - 1);
} else if (char_pos_x > 12) {
oled_play_animation(&zombie_left, (uint8_t) (char_pos_x/12) - 1);
}
} else {
// happy walk back
if (char_pos_x < -16) {
oled_play_animation(&walking, (uint8_t) (-char_pos_x/16) - 1);
} else if (char_pos_x > 16) {
oled_play_animation(&walking_left, (uint8_t) (char_pos_x/16) - 1);
}
}
}
void oled_anim_next_frame() {
if (oled_anim_state == OLED_ANIM_DONE)
return;
do_move(anim_data->movement[anim_index]);
oled_anim_disp_frame(anim_data, anim_index);
anim_index++;
// If we need to loop, loop:
if (anim_loops && anim_data->looped) {
if (anim_index == anim_data->loop_end) {
anim_index = anim_data->loop_start;
anim_loops--;
}
} else if (anim_loops && anim_index == anim_data->len) {
anim_index = 0;
anim_loops--;
}
if (anim_index == anim_data->len) {
s_oled_needs_redrawn_idle = 1;
oled_anim_state = OLED_ANIM_DONE;
oled_consider_walking_back();
}
}
void oled_play_animation(const qc12_anim_t *anim, uint8_t loops) {
anim_index = 0;
anim_loops = loops;
anim_data = anim;
anim_frame_skip = anim->speed;
oled_anim_state = OLED_ANIM_START;
s_oled_needs_redrawn_idle = 0;
}
void oled_print(uint8_t x, uint8_t y, const char str[], uint8_t opaque, uint8_t centered) {
GrContextFontSet(&g_sContext, &SYS_FONT);
uint8_t curr_y = y;
uint8_t str_start = 0;
uint8_t str_end = 0;
uint8_t str_end_candidate = 0;
uint8_t str_len = strlen(str);
// Note: This could spin forever if we use long words.
while (str_end < str_len) {
// find the next space, call it str_end_candidate
while (str_end_candidate < str_len && str[str_end_candidate] != ' ') {
str_end_candidate++;
}
// If the string, from start to end, fits, we continue (find next space).
// str_end becomes str_end_candidate.
if (x + GrStringWidthGet(&g_sContext, &str[str_start], str_end_candidate - str_start) < 63) {
str_end = str_end_candidate;
str_end_candidate++; // Skip the space.
} else {
// If it doesn't fit, we halt and catch fire. Make sure it fits.
if (centered) {
GrStringDrawCentered(&g_sContext, &str[str_start], str_end - str_start, (64+x)/2, curr_y+SYS_FONT_HEIGHT/2, opaque);
} else {
GrStringDraw(&g_sContext, &str[str_start], str_end - str_start, x, curr_y, opaque);
}
curr_y += SYS_FONT_HEIGHT+1;
str_start = str_end+1; // Skip the space.
}
}
// If there's still something to draw:
if (str_start < str_len) {
if (centered) {
GrStringDrawCentered(&g_sContext, &str[str_start], -1, (64+x)/2, curr_y+SYS_FONT_HEIGHT/2, opaque);
}
else {
GrStringDraw(&g_sContext, &str[str_start], -1, x, curr_y, opaque);
}
}
}
void oled_timestep() {
if (oled_overhead_loops && oled_overhead_type != OLED_OVERHEAD_OFF) {
if (oled_overhead_loops != 0xFF) {
oled_overhead_loops--;
}
if (!oled_overhead_loops) {
oled_overhead_type = OLED_OVERHEAD_OFF;
s_overhead_done = 1;
if (!oled_anim_state) {
s_oled_needs_redrawn_idle = 1;
}
}
}
if (s_overhead_done) {
s_overhead_done = 0;
if (my_conf.mood < MOOD_THRESH_SAD) { // We don't really do anything if we're moody.
if (rand() % 2) {
oled_set_overhead_image(&lightning, 10);
} else {
oled_set_overhead_image(&cloud, 100);
}
} else if (am_puppy) {
oled_set_overhead_image(&puppy, 100);
} else {
for (uint8_t i=0; i<FAVORITE_COUNT; i++) {
if (neighbor_badges[fav_badges_ids[i]]) {
// favorite nearby.
if ((BADGE_FRIEND_BIT & badges_seen[fav_badges_ids[i]])) {
// Favorite friend nearby:
oled_set_overhead_image(&heart, 100);
break;
} else {
// Favorite non-friend nearby:
oled_set_overhead_image(&empty_heart, 100);
}
}
}
}
}
if (oled_anim_state) {
if (anim_frame_skip) {
anim_frame_skip--;
} else {
anim_frame_skip = anim_data->speed;
oled_anim_next_frame();
}
}
}