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.

84 lines
2.4 KiB

  1. /*
  2. Copyright 2016-2017 Ralf Schmitt <ralf@bunkertor.net> Rasmus Schults <rasmusx@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/interrupt.h>
  15. #include <avr/io.h>
  16. #include <stdbool.h>
  17. #include <util/delay.h>
  18. #include "duck_led/duck_led.h"
  19. #define LED_T1H 900
  20. #define LED_T1L 600
  21. #define LED_T0H 400
  22. #define LED_T0L 900
  23. void send_bit_d4(bool bitVal) {
  24. if(bitVal) {
  25. asm volatile (
  26. "sbi %[port], %[bit] \n\t"
  27. ".rept %[onCycles] \n\t"
  28. "nop \n\t"
  29. ".endr \n\t"
  30. "cbi %[port], %[bit] \n\t"
  31. ".rept %[offCycles] \n\t"
  32. "nop \n\t"
  33. ".endr \n\t"
  34. ::
  35. [port] "I" (_SFR_IO_ADDR(PORTD)),
  36. [bit] "I" (4),
  37. [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2),
  38. [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
  39. } else {
  40. asm volatile (
  41. "sbi %[port], %[bit] \n\t"
  42. ".rept %[onCycles] \n\t"
  43. "nop \n\t"
  44. ".endr \n\t"
  45. "cbi %[port], %[bit] \n\t"
  46. ".rept %[offCycles] \n\t"
  47. "nop \n\t"
  48. ".endr \n\t"
  49. ::
  50. [port] "I" (_SFR_IO_ADDR(PORTD)),
  51. [bit] "I" (4),
  52. [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2),
  53. [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
  54. }
  55. }
  56. void send_value(uint8_t byte, enum Device device) {
  57. for(uint8_t b = 0; b < 8; b++) {
  58. if(device == Device_STATUSLED) {
  59. send_bit_d4(byte & 0b10000000);
  60. byte <<= 1;
  61. }
  62. }
  63. }
  64. void send_color(uint8_t r, uint8_t g, uint8_t b, enum Device device) {
  65. send_value(r, device);
  66. send_value(g, device);
  67. send_value(b, device);
  68. }
  69. void indicator_leds_set(bool leds[8]) {
  70. cli();
  71. send_color(leds[1] ? 255 : 0, leds[2] ? 255 : 0, leds[0] ? 255 : 0, Device_STATUSLED);
  72. send_color(leds[4] ? 255 : 0, leds[5] ? 255 : 0, leds[3] ? 255 : 0, Device_STATUSLED);
  73. send_color(leds[6] ? 255 : 0, leds[7] ? 255 : 0, 0, Device_STATUSLED);
  74. sei();
  75. show();
  76. }