You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.4KB

  1. // Copyright 2011 Peter Kvitek
  2. //
  3. // Author: Peter Kvitek (pete@kvitek.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Single pin LED device, sink or source current. Also a flashing LED device:
  19. // being set to on LED stays lit for specified about of tick cycles.
  20. #ifndef AVRLIB_DEVICES_LED_H_
  21. #define AVRLIB_DEVICES_LED_H_
  22. #include <avr/io.h>
  23. #include "avrlib/gpio.h"
  24. namespace avrlib {
  25. enum LedMode {
  26. LED_SINK_CURRENT,
  27. LED_SOURCE_CURRENT,
  28. };
  29. template<typename LedPort,
  30. LedMode led_mode = LED_SINK_CURRENT>
  31. class Led {
  32. public:
  33. static void Init() {
  34. LedPort::set_mode(DIGITAL_OUTPUT);
  35. Off();
  36. }
  37. static inline void On(bool on) {
  38. if (on) {
  39. On();
  40. } else
  41. Off();
  42. }
  43. static inline void On() {
  44. if (led_mode == LED_SINK_CURRENT) {
  45. LedPort::Low();
  46. } else
  47. LedPort::High();
  48. }
  49. static inline void Off() {
  50. if (led_mode == LED_SINK_CURRENT) {
  51. LedPort::High();
  52. } else
  53. LedPort::Low();
  54. }
  55. };
  56. template<typename LedT,
  57. uint8_t on_count = 10>
  58. class FlashLed {
  59. public:
  60. FlashLed() { }
  61. static void Init() {
  62. LedT::Init();
  63. on_count_ = 0;
  64. }
  65. static inline void On() {
  66. LedT::On();
  67. on_count_ = on_count;
  68. }
  69. static inline void Off() {
  70. LedT::Off();
  71. on_count_ = 0;
  72. }
  73. static inline void Tick() {
  74. if (on_count_) {
  75. if (--on_count_ == 0) {
  76. LedT::Off();
  77. }
  78. }
  79. }
  80. private:
  81. static volatile uint8_t on_count_;
  82. DISALLOW_COPY_AND_ASSIGN(FlashLed);
  83. };
  84. // Static variables created for each instance
  85. template<typename LedT,
  86. uint8_t on_count>
  87. volatile uint8_t FlashLed<LedT, on_count>::on_count_ = 0;
  88. } // namespace avrlib
  89. #endif // AVRLIB_DEVICES_LED_H_