Download the file
  1. #include "TwoColorLED.h"
  2.  
  3. #define COLOR 0
  4. #define ONOFF 1
  5. #define YELLOW 2
  6. #define ACTIVE 3
  7.  
  8. TwoColorLED::TwoColorLED(uint8_t pin1, uint8_t pin2) {
  9. red_pin = pin1;
  10. green_pin = pin2;
  11. state = 0;
  12. pinMode(red_pin, OUTPUT);
  13. pinMode(green_pin, OUTPUT);
  14. digitalWrite(red_pin, LOW);
  15. digitalWrite(green_pin, LOW);
  16. }
  17.  
  18. void TwoColorLED::red() {
  19. state |= 1<<COLOR; // RED is 1
  20. state &= ~(1<<YELLOW);
  21. state |= 1<<ACTIVE;
  22. on();
  23. }
  24.  
  25. void TwoColorLED::green() {
  26. state &= ~(1<<COLOR); // GREEN is 0
  27. state &= ~(1<<YELLOW);
  28. state |= 1<<ACTIVE;
  29. on();
  30. }
  31.  
  32. void TwoColorLED::yellow() {
  33. state |= 1<<YELLOW;
  34. state |= 1<<ACTIVE;
  35. on();
  36. }
  37.  
  38. void TwoColorLED::deactivate() {
  39. state &= ~(1<<ACTIVE);
  40. }
  41.  
  42. void TwoColorLED::on() {
  43. state |= 1<<ONOFF;
  44. }
  45.  
  46. void TwoColorLED::off() {
  47. state &= ~(1<<ONOFF);
  48. }
  49.  
  50. void TwoColorLED::apply() {
  51. if (state & (1<<COLOR)) {
  52. digitalWrite(green_pin, LOW);
  53. if (state & (1<<ONOFF))
  54. digitalWrite(red_pin, HIGH);
  55. else
  56. digitalWrite(red_pin, LOW);
  57. }
  58. else {
  59. digitalWrite(red_pin, LOW);
  60. if (state & (1<<ONOFF))
  61. digitalWrite(green_pin, HIGH);
  62. else
  63. digitalWrite(green_pin, LOW);
  64. }
  65. }
  66.  
  67. void TwoColorLED::pulse(uint8_t onoff) {
  68. uint8_t old_state = state;
  69. if (state & (1<<ACTIVE)) {
  70. if (onoff) {
  71. if (state & (1<<YELLOW))
  72. state ^= (1<<COLOR);
  73. on();
  74. }
  75. else {
  76. off();
  77. }
  78. }
  79. else {
  80. off();
  81. }
  82. if (state != old_state)
  83. apply();
  84. }
  85.  
  86. void TwoColorLED::print() {
  87. Serial.println(state, BIN);
  88. }
  89. // vim:ft=arduino
  90.