led.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* sd2snes - SD card based universal cartridge for the SNES
  2. Copyright (C) 2009-2010 Maximilian Rehkopf <otakon@gmx.net>
  3. AVR firmware portion
  4. Inspired by and based on code from sd2iec, written by Ingo Korb et al.
  5. See sdcard.c|h, config.h.
  6. FAT file system access based on code by ChaN, Jim Brain, Ingo Korb,
  7. see ff.c|h.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; version 2 of the License only.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. led.c: LED control
  19. */
  20. #include <avr/io.h>
  21. #include <util/delay.h>
  22. #include "config.h"
  23. #include "led.h"
  24. static uint8_t led_bright[16]={255,253,252,251,249,247,244,239,232,223,210,191,165,127,74,0};
  25. static uint8_t curr_bright = 15;
  26. static uint8_t led_bounce_dir = 1;
  27. volatile uint8_t led_state;
  28. void led_panic(void) {
  29. led_std();
  30. while(1) {
  31. set_pwr_led(1);
  32. set_busy_led(1);
  33. _delay_ms(150);
  34. set_pwr_led(0);
  35. set_busy_led(0);
  36. _delay_ms(150);
  37. }
  38. }
  39. void toggle_busy_led(void) {
  40. PORTB &= ~_BV(PB3);
  41. DDRB ^= _BV(PB3);
  42. }
  43. void toggle_pwr_led(void) {
  44. PORTB &= ~_BV(PB0);
  45. DDRB ^= _BV(PB0);
  46. }
  47. void set_busy_led(uint8_t state) {
  48. PORTB &= ~_BV(PB3);
  49. if(state) {
  50. DDRB |= _BV(PB3);
  51. } else {
  52. DDRB &= ~_BV(PB3);
  53. }
  54. }
  55. void set_pwr_led(uint8_t state) {
  56. PORTB &= ~_BV(PB0);
  57. if(state) {
  58. DDRB |= _BV(PB0);
  59. } else {
  60. DDRB &= ~_BV(PB0);
  61. }
  62. }
  63. void set_busy_pwm(uint8_t brightness) {
  64. OCR0A = led_bright[brightness];
  65. set_busy_led(1);
  66. }
  67. void bounce_busy_led() {
  68. set_busy_pwm(curr_bright);
  69. if(led_bounce_dir) {
  70. curr_bright--;
  71. if(curr_bright==0) {
  72. led_bounce_dir = 0;
  73. }
  74. } else {
  75. curr_bright++;
  76. if(curr_bright==15) {
  77. led_bounce_dir = 1;
  78. }
  79. }
  80. }
  81. void led_pwm() {
  82. set_busy_led(1);
  83. TCCR0A = 0x83;
  84. TCCR0B = 0x01;
  85. }
  86. void led_std() {
  87. set_busy_led(0);
  88. TCCR0A = 0;
  89. TCCR0B = 0;
  90. }