Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef OLED_DRIVER_ENABLE // --------------------------------------------------
- //configuration settings
- #define WPM_THRESHOLD 70
- #define WPM_DURATION 4000
- #define LOWEST_WPM 60
- #define FRAMERATE 20 //20 frames per second
- #define NUM_FRAMES 5 //number of frames in the animation
- const uint16_t FRAME_DURATION = 1000/FRAMERATE; //length of each frame in ms
- void render_anim(void) {
- static uint16_t frame_timer = 0; //when was the last frame drawn?
- static uint16_t current_frame = 0; //which frame is the animation on?
- static bool anim_active = false; //is the animation going?
- static uint16_t wpm_timer = 0; //when was WPM_THRESHOLD reached?
- //exit and do nothing if it is not yet time to draw the next frame
- if (timer_elapsed(frame_timer) < FRAME_DURATION)
- return;
- frame_timer = timer_read(); //set the time that this frame is calculated/drawn
- uint8_t wpm = get_current_wpm(); //store the current wpm on each program loop
- //if wpm is below LOWEST_WPM, reset the timer to 0
- if (wpm < LOWEST_WPM) {
- wpm_timer = 0;
- //reset OLED if animation was started
- if (anim_active) {
- oled_clear();
- anim_active = false;
- current_frame = 0;
- }
- return;
- }
- //if wpm_timer is 0, mark the time when wpm reaches WPM_THRESHOLD
- if (!wpm_timer && wpm > WPM_THRESHOLD) {
- wpm_timer = timer_read();
- }
- //reset wpm_timer if it is set and wpm goes below WPM_THRESHOLD before WPM_DURATION
- if (wpm_timer && !anim_active && wpm < WPM_THRESHOLD) {
- wpm_timer = 0;
- }
- //if WPM_THRESHOLD has been maintained for WPM_DURATION time, start animation
- if (wpm_timer && timer_elapsed(wpm_timer) >= WPM_DURATION) {
- anim_active = true;
- }
- //if animation has started and wpm is above LOWEST_WPM, draw a frame and advance to the next one
- if (anim_active && wpm >= LOWEST_WPM) {
- oled_write_raw_P(frame[current_frame], FRAME_SIZE);
- current_frame = (current_frame + 1) % NUM_FRAMES;
- }
- }
- //this is called automatically on every program loop
- void oled_task_user(void) {
- render_anim();
- }
- #endif //OLED_DRIVER_ENABLE --------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement