-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_state_display.cpp
76 lines (70 loc) · 1.56 KB
/
led_state_display.cpp
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
#include <Arduino.h>
#include "led_state_display.h"
LED_state_display::LED_state_display()
{
this->curr_millis = millis();
this->last_millis = this->curr_millis;
this->total_blinks = 0;
this->curr_blink = 0;
this->led_on = false;
this->pin = -1;
}
void LED_state_display::setup(int pin)
{
this->pin = pin;
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
this->led_on = false;
}
void LED_state_display::set_blinks(unsigned int blinks)
{
this->curr_blink = 0;
this->total_blinks = blinks;
}
unsigned int LED_state_display::get_blinks(void)
{
return this->total_blinks;
}
void LED_state_display::update(void)
{
this->curr_millis = millis();
if (this->total_blinks != 0)
{
if (this->led_on == false) // LED is off
{
if (this->curr_blink < this->total_blinks)
{
if ((this->curr_millis - this->last_millis) > LED_PAUSE_SHORT)
{
digitalWrite(this->pin, LOW);
this->led_on = true;
this->curr_blink++;
if (this->curr_blink > this->total_blinks)
this->curr_blink = 1;
this->last_millis = this->curr_millis;
}
}
else
{
if ((this->curr_millis - this->last_millis) > LED_PAUSE_LONG)
{
digitalWrite(this->pin, LOW);
this->led_on = true;
this->curr_blink++;
if (this->curr_blink > this->total_blinks)
this->curr_blink = 1;
this->last_millis = this->curr_millis;
}
}
}
else // LED is on
{
if ((this->curr_millis - this->last_millis) > LED_ON_DURATION)
{
digitalWrite(this->pin, HIGH);
this->led_on = false;
this->last_millis = this->curr_millis;
}
}
}
}