Advertisement
harinin

Untitled

Aug 2nd, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * File:   events.h
  3.  * Author: maxL
  4.  *
  5.  * Created on July 28, 2016, 12:36 PM
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <timers.h>
  10. #include <AD.h>
  11. #include "events.h"
  12. #include "roach.h"
  13. #include "CalibConstants.h"
  14.  
  15. // Check for all events:
  16.  
  17. event_t CheckForEvents(void) {
  18.     event_t thisEvent = NO_EVENT;
  19.  
  20.     thisEvent = CheckForEnteredLight();
  21.     if (thisEvent != NO_EVENT) {
  22.         return thisEvent;
  23.     }
  24.  
  25.     thisEvent = CheckForEnteredDark();
  26.     if (thisEvent != NO_EVENT) {
  27.         return thisEvent;
  28.     }
  29.    
  30.     thisEvent = CheckForFrontBumpDown();
  31.     if (thisEvent != NO_EVENT) {
  32.         return thisEvent;
  33.     }
  34.    
  35.     thisEvent = CheckForFLBumpDown();
  36.     if (thisEvent != NO_EVENT) {
  37.         return thisEvent;
  38.     }
  39.    
  40.     thisEvent = CheckForFRBumpDown();
  41.     if (thisEvent != NO_EVENT) {
  42.         return thisEvent;
  43.     }
  44.    
  45.     thisEvent = CheckForBackBumpDown();
  46.     if (thisEvent != NO_EVENT) {
  47.         return thisEvent;
  48.     }
  49.  
  50.     thisEvent = CheckForTimer0Expired();
  51.     if (thisEvent != NO_EVENT) {
  52.         return thisEvent;
  53.     }
  54.     return NO_EVENT;
  55. }
  56.  
  57. // define each event checker
  58.  
  59. event_t CheckForEnteredLight(void) {
  60.     // all event checkers remember their previous Event:
  61.     static int previousEvent;
  62.  
  63.     // all event checkers update their event:
  64.     int currentEvent = (Roach_LightLevel() < LIGHT_THRESHOLD);
  65.     // current_status = ???
  66.  
  67.     // if there has been an important change, return an event:
  68.     if (!previousEvent && currentEvent) {
  69.         previousEvent = currentEvent;
  70.         return ENTERED_LIGHT;
  71.         //return SOME_EVENT
  72.     }
  73.  
  74.     //and if there has not been an important change,
  75.     return NO_EVENT;
  76. }
  77.  
  78. event_t CheckForEnteredDark(void) {
  79.     // all event checkers remember their previous Event:
  80.     static int previousEvent;
  81.  
  82.     // all event checkers update their event:
  83.     int currentEvent = (Roach_LightLevel() > DARK_THRESHOLD);
  84.     // current_status = ???
  85.  
  86.     // if there has been an important change, return an event:
  87.     if (!previousEvent && currentEvent) {
  88.         previousEvent = currentEvent;
  89.         return ENTERED_DARK;
  90.         //return SOME_EVENT
  91.     }
  92.  
  93.     //and if there has not been an important change,
  94.     return NO_EVENT;
  95. }
  96.  
  97. event_t CheckForFrontBumpDown(void) {
  98.     // all event checkers remember their previous Event:
  99.     static int previousEvent;
  100.  
  101.     // all event checkers update their event:
  102.     int currentEvent = (Roach_ReadFrontLeftBumper() << 1) + Roach_ReadFrontRightBumper();
  103.  
  104.     // if there has been an important change, return an event:
  105.     if (!previousEvent && currentEvent) {
  106.         previousEvent = currentEvent;
  107.         return FRONT_BUMP_DOWN;
  108.     }
  109.  
  110.     //and if there has not been an important change,
  111.     return NO_EVENT;
  112. }
  113.  
  114. event_t CheckForFLBumpDown(void) {
  115.     // all event checkers remember their previous Event:
  116.     static int previousEvent;
  117.  
  118.     // all event checkers update their event:
  119.     int currentEvent = Roach_ReadFrontLeftBumper();
  120.  
  121.     // if there has been an important change, return an event:
  122.     if (!previousEvent && currentEvent) {
  123.         previousEvent = currentEvent;
  124.         return FL_BUMP_DOWN;
  125.     }
  126.  
  127.     //and if there has not been an important change,
  128.     return NO_EVENT;
  129. }
  130.  
  131. event_t CheckForFRBumpDown(void) {
  132.     // all event checkers remember their previous Event:
  133.     static int previousEvent;
  134.  
  135.     // all event checkers update their event:
  136.     int currentEvent = Roach_ReadFrontRightBumper();
  137.  
  138.     // if there has been an important change, return an event:
  139.     if (!previousEvent && currentEvent) {
  140.         previousEvent = currentEvent;
  141.         return FR_BUMP_DOWN;
  142.     }
  143.  
  144.     //and if there has not been an important change,
  145.     return NO_EVENT;
  146. }
  147.  
  148. event_t CheckForBackBumpDown(void) {
  149.     // all event checkers remember their previous Event:
  150.     static int previousEvent;
  151.  
  152.     // all event checkers update their event:
  153.     int currentEvent = (Roach_ReadRearRightBumper() << 1) + Roach_ReadRearLeftBumper();
  154.  
  155.     // if there has been an important change, return an event:
  156.     if (!previousEvent && currentEvent) {
  157.         previousEvent = currentEvent;
  158.         return BACK_BUMP_DOWN;
  159.     }
  160.  
  161.     //and if there has not been an important change,
  162.     return NO_EVENT;
  163. }
  164.  
  165. event_t CheckForTimer0Expired(void) {
  166.     // all event checkers remember their previous Event:...NAH
  167.     static int previousEvent;
  168.  
  169.     // all event checkers update their event:
  170.     int currentEvent = IsTimerExpired(0);
  171.     // current_status = ???
  172.  
  173.     // if there has been an important change, return an event:
  174.     if (!previousEvent && currentEvent) {
  175.         previousEvent = currentEvent;
  176.         return TIMER_0_EXPIRED;
  177.     }
  178.    
  179.     //and if there has not been an important change,
  180.     return NO_EVENT;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement