traffic_light_arduino.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "Arduino.h"
  2. #include <avr/sleep.h>
  3. #include "pushbutton.h"
  4. #include "TrafficLightCtrl.h"
  5. #include "TrafficLightCtrlRequired.h"
  6. #include "scutil/sc_timer_service.h"
  7. //! As we make use of time triggers (after & every) we make use of a generic timer implementation and need a defined number of timers.
  8. #define MAX_TIMERS 20
  9. const int button_1_pin = 3; // the number of the pushbutton 1 pin
  10. const int button_2_pin = 2; // the number of the pushbutton 2 pin
  11. const int led_green_pin = 8;
  12. const int led_yellow_pin = 12;
  13. const int led_red_pin = 9;
  14. const int ped_led_red_pin = 4;
  15. const int ped_led_green_pin = 5;
  16. const int ped_led_request_pin = 6;
  17. pushbutton_t pushbutton_1; // first pushbutton
  18. pushbutton_t pushbutton_2; // second pushbutton
  19. static TrafficLightCtrl trafficLight;
  20. //! We allocate the desired array of timers.
  21. static sc_timer_t timers[MAX_TIMERS];
  22. //! The timers are managed by a timer service. */
  23. static sc_timer_service_t timer_service;
  24. //! callback implementation for the setting up time events
  25. void trafficLightCtrl_setTimer(TrafficLightCtrl* handle, const sc_eventid evid, const sc_integer time_ms, const sc_boolean periodic){
  26. sc_timer_start(&timer_service, (void*) handle, evid, time_ms, periodic);
  27. }
  28. //! callback implementation for canceling time events.
  29. void trafficLightCtrl_unsetTimer(TrafficLightCtrl* handle, const sc_eventid evid) {
  30. sc_timer_cancel(&timer_service, evid);
  31. }
  32. static void button_1_changed(pushbutton_t *button) {
  33. if (!pushbutton_1.state) trafficLightCtrlIface_raise_pedestrianRequest(&trafficLight);
  34. }
  35. static void button_2_changed(pushbutton_t *button) {
  36. if (!pushbutton_2.state) trafficLightCtrlIface_raise_onOff(&trafficLight);
  37. }
  38. //The setup function is called once at startup of the sketch
  39. void setup()
  40. {
  41. setup_pushbutton(&pushbutton_1, button_1_pin, button_1_changed);
  42. setup_pushbutton(&pushbutton_2, button_2_pin, button_2_changed);
  43. pinMode(led_green_pin, OUTPUT);
  44. pinMode(led_yellow_pin, OUTPUT);
  45. pinMode(led_red_pin, OUTPUT);
  46. pinMode(ped_led_red_pin, OUTPUT);
  47. pinMode(ped_led_green_pin, OUTPUT);
  48. pinMode(ped_led_request_pin, OUTPUT);
  49. sc_timer_service_init(
  50. &timer_service,
  51. timers, MAX_TIMERS,
  52. (sc_raise_time_event_fp) &trafficLightCtrl_raiseTimeEvent);
  53. trafficLightCtrl_init(&trafficLight);
  54. trafficLightCtrl_enter(&trafficLight);
  55. }
  56. #define CYCLE_PERIOD (10)
  57. static unsigned long cycle_count = 0L;
  58. static unsigned long last_cycle_time = 0L;
  59. void loop() {
  60. unsigned long current_millies = millis();
  61. read_pushbutton(&pushbutton_1);
  62. read_pushbutton(&pushbutton_2);
  63. if ( cycle_count == 0L || (current_millies >= last_cycle_time + CYCLE_PERIOD) ) {
  64. sc_timer_service_proceed(&timer_service, current_millies - last_cycle_time);
  65. trafficLightCtrl_runCycle(&trafficLight);
  66. last_cycle_time = current_millies;
  67. cycle_count++;
  68. }
  69. }
  70. void trafficLightCtrlIface_synchronize(const TrafficLightCtrl *handle)
  71. {
  72. //synchronize red traffic light
  73. if(trafficLightCtrlIfaceTrafficLight_get_red(handle) == true)
  74. digitalWrite(led_red_pin, HIGH);
  75. else
  76. digitalWrite(led_red_pin, LOW);
  77. //synchronize yellow traffic light
  78. if(trafficLightCtrlIfaceTrafficLight_get_yellow(handle) == true)
  79. digitalWrite(led_yellow_pin, HIGH);
  80. else
  81. digitalWrite(led_yellow_pin, LOW);
  82. //synchronize green traffic light
  83. if(trafficLightCtrlIfaceTrafficLight_get_green(handle) == true)
  84. digitalWrite(led_green_pin, HIGH);
  85. else
  86. digitalWrite(led_green_pin, LOW);
  87. //synchronize red pedestrian traffic light
  88. if(trafficLightCtrlIfacePedestrian_get_red(handle) == true)
  89. digitalWrite(ped_led_red_pin, HIGH);
  90. else
  91. digitalWrite(ped_led_red_pin, LOW);
  92. //synchronize green pedestrian traffic light
  93. if(trafficLightCtrlIfacePedestrian_get_green(handle) == true)
  94. digitalWrite(ped_led_green_pin, HIGH);
  95. else
  96. digitalWrite(ped_led_green_pin, LOW);
  97. //synchronize pedestrian waiting light
  98. if(trafficLightCtrlIfacePedestrian_get_request(handle) == true)
  99. digitalWrite(ped_led_request_pin, HIGH);
  100. else
  101. digitalWrite(ped_led_request_pin, LOW);
  102. }