Guest User

Untitled

a guest
Sep 9th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.41 KB | None | 0 0
  1. #include "h/tube.h"
  2. #include <math.h>
  3.  
  4. // ANIMATION NONE
  5. animation_state_generic animation_none = {
  6.   .animation_init = NULL,
  7.   .animation_step = NULL,
  8. };
  9.  
  10.  
  11. // ANIMATION TRANSITION
  12. display_digits* display_animation_transition_step(display_state* ds);
  13.  
  14. animation_state_generic animation_transition = {
  15.   .animation_init = NULL,
  16.   .animation_step = display_animation_transition_step,
  17.   .duration = 10,
  18. };
  19.  
  20.  
  21. display_digits* display_animation_transition_step(display_state* ds){
  22.   animation_state_generic* a = ds->animation;
  23.  
  24.   if(a->step > a->duration || a->step % 2){
  25.     return &ds->digits;
  26.   }else {
  27.     return &a->previous;
  28.   }
  29.  
  30. }
  31.  
  32. //ANIMATION ROLL
  33. display_digits* display_animation_roll_step(display_state* ds);
  34.  
  35. typedef struct {
  36.   union {
  37.     animation_state_generic;
  38.     animation_state_generic as_generic;
  39.   };
  40.  
  41.   display_digits digits;
  42.  
  43. } animation_state_roll;
  44.  
  45. animation_state_roll animation_roll = {
  46.   .animation_init = NULL,
  47.   .animation_step = display_animation_roll_step,
  48.   .duration = 20
  49. };
  50.  
  51.  
  52. display_digits* display_animation_roll_step(display_state* ds){
  53.   animation_state_roll* a = (animation_state_roll*)ds->animation;
  54.  
  55.  
  56.   if(a->step > a->duration){
  57.     return &ds->digits;
  58.   }
  59.  
  60.   uint16_t step = a->step / (uint16_t) ceil( a->duration / 10 );
  61.  
  62.  
  63.   display_digits* curr = &ds->digits;
  64.   display_digits* prev = &a->previous;
  65.   display_digits* out = &a->digits;
  66.  
  67.   for(uint8_t i = 0; i < DISPLAY_DIGITS_COUNT; i++){
  68.    
  69.     if(curr->digit[i] != prev->digit[i]){
  70.      
  71.       out->digit[i] = (ds->digits.digit[i] + step) % 10;
  72.      
  73.     } else {
  74.       out->digit[i] = curr->digit[i];
  75.     }
  76.      
  77.   }
  78.  
  79.   out->dots = curr->dots;
  80.  
  81.   return out;
  82. }
  83.  
  84.  
  85.  
  86.  
  87. //ANIMATION SWITCH
  88.  
  89. void display_animation_switch_init(display_state* ds);
  90. display_digits* display_animation_switch_step(display_state* ds);
  91.  
  92. typedef struct {
  93.   union {
  94.     animation_state_generic;
  95.     animation_state_generic as_generic;
  96.   };
  97.  
  98.   uint16_t cnt;
  99.  
  100.   uint16_t curr_period;
  101.   uint16_t prev_period;
  102. } animation_state_switch;
  103.  
  104.  
  105.  
  106. animation_state_switch animation_switch = {
  107.   .animation_init = display_animation_switch_init,
  108.   .animation_step = display_animation_switch_step,
  109.   .duration = 20,
  110. };
  111.  
  112.  
  113.  
  114.  
  115. void display_animation_switch_init(display_state* ds){
  116.   animation_state_switch* state = (animation_state_switch*) ds->animation;
  117.  
  118.   state->curr_period = 4;
  119.   state->prev_period = 4;
  120. }  
  121.  
  122. display_digits* display_animation_switch_step(display_state* ds){
  123.   animation_state_switch* a = (animation_state_switch*) ds->animation;
  124.  
  125.  
  126.   a->cnt++;
  127.  
  128.   uint8_t ret_curr_d = 1;
  129.  
  130.   if(a->curr_period > 1){
  131.    
  132.     if(a->cnt % a->curr_period){
  133.       ret_curr_d = 1;
  134.     } else {
  135.       ret_curr_d = 0;
  136.      
  137.       a->cnt = 0;
  138.       a->curr_period--;
  139.     }
  140.      
  141.   }else if(a->prev_period > 1){
  142.    
  143.     if(a->cnt % a->prev_period){
  144.       ret_curr_d = 0;
  145.     } else {
  146.       ret_curr_d = 1;
  147.      
  148.       a->cnt = 0;
  149.       a->prev_period--;
  150.     }
  151.    
  152.   }else {
  153.     ret_curr_d = 1;
  154.   }
  155.  
  156.   if(ret_curr_d){
  157.     return &ds->digits;
  158.   }else {
  159.     return &a->previous;
  160.   }
  161.  
  162.  
  163. }      
  164.  
  165.  
  166.  
  167.  // ANIMATION_NONE = 0,
  168.  // ANIMATION_TRANSITION = 1,
  169.  // ANIMATION_SWITCH = 2,
  170.  // ANIMATION_ROLL = 3,
  171.  
  172. void* animations_list[4] = {
  173.   &animation_none,
  174.   &animation_transition,
  175.   &animation_switch,
  176.   &animation_roll,
  177. };
Add Comment
Please, Sign In to add comment