Skip to content

Commit

Permalink
Small tweaks: (#221)
Browse files Browse the repository at this point in the history
* Small tweaks:

1) Extracted debugging stuff to the "debug.h"
2) "release" profile, which removes all debug information.
3) pinned `espressif32 @ 6.10.0`
4) removed warnings

* fixed forward declaration

* added headers

* added header

* Cosmetics

* Cosmetics

* Cosmetics
  • Loading branch information
dkaukov authored Feb 11, 2025
1 parent 99939fc commit 955e72b
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 218 deletions.
131 changes: 131 additions & 0 deletions microcontroller-src/kv4p_ht_esp32_wroom_32/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
KV4P-HT (see http://kv4p.com)
Copyright (C) 2024 Vance Vagell
This program is free software: you can redistribute it and/or modify
it 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.
This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once

#include <Arduino.h>
#include "globals.h"

#ifndef RELEASE
#define _LOGE(fmt, ...) \
{ \
debug_log_printf(COMMAND_DEBUG_ERROR, ARDUHAL_LOG_FORMAT(E, fmt), ##__VA_ARGS__); \
}
#define _LOGW(fmt, ...) \
{ \
debug_log_printf(COMMAND_DEBUG_WARN, ARDUHAL_LOG_FORMAT(W, fmt), ##__VA_ARGS__); \
}
#define _LOGI(fmt, ...) \
{ \
debug_log_printf(COMMAND_DEBUG_INFO, ARDUHAL_LOG_FORMAT(I, fmt), ##__VA_ARGS__); \
}
#define _LOGD(fmt, ...) \
{ \
debug_log_printf(COMMAND_DEBUG_DEBUG, ARDUHAL_LOG_FORMAT(D, fmt), ##__VA_ARGS__); \
}
#define _LOGT(fmt, ...) \
{ \
debug_log_printf(COMMAND_DEBUG_TRACE, ARDUHAL_LOG_FORMAT(T, fmt), ##__VA_ARGS__); \
}
#else
#define _LOGE(fmt, ...) \
{ \
}
#define _LOGW(fmt, ...) \
{ \
}
#define _LOGI(fmt, ...) \
{ \
}
#define _LOGD(fmt, ...) \
{ \
}
#define _LOGT(fmt, ...) \
{ \
}
#endif

int debug_log_printf(uint8_t cmd, const char* format, ...) {
static char loc_buf[256];
char* temp = loc_buf;
int len;
va_list arg;
va_list copy;
va_start(arg, format);
va_copy(copy, arg);
len = vsnprintf(NULL, 0, format, arg);
va_end(copy);
if (len >= sizeof(loc_buf)) {
temp = (char*)malloc(len + 1);
if (temp == NULL) {
return 0;
}
}
vsnprintf(temp, len + 1, format, arg);
sendCmdToAndroid(cmd, (byte*) temp, len);
va_end(arg);
if (len >= sizeof(loc_buf)) {
free(temp);
}
return len;
}

void printEnvironment() {
#ifndef RELEASE
_LOGI("---");
_LOGI("Heap Size: %d", ESP.getHeapSize());
_LOGI("SDK Version: %s", ESP.getSdkVersion());
_LOGI("CPU Freq: %d", ESP.getCpuFreqMHz());
_LOGI("Sketch MD5: %s", ESP.getSketchMD5().c_str());
_LOGI("Chip model: %s", ESP.getChipModel());
_LOGI("PSRAM size: %d", ESP.getPsramSize());
_LOGI("FLASH size: %d", ESP.getFlashChipSize());
_LOGI("EFUSE mac: 0x%llx", ESP.getEfuseMac());
_LOGI("Hardware version: 0x%02x", hardware_version);
_LOGI("---");
#endif
}

void measureLoopFrequency() {
#ifndef RELEASE
// Exponential Weighted Moving Average (EWMA) for loop time
static float avgLoopTime = 0;
const float alpha = 0.1; // Smoothing factor (adjust as needed)
static uint32_t lastTime = 0;
static uint32_t startTime = 0;
// Measure time per iteration
uint32_t now = micros();
uint32_t duration = now - startTime;
startTime = now;
// Apply EWMA filtering
avgLoopTime = alpha * duration + (1 - alpha) * avgLoopTime;
// Report every second
if (now - lastTime >= 1000000) { // 1,000,000 µs = 1 second
float frequency = 1e6 / avgLoopTime; // Convert loop time to frequency
_LOGI("Loop Time: %.2f µs, Frequency: %.2f Hz", avgLoopTime, frequency);
lastTime = now;
}
#endif
}

void inline debugSetup() {
printEnvironment();
}

void inline debugLoop() {
measureLoopFrequency();
}
74 changes: 74 additions & 0 deletions microcontroller-src/kv4p_ht_esp32_wroom_32/globals.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
KV4P-HT (see http://kv4p.com)
Copyright (C) 2024 Vance Vagell
This program is free software: you can redistribute it and/or modify
it 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.
This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once

#include <Arduino.h>

// Hardware version detection
#define HW_VER_PIN_0 39 // 0xF0
#define HW_VER_PIN_1 36 // 0x0F
// LOW = 0, HIGH = F, 1 <= analog values <= E
#define HW_VER_V1 (0x00)
#define HW_VER_V2_0C (0xFF)
#define HW_VER_V2_0D (0xF0)
// #define HW_VER_?? (0x0F) // Unused

typedef uint8_t hw_ver_t; // This allows us to do a lot more in the future if we want.
hw_ver_t hardware_version = HW_VER_V1; // lowest common denominator

// Commands defined here must match the Android app
const uint8_t COMMAND_PTT_DOWN = 1; // start transmitting audio that Android app will send
const uint8_t COMMAND_PTT_UP = 2; // stop transmitting audio, go into RX mode
const uint8_t COMMAND_TUNE_TO = 3; // change the frequency
const uint8_t COMMAND_FILTERS = 4; // toggle filters on/off
const uint8_t COMMAND_STOP = 5; // stop everything, just wait for next command
const uint8_t COMMAND_GET_FIRMWARE_VER = 6; // report FIRMWARE_VER in the format '00000001' for 1 (etc.)

// Outgoing commands (ESP32 -> Android)
const byte COMMAND_SMETER_REPORT = 0x53; // 'S'
const byte COMMAND_PHYS_PTT_DOWN = 0x44; // 'D'
const byte COMMAND_PHYS_PTT_UP = 0x55; // 'U'
const byte COMMAND_DEBUG_INFO = 0x01;
const byte COMMAND_DEBUG_ERROR = 0x02;
const byte COMMAND_DEBUG_WARN = 0x03;
const byte COMMAND_DEBUG_DEBUG = 0x04;
const byte COMMAND_DEBUG_TRACE = 0x05;

// Mode of the app, which is essentially a state machine
enum Mode {
MODE_TX,
MODE_RX,
MODE_STOPPED
};
Mode mode = MODE_STOPPED;

// Current SQ status
bool squelched = false;

////////////////////////////////////////////////////////////////////////////////
/// Forward Declarations
////////////////////////////////////////////////////////////////////////////////
void initI2SRx();
void initI2STx();
void tuneTo(float freqTx, float freqRx, int txTone, int rxTone, int squelch, String bandwidth);
void setMode(int newMode);
void processTxAudio(uint8_t tempBuffer[], int bytesRead);
void iir_lowpass_reset();
hw_ver_t get_hardware_version();
void reportPhysPttState();
void sendCmdToAndroid(byte cmdByte, const byte *params, size_t paramsLen);
Loading

0 comments on commit 955e72b

Please sign in to comment.