Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed sending message from PC / Fix for ENABLE_MESSENGER_UART #120

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "misc.h"
#include "settings.h"
#include "ui/ui.h"
#include "ui/fmradio.h"

const uint16_t FM_RADIO_MAX_FREQ = 1080; // 108 Mhz
const uint16_t FM_RADIO_MIN_FREQ = 875; // 87.5 Mhz
Expand Down Expand Up @@ -62,19 +63,20 @@ static void Key_EXIT()

static void Key_UP_DOWN(bool direction)
{
UI_DisplayFM_text("[SCN]");
BK1080_TuneNext(direction);
gEeprom.FM_FrequencyPlaying = BK1080_GetFrequency();
// save
gRequestSaveSettings = true;
}

void FM_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld)
{
{
uint8_t state = bKeyPressed + 2 * bKeyHeld;

if (state == 0) {
switch (Key)
{
{
case KEY_UP:
Key_UP_DOWN(true);
break;
Expand Down
54 changes: 35 additions & 19 deletions app/messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
#include "frequencies.h"
#include "driver/system.h"
#include "app/messenger.h"
#include "app/action.h"
#include "ui/ui.h"
#ifdef ENABLE_ENCRYPTION
#include "helper/crypto.h"
#endif

#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
#include "bsp/dp32g030/gpio.h"
#include "driver/gpio.h"

#if (defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)) || (defined(ENABLE_MESSENGER_UART))
#include "driver/uart.h"
#endif

Expand Down Expand Up @@ -587,7 +591,7 @@ void MSG_SendPacket() {
FUNCTION_Select(FUNCTION_TRANSMIT);

SYSTEM_DelayMs(50);

MSG_FSKSendData();

SYSTEM_DelayMs(50);
Expand All @@ -607,7 +611,7 @@ void MSG_SendPacket() {

// clear packet buffer
MSG_ClearPacketBuffer();

msgStatus = READY;

} else {
Expand Down Expand Up @@ -728,12 +732,21 @@ void MSG_HandleReceive(){
#else
snprintf(rxMessage[3], PAYLOAD_LENGTH + 2, "< %s", dataPacket.data.payload);
#endif
#ifdef ENABLE_MESSENGER_LED
if(strncmp((char*)dataPacket.data.payload,"LED", 3) == 0){
gFlashLightState = dataPacket.data.payload[3]-'0';
if(gFlashLightState){
GPIO_SetBit(&GPIOC->DATA, GPIOC_PIN_FLASHLIGHT);
} else {
GPIO_ClearBit(&GPIOC->DATA, GPIOC_PIN_FLASHLIGHT);
}
}
#endif
#ifdef ENABLE_MESSENGER_UART
UART_printf("SMS<%s\r\n", dataPacket.data.payload);
#endif
}

#ifdef ENABLE_MESSENGER_UART
UART_printf("SMS<%s\r\n", dencryptedTxMessage);
#endif

if ( gScreenToDisplay != DISPLAY_MSG ) {
hasNewMessage = 1;
gUpdateStatus = true;
Expand All @@ -748,8 +761,8 @@ void MSG_HandleReceive(){
}

// Transmit a message to the sender that we have received the message
if (dataPacket.data.header == MESSAGE_PACKET ||
dataPacket.data.header == ENCRYPTED_MESSAGE_PACKET)
if (dataPacket.data.header == MESSAGE_PACKET ||
dataPacket.data.header == ENCRYPTED_MESSAGE_PACKET)
{
// wait so the correspondent radio can properly receive it
SYSTEM_DelayMs(700);
Expand Down Expand Up @@ -819,7 +832,7 @@ void processBackspace() {

void MSG_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) {
uint8_t state = bKeyPressed + 2 * bKeyHeld;

if (state == MSG_BUTTON_EVENT_SHORT) {

switch (Key)
Expand Down Expand Up @@ -856,14 +869,7 @@ void MSG_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld) {
break;*/
case KEY_MENU:
// Send message
MSG_ClearPacketBuffer();
#ifdef ENABLE_ENCRYPTION
dataPacket.data.header=ENCRYPTED_MESSAGE_PACKET;
#else
dataPacket.data.header=MESSAGE_PACKET;
#endif
memcpy(dataPacket.data.payload, cMessage, sizeof(dataPacket.data.payload));
MSG_SendPacket();
MSG_Send(cMessage);
break;
case KEY_EXIT:
gRequestDisplayScreen = DISPLAY_MAIN;
Expand Down Expand Up @@ -894,5 +900,15 @@ void MSG_ClearPacketBuffer()
memset(dataPacket.serializedArray, 0, sizeof(dataPacket.serializedArray));
}

void MSG_Send(const char *cMessage){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this refactor. Good job!

MSG_ClearPacketBuffer();
#ifdef ENABLE_ENCRYPTION
dataPacket.data.header=ENCRYPTED_MESSAGE_PACKET;
#else
dataPacket.data.header=MESSAGE_PACKET;
#endif
memcpy(dataPacket.data.payload, cMessage, sizeof(dataPacket.data.payload));
MSG_SendPacket();
}

#endif
#endif
5 changes: 3 additions & 2 deletions app/messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef enum PacketType {

// Data Packet definition // 2024 kamilsss655
union DataPacket
{
{
struct{
uint8_t header;
uint8_t payload[PAYLOAD_LENGTH];
Expand All @@ -63,7 +63,8 @@ void MSG_FSKSendData();
void MSG_ClearPacketBuffer();
void MSG_SendAck();
void MSG_HandleReceive();
void MSG_Send(const char *cMessage);

#endif

#endif
#endif
53 changes: 42 additions & 11 deletions app/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
#include "sram-overlay.h"
#endif
#include "version.h"
#ifdef ENABLE_MESSENGER
#ifdef ENABLE_MESSENGER_UART
#include "app/messenger.h"
#include "external/printf/printf.h"
#endif
#endif


#define DMA_INDEX(x, y) (((x) + (y)) % sizeof(UART_DMA_Buffer))

Expand Down Expand Up @@ -212,7 +219,7 @@ static void CMD_0514(const uint8_t *pBuffer)
#endif

gSerialConfigCountDown_500ms = 12; // 6 sec

// turn the LCD backlight off
BACKLIGHT_TurnOff();

Expand Down Expand Up @@ -260,7 +267,7 @@ static void CMD_051D(const uint8_t *pBuffer)
return;

gSerialConfigCountDown_500ms = 12; // 6 sec

bReloadEeprom = false;

#ifdef ENABLE_FMRADIO
Expand Down Expand Up @@ -369,6 +376,30 @@ bool UART_IsCommandAvailable(void)
if (gUART_WriteIndex == DmaLength)
return false;

#ifdef ENABLE_MESSENGER
#ifdef ENABLE_MESSENGER_UART
if (strncmp(((char*)UART_DMA_Buffer) + gUART_WriteIndex, "SMS:",4) == 0)
{

char txMessage[PAYLOAD_LENGTH + 4];
memset(txMessage, 0, sizeof(txMessage));
snprintf(txMessage, (PAYLOAD_LENGTH + 4), "%s", &UART_DMA_Buffer[gUART_WriteIndex + 4]);

for (int i = 0; txMessage[i] != '\0'; i++)
{
if (txMessage[i] == '\r' || txMessage[i] == '\n')
txMessage[i] = '\0';
}
if (strlen(txMessage) > 0)
{
MSG_Send(txMessage);
UART_printf("SMS>%s\r\n", txMessage);
gUpdateDisplay = true;
}
}

#endif
#endif
while (gUART_WriteIndex != DmaLength && UART_DMA_Buffer[gUART_WriteIndex] != 0xABU)
gUART_WriteIndex = DMA_INDEX(gUART_WriteIndex, 1);

Expand Down Expand Up @@ -442,7 +473,7 @@ bool UART_IsCommandAvailable(void)
for (i = 0; i < (Size + 2u); i++)
UART_Command.Buffer[i] ^= Obfuscation[i % 16];
}

CRC = UART_Command.Buffer[Size] | (UART_Command.Buffer[Size + 1] << 8);

return (CRC_Calculate(UART_Command.Buffer, Size) != CRC) ? false : true;
Expand All @@ -455,33 +486,33 @@ void UART_HandleCommand(void)
case 0x0514:
CMD_0514(UART_Command.Buffer);
break;

case 0x051B:
CMD_051B(UART_Command.Buffer);
break;

case 0x051D:
CMD_051D(UART_Command.Buffer);
break;

case 0x051F: // Not implementing non-authentic command
break;

case 0x0521: // Not implementing non-authentic command
break;

case 0x0527:
CMD_0527();
break;

case 0x0529:
CMD_0529();
break;

case 0x052F:
CMD_052F(UART_Command.Buffer);
break;

case 0x05DD:
#if defined(ENABLE_OVERLAY)
overlay_FLASH_RebootToBootloader();
Expand Down
14 changes: 14 additions & 0 deletions driver/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "bsp/dp32g030/syscon.h"
#include "bsp/dp32g030/uart.h"
#include "driver/uart.h"
#include "external/printf/printf.h"

static bool UART_IsLogEnabled;
uint8_t UART_DMA_Buffer[256];
Expand Down Expand Up @@ -102,3 +103,16 @@ void UART_LogSend(const void *pBuffer, uint32_t Size)
UART_Send(pBuffer, Size);
}
}

void UART_printf(const char *str, ...)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we reuse LogUart() from debugging.h ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not for debug, but for send/recv via serial port. I think it should not be mixed.

{
char text[256];
int len;

va_list va;
va_start(va, str);
len = vsnprintf(text, sizeof(text), str, va);
va_end(va);

UART_Send(text, len);
}
3 changes: 2 additions & 1 deletion driver/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ void UART_Init(void);
void UART_Send(const void *pBuffer, uint32_t Size);
void UART_LogSend(const void *pBuffer, uint32_t Size);

#endif
void UART_printf(const char *str, ...);

#endif
11 changes: 8 additions & 3 deletions ui/fmradio.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "settings.h"
#include "ui/helper.h"

void UI_DisplayFM(void)
void UI_DisplayFM_text(char *text)
{
char String[16];

Expand All @@ -38,15 +38,20 @@ void UI_DisplayFM(void)
UI_PrintString(String, 0, 127, 0, 12);

memset(String, 0, sizeof(String));

strcpy(String, text);
UI_PrintString(String, 0, 127, 2, 10);

memset(String, 0, sizeof(String));

sprintf(String, "%3d.%d", gEeprom.FM_FrequencyPlaying / 10, gEeprom.FM_FrequencyPlaying % 10);
UI_DisplayFrequency(String, 32, 4, true);
UI_DisplayFrequency(String, 32, 4, true);

ST7565_BlitFullScreen();
}

void UI_DisplayFM(void)
{
UI_DisplayFM_text("");
Copy link
Owner

@kamilsss655 kamilsss655 Feb 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whereas I like this improvement, the implementation is not slim with the wrapper function approach. And considering we're at 99% mark of space utilization it is quite important.

It could be rewritten to use a global boolean (which probably is already defined, but not used) to indicate scan in progress and then the ui function can determine whether or not display SCN.

}

#endif
2 changes: 1 addition & 1 deletion ui/fmradio.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#define UI_FM_H

#ifdef ENABLE_FMRADIO
void UI_DisplayFM_text(char*);
void UI_DisplayFM(void);
#endif

#endif