forked from microfarad-de/LiCharger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiCharger.h
105 lines (86 loc) · 2.77 KB
/
LiCharger.h
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
/*
* Lithium-Ion Battery Charger Class
*
* This source file can be found under:
* http://www.github.com/arduino-library/LiCharger
*
* Please visit:
* http://www.microfarad.de
* http://www.github.com/microfarad-de
* http://www.github.com/arduino-library
*
* Copyright (C) 2019 Karim Hraibi (khraibi at gmail.com)
*
* 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/>.
*
*/
#ifndef __LiCharger_H_
#define __LiCharger_H_
#include <Arduino.h>
/*
* State machine states
*/
enum LiChargerState_t {
LI_CHARGER_STATE_STANDBY_E, // Standby (state entry)
LI_CHARGER_STATE_STANDBY, // Standby
LI_CHARGER_STATE_CHARGE_E, // Charging (state entry)
LI_CHARGER_STATE_CHARGE // Charging
};
/*
* Lithium-Ion battery charger class
*/
class LiChargerClass {
public:
/*
* Initialization function
*/
void initialize (
uint8_t nCells, // N_cells - Number of Lithium-Ion cells
uint16_t iChrg, // I_chrg - Constant charging current in mA
void (*callbackFct)( // Callback function for controlling the PWM hardware
uint8_t pwm // PWM duty cycle
)
);
/*
* This function must be called within the Arduino main loop
* it provides the Li-Ion charger with the latest voltage and
* current readings
*/
void loopHandler (
uint32_t v, // V - battery voltage in µV
uint32_t i // I - battery current in µA
);
/*
* Start the charging process
*/
void start (void);
/*
* Stop the charging process
*/
void stop (void);
bool active = false; // Indicates if the charging is active
uint8_t nCells = 1; // N_cells - Number of Lithium-Ion cells
uint16_t iChrg = 0; // I_chrg - Maximum charging current in mA
uint8_t pwm = 0; // PWM duty cycle (0..255)
LiChargerState_t state = LI_CHARGER_STATE_STANDBY_E; // Charger state
private:
void (*callbackFct)(uint8_t pwm);
uint32_t updateTs = 0;
uint32_t startTs = 0;
uint32_t fullTs = 0;
uint32_t errorTs = 0;
uint32_t iMax = 0;
bool safeCharge = true;
};
#endif /* __LiCharger_H_ */