Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. #include "h/button.h"
  2.  
  3.  
  4. static button_t* first_btn = NULL;
  5. static button_t* last_btn = NULL;
  6.  
  7.  
  8. void button_init(button_t *btn, GPIO_TypeDef* port, uint8_t pin){
  9.     memset(btn, 0, sizeof(button_t) );
  10.  
  11.     btn->port = port;
  12.     btn->pin = pin;
  13.    
  14.     btn->hold_thresh = BUTTON_HOLD_THRESH;
  15.     btn->prev_state = ! PRESSED_STATE;
  16.    
  17.     btn->hold_recall = 1;
  18.    
  19.     if(!first_btn){
  20.       first_btn = btn;
  21.     }
  22.    
  23.     if(last_btn){ // replace with search maybe?
  24.       last_btn->next = btn;
  25.     }
  26.    
  27.     last_btn = btn;
  28. }
  29.  
  30.  
  31. void button_proccess(button_t *btn){
  32.     int current_state = HAL_GPIO_ReadPin(btn->port, btn->pin);
  33.  
  34.    
  35.     if(current_state == PRESSED_STATE){
  36.      
  37.       btn->pressed_time += BUTTON_POLL_INTERVAL;
  38.  
  39.       if( btn->onpress && (btn->prev_state != current_state)){
  40.         btn->onpress(btn);
  41.       }
  42.      
  43.       else if(btn->onhold && btn->hold_recall && (btn->pressed_time >= btn->hold_thresh) ){
  44.         btn->hold_recall = btn->onhold(btn);
  45.       }
  46.      
  47.        
  48.     } else {
  49.      
  50.       if(btn->prev_state != current_state){
  51.         btn->hold_recall = 1;
  52.         btn->pressed_time = 0;
  53.        
  54.         if(btn->onrelease){
  55.           btn->onrelease(btn);
  56.         }
  57.       }
  58.      
  59.     }
  60.    
  61.     btn->prev_state = current_state;
  62. }
  63.  
  64.  
  65. void button_process_all(void){
  66.     button_t* next = first_btn;
  67.    
  68.     while(next){
  69.       button_proccess(next);
  70.      
  71.       next = next->next;
  72.     }
  73. }
  74.  
  75. /* button class */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement