-
Notifications
You must be signed in to change notification settings - Fork 15
/
fade.c
72 lines (61 loc) · 2.11 KB
/
fade.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Open-BLDC Firmware, Firmware for BrushLess Drive Controllers
* Copyright (C) 2009 Piotr Esden-Tempski <piotr at esden.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <avr/io.h>
#include <stdint.h>
#include "led.h"
#include "fade.h"
#define LED_DUTY_CYCLE_SIZE 200
uint8_t led_bright_red_counter = 0;
uint8_t led_bright_red_dir = 1;
uint8_t led_bright_red = 0;
uint8_t led_bright_green_counter = 200;
uint8_t led_bright_green_dir = -1;
uint8_t led_bright_green = 0;
uint8_t led_duty_step = 0;
uint8_t calculate_led_bright_red();
uint8_t calculate_led_bright_green();
void fade(){
if(led_duty_step == 0){
LED_RED_ON();
LED_GREEN_ON();
}
if(led_duty_step == led_bright_red){
LED_RED_OFF();
}
if(led_duty_step == led_bright_green){
LED_GREEN_OFF();
}
led_duty_step++;
if(led_duty_step > LED_DUTY_CYCLE_SIZE){
led_duty_step = 0;
led_bright_red = calculate_led_bright_red();
led_bright_green = calculate_led_bright_green();
}
}
uint8_t calculate_led_bright_red(){
led_bright_red_counter+=led_bright_red_dir;
if(led_bright_red_counter == 200) led_bright_red_dir = -1;
if(led_bright_red_counter == 0) led_bright_red_dir = 1;
return led_bright_red_counter;
}
uint8_t calculate_led_bright_green(){
led_bright_green_counter+=led_bright_green_dir;
if(led_bright_green_counter == 200) led_bright_green_dir = -1;
if(led_bright_green_counter == 0) led_bright_green_dir = 1;
return (led_bright_green_counter * 0.3);
}