MartinSRB

[НРС] Вежбе 14 - Задатак 1

May 12th, 2023
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include<arduinoPlatform.h>
  2. #include<tasks.h>
  3. #include<interrupts.h>
  4. #include<stdio.h>
  5. #include<serial.h>
  6. #include <pwm.h>
  7. #include<data.h>
  8.  
  9. extern serial Serial;
  10.  
  11. typedef void interruptFunction();
  12.  
  13. #define BTN1 4
  14. #define LD2 27
  15.  
  16. short BTN1_old_state;
  17. interruptFunction *falling_edge_function = NULL,
  18.                   *rising_edge_function  = NULL;
  19.  
  20. bool attachInterrupt_BTN1(interruptFunction *func, int edge_mode){
  21.     if(edge_mode == RISING){
  22.         rising_edge_function = func;
  23.         return true;
  24.     }else if(edge_mode == FALLING){
  25.         falling_edge_function = func;
  26.         return true;
  27.     }
  28.     return false;
  29. }
  30.  
  31. bool detachInterrupt_BTN1(int edge_mode){ //u potpunosti nepotrebna funckija za zadatak...
  32.     if(edge_mode == RISING && rising_edge_function != NULL){
  33.         rising_edge_function = NULL;
  34.         return true;
  35.     }else if(edge_mode == FALLING && falling_edge_function != NULL){
  36.         falling_edge_function = NULL;
  37.         return true;
  38.     }
  39.     return false;
  40. }
  41.  
  42. void interruptRising(){
  43.     digitalWrite(LD2, HIGH);
  44. }
  45.  
  46. void interruptFalling(){
  47.     digitalWrite(LD2, LOW);
  48. }
  49.  
  50. void button_task(int id, void *ptrt){
  51.     int new_state = digitalRead(BTN1);
  52.     if(BTN1_old_state < new_state){
  53.         if(rising_edge_function != NULL){
  54.             rising_edge_function();
  55.         }
  56.     }else if(BTN1_old_state > new_state){
  57.         if(falling_edge_function != NULL){
  58.             falling_edge_function();
  59.         }
  60.     }
  61.     if(BTN1_old_state != new_state){
  62.         BTN1_old_state = new_state;
  63.     }
  64. }
  65.  
  66. void setup()
  67. {
  68.     BTN1_old_state = digitalRead(BTN1);
  69.     attachInterrupt_BTN1(interruptFalling, FALLING);
  70.     attachInterrupt_BTN1(interruptRising, RISING);
  71.     createTask(button_task, 50, TASK_ENABLE, NULL);
  72. }
  73.  
  74. void loop()
  75. {
  76.  
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment