【类 型】:refactor

【主题】:代码融合,田工代码分离到commser类;更换Ticker同名库,调用代码也更换
【描	述】:
	[原因]:
	[过程]:更改订阅命令单主题 cmd/macadd
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
This commit is contained in:
tk 2024-07-02 21:29:01 +08:00
parent 26d48db32b
commit 0d82651ace
10 changed files with 299 additions and 47 deletions

View File

@ -0,0 +1,51 @@
/*
* This example demonstrates used of Ticker with arguments.
* You can call the same callback function with different argument on different times.
* Based on the argument the callback can perform different tasks.
*/
#include <Arduino.h>
#include <Ticker.h>
// Arguments for the function must remain valid (not run out of scope) otherwise the function would read garbage data.
int LED_PIN_1 = 4;
#ifdef LED_BUILTIN
int LED_PIN_2 = LED_BUILTIN;
#else
int LED_PIN_2 = 8;
#endif
Ticker tickerSetHigh;
Ticker tickerSetLow;
// Argument to callback must always be passed a reference
void swapState(int *pin) {
static int led_1_state = 1;
static int led_2_state = 1;
if(*pin == LED_PIN_1){
Serial.printf("[%lu ms] set pin %d to state: %d\n", millis(), *pin, led_1_state);
digitalWrite(*pin, led_1_state);
led_1_state = led_1_state ? 0 : 1; // reverse for next pass
}else if(*pin == LED_PIN_2){
Serial.printf("[%lu ms] set pin %d to state: %d\n", millis(), *pin, led_2_state);
digitalWrite(*pin, led_2_state);
led_2_state = led_2_state ? 0 : 1; // reverse for next pass
}
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN_1, OUTPUT);
pinMode(LED_PIN_2, OUTPUT);
//digitalWrite(1, LOW);
// Blink LED every 500 ms on LED_PIN_1
tickerSetLow.attach_ms(500, swapState, &LED_PIN_1);
// Blink LED every 1000 ms on LED_PIN_2
tickerSetHigh.attach_ms(1000, swapState, &LED_PIN_2);
}
void loop() {
}

View File

