diff --git a/scripts/onoff.py b/scripts/onoff.py new file mode 100755 index 0000000..6442d24 --- /dev/null +++ b/scripts/onoff.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import tlay2_client +import struct + +def send_onoff(state): + a=tlay2_client.Tlay2_msg(1) + a.msg(struct.pack("?",state)) + +if __name__ == "__main__": + import argparse + parser = argparse.ArgumentParser(description='Control power') + + group = parser.add_mutually_exclusive_group() + group.add_argument('-0', '--on', action='store_true') + group.add_argument('-1', '--off', action='store_true') + + args = parser.parse_args() + + if args.on: + send_onoff(True) + if args.off: + send_onoff(False) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9ececf0..9daa29d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -61,7 +61,7 @@ void bt_enabled(int err) barrot_init(); } -#define DISPLAY_STACK_SIZE 500 +#define DISPLAY_STACK_SIZE 1000 K_THREAD_STACK_DEFINE(display_stack_area, DISPLAY_STACK_SIZE); struct k_thread display_thread_data; @@ -153,12 +153,11 @@ int main(void) } uint16_t onoff; - if(modbus_read_holding_regs(client_iface, 1, 0x902C, &onoff,1)!=0) + if(modbus_read_input_regs(client_iface, 1, 0x3035, &onoff,1)!=0) { printt("Read fail3"); continue; } - printt("%d", onoff); k_mutex_lock(®_data.mut, K_FOREVER); reg_data.d.b_soc = holding_reg[0]; reg_data.d.b_v = holding_reg[1]; @@ -167,7 +166,7 @@ int main(void) reg_data.d.l_a = holding_reg[6]; reg_data.d.s_v = holding_reg[9]; reg_data.d.s_a = holding_reg[10]; - reg_data.d.on = onoff == 0; + reg_data.d.on = (onoff&1) == 1; k_mutex_unlock(®_data.mut); k_sem_give(®_data.new_sample); printt("Read ok"); diff --git a/src/main_modules.hpp b/src/main_modules.hpp index 9a0c262..4da1159 100644 --- a/src/main_modules.hpp +++ b/src/main_modules.hpp @@ -37,6 +37,7 @@ extern Tlay2<128> tlay2; extern Display display; +extern int client_iface; diff --git a/src/tlay2.hpp b/src/tlay2.hpp index 2cfec4c..45a9f31 100644 --- a/src/tlay2.hpp +++ b/src/tlay2.hpp @@ -96,7 +96,8 @@ public: ret = uart.Init(); if(!ret) return ret; - k_thread_create(&rx_thread,rx_stack, rx_stack_size,rx_thread_c,this,NULL,NULL,5,0,K_NO_WAIT); + k_tid_t tid = k_thread_create(&rx_thread,rx_stack, rx_stack_size,rx_thread_c,this,NULL,NULL,5,0,K_NO_WAIT); + k_thread_name_set(tid,"Tlay2"); return true; } diff --git a/src/uart.cpp b/src/uart.cpp index a8cefe0..cb53661 100644 --- a/src/uart.cpp +++ b/src/uart.cpp @@ -1,7 +1,8 @@ #include "main_modules.hpp" #include +#include -K_KERNEL_STACK_DEFINE(tlay2_stack,300); +K_KERNEL_STACK_DEFINE(tlay2_stack,1000); void process_packet(Tlay2<128>* obj, uint8_t*data,size_t len); @@ -18,6 +19,14 @@ void process_packet(Tlay2<128>* obj, uint8_t*data,size_t len) obj->tx_byte(data[i]); obj->tx_end(); break; + case 1: // set power + { + int ret = modbus_write_coil(client_iface, 1, 0x0000, !data[2]); + obj->tx_init_reply(); + obj->tx_u32(ret); + obj->tx_end(); + break; + } default: break; }