diff --git a/433/rc-switch-pico/examples/Forward/CMakeLists.txt b/433/rc-switch-pico/examples/Forward/CMakeLists.txt new file mode 100644 index 0000000..589b57f --- /dev/null +++ b/433/rc-switch-pico/examples/Forward/CMakeLists.txt @@ -0,0 +1,31 @@ +add_executable(forward + Forward.cc + ../../radio-switch.cc + ) + + +add_compile_options(-Wall +-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int +-Wno-unused-function # we have some for the docs that aren't called +-Wno-maybe-uninitialized +) + +# Pull in our pico_stdlib which pulls in commonly used features + +target_link_libraries(forward +pico_stdlib +hardware_adc +pico_multicore) + +# enable usb output, disable uart output +pico_enable_stdio_usb(forward 1) +pico_enable_stdio_uart(forward 0) + +# create map/bin/hex file etc. +pico_add_extra_outputs(forward) + + + + + + diff --git a/433/rc-switch-pico/examples/Forward/Forward.cc b/433/rc-switch-pico/examples/Forward/Forward.cc new file mode 100644 index 0000000..5a09161 --- /dev/null +++ b/433/rc-switch-pico/examples/Forward/Forward.cc @@ -0,0 +1,220 @@ +#include +#include "pico/stdlib.h" +#include "../../radio-switch.h" +#include "pico/stdio.h" +#include "pico/multicore.h" +#include // map +#include + + +#define BUFFER_SIZ 1024 + + +const uint RADIO_TRANSMIT_PIN = 16; // 433发射模块引脚 +const uint RADIO_RECEIVER_PIN = 17; // 433接收模块引脚 + + +// 闪烁LED +void light() +{ + const uint LED_PIN = PICO_DEFAULT_LED_PIN; + gpio_init(LED_PIN); + gpio_set_dir(LED_PIN, GPIO_OUT); + gpio_put(LED_PIN, 1); + sleep_ms(100); + gpio_put(LED_PIN, 0); + sleep_ms(100); + +} + +static char pool[] = { + '1', '2', '3', '4', '5', '6', '7', '8', '9' +}; + +// 随即数 +static int RAND() +{ + int PASSWD_LEN = 3; + char password[BUFFER_SIZ]; + int i = 0; + FILE *fp; + + memset(password, 0, BUFFER_SIZ); + srand(time(NULL)); + + while (i != PASSWD_LEN) { + password[i++] = pool[rand() % sizeof(pool)]; + } + + //printf("%d\n", atoi(password)); + return atoi(password); +} + +static void SEND(const int ID) +{ + + const uint BUTTON = 21; // 按钮 + int RANDOM = 500; + int LOOP_NUM = 1; // 循环发送次数 + + const uint PULSE_LENGTH = 169; // set this to PULSELENGTH RECIEVED + const uint REPEAT_TRANSMIT = 4; // set this to whatever works best for you. // 重复发送 + const uint PROTOCOL = 1; // set this to PROTOCOL RECIEVED + const uint BIT_LENGTH = 24; // set this to BIT LENGTH RECIEVED + + gpio_init(RADIO_TRANSMIT_PIN); + RCSwitch mySwitch = RCSwitch(); + mySwitch.enableTransmit(RADIO_TRANSMIT_PIN); + mySwitch.setProtocol(PROTOCOL); + mySwitch.setPulseLength(PULSE_LENGTH); + mySwitch.setRepeatTransmit(REPEAT_TRANSMIT); + + + + + for (int i = 0; i <= LOOP_NUM; i++) { + RANDOM = RAND(); + light(); // 灯闪烁 + + sleep_ms(RANDOM * 2 / 3); // 等待随机时间 + + mySwitch.send(ID, BIT_LENGTH); // 第一次发射 + sleep_ms(130); + + + //mySwitch.send(ID + 1, BIT_LENGTH); // 第二次发射 + //sleep_ms(130); + } + +} + +int int_string(int val, char *string, int string_len, int *str_len, char *dest, int *dest_len) +{ + memset(string, 0, string_len); + memset(dest, 0, string_len); + + char s[] = "55"; + char d = '1'; + + snprintf(string, string_len, "%d", val); + *str_len = strlen(string); + + if (0 == strncasecmp(string, s, 2)) { + dest[0] = d; + dest[1] = d; + strncpy(dest + 2, string + 2, (*str_len) - 2); + + *dest_len = strlen(dest); + } + + return 0; +} + +// 核心0发送数据到核心1, 核心1判断是否有数据到来, 然后打印. +void core1_main() +{ + + gpio_init(RADIO_RECEIVER_PIN); + + RCSwitch rcSwitch = RCSwitch(); + rcSwitch.enableReceive(RADIO_RECEIVER_PIN); + + char str[270]; + char dest[270]; + int str_len; + int dest_len; + const char s = '5'; + + while (true) + { + if (rcSwitch.available()) + { + light(); + + uint32_t val = rcSwitch.getReceivedValue(); + //printf("核心1接收到的433MHZ数值%u\n", val); + + int_string(val, str, 270, &str_len, dest, &dest_len); + + if (val != 0) { + if (str[0] == s && str[1] == s) { + + //printf("核心1处理id后发射值%s\n", dest); + multicore_fifo_push_blocking(atoi(dest)); + } else { + rcSwitch.resetAvailable(); + val = 0; + continue; + } + + } + + rcSwitch.resetAvailable(); + val = 0; + } + + + sleep_ms(130); + + } + + return; +} + +int main() +{ + stdio_init_all(); + + std::map < int, int >idcode; + int count=0; + uint32_t i=0; + + + multicore_reset_core1(); + multicore_launch_core1(core1_main); + + + + while (1) + { + if (multicore_fifo_rvalid()) { + i = multicore_fifo_pop_blocking(); // 读取核心1发送来的数据 + + idcode.insert( { // 插入map + i, i} + ); + + } + + if (idcode.size() >= 3) { // 等于3个时发送 + for (auto it: idcode) { + + printf("核心0转发433MHZ %u\n", it.first); + SEND(it.first); + } + + idcode.clear(); + } + else // 一直没有3个时候, 过一段时间发送 + { + count++; // 计数 + + if (count == 600) // 60秒 + { + for (auto it: idcode) { + printf("核心0转发433MHZ %u\n", it.first); + SEND(it.first); + } + + idcode.clear(); + count = 0; + } + } + + + sleep_ms(100); + } + + + return 0; +} diff --git a/433/rc-switch-pico/examples/Receive/Receive.cc b/433/rc-switch-pico/examples/Receive/Receive.cc index 7f6cf29..1375d5f 100644 --- a/433/rc-switch-pico/examples/Receive/Receive.cc +++ b/433/rc-switch-pico/examples/Receive/Receive.cc @@ -2,18 +2,18 @@ #include "pico/stdlib.h" #include "../../radio-switch.h" #include "pico/stdio.h" - -#include -#include "pico/stdlib.h" #include "pico/multicore.h" - -//#include "hardware/adc.h" -//#include - +#include // map #include + #define BUFFER_SIZ 1024 + +//const uint RADIO_TRANSMIT_PIN = 16; // 433发射模块引脚 +const uint RADIO_RECEIVER_PIN = 17; // 433接收模块引脚 + + // 闪烁LED void light() { @@ -27,94 +27,13 @@ void light() } -/* -int temperature() { - // 数模转换器为 0~3.3v 最大值为 12bit - const float conversion_factor = 3.3f / (1 << 12); - - float v; - float t; - stdio_init_all(); - printf("Use adc channel 4, measuring temptutre\n"); - // ADC初始化 - adc_init(); - adc_set_temp_sensor_enabled(true); - adc_select_input(4); - - // 数字转换为电压 - v = adc_read() * conversion_factor; - t = 27 - (v - 0.706) / 0.001721; - printf("t:%.2f v:%.2f\n", t, v); - - sleep_ms(100); - - return 0; -} -*/ - -static char pool[] = { - '1', '2', '3', '4', '5', '6', '7', '8', '9' -}; - -// 随即数 -static int RAND() -{ - int PASSWD_LEN = 3; - char password[BUFFER_SIZ]; - int i = 0; - FILE *fp; - - memset(password, 0, BUFFER_SIZ); - srand(time(NULL)); - - while (i != PASSWD_LEN) { - password[i++] = pool[rand() % sizeof(pool)]; - } - - //printf("%d\n", atoi(password)); - return atoi(password); -} - -static void SEND(const int ID) -{ - const uint RADIO_TRANSMIT_PIN = 16; // 433发射模块引脚 - const uint BUTTON = 21; // 按钮发射 - - const uint PULSE_LENGTH = 169; // set this to PULSELENGTH RECIEVED - const uint REPEAT_TRANSMIT = 4; // set this to whatever works best for you. // 重复发送 - const uint PROTOCOL = 1; // set this to PROTOCOL RECIEVED - const uint BIT_LENGTH = 24; // set this to BIT LENGTH RECIEVED - - gpio_init(RADIO_TRANSMIT_PIN); - RCSwitch mySwitch = RCSwitch(); - mySwitch.enableTransmit(RADIO_TRANSMIT_PIN); - mySwitch.setProtocol(PROTOCOL); - mySwitch.setPulseLength(PULSE_LENGTH); - mySwitch.setRepeatTransmit(REPEAT_TRANSMIT); - - int RANDOM = 500; - int LOOP_NUM = 1; // 循环发送次数 - - for (int i = 0; i <= LOOP_NUM; i++) { - RANDOM = RAND(); - light(); // 灯闪烁 - - sleep_ms(RANDOM * 2 / 3); // 等待随机时间 - - mySwitch.send(ID, BIT_LENGTH); // 第一次发射 - sleep_ms(130); - - mySwitch.send(ID + 1, BIT_LENGTH); // 第二次发射 - sleep_ms(130); - } - -} - - int int_string(int val, char *string, int string_len, int *str_len, char *dest, int *dest_len) { + memset(string, 0, string_len); + memset(dest, 0, string_len); + char s[] = "55"; - char d = '2'; + char d = '1'; snprintf(string, string_len, "%d", val); *str_len = strlen(string); @@ -130,106 +49,108 @@ int int_string(int val, char *string, int string_len, int *str_len, char *dest, return 0; } - - // 核心0发送数据到核心1, 核心1判断是否有数据到来, 然后打印. void core1_main() { - const uint RADIO_RECEIVER_PIN = 17; + gpio_init(RADIO_RECEIVER_PIN); RCSwitch rcSwitch = RCSwitch(); rcSwitch.enableReceive(RADIO_RECEIVER_PIN); - - char str[270]; - char dest[270]; + + const char s = '1'; + uint32_t val = 0; int str_len; int dest_len; - memset(str, 0, 270); - memset(dest, 0, 270); + char str[270]; + char dest[270]; - while (true) { - if (rcSwitch.available()) { + + while (true) + { + if (rcSwitch.available()) + { light(); - uint32_t val = rcSwitch.getReceivedValue(); - //printf("核心1接收到的433MHZ数值%u\n", val); - + val = rcSwitch.getReceivedValue(); + int_string(val, str, 270, &str_len, dest, &dest_len); + if (val != 0) { - if (str[0] == '5' && str[1] == '5') - { - + if (str[0] == s && str[1] == s) { + multicore_fifo_push_blocking(atoi(dest)); - } - else - { + } else { rcSwitch.resetAvailable(); val = 0; continue; } - - } - + } rcSwitch.resetAvailable(); val = 0; } - - -/* - if (multicore_fifo_rvalid()) { - uint32_t i = multicore_fifo_pop_blocking(); - printf("核心1接收到核心0的数值%u\n", i); - - } -*/ sleep_ms(130); - - + } - - return ; + return; } int main() { stdio_init_all(); - + + std::map < int, int >idcode; + int count=0; + uint32_t i=0; + + multicore_reset_core1(); multicore_launch_core1(core1_main); + - while (1) { - + while (1) + { if (multicore_fifo_rvalid()) { - uint32_t i = multicore_fifo_pop_blocking(); // 读取核心1发送来的数据 - //printf("核心0接收到核心1的数值%u\n", i); - - //multicore_reset_core1(); // 关闭核心1 - //SEND(i); // 433MHZ发送 - //multicore_launch_core1(core1_main); // 开启核心1 - //multicore_fifo_push_blocking(i); // 给核心1发送数据 - - - - - - SEND(i); - printf("核心0转发433MHZ %u\n", i); + i = multicore_fifo_pop_blocking(); // 读取核心1发送来的数据 + idcode.insert( { // 插入map + i, i} + ); } + + if (idcode.size() >= 3) { // 等于3个时发送 + for (auto it: idcode) { + printf("核心0接收433MHZ %u\n", it.first); + } + idcode.clear(); + } + else // 一直没有3个时候, 过一段时间发送 + { + count++; // 计数 + + if (count == 600) // 60秒 + { + for (auto it: idcode) { + printf("核心0接收433MHZ %u\n", it.first); + } - sleep_ms(130); + idcode.clear(); + count = 0; + } + } + + sleep_ms(100); } - + return 0; } diff --git a/433/rc-switch-pico/examples/Transmit/CMakeLists.txt b/433/rc-switch-pico/examples/Transmit/CMakeLists.txt index 67a46cc..6536b1b 100644 --- a/433/rc-switch-pico/examples/Transmit/CMakeLists.txt +++ b/433/rc-switch-pico/examples/Transmit/CMakeLists.txt @@ -13,9 +13,14 @@ add_compile_options(-Wall # Pull in our pico_stdlib which pulls in commonly used features target_link_libraries(transmit pico_stdlib) + + # enable usb output, disable uart output pico_enable_stdio_usb(transmit 1) pico_enable_stdio_uart(transmit 0) + +pico_enable_stdio_uart(transmit ENABLED) + # create map/bin/hex file etc. pico_add_extra_outputs(transmit) diff --git a/433/rc-switch-pico/examples/Transmit/Transmit.cc b/433/rc-switch-pico/examples/Transmit/Transmit.cc index 1f8fd0e..84eaf67 100644 --- a/433/rc-switch-pico/examples/Transmit/Transmit.cc +++ b/433/rc-switch-pico/examples/Transmit/Transmit.cc @@ -6,7 +6,6 @@ #define BUFFER_SIZ 1024 - // 闪烁LED static void light() { @@ -31,32 +30,29 @@ static int RAND() char password[BUFFER_SIZ]; int i = 0; FILE *fp; - - + memset(password, 0, BUFFER_SIZ); srand(time(NULL)); - + while (i != PASSWD_LEN) { password[i++] = pool[rand() % sizeof(pool)]; } - - - - printf("%d\n", atoi(password)); - + + //printf("%d\n", atoi(password)); + return atoi(password); } -int main(void) { +int main(void) +{ - const uint RADIO_TRANSMIT_PIN = 16; // 433发射模块引脚 - const uint BUTTON = 17; // 按钮发射 - + const uint RADIO_TRANSMIT_PIN = 16; // 433发射模块引脚 + const uint BUTTON = 17; // 按钮发射 - const uint PULSE_LENGTH = 169; // set this to PULSELENGTH RECIEVED - const uint REPEAT_TRANSMIT = 4; // set this to whatever works best for you. // 重复发送 - const uint PROTOCOL = 1; // set this to PROTOCOL RECIEVED - const uint BIT_LENGTH = 24; // set this to BIT LENGTH RECIEVED + const uint PULSE_LENGTH = 169; // set this to PULSELENGTH RECIEVED + const uint REPEAT_TRANSMIT = 4; // set this to whatever works best for you. // 重复发送 + const uint PROTOCOL = 1; // set this to PROTOCOL RECIEVED + const uint BIT_LENGTH = 24; // set this to BIT LENGTH RECIEVED stdio_init_all(); gpio_init(RADIO_TRANSMIT_PIN); @@ -67,35 +63,32 @@ int main(void) { mySwitch.setRepeatTransmit(REPEAT_TRANSMIT); int RANDOM = 30; - int LOOP_NUM = 2; // 循环发送次数 - - - const int but = 55001; - + int LOOP_NUM = 1; // 循环发送次数 - while(1) - { + const int but = 55001; + + stdio_uart_init_full(uart0, 115200, 6, 7); + + while (1) { + //fprintf(uart0_handle, "Hello from uart0!\r\n"); //if (1 == gpio_get(BUTTON)) { // 按钮按下 - for (int i=0; i<=LOOP_NUM; i++) { - RANDOM = RAND(); - light(); // 灯闪烁 - - sleep_ms(RANDOM * 2 / 3); // 等待随机时间 - - mySwitch.send(but, BIT_LENGTH); // 第一次发射 - sleep_ms(130); - + for (int i = 0; i <= LOOP_NUM; i++) { + RANDOM = RAND(); + light(); // 灯闪烁 - //mySwitch.send(but+1, BIT_LENGTH); // 第二次发射 - //sleep_ms(130); - } + sleep_ms(RANDOM * 2 / 3); // 等待随机时间 + + mySwitch.send(but, BIT_LENGTH); // 第一次发射 + sleep_ms(130); + + //mySwitch.send(but+1, BIT_LENGTH); // 第二次发射 + //sleep_ms(130); + } //} sleep_ms(3000); } - - + return 0; } -