@ -0,0 +1,42 @@
#include <Arduino.h>
#include <Ticker.h>
// attach a LED to pPIO 21
#define LED_PIN 21
Ticker blinker;
Ticker toggler;
Ticker changer;
float blinkerPace = 0.1; //seconds
const float togglePeriod = 5; //seconds
void change() {
blinkerPace = 0.5;
}
void blink() {
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
void toggle() {
static bool isBlinking = false;
if (isBlinking) {
blinker.detach();
isBlinking = false;
}
else {
blinker.attach(blinkerPace, blink);
isBlinking = true;
}
digitalWrite(LED_PIN, LOW); //make sure LED on on after toggling (pin LOW = led ON)
}
void setup() {
pinMode(LED_PIN, OUTPUT);
toggler.attach(togglePeriod, toggle);
changer.once(30, change);
}
void loop() {
}

63
lib/Ticker/src/Ticker.cpp Normal file
View File

@ -0,0 +1,63 @@
/*
Ticker.cpp - esp32 library that calls functions periodically
Copyright (c) 2017 Bert Melis. All rights reserved.
Based on the original work of:
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
The original version is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Ticker.h"
Ticker::Ticker() :
_timer(nullptr) {}
Ticker::~Ticker() {
detach();
}
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg) {
esp_timer_create_args_t _timerConfig;
_timerConfig.arg = reinterpret_cast<void*>(arg);
_timerConfig.callback = callback;
_timerConfig.dispatch_method = ESP_TIMER_TASK;
_timerConfig.name = "Ticker";
if (_timer) {
esp_timer_stop(_timer);
esp_timer_delete(_timer);
}
esp_timer_create(&_timerConfig, &_timer);
if (repeat) {
esp_timer_start_periodic(_timer, milliseconds * 1000ULL);
} else {
esp_timer_start_once(_timer, milliseconds * 1000ULL);
}
}
void Ticker::detach() {
if (_timer) {
esp_timer_stop(_timer);
esp_timer_delete(_timer);
_timer = nullptr;
}
}
bool Ticker::active() {
if (!_timer) return false;
return esp_timer_is_active(_timer);
}

107
lib/Ticker/src/Ticker.h Normal file
View File

@ -0,0 +1,107 @@
/*
Ticker.h - esp32 library that calls functions periodically
Copyright (c) 2017 Bert Melis. All rights reserved.
Based on the original work of:
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
The original version is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TICKER_H
#define TICKER_H
extern "C" {
#include "esp_timer.h"
}
class Ticker
{
public:
Ticker();
~Ticker();
typedef void (*callback_t)(void);
typedef void (*callback_with_arg_t)(void*);
void attach(float seconds, callback_t callback)
{
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
void attach_ms(uint32_t milliseconds, callback_t callback)
{
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
template<typename TArg>
void attach(float seconds, void (*callback)(TArg), TArg arg)
{
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
// C-cast serves two purposes:
// static_cast for smaller integer types,
// reinterpret_cast + const_cast for pointer types
uint32_t arg32 = (uint32_t)arg;
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
template<typename TArg>
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
{
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
uint32_t arg32 = (uint32_t)arg;
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
void once(float seconds, callback_t callback)
{
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
void once_ms(uint32_t milliseconds, callback_t callback)
{
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
template<typename TArg>
void once(float seconds, void (*callback)(TArg), TArg arg)
{
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
uint32_t arg32 = (uint32_t)(arg);
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
template<typename TArg>
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
{
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
uint32_t arg32 = (uint32_t)(arg);
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
void detach();
bool active();
protected:
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);
protected:
esp_timer_handle_t _timer;
};
#endif // TICKER_H

View File

@ -18,7 +18,6 @@ upload-port = COM[14]
lib_deps =
bogde/HX711@^0.7.5
mathertel/OneButton@^2.0.3
sstaub/Ticker@^4.4.0
robtillaart/CRC@^0.3.3
sandeepmistry/CAN@^0.3.1
fastled/FastLED@^3.5.0

View File

@ -178,10 +178,9 @@ bool FoodCube::checkWiFiStatus()
}
/**
* @description: mqtt
* @param {String[]} topicSub
* @param {int} topicSubCount
* @param {String} topicSub
*/
void FoodCube::connectMqtt(String topicSub[], int topicSubCount)
void FoodCube::connectMqtt(String topicSub)
{
if (mqttClient->connected())
return;
@ -198,7 +197,7 @@ void FoodCube::connectMqtt(String topicSub[], int topicSubCount)
logln(macAdd);
/*连接成功 订阅主题*/
// 订阅主题 PS:连接上mqtt服务器 订阅主题 收到信息触发回调函数 receiveCallback
subscribeTopic("cmd", 1);
subscribeTopic(topicSub, 1);
delay(500);
playText("服务器已连接");
}
@ -216,18 +215,17 @@ void FoodCube::connectMqtt(String topicSub[], int topicSubCount)
/**
* @description: loop函数里 mqtt连接情况
* @param {String[]} topicSub
* @param {int} topicSubCount
* @param {String} topicSub
*/
void FoodCube::mqttLoop(String topicSub[], int topicSubCount)
void FoodCube::mqttLoop(String topicSub)
{
if (mqttClient->connected())
{ // 检测 如果开发板成功连接服务器
mqttClient->loop(); // 保持心跳
}
else
{ // 如果开发板未能成功连接服务器
connectMqtt(topicSub, topicSubCount); // 则尝试连接服务器
{ // 如果开发板未能成功连接服务器
connectMqtt(topicSub); // 则尝试连接服务器
}
}

View File

@ -10,7 +10,7 @@
/*json库*/
#include "ArduinoJson.h"
/*异步库*/
#include "Ticker.h"
#include "Ticker.h" //调用Ticker.h库
/*udp发送*/
#include "WiFiUdp.h"
@ -49,8 +49,8 @@ public:
void connectWifi();
/*mqtt*/
PubSubClient *mqttClient; // 指向 mqtt服务器连接 对象
void connectMqtt(String topicSub[], int topicSubCount);
void mqttLoop(String topicSub[], int topicSubCount);
void connectMqtt(String topicSub);
void mqttLoop(String topicSub);
void subscribeTopic(String topicString, int Qos);
void pubMQTTmsg(String topicString, String messageString);
/*串口输出*/

View File

@ -41,9 +41,9 @@ String topicHandle[] = {"crosFrequency"};
boolean isPush = false; // 记得删除 板子按钮状态 ps:D3引脚下拉微动开关
/*异步线程对象*/
Ticker pubTicker(pubThread, 1000); // 定时发布主题 线程
Ticker mavTicker(mavThread, 30000); // 定时 指定飞控mavlink 串口返回的数据类别 防止飞控启动滞后
Ticker flashTicker(flashThread, 50); // 单片机主动 按钮主动发布主题 线程
Ticker pubTicker; // 定时发布主题 线程
Ticker mavTicker; // 定时 指定飞控mavlink 串口返回的数据类别 防止飞控启动滞后
Ticker flashTicker; // 单片机主动 按钮主动发布主题 线程
// Ticker chanTicker; //定时向飞控 发送油门指定

View File

@ -2,18 +2,21 @@
#define COMMSER_H
#include "Arduino.h"
#include <Ticker.h> //调用Ticker.h库
#include "Ticker.h" //调用Ticker.h库
#include "FoodDeliveryBase.h"
#include "config.h"
void mqtt_receiveCallback(char *topic, byte *payload, unsigned int length);
void mavThread();
void pubThread();
void flashThread();
void writeRoute(String topicStr);
void mavlink_receiveCallback(uint8_t c);
extern void mqtt_receiveCallback(char *topic, byte *payload, unsigned int length);
extern void mavThread();
extern void pubThread();
extern void flashThread();
extern void writeRoute(String topicStr);
extern void mavlink_receiveCallback(uint8_t c);
extern String topicPub[];
extern int topicPubCount;
extern String topicPubMsg[];
extern String topicHandle[];
extern String oldMsg[];
extern FoodCube fc; // 创建项目对象
extern Ticker pubTicker; // 定时发布主题 线程
extern Ticker mavTicker; // 定时 指定飞控mavlink 串口返回的数据类别 防止飞控启动滞后

View File

@ -35,7 +35,7 @@ CRGB leds[NUM_LEDS];
Motocontrol motocontrol;
void led_show(uint8_t cr, uint8_t cg, uint8_t cb);
void sendinfo();
Ticker tksendinfo(sendinfo, 1000); // 发送状态
Ticker tksendinfo; // 发送状态
// 收
void upbtn_click();
@ -130,7 +130,6 @@ void setup()
if (!motocontrol.init(&myservo)) // 初始化电机控制
ESP_LOGE(MOUDLENAME, "motocontrol init fault");
tksendinfo.start();
initstatus = IS_WaitStart;
_tm_waitinit = millis();
_needweightalign = (wei_offset == 0);
@ -142,15 +141,16 @@ void setup()
Serial1.begin(115200, SERIAL_8N1, SERIAL_REPORT_RX, SERIAL_REPORT_TX); // 声音模块引串口脚映射
fc.playText("开始启动");
fc.connectWifi(); // 连接wifi
fc.playText("正在连接服务器");
fc.connectMqtt(topicPub, topicPubCount); // 连接mqtt
// fc.playText("正在连接服务器");
// fc.connectMqtt("cmd"); // 连接mqtt
fc.mqttClient->setCallback(mqtt_receiveCallback); // 设置订阅成功 回调
fc.mav_request_data(); // 指定飞控串口返回的数据类别(飞控启动之后发送才有意义)
/*异步线程*/
pubTicker.start(); // 定时 发布主题
mavTicker.start(); // 定时 指定飞控mavlink 串口返回的数据类别 防止飞控启动滞后
flashTicker.start(); // 监听 按flash键时 主动发布对频主题
tksendinfo.attach(1, sendinfo); // 发送状态
pubTicker.attach(1, pubThread); // 定时 发布主题
mavTicker.attach(10, mavThread); // 定时 指定飞控mavlink 串口返回的数据类别 防止飞控启动滞后
// flashTicker.start(); // 监听 按flash键时 主动发布对频主题
/////////////////////////////////MQTT_语音_MAVLINK 部分结束
// if (motocontrol.getstatus()==MS_Stop)
@ -212,8 +212,8 @@ void showinfo()
// if (pullweight > 10)
// printf("PullWeight:%d\n", pullweight); //发送重量到mqtt
// topicPubMsg[14]=motocontrol.gethooktatus_str() ;
// topicPubMsg[13]=pullweight;
topicPubMsg[14] = motocontrol.gethooktatus_str();
topicPubMsg[13] = pullweight;
// control_status_t cs=motocontrol.getcontrolstatus() ;
@ -400,10 +400,6 @@ void set_locked(bool locked)
// 在核心1上执行重要的延迟低的
void loop()
{
tksendinfo.update(); // 定时发送信息任务
// pubTicker.update(); //定时 发布主题
// mavTicker.update(); //定时 指定飞控mavlink 串口返回的数据类别 防止飞控启动滞后
// sercomm.getcommand(); // 得到控制命令
button_checktop.tick(); // 按钮
button_down.tick(); // 按钮
@ -441,8 +437,6 @@ void loop()
/////////////////////////////////MQTT_语音_MAVLINK 部分
/*从飞控拿数据*/
fc.comm_receive(mavlink_receiveCallback);
/*保持mqtt心跳*/
fc.mqttLoop(topicPub, topicPubCount);
/////////////////////////////////MQTT_语音_MAVLINK 部分结束
delay(1);
}
@ -466,13 +460,9 @@ void Task1(void *pvParameters)
// 显示重量
// printf("pullweight: %d \n", pullweight);
/*保持mqtt心跳*/
// fc.mqttLoop(topicSub, topicSubCount);
/// px1
// if (fc.checkWiFiStatus())
/*保持mqtt心跳,如果Mqtt没有连接会自动连接*/
// fc.mqttLoop(topicSub, topicSubCount);
if (fc.checkWiFiStatus())
/*保持mqtt心跳,如果Mqtt没有连接会自动连接*/
fc.mqttLoop("cmd");
vTaskDelay(10);
}
}
@ -712,8 +702,7 @@ void btn_presssatonce()
ESP_LOGD(MOUDLENAME, "UP_presssatonce");
led_show(255, 255, 255); // 高亮一下
fc.playText("发送对频信息");
/// px1
// fc.pubMQTTmsg(topicHandle[0], fc.getMacAdd());
fc.pubMQTTmsg(topicHandle[0], fc.getMacAdd());
}
// 收线按钮-长按
void upbtn_pressstart()