raspberrypi/433/rc-switch-pico/examples/Receive/Receive.cc

157 lines
3.2 KiB
C++
Raw Normal View History

#include <iostream>
#include "pico/stdlib.h"
#include "../../radio-switch.h"
#include "pico/stdio.h"
#include "pico/multicore.h"
2023-04-14 10:05:31 +08:00
#include <map> // map
#include <time.h>
2023-04-14 10:05:31 +08:00
#define BUFFER_SIZ 1024
2023-04-14 10:05:31 +08:00
//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);
}
int int_string(int val, char *string, int string_len, int *str_len, char *dest, int *dest_len)
{
2023-04-14 10:05:31 +08:00
memset(string, 0, string_len);
memset(dest, 0, string_len);
char s[] = "55";
2023-04-14 10:05:31 +08:00
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()
{
2023-04-14 10:05:31 +08:00
gpio_init(RADIO_RECEIVER_PIN);
RCSwitch rcSwitch = RCSwitch();
rcSwitch.enableReceive(RADIO_RECEIVER_PIN);
2023-04-14 10:05:31 +08:00
const char s = '1';
uint32_t val = 0;
int str_len;
int dest_len;
2023-04-14 10:05:31 +08:00
char str[270];
char dest[270];
2023-04-14 10:05:31 +08:00
while (true)
{
if (rcSwitch.available())
{
light();
2023-04-14 10:05:31 +08:00
val = rcSwitch.getReceivedValue();
int_string(val, str, 270, &str_len, dest, &dest_len);
2023-04-14 10:05:31 +08:00
if (val != 0) {
2023-04-14 10:05:31 +08:00
if (str[0] == s && str[1] == s) {
multicore_fifo_push_blocking(atoi(dest));
2023-04-14 10:05:31 +08:00
} else {
rcSwitch.resetAvailable();
val = 0;
continue;
}
2023-04-14 10:05:31 +08:00
}
rcSwitch.resetAvailable();
val = 0;
}
sleep_ms(130);
2023-04-14 10:05:31 +08:00
}
2023-04-14 10:05:31 +08:00
return;
}
int main()
{
stdio_init_all();
2023-04-14 10:05:31 +08:00
std::map < int, int >idcode;
int count=0;
uint32_t i=0;
multicore_reset_core1();
multicore_launch_core1(core1_main);
2023-04-14 10:05:31 +08:00
2023-04-14 10:05:31 +08:00
while (1)
{
if (multicore_fifo_rvalid()) {
2023-04-14 10:05:31 +08:00
i = multicore_fifo_pop_blocking(); // 读取核心1发送来的数据
2023-04-14 10:05:31 +08:00
idcode.insert( { // 插入map
i, i}
);
}
2023-04-14 10:05:31 +08:00
if (idcode.size() >= 3) { // 等于3个时发送
for (auto it: idcode) {
printf("核心0接收433MHZ %u\n", it.first);
}
2023-04-14 10:05:31 +08:00
idcode.clear();
}
else // 一直没有3个时候, 过一段时间发送
{
count++; // 计数
if (count == 600) // 60秒
{
for (auto it: idcode) {
printf("核心0接收433MHZ %u\n", it.first);
}
2023-04-14 10:05:31 +08:00
idcode.clear();
count = 0;
}
}
sleep_ms(100);
}
2023-04-14 10:05:31 +08:00
return 0;
}