2 #include "pulse_demod.h"
6 static int current_cost_callback(bitbuffer_t *bitbuffer) {
7 bitbuffer_invert(bitbuffer);
8 bitrow_t *bb = bitbuffer->bb;
11 char time_str[LOCAL_TIME_BUFLEN];
13 local_time_str(0, time_str);
15 uint8_t init_pattern[] = {
21 0b01011101, //45 (! last 3 bits is not init)
23 unsigned int start_pos = bitbuffer_search(bitbuffer, 0, 0, init_pattern, 45);
25 if(start_pos == bitbuffer->bits_per_row[0]){
30 bitbuffer_t packet_bits = {0};
32 start_pos = bitbuffer_manchester_decode(bitbuffer, 0, start_pos, &packet_bits, 0);
34 uint8_t *packet = packet_bits.bb[0];
36 if(packet_bits.bits_per_row[0] >= 56 && ((packet[0] & 0xf0) == 0) ){
37 uint16_t device_id = (packet[0] & 0x0f) << 8 | packet[1];
39 uint16_t watt0 = (packet[2] & 0x7F) << 8 | packet[3] ;
40 uint16_t watt1 = (packet[4] & 0x7F) << 8 | packet[5] ;
41 uint16_t watt2 = (packet[6] & 0x7F) << 8 | packet[7] ;
42 data = data_make("time", "", DATA_STRING, time_str,
43 "model", "", DATA_STRING, "CurrentCost TX", //TODO: it may have different CC Model ? any ref ?
44 //"rc", "Rolling Code", DATA_INT, rc, //TODO: add rolling code b[1] ? test needed
45 "dev_id", "Device Id", DATA_FORMAT, "%d", DATA_INT, device_id,
46 "power0", "Power 0", DATA_FORMAT, "%d W", DATA_INT, watt0,
47 "power1", "Power 1", DATA_FORMAT, "%d W", DATA_INT, watt1,
48 "power2", "Power 2", DATA_FORMAT, "%d W", DATA_INT, watt2,
49 //"battery", "Battery", DATA_STRING, battery_low ? "LOW" : "OK", //TODO is there some low battery indicator ?
51 data_acquired_handler(data);
57 static char *output_fields[] = {
67 r_device current_cost = {
68 .name = "CurrentCost Current Sensor",
69 .modulation = FSK_PULSE_PCM,
71 .long_limit = 250, // NRZ
73 .json_callback = ¤t_cost_callback,
75 .fields = output_fields,