Advertisement
hakbraley

QMK Oled Code

Aug 3rd, 2021 (edited)
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1. #ifdef OLED_DRIVER_ENABLE // --------------------------------------------------
  2.  
  3. //configuration settings
  4. #define WPM_THRESHOLD 70
  5. #define WPM_DURATION 4000
  6. #define LOWEST_WPM 60
  7. #define FRAMERATE 20  //20 frames per second
  8. #define NUM_FRAMES 5  //number of frames in the animation
  9.  
  10. const uint16_t FRAME_DURATION = 1000/FRAMERATE;  //length of each frame in ms
  11.  
  12. void render_anim(void) {
  13.     static uint16_t frame_timer = 0;    //when was the last frame drawn?
  14.     static uint16_t current_frame = 0;  //which frame is the animation on?
  15.     static bool anim_active = false;    //is the animation going?
  16.     static uint16_t wpm_timer = 0;      //when was WPM_THRESHOLD reached?
  17.    
  18.     //exit and do nothing if it is not yet time to draw the next frame
  19.     if (timer_elapsed(frame_timer) < FRAME_DURATION)
  20.         return;
  21.    
  22.     frame_timer = timer_read();  //set the time that this frame is calculated/drawn
  23.    
  24.     uint8_t wpm = get_current_wpm();    //store the current wpm on each program loop
  25.    
  26.     //if wpm is below LOWEST_WPM, reset the timer to 0
  27.     if (wpm < LOWEST_WPM) {
  28.         wpm_timer = 0;
  29.         //reset OLED if animation was started
  30.         if (anim_active) {
  31.             oled_clear();
  32.             anim_active = false;
  33.             current_frame = 0;
  34.         }
  35.         return;
  36.     }
  37.    
  38.     //if wpm_timer is 0, mark the time when wpm reaches WPM_THRESHOLD
  39.     if (!wpm_timer && wpm > WPM_THRESHOLD) {
  40.         wpm_timer = timer_read();
  41.     }
  42.    
  43.     //reset wpm_timer if it is set and wpm goes below WPM_THRESHOLD before WPM_DURATION
  44.     if (wpm_timer && !anim_active && wpm < WPM_THRESHOLD) {
  45.         wpm_timer = 0;
  46.     }
  47.    
  48.     //if WPM_THRESHOLD has been maintained for WPM_DURATION time, start animation
  49.     if (wpm_timer && timer_elapsed(wpm_timer) >= WPM_DURATION) {
  50.         anim_active = true;
  51.     }
  52.    
  53.     //if animation has started and wpm is above LOWEST_WPM, draw a frame and advance to the next one
  54.     if (anim_active && wpm >= LOWEST_WPM) {
  55.         oled_write_raw_P(frame[current_frame], FRAME_SIZE);
  56.         current_frame = (current_frame + 1) % NUM_FRAMES;
  57.     }
  58. }
  59.  
  60.  
  61. //this is called automatically on every program loop
  62. void oled_task_user(void) {
  63.     render_anim();
  64. }
  65.  
  66. #endif  //OLED_DRIVER_ENABLE --------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement