20
20
#include "print.h"
21
21
22
22
// Calculated Parameters
23
- #define TWINKLE_PROBABILITY_MODULATOR 100 / TWINKLE_PROBABILITY // CALCULATED: Don't Touch
24
- #define TOTAL_STARS STARS_PER_LINE *NUMBER_OF_STAR_LINES // CALCULATED: Don't Touch
25
- #define OCEAN_ANIMATION_MODULATOR NUMBER_OF_FRAMES / OCEAN_ANIMATION_SPEED // CALCULATED: Don't Touch
26
- #define SHOOTING_STAR_ANIMATION_MODULATOR NUMBER_OF_FRAMES / SHOOTING_STAR_ANIMATION_SPEED // CALCULATED: Don't Touch
27
- #define STAR_ANIMATION_MODULATOR NUMBER_OF_FRAMES / STAR_ANIMATION_SPEED // CALCULATED: Don't Touch
23
+ #define TWINKLE_PROBABILITY_MODULATOR 100 / TWINKLE_PROBABILITY // CALCULATED: Don't Touch
24
+ #define TOTAL_STARS STARS_PER_LINE *NUMBER_OF_STAR_LINES // CALCULATED: Don't Touch
25
+ #define OCEAN_ANIMATION_MODULATOR NUMBER_OF_FRAMES / OCEAN_ANIMATION_SPEED // CALCULATED: Don't Touch
26
+ #define SHOOTING_STAR_ANIMATION_MODULATOR NUMBER_OF_FRAMES / SHOOTING_STAR_ANIMATION_SPEED // CALCULATED: Don't Touch
27
+ #define STAR_ANIMATION_MODULATOR NUMBER_OF_FRAMES / STAR_ANIMATION_SPEED // CALCULATED: Don't Touch
28
28
29
- uint8_t animation_counter = 0 ; // global animation counter.
29
+ uint8_t animation_counter = 0 ; // global animation counter.
30
30
bool is_calm = false;
31
31
uint32_t starry_night_anim_timer = 0 ;
32
32
uint32_t starry_night_anim_sleep = 0 ;
@@ -52,10 +52,10 @@ static uint8_t decrement_counter(uint8_t counter, uint8_t max) {
52
52
}
53
53
#endif
54
54
55
- #ifdef ENABLE_MOON // region
55
+ #ifdef ENABLE_MOON // region
56
56
# ifndef STATIC_MOON
57
- uint8_t moon_animation_frame = 0 ; // keeps track of current moon frame
58
- uint16_t moon_animation_counter = 0 ; // counts how many frames to wait before animating moon to next frame
57
+ uint8_t moon_animation_frame = 0 ; // keeps track of current moon frame
58
+ uint16_t moon_animation_counter = 0 ; // counts how many frames to wait before animating moon to next frame
59
59
# endif
60
60
61
61
# ifdef STATIC_MOON
@@ -99,9 +99,9 @@ static void draw_moon(void) {
99
99
}
100
100
# endif
101
101
}
102
- #endif // endregion
102
+ #endif // endregion
103
103
104
- #ifdef ENABLE_WAVE // region
104
+ #ifdef ENABLE_WAVE // region
105
105
uint8_t starry_night_wave_frame_width_counter = 31 ;
106
106
uint8_t rough_waves_frame_counter = 0 ;
107
107
@@ -193,8 +193,8 @@ static const char PROGMEM ocean_bottom[8][32] = {
193
193
// clang-format on
194
194
195
195
static void animate_waves (void ) {
196
- starry_night_wave_frame_width_counter = decrement_counter (starry_night_wave_frame_width_counter , WIDTH - 1 ); // only 3 frames for last wave type
197
- rough_waves_frame_counter = increment_counter (rough_waves_frame_counter , 3 ); // only 3 frames for last wave type
196
+ starry_night_wave_frame_width_counter = decrement_counter (starry_night_wave_frame_width_counter , WIDTH - 1 ); // only 3 frames for last wave type
197
+ rough_waves_frame_counter = increment_counter (rough_waves_frame_counter , 3 ); // only 3 frames for last wave type
198
198
199
199
void draw_ocean (uint8_t frame , uint16_t offset , uint8_t byte_index ) {
200
200
oled_write_raw_byte (pgm_read_byte (ocean_top [frame ] + byte_index ), offset );
@@ -218,9 +218,9 @@ static void animate_waves(void) {
218
218
}
219
219
}
220
220
}
221
- #endif // endregion
221
+ #endif // endregion
222
222
223
- #ifdef ENABLE_ISLAND // region
223
+ #ifdef ENABLE_ISLAND // region
224
224
uint8_t island_frame_1 = 0 ;
225
225
226
226
// clang-format off
@@ -276,17 +276,17 @@ static void animate_island(void) {
276
276
draw_island_parts (island_frame_1 + 4 );
277
277
}
278
278
}
279
- #endif // endregion
279
+ #endif // endregion
280
280
281
- #ifdef ENABLE_STARS // region
282
- bool stars_setup = false; // only setup stars once, then we just twinkle them
281
+ #ifdef ENABLE_STARS // region
282
+ bool stars_setup = false; // only setup stars once, then we just twinkle them
283
283
struct Coordinate {
284
284
int x ;
285
285
int y ;
286
286
bool exists ;
287
287
};
288
288
289
- struct Coordinate stars [TOTAL_STARS ]; // tracks all stars/coordinates
289
+ struct Coordinate stars [TOTAL_STARS ]; // tracks all stars/coordinates
290
290
291
291
/**
292
292
* Setup all the initial stars on the screen
@@ -334,18 +334,18 @@ static void twinkle_stars(void) {
334
334
continue ;
335
335
}
336
336
if (rand () % TWINKLE_PROBABILITY_MODULATOR == 0 ) {
337
- oled_write_pixel (star .x , star .y , false); // black out pixel
337
+ oled_write_pixel (star .x , star .y , false); // black out pixel
338
338
339
339
// don't allow stars to leave their own region
340
- if (star .x == (column_group * 8 )) { // star is the farthest left it can go in its region
341
- star .x ++ ; // move it right immediately
342
- } else if (star .x == (((column_group + 1 ) * 8 ) - 1 )) { // star is farthest right it can go in its region
343
- star .x -- ; // move it left immediately
340
+ if (star .x == (column_group * 8 )) { // star is the farthest left it can go in its region
341
+ star .x ++ ; // move it right immediately
342
+ } else if (star .x == (((column_group + 1 ) * 8 ) - 1 )) { // star is farthest right it can go in its region
343
+ star .x -- ; // move it left immediately
344
344
}
345
- if (star .y == (line * 8 )) { // star is the farthest up it can go in its region
346
- star .y ++ ; // move it down immediately
347
- } else if (star .y == (((line + 1 ) * 8 ) - 1 )) { // star is farthest down it can go in its region
348
- star .y -- ; // move it up immediately
345
+ if (star .y == (line * 8 )) { // star is the farthest up it can go in its region
346
+ star .y ++ ; // move it down immediately
347
+ } else if (star .y == (((line + 1 ) * 8 ) - 1 )) { // star is farthest down it can go in its region
348
+ star .y -- ; // move it up immediately
349
349
}
350
350
351
351
// now decide direction
@@ -389,10 +389,10 @@ static void animate_stars(void) {
389
389
twinkle_stars ();
390
390
}
391
391
}
392
- #endif // endregion
392
+ #endif // endregion
393
393
394
- #ifdef ENABLE_SHOOTING_STARS // region
395
- bool shooting_stars_setup = false; // only setup shooting stars array once with defaults
394
+ #ifdef ENABLE_SHOOTING_STARS // region
395
+ bool shooting_stars_setup = false; // only setup shooting stars array once with defaults
396
396
397
397
struct ShootingStar {
398
398
int x_1 ;
@@ -404,11 +404,11 @@ struct ShootingStar {
404
404
int delay ;
405
405
};
406
406
407
- struct ShootingStar shooting_stars [MAX_NUMBER_OF_SHOOTING_STARS ]; // tracks all the shooting stars
407
+ struct ShootingStar shooting_stars [MAX_NUMBER_OF_SHOOTING_STARS ]; // tracks all the shooting stars
408
408
409
409
static void setup_shooting_star (struct ShootingStar * shooting_star ) {
410
410
int column_to_start = rand () % (WIDTH / 2 );
411
- int row_to_start = rand () % (HEIGHT - 48 ); // shooting_stars travel diagonally 1 down, 1 across. So the lowest a shooting_star can start and not 'hit' the ocean is 32 above the ocean.
411
+ int row_to_start = rand () % (HEIGHT - 48 ); // shooting_stars travel diagonally 1 down, 1 across. So the lowest a shooting_star can start and not 'hit' the ocean is 32 above the ocean.
412
412
413
413
shooting_star -> x_1 = column_to_start ;
414
414
shooting_star -> y_1 = row_to_start ;
@@ -494,60 +494,61 @@ static void animate_shooting_stars(void) {
494
494
end_extra_stars (number_of_shooting_stars );
495
495
}
496
496
}
497
- #endif // endregion
497
+ #endif // endregion
498
498
499
499
/**
500
500
* Main rendering function
501
501
*
502
502
* Calls all different animations at different rates
503
503
*/
504
504
void render_stars (void ) {
505
- // // animation timer
506
- if (timer_elapsed32 (starry_night_anim_timer ) > STARRY_NIGHT_ANIM_FRAME_DURATION ) {
507
- starry_night_anim_timer = timer_read32 ();
508
- current_wpm = get_current_wpm ();
505
+ current_wpm = get_current_wpm ();
509
506
507
+ void render_stars_anim (void ) {
510
508
#ifdef ENABLE_ISLAND
511
- animate_island ();
509
+ animate_island ();
512
510
#endif
513
511
514
512
#ifdef ENABLE_SHOOTING_STARS
515
- if (animation_counter % SHOOTING_STAR_ANIMATION_MODULATOR == 0 ) {
516
- animate_shooting_stars ();
517
- }
513
+ if (animation_counter % SHOOTING_STAR_ANIMATION_MODULATOR == 0 ) {
514
+ animate_shooting_stars ();
515
+ }
518
516
#endif
519
517
520
518
#ifdef ENABLE_STARS
521
- // TODO offsetting the star animation from the wave animation would look better,
522
- // but if I do that, then the stars appear in the water because
523
- // the ocean animation has to wait a bunch of frames to overwrite it.
524
- // Possible solutions:
525
- // 1. Only draw stars to the top of the island/ocean.
526
- // 2. Draw ocean every frame, only move ocean on frames matching modulus
527
- // Problems:
528
- // 1. What if someone wants to move the island up a bit, or they want to have the stars reflect in the water?
529
- // 2. More cpu intensive. And I'm already running out of cpu as it is...
530
- if (animation_counter % STAR_ANIMATION_MODULATOR == 0 ) {
531
- animate_stars ();
532
- }
519
+ // TODO offsetting the star animation from the wave animation would look better,
520
+ // but if I do that, then the stars appear in the water because
521
+ // the ocean animation has to wait a bunch of frames to overwrite it.
522
+ // Possible solutions:
523
+ // 1. Only draw stars to the top of the island/ocean.
524
+ // 2. Draw ocean every frame, only move ocean on frames matching modulus
525
+ // Problems:
526
+ // 1. What if someone wants to move the island up a bit, or they want to have the stars reflect in the water?
527
+ // 2. More cpu intensive. And I'm already running out of cpu as it is...
528
+ if (animation_counter % STAR_ANIMATION_MODULATOR == 0 ) {
529
+ animate_stars ();
530
+ }
533
531
#endif
534
532
535
533
#ifdef ENABLE_WAVE
536
- if (animation_counter % OCEAN_ANIMATION_MODULATOR == 0 ) {
537
- animate_waves ();
538
- }
534
+ if (animation_counter % OCEAN_ANIMATION_MODULATOR == 0 ) {
535
+ animate_waves ();
536
+ }
539
537
#endif
540
538
541
539
#ifdef ENABLE_MOON
542
- draw_moon ();
540
+ draw_moon ();
543
541
#endif
544
542
545
- animation_counter = increment_counter (animation_counter , NUMBER_OF_FRAMES );
543
+ animation_counter = increment_counter (animation_counter , NUMBER_OF_FRAMES );
546
544
}
547
545
548
- // this fixes the screen on and off bug
549
- if (current_wpm > 0 ) {
546
+
547
+ // Turn screen on/off based on typing and timeout
548
+ if (current_wpm > 0 && timer_elapsed32 (starry_night_anim_timer ) > STARRY_NIGHT_ANIM_FRAME_DURATION ) {
549
+ starry_night_anim_timer = timer_read32 ();
550
550
oled_on ();
551
+ render_stars_anim ();
551
552
starry_night_anim_sleep = timer_read32 ();
552
553
} else if (timer_elapsed32 (starry_night_anim_sleep ) > OLED_TIMEOUT ) {
553
554
oled_off ();
0 commit comments