-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
134 lines (119 loc) · 2.87 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <avr/io.h>
#include "wiring.h"
#include "fast_random.h"
#define HIGH 1
#define LOW 0
// Analog pins
#define A0 0x00 // PB5
#define A1 0x01 // PB2
#define A2 0x02 // PB4
#define A3 0x03 // PB3
#define PIN_COUNT 3
#define INTERVAL 6000UL
#define CHANGE_DELAY 10UL
#define PWM_MAX 255
#define MICRO_INTERVAL 60
uint32_t current_micros = 0;
uint32_t last_micros = 0;
struct pwm_pin
{
uint8_t pin_number;
uint8_t pwm_value;
uint8_t pwm_tick_count;
uint8_t pwm_pin_state;
uint8_t pin_state;
};
static void init_pin_default(struct pwm_pin *pin)
{
pin->pwm_value = 0;
pin->pwm_tick_count = 0;
pin->pwm_pin_state = HIGH;
pin->pin_state = HIGH;
}
static void handle_pwm(struct pwm_pin *pins)
{
if (current_micros - last_micros >= MICRO_INTERVAL)
{
for (uint8_t i = 0; i < PIN_COUNT; ++i)
{
if (pins[i].pwm_value == PWM_MAX)
PORTB |= _BV(pins[i].pin_number);
else if (pins[i].pwm_value == 0)
PORTB &= ~_BV(pins[i].pin_number);
else
{
++(pins[i].pwm_tick_count);
if (pins[i].pwm_pin_state == HIGH)
{
if (pins[i].pwm_tick_count >= pins[i].pwm_value)
pins[i].pwm_pin_state = LOW;
}
else
{
if (pins[i].pwm_tick_count >= PWM_MAX)
{
pins[i].pwm_pin_state = HIGH;
pins[i].pwm_tick_count = 0;
}
}
if (pins[i].pwm_pin_state == HIGH)
PORTB |= _BV(pins[i].pin_number);
else
PORTB &= ~_BV(pins[i].pin_number);
}
}
last_micros = current_micros;
}
}
uint8_t enabled_pin_count(struct pwm_pin *pins)
{
uint8_t count = 0;
for (uint8_t i = 0; i < PIN_COUNT; ++i)
{
if (pins[i].pin_state == HIGH)
++count;
}
return count;
}
int main(void)
{
init();
fast_random_seed(analog_read(A3) % 255);
DDRB |= _BV(DDB0) | _BV(DDB1) | _BV(DDB2);
struct pwm_pin pins[PIN_COUNT];
for (uint8_t i = 0; i < PIN_COUNT; ++i)
init_pin_default(&pins[i]);
pins[0].pin_number = PB0;
pins[1].pin_number = PB1;
pins[2].pin_number = PB2;
uint32_t last_step_micros = 0UL;
uint32_t last_update_time = 0UL;
while (1)
{
current_micros = micros();
handle_pwm(pins);
if (current_micros - last_step_micros >= CHANGE_DELAY * 1000UL)
{
for (uint8_t i = 0; i < PIN_COUNT; ++i)
{
if (current_micros - last_update_time >= INTERVAL * 1000UL)
{
pins[i].pin_state = enabled_pin_count(pins) > 1 ? fast_random() < 128 : HIGH;
if (i == PIN_COUNT - 1)
last_update_time = current_micros;
}
if (pins[i].pin_state == HIGH)
{
if (pins[i].pwm_value < 255)
++(pins[i].pwm_value);
}
else
{
if (pins[i].pwm_value > 0)
--(pins[i].pwm_value);
}
}
last_step_micros = current_micros;
}
}
}