Switch branches/tags
Nothing to show
Find file Copy path
02f556d Oct 29, 2018
1 contributor

Users who have contributed to this file

201 lines (168 sloc) 7.56 KB
Copyright (C) AC SOFTWARE SP. Z O.O.
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 2
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
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <gpio.h>
typedef struct {
char tag[3];
_supla_int64_t counter[4];
} _ic_storage_t;
uint8 storage_offset;
ETSTimer storage_timer1;
_supla_int64_t counter;
uint8 counter_changed;
uint8 input_last_value = 0;
uint32 input_last_time = 0;
const uint8_t rsa_public_key_bytes[512] = {
0xf2, 0xd5, 0xab, 0xa5, 0x1a, 0x6b, 0x72, 0x3b, 0xc1, 0xb1, 0x6f, 0xc0,
0x30, 0x43, 0x6b, 0x0f, 0x01, 0x14, 0x12, 0x32, 0x38, 0x4e, 0xe4, 0x54,
0xfe, 0x46, 0x1c, 0x97, 0xe8, 0x19, 0x9d, 0xb2, 0xd7, 0x12, 0x33, 0x18,
0x53, 0xf1, 0x73, 0xd8, 0xfa, 0x06, 0x7a, 0xf0, 0x30, 0x44, 0x10, 0xf3,
0x7e, 0xd9, 0x17, 0x58, 0xa0, 0x41, 0x10, 0x3e, 0x4b, 0x0b, 0x92, 0x6f,
0x7c, 0x15, 0x6f, 0xff, 0x60, 0xc7, 0xf0, 0xb6, 0x85, 0xfc, 0xca, 0x8a,
0xad, 0xde, 0x0b, 0xc8, 0x6b, 0x62, 0xc2, 0x2d, 0xb7, 0xce, 0xe8, 0x04,
0x7f, 0xa8, 0x8e, 0x5e, 0x98, 0x9c, 0x5b, 0xd8, 0xa7, 0x9d, 0xa1, 0x73,
0x2a, 0xae, 0xf4, 0x15, 0x46, 0x84, 0x54, 0x10,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x33, 0x0c, 0x0a, 0x13, 0xa0,
0x00, 0xe5, 0x17, 0x74, 0xdf, 0x0c, 0xd1, 0x90, 0x15, 0x84, 0xdc, 0x34,
0x30, 0x37, 0xae, 0xbd, 0x4b, 0xdd, 0xbc, 0x15, 0x07, 0x2c, 0x89, 0xa3,
0xcc, 0x7f, 0x2d, 0x48, 0xf7, 0x56, 0xb9, 0x43};
void supla_esp_board_set_device_name(char *buffer, uint8 buffer_size) {
ets_snprintf(buffer, buffer_size, "IMPULSE COUNTER");
void supla_esp_board_gpio_init(void) {
supla_input_cfg[0].type = INPUT_TYPE_BTN_MONOSTABLE;
supla_input_cfg[0].gpio_id = B_CFG_PORT;
supla_input_cfg[0].flags = INPUT_FLAG_PULLUP | INPUT_FLAG_CFG_BTN;
supla_input_cfg[1].type = INPUT_TYPE_CUSTOM;
supla_input_cfg[1].gpio_id = IMPULSE_PORT;
void ICACHE_FLASH_ATTR supla_esp_board_on_storage_timer(void *ptr) {
if (counter_changed) {
counter_changed = 0;
storage_offset = storage_offset == 1 ? 2 : 1;
int a;
_ic_storage_t storage;
storage.tag[0] = 'I';
storage.tag[1] = 'C';
storage.tag[2] = 1;
_supla_int64_t c = counter;
for (a = 0; a < 4; a++) {
storage.counter[a] = c;
spi_flash_erase_sector(CFG_SECTOR + STATE_SECTOR_OFFSET + storage_offset);
spi_flash_write((CFG_SECTOR + STATE_SECTOR_OFFSET + storage_offset) *
(uint32 *)&storage, sizeof(_ic_storage_t));
uint8 ICACHE_FLASH_ATTR supla_esp_board_load(uint8 offset) {
_ic_storage_t storage;
memset(&storage, 0, sizeof(_ic_storage_t));
(uint32 *)&storage, sizeof(_ic_storage_t))) {
if (storage.tag[0] == 'I' && storage.tag[1] == 'C' && storage.tag[2] == 1) {
for (int a = 1; a < 4; a++) {
if (storage.counter[0] != storage.counter[a]) {
return 0;
counter = storage.counter[0];
counter_changed = 0;
return 1;
return 0;
uint8 supla_esp_board_intr_handler(uint32 gpio_status) {
if (gpio_status & BIT(IMPULSE_PORT)) {
gpio_status & BIT(IMPULSE_PORT)); // clear interrupt status
uint8 v = gpio__input_get(IMPULSE_PORT);
if (v != input_last_value) {
if (v == 1 && system_get_time() - input_last_time > 50000) {
counter_changed = 1;
input_last_value = v;
input_last_time = system_get_time();
return 1;
return 0;
void ICACHE_FLASH_ATTR supla_esp_board_starting(void) {
counter = 0;
counter_changed = 0;
storage_offset = 1;
if (supla_esp_board_load(1) == 0) {
input_last_value = gpio__input_get(IMPULSE_PORT);
(os_timer_func_t *)supla_esp_board_on_storage_timer, NULL);
os_timer_arm(&storage_timer1, 1000, 1);
uint8 ICACHE_FLASH_ATTR supla_esp_board_get_impulse_counter(
unsigned char channel_number, TDS_ImpulseCounter_Value *icv) {
if (channel_number == 0) {
icv->counter = counter;
return 1;
return 0;
void supla_esp_board_set_channels(TDS_SuplaDeviceChannel_C *channels,
unsigned char *channel_count) {
*channel_count = 1;
channels[0].Number = 0;
supla_esp_ic_get_value(0, channels[0].value);
void supla_esp_board_send_channel_values_with_delay(void *srpc) {}