Repository navigation
Expand file tree
/
Copy pathESP8266PowerClass.h
More file actions
112 lines (87 loc) · 3.65 KB
/
Copy pathESP8266PowerClass.h
File metadata and controls
112 lines (87 loc) · 3.65 KB
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
/*
This libary is an enhanced version of the ITEAD.cc library
It reads the values from the Sonoff POW HLW8012 chip and calculates the respective Power, current, and oltage values. Only voltage or current reading can be chosen.
Copyright (c) [2016] [Iteand.cc and Andreas Spiess]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef __ESP8266PowerClass__H_
#define __ESP8266PowerClass__H_
#include <functional>
#include <Arduino.h>
extern "C" {
#include "user_interface.h"
#include <string.h>
}
typedef struct _DEVPARAM{
double param_a;
double param_b;
}DEVPARAM;
typedef enum{
CURRENT,
VOLTAGE,
}MEASURETYPE;
typedef enum
{
IOTGO_HLW8012_TEST_MODE_REPEAT,
IOTGO_HLW8012_TEST_MODE_ONCE,
}TESTMODE;
class ESP8266PowerClass{
public:
ESP8266PowerClass();
ESP8266PowerClass(int8_t power_pin,int8_t current_voltage_pin,int8_t sel_pin);
void enableMeasurePower(void);
void selectMeasureCurrentOrVoltage(MEASURETYPE dev_type);
void setPowerParam(double param_a,double param_b);
void setCurrentParam(double param_a,double param_b);
void setVoltageParam(double param_a,double param_b);
DEVPARAM getPowerParam(void);
DEVPARAM getCurrentParam(void);
DEVPARAM getvoltageParam(void);
void startMeasure(void);
double getPower(void);
double getCurrent(void);
double getVoltage(void);
double getCurrFrequency(void);
private:
static ICACHE_RAM_ATTR void measurePowerFreq(void);
static ICACHE_RAM_ATTR void measureCurrenFreq(void);
static ICACHE_RAM_ATTR void measureVoltageFreq(void);
static void timerCallback(void);
static void getFreq(uint32_t &cnt,uint32_t &freq_cnt,uint8_t &test_mode,uint32_t &freq,
uint8_t &hlw8012_bad_once_cnt,uint8_t &hlw8012_bad_repeat_cnt,uint32_t (&history_queue)[10]);
static void getAverageFreq(uint32_t (&history_queue)[10],uint32_t &freq);
static uint32_t power_freq_cnt;
static uint32_t voltage_freq_cnt;
static uint32_t current_freq_cnt;
static uint8_t power_test_mode;
static uint8_t current_test_mode;
static uint8_t voltage_test_mode;
static bool power_flag;
static bool current_flag;
static bool voltage_flag;
static uint32_t power_freq;
static uint32_t voltage_freq;
static uint32_t current_freq;
DEVPARAM power_param;
DEVPARAM current_param;
DEVPARAM voltage_param;
int8_t power_pin;
int8_t current_voltage_pin;
int8_t sel_pin;
os_timer_t myTimer;
};
#endif