1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-12 19:10:32 +03:00

Refactoring of PR 13050 - support for custom OSD messages from external device (#14097)

This commit is contained in:
Petr Ledvina 2024-12-22 05:44:08 +01:00 committed by GitHub
parent cab60b6594
commit ac82d8b998
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 135 additions and 35 deletions

View file

@ -22,6 +22,7 @@ PG_SRC = \
pg/motor.c \ pg/motor.c \
pg/msp.c \ pg/msp.c \
pg/pg.c \ pg/pg.c \
pg/pilot.c \
pg/piniobox.c \ pg/piniobox.c \
pg/pinio.c \ pg/pinio.c \
pg/pin_pull_up_down.c \ pg/pin_pull_up_down.c \

View file

@ -81,8 +81,9 @@
#include "pg/alt_hold.h" #include "pg/alt_hold.h"
#include "pg/autopilot.h" #include "pg/autopilot.h"
#include "pg/motor.h" #include "pg/motor.h"
#include "pg/rx.h" #include "pg/pilot.h"
#include "pg/pos_hold.h" #include "pg/pos_hold.h"
#include "pg/rx.h"
#include "rx/rx.h" #include "rx/rx.h"

View file

@ -139,6 +139,7 @@ bool cliMode = false;
#include "pg/max7456.h" #include "pg/max7456.h"
#include "pg/mco.h" #include "pg/mco.h"
#include "pg/motor.h" #include "pg/motor.h"
#include "pg/pilot.h"
#include "pg/pinio.h" #include "pg/pinio.h"
#include "pg/pin_pull_up_down.h" #include "pg/pin_pull_up_down.h"
#include "pg/pg.h" #include "pg/pg.h"

View file

@ -95,6 +95,7 @@
#include "pg/msp.h" #include "pg/msp.h"
#include "pg/pg.h" #include "pg/pg.h"
#include "pg/pg_ids.h" #include "pg/pg_ids.h"
#include "pg/pilot.h"
#include "pg/pinio.h" #include "pg/pinio.h"
#include "pg/piniobox.h" #include "pg/piniobox.h"
#include "pg/pos_hold.h" #include "pg/pos_hold.h"

View file

@ -106,13 +106,6 @@ pidProfile_t *currentPidProfile;
#define RX_SPI_DEFAULT_PROTOCOL 0 #define RX_SPI_DEFAULT_PROTOCOL 0
#endif #endif
PG_REGISTER_WITH_RESET_TEMPLATE(pilotConfig_t, pilotConfig, PG_PILOT_CONFIG, 2);
PG_RESET_TEMPLATE(pilotConfig_t, pilotConfig,
.craftName = { 0 },
.pilotName = { 0 },
);
PG_REGISTER_WITH_RESET_TEMPLATE(systemConfig_t, systemConfig, PG_SYSTEM_CONFIG, 3); PG_REGISTER_WITH_RESET_TEMPLATE(systemConfig_t, systemConfig, PG_SYSTEM_CONFIG, 3);
PG_RESET_TEMPLATE(systemConfig_t, systemConfig, PG_RESET_TEMPLATE(systemConfig_t, systemConfig,

View file

@ -25,20 +25,11 @@
#include "pg/pg.h" #include "pg/pg.h"
#define MAX_NAME_LENGTH 16u
typedef enum { typedef enum {
CONFIGURATION_STATE_UNCONFIGURED = 0, CONFIGURATION_STATE_UNCONFIGURED = 0,
CONFIGURATION_STATE_CONFIGURED, CONFIGURATION_STATE_CONFIGURED,
} configurationState_e; } configurationState_e;
typedef struct pilotConfig_s {
char craftName[MAX_NAME_LENGTH + 1];
char pilotName[MAX_NAME_LENGTH + 1];
} pilotConfig_t;
PG_DECLARE(pilotConfig_t, pilotConfig);
typedef struct systemConfig_s { typedef struct systemConfig_s {
uint8_t pidProfileIndex; uint8_t pidProfileIndex;
uint8_t activeRateProfile; uint8_t activeRateProfile;

View file

@ -124,6 +124,7 @@
#include "pg/gps_rescue.h" #include "pg/gps_rescue.h"
#include "pg/gyrodev.h" #include "pg/gyrodev.h"
#include "pg/motor.h" #include "pg/motor.h"
#include "pg/pilot.h"
#include "pg/pos_hold.h" #include "pg/pos_hold.h"
#include "pg/rx.h" #include "pg/rx.h"
#include "pg/rx_spi.h" #include "pg/rx_spi.h"
@ -1158,12 +1159,7 @@ static bool mspProcessOutCommand(mspDescriptor_t srcDesc, int16_t cmdMSP, sbuf_t
break; break;
case MSP_NAME: case MSP_NAME:
{ sbufWriteString(dst, pilotConfig()->craftName);
const int nameLen = strlen(pilotConfig()->craftName);
for (int i = 0; i < nameLen; i++) {
sbufWriteU8(dst, pilotConfig()->craftName[i]);
}
}
break; break;
#ifdef USE_SERVOS #ifdef USE_SERVOS
@ -2610,9 +2606,7 @@ static mspResult_e mspFcProcessOutCommandWithArg(mspDescriptor_t srcDesc, int16_
// type byte, then length byte followed by the actual characters // type byte, then length byte followed by the actual characters
sbufWriteU8(dst, textType); sbufWriteU8(dst, textType);
sbufWriteU8(dst, textLength); sbufWriteU8(dst, textLength);
for (unsigned int i = 0; i < textLength; i++) { sbufWriteData(dst, textVar, textLength);
sbufWriteU8(dst, textVar[i]);
}
} }
break; break;
#ifdef USE_LED_STRIP #ifdef USE_LED_STRIP
@ -3985,10 +3979,8 @@ static mspResult_e mspProcessInCommand(mspDescriptor_t srcDesc, int16_t cmdMSP,
#endif #endif
case MSP_SET_NAME: case MSP_SET_NAME:
memset(pilotConfigMutable()->craftName, 0, ARRAYLEN(pilotConfig()->craftName)); memset(pilotConfigMutable()->craftName, 0, sizeof(pilotConfigMutable()->craftName));
for (unsigned int i = 0; i < MIN(MAX_NAME_LENGTH, dataSize); i++) { sbufReadData(src, pilotConfigMutable()->craftName, MIN(ARRAYLEN(pilotConfigMutable()->craftName) - 1, dataSize));
pilotConfigMutable()->craftName[i] = sbufReadU8(src);
}
#ifdef USE_OSD #ifdef USE_OSD
osdAnalyzeActiveElements(); osdAnalyzeActiveElements();
#endif #endif
@ -4068,35 +4060,51 @@ static mspResult_e mspProcessInCommand(mspDescriptor_t srcDesc, int16_t cmdMSP,
case MSP2_SET_TEXT: case MSP2_SET_TEXT:
{ {
// type byte, then length byte followed by the actual characters // type byte, then length byte followed by the actual characters
const uint8_t textType = sbufReadU8(src); const unsigned textType = sbufReadU8(src);
char* textVar; char* textVar;
const uint8_t textLength = MIN(MAX_NAME_LENGTH, sbufReadU8(src)); unsigned textSpace;
switch (textType) { switch (textType) {
case MSP2TEXT_PILOT_NAME: case MSP2TEXT_PILOT_NAME:
textVar = pilotConfigMutable()->pilotName; textVar = pilotConfigMutable()->pilotName;
textSpace = sizeof(pilotConfigMutable()->pilotName) - 1;
break; break;
case MSP2TEXT_CRAFT_NAME: case MSP2TEXT_CRAFT_NAME:
textVar = pilotConfigMutable()->craftName; textVar = pilotConfigMutable()->craftName;
textSpace = sizeof(pilotConfigMutable()->craftName) - 1;
break; break;
case MSP2TEXT_PID_PROFILE_NAME: case MSP2TEXT_PID_PROFILE_NAME:
textVar = currentPidProfile->profileName; textVar = currentPidProfile->profileName;
textSpace = sizeof(currentPidProfile->profileName) - 1;
break; break;
case MSP2TEXT_RATE_PROFILE_NAME: case MSP2TEXT_RATE_PROFILE_NAME:
textVar = currentControlRateProfile->profileName; textVar = currentControlRateProfile->profileName;
textSpace = sizeof(currentControlRateProfile->profileName) - 1;
break; break;
case MSP2TEXT_CUSTOM_MSG_0:
case MSP2TEXT_CUSTOM_MSG_0 + 1:
case MSP2TEXT_CUSTOM_MSG_0 + 2:
case MSP2TEXT_CUSTOM_MSG_0 + 3: {
unsigned msgIdx = textType - MSP2TEXT_CUSTOM_MSG_0;
if (msgIdx < OSD_CUSTOM_MSG_COUNT) {
textVar = pilotConfigMutable()->message[msgIdx];
textSpace = sizeof(pilotConfigMutable()->message[msgIdx]) - 1;
} else {
return MSP_RESULT_ERROR;
}
break;
}
default: default:
return MSP_RESULT_ERROR; return MSP_RESULT_ERROR;
} }
const unsigned textLength = MIN(textSpace, sbufReadU8(src));
memset(textVar, 0, strlen(textVar)); memset(textVar, 0, strlen(textVar));
for (unsigned int i = 0; i < textLength; i++) { sbufReadData(src, textVar, textLength);
textVar[i] = sbufReadU8(src);
}
#ifdef USE_OSD #ifdef USE_OSD
if (textType == MSP2TEXT_PILOT_NAME || textType == MSP2TEXT_CRAFT_NAME) { if (textType == MSP2TEXT_PILOT_NAME || textType == MSP2TEXT_CRAFT_NAME) {

View file

@ -38,3 +38,6 @@
#define MSP2TEXT_RATE_PROFILE_NAME 4 #define MSP2TEXT_RATE_PROFILE_NAME 4
#define MSP2TEXT_BUILDKEY 5 #define MSP2TEXT_BUILDKEY 5
#define MSP2TEXT_RELEASENAME 6 #define MSP2TEXT_RELEASENAME 6
#define MSP2TEXT_CUSTOM_MSG_0 7 // CUSTOM_MSG_MAX_NUM entries are allocated
#define CUSTOM_MSG_MAX_NUM 4
// next new variable type must be >= MSP2TEXT_CUSTOM_MSG_0 + CUSTOM_MSG_MAX_NUM (11)

View file

@ -190,6 +190,10 @@ typedef enum {
OSD_GPS_LAP_TIME_PREVIOUS, OSD_GPS_LAP_TIME_PREVIOUS,
OSD_GPS_LAP_TIME_BEST3, OSD_GPS_LAP_TIME_BEST3,
OSD_DEBUG2, OSD_DEBUG2,
OSD_CUSTOM_MSG0,
OSD_CUSTOM_MSG1,
OSD_CUSTOM_MSG2,
OSD_CUSTOM_MSG3,
OSD_ITEM_COUNT // MUST BE LAST OSD_ITEM_COUNT // MUST BE LAST
} osd_items_e; } osd_items_e;

View file

@ -161,6 +161,7 @@
#include "osd/osd_warnings.h" #include "osd/osd_warnings.h"
#include "pg/motor.h" #include "pg/motor.h"
#include "pg/pilot.h"
#include "pg/stats.h" #include "pg/stats.h"
#include "rx/rx.h" #include "rx/rx.h"
@ -810,6 +811,18 @@ static void osdElementCompassBar(osdElementParms_t *element)
element->buff[9] = 0; element->buff[9] = 0;
} }
//display custom message from MSPv2
static void osdElementCustomMsg(osdElementParms_t *element)
{
int msgIndex = element->item - OSD_CUSTOM_MSG0;
if (msgIndex < 0 || msgIndex >= OSD_CUSTOM_MSG_COUNT || pilotConfig()->message[msgIndex][0] == '\0') {
tfp_sprintf(element->buff, "CUSTOM_MSG%d", msgIndex + 1);
} else {
strncpy(element->buff, pilotConfig()->message[msgIndex], MAX_NAME_LENGTH);
element->buff[MAX_NAME_LENGTH] = 0; // terminate maximum-length string
}
}
#ifdef USE_ADC_INTERNAL #ifdef USE_ADC_INTERNAL
static void osdElementCoreTemperature(osdElementParms_t *element) static void osdElementCoreTemperature(osdElementParms_t *element)
{ {
@ -1804,6 +1817,10 @@ static const uint8_t osdElementDisplayOrder[] = {
OSD_MAH_DRAWN, OSD_MAH_DRAWN,
OSD_WATT_HOURS_DRAWN, OSD_WATT_HOURS_DRAWN,
OSD_CRAFT_NAME, OSD_CRAFT_NAME,
OSD_CUSTOM_MSG0,
OSD_CUSTOM_MSG1,
OSD_CUSTOM_MSG2,
OSD_CUSTOM_MSG3,
OSD_ALTITUDE, OSD_ALTITUDE,
OSD_ROLL_PIDS, OSD_ROLL_PIDS,
OSD_PITCH_PIDS, OSD_PITCH_PIDS,
@ -1902,6 +1919,10 @@ const osdElementDrawFn osdElementDrawFunction[OSD_ITEM_COUNT] = {
[OSD_ITEM_TIMER_2] = osdElementTimer, [OSD_ITEM_TIMER_2] = osdElementTimer,
[OSD_FLYMODE] = osdElementFlymode, [OSD_FLYMODE] = osdElementFlymode,
[OSD_CRAFT_NAME] = NULL, // only has background [OSD_CRAFT_NAME] = NULL, // only has background
[OSD_CUSTOM_MSG0] = osdElementCustomMsg,
[OSD_CUSTOM_MSG1] = osdElementCustomMsg,
[OSD_CUSTOM_MSG2] = osdElementCustomMsg,
[OSD_CUSTOM_MSG3] = osdElementCustomMsg,
[OSD_THROTTLE_POS] = osdElementThrottlePosition, [OSD_THROTTLE_POS] = osdElementThrottlePosition,
#ifdef USE_VTX_COMMON #ifdef USE_VTX_COMMON
[OSD_VTX_CHANNEL] = osdElementVtxChannel, [OSD_VTX_CHANNEL] = osdElementVtxChannel,

38
src/main/pg/pilot.c Normal file
View file

@ -0,0 +1,38 @@
/*
* This file is part of Betaflight.
*
* Betaflight is free software. You can redistribute this software
* and/or modify this software under the terms of the GNU General
* Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later
* version.
*
* Betaflight 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <string.h>
#include "platform.h"
#include "pg/pg.h"
#include "pg/pg_ids.h"
#include "pilot.h"
PG_REGISTER_WITH_RESET_TEMPLATE(pilotConfig_t, pilotConfig, PG_PILOT_CONFIG, 2);
PG_RESET_TEMPLATE(pilotConfig_t, pilotConfig,
.craftName = { 0 },
.pilotName = { 0 },
.message = { {0} },
);

35
src/main/pg/pilot.h Normal file
View file

@ -0,0 +1,35 @@
/*
* This file is part of Betaflight.
*
* Betaflight is free software. You can redistribute this software
* and/or modify this software under the terms of the GNU General
* Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later
* version.
*
* Betaflight 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "pg/pg.h"
#define MAX_NAME_LENGTH 16u
#define OSD_CUSTOM_MSG_COUNT 4
typedef struct pilotConfig_s {
char craftName[MAX_NAME_LENGTH + 1];
char pilotName[MAX_NAME_LENGTH + 1];
char message[OSD_CUSTOM_MSG_COUNT][MAX_NAME_LENGTH + 1];
} pilotConfig_t;
PG_DECLARE(pilotConfig_t, pilotConfig);

View file

@ -53,6 +53,7 @@ extern "C" {
#include "pg/pg_ids.h" #include "pg/pg_ids.h"
#include "pg/beeper.h" #include "pg/beeper.h"
#include "pg/gps.h" #include "pg/gps.h"
#include "pg/pilot.h"
#include "pg/rx.h" #include "pg/rx.h"
#include "rx/rx.h" #include "rx/rx.h"
#include "scheduler/scheduler.h" #include "scheduler/scheduler.h"

View file

@ -61,6 +61,7 @@ extern "C" {
#include "pg/pg.h" #include "pg/pg.h"
#include "pg/pg_ids.h" #include "pg/pg_ids.h"
#include "pg/pilot.h"
#include "pg/rx.h" #include "pg/rx.h"
#include "rx/rx.h" #include "rx/rx.h"

View file

@ -58,6 +58,7 @@ extern "C" {
#include "pg/gps_rescue.h" #include "pg/gps_rescue.h"
#include "pg/pg.h" #include "pg/pg.h"
#include "pg/pg_ids.h" #include "pg/pg_ids.h"
#include "pg/pilot.h"
#include "pg/rx.h" #include "pg/rx.h"
#include "sensors/acceleration.h" #include "sensors/acceleration.h"