46 lines
1.1 KiB
C++
46 lines
1.1 KiB
C++
#include <SmingCore/SmingCore.h>
|
|
|
|
|
|
extern "C" {
|
|
#include <sonoff_pow.h>
|
|
}
|
|
|
|
struct cse cse;
|
|
|
|
static struct impuls_counter power_counter;
|
|
static struct impuls_counter current_counter;
|
|
static struct impuls_counter voltage_counter;
|
|
|
|
void IRAM_ATTR cse_measure_isr ( void ) {
|
|
bool cf, cf1;
|
|
cf = digitalRead( CF_PIN );
|
|
cf1 = digitalRead( CF1_PIN );
|
|
impuls_counter_update( &power_counter, cf );
|
|
impuls_counter_update( ¤t_counter, cf1 );
|
|
}
|
|
|
|
void cse_init( uint32_t cycle_len ) {
|
|
pinMode(CF1_PIN, INPUT);
|
|
pinMode(CF_PIN, INPUT);
|
|
pinMode(SEL_PIN, OUTPUT);
|
|
impuls_counter_init( &power_counter, cycle_len);
|
|
impuls_counter_init( ¤t_counter , cycle_len);
|
|
|
|
cse.power.samples = 0;
|
|
cse.power.changes = 0;
|
|
cse.current.samples = 0;
|
|
cse.current.changes = 0;
|
|
cse.voltage.samples = 0;
|
|
cse.voltage.changes = 0;
|
|
cse.energy = 0;
|
|
}
|
|
|
|
void cse_update() {
|
|
bool pm, cm;
|
|
pm = impuls_counter_get_measurement( &power_counter, &cse.power );
|
|
cm = impuls_counter_get_measurement( ¤t_counter, &cse.current );
|
|
if ( pm == true ) {
|
|
cse.energy += cse.power.changes;
|
|
}
|
|
}
|