raspberrypi/433MHZ/rc-switch-pico/examples/Forward/Forward.cc

222 lines
5.2 KiB
C++
Raw Permalink Normal View History

2023-04-14 10:05:31 +08:00
#include <iostream>
#include "pico/stdlib.h"
#include "../../radio-switch.h"
#include "pico/stdio.h"
#include "pico/multicore.h"
#include <map> // map
#include <time.h>
#include "hardware/watchdog.h"
2023-04-14 10:05:31 +08:00
2023-04-18 19:42:00 +08:00
#define BUFFER_SIZ 270
2023-04-14 10:05:31 +08:00
2023-04-18 19:42:00 +08:00
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
const uint RADIO_TRANSMIT_PIN = 16; // 433发射模块引脚
const uint RADIO_RECEIVER_PIN = 17; // 433接收模块引脚
const uint PASSWD_LEN = 3; // 随机数几位
const uint QUEUES_NUM = 3; // 队列数量, 满了发送
const uint QUEUES_WAIT = 4; // 队列等待时间, 单位秒, 队列未满时
2023-04-14 10:05:31 +08:00
2023-04-18 19:42:00 +08:00
char ins[] = "55";
char outs = '1';
2023-04-14 10:05:31 +08:00
// 闪烁LED
2023-04-18 19:42:00 +08:00
static void light()
2023-04-14 10:05:31 +08:00
{
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);
return;
2023-04-14 10:05:31 +08:00
}
2023-04-18 19:42:00 +08:00
static const char pool[] = {
2023-04-14 10:05:31 +08:00
'1', '2', '3', '4', '5', '6', '7', '8', '9'
};
// 随即数
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));
return atoi(password);
}
static void SEND(const int ID)
{
int RANDOM = 500;
const int LOOP_NUM = 1; // 循环发送次数
const uint PULSE_LENGTH = 169; // set this to PULSELENGTH RECIEVED
const uint REPEAT_TRANSMIT = 5; // 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
2023-04-14 10:05:31 +08:00
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 = 1; i <= LOOP_NUM; i++) {
2023-04-14 10:05:31 +08:00
RANDOM = RAND();
light(); // 灯闪烁
2023-04-14 10:05:31 +08:00
sleep_ms(RANDOM * 2 / 3); // 等待随机时间
2023-04-14 10:05:31 +08:00
mySwitch.send(ID, BIT_LENGTH); // 发射
sleep_ms(120);
2023-04-14 10:05:31 +08:00
}
return;
2023-04-14 10:05:31 +08:00
}
2023-04-18 19:42:00 +08:00
static int int_string(int val, char *inchars, int SIZE, int *inchars_len, char *outchars, int *outchars_len)
2023-04-14 10:05:31 +08:00
{
2023-04-18 19:42:00 +08:00
memset(inchars, 0, SIZE);
memset(outchars, 0, SIZE);
2023-04-14 10:05:31 +08:00
2023-04-18 19:42:00 +08:00
snprintf(inchars, SIZE, "%d", val);
*inchars_len = strlen(inchars);
2023-04-14 10:05:31 +08:00
2023-04-18 19:42:00 +08:00
if (0 == strncasecmp(inchars, ins, 2)) {
outchars[0] = outs;
outchars[1] = outs;
strncpy(outchars + 2, inchars + 2, (*inchars_len) - 2);
2023-04-14 10:05:31 +08:00
2023-04-18 19:42:00 +08:00
*outchars_len = strlen(outchars);
2023-04-14 10:05:31 +08:00
}
return 0;
}
// 核心0发送数据到核心1, 核心1判断是否有数据到来, 然后打印.
2023-04-18 19:42:00 +08:00
static 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-18 19:42:00 +08:00
char inchars[BUFFER_SIZ];
char outchars[BUFFER_SIZ];
int inchars_len;
int outchars_len;
2023-04-14 10:05:31 +08:00
while (true) {
if (rcSwitch.available()) {
light(); // 灯闪烁
2023-04-14 10:05:31 +08:00
uint32_t val = rcSwitch.getReceivedValue();
2023-04-18 19:42:00 +08:00
int_string(val, inchars, BUFFER_SIZ, &inchars_len, outchars, &outchars_len);
2023-04-14 10:05:31 +08:00
if (val != 0) {
if (inchars[0] == ins[0] && inchars[1] == ins[1]) {
2023-04-18 19:42:00 +08:00
multicore_fifo_push_blocking(atoi(outchars));
} else {
2023-04-14 10:05:31 +08:00
rcSwitch.resetAvailable();
val = 0;
continue;
}
}
rcSwitch.resetAvailable();
val = 0;
}
sleep_ms(120);
2023-04-14 10:05:31 +08:00
}
return;
2023-04-14 10:05:31 +08:00
}
2023-04-18 19:42:00 +08:00
int main(void)
2023-04-14 10:05:31 +08:00
{
stdio_init_all();
sleep_ms(7000);
2023-04-14 10:05:31 +08:00
std::map < int, int >idcode;
int count=0;
uint32_t i=0;
2023-04-14 10:05:31 +08:00
multicore_reset_core1();
multicore_launch_core1(core1_main);
if (watchdog_caused_reboot()) {
printf("Rebooted by Watchdog!\n");
} else {
printf("Clean boot\n");
}
watchdog_enable(8000, 1);
2023-04-14 10:05:31 +08:00
while (1)
{
watchdog_update();
2023-04-18 19:42:00 +08:00
if (multicore_fifo_rvalid())
{
i = multicore_fifo_pop_blocking(); // 读取核心1发送来的数据
2023-04-14 10:05:31 +08:00
2023-04-18 19:42:00 +08:00
idcode.insert( { // 插入map
2023-04-14 10:05:31 +08:00
i, i}
);
}
2023-04-18 19:42:00 +08:00
if (idcode.size() >= QUEUES_NUM) // 等于3个时发送
{
for (auto it: idcode)
{
watchdog_update();
printf("核心0转发433MHZ 1bin %u\n", it.first);
2023-04-14 10:05:31 +08:00
SEND(it.first);
}
idcode.clear();
}
2023-04-18 19:42:00 +08:00
else // 一直没有3个时候, 过一段时间发送
2023-04-14 10:05:31 +08:00
{
2023-04-18 19:42:00 +08:00
count++; // 计数
2023-04-14 10:05:31 +08:00
if (count == (QUEUES_WAIT*10)) // 秒
2023-04-14 10:05:31 +08:00
{
2023-04-18 19:42:00 +08:00
for (auto it: idcode)
{
watchdog_update();
printf("核心0转发433MHZ 2bin %u\n", it.first);
2023-04-14 10:05:31 +08:00
SEND(it.first);
}
idcode.clear();
count = 0;
}
}
sleep_ms(100);
2023-04-14 10:05:31 +08:00
}
return 0;
}