Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
VanceVagell committed Feb 7, 2025
2 parents 4c19275 + 8d4737c commit 1b8b5c0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 44 deletions.
52 changes: 20 additions & 32 deletions microcontroller-src/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
## Table of Contents

- [Table of Contents](#table-of-contents)
- [Prerequisites](#prerequisites)
- [Install ESP32 Drivers](#install-esp32-drivers)
- [Install ESP32 Drivers](#install-esp32-drivers)
- [Option 1: Arduino IDE](#option-1-arduino-ide)
- [Install Arduino IDE](#install-arduino-ide)
- [Install ESP32 Board Support](#install-esp32-board-support)
- [Install Required Libraries](#install-required-libraries)
- [Opening the Project](#opening-the-project-arduino-ide)
- [Building the Project](#building-the-project-arduino-ide)
- [Uploading to the ESP32](#uploading-to-the-esp32-arduino-ide)
- [Install Arduino IDE](#install-arduino-ide)
- [Install ESP32 Board Support](#install-esp32-board-support)
- [Install Required Libraries](#install-required-libraries)
- [Opening the Project (Arduino IDE)](#opening-the-project-arduino-ide)
- [Building the Project (Arduino IDE)](#building-the-project-arduino-ide)
- [Uploading to the ESP32 (Arduino IDE)](#uploading-to-the-esp32-arduino-ide)
- [Option 2: PlatformIO](#option-2-platformio)
- [Install PlatformIO](#install-platformio)
- [Opening the Project](#opening-the-project-platformio)
- [Building the Project](#building-the-project-platformio)
- [Uploading to the ESP32](#uploading-to-the-esp32-platformio)
- [PlatformIO Specific Notes](#platformio-specific-notes)
- [Install PlatformIO](#install-platformio)
- [Opening the Project (PlatformIO)](#opening-the-project-platformio)
- [Building the Project (PlatformIO)](#building-the-project-platformio)
- [Uploading to the ESP32 (PlatformIO)](#uploading-to-the-esp32-platformio)
- [PlatformIO Specific Notes](#platformio-specific-notes)
- [Additional Notes](#additional-notes)




## Prerequisites

### Install ESP32 Drivers
Expand All @@ -31,7 +29,6 @@
- **Windows:** Check the Device Manager for the COM port associated with your ESP32.
- **macOS/Linux:** Verify the presence of `/dev/tty.*` devices corresponding to your ESP32.


## Option 1: Arduino IDE

### Install Arduino IDE
Expand All @@ -58,14 +55,12 @@
### Install Required Libraries

1. **Install EspSoftwareSerial:**

- Navigate to `Sketch` > `Include Library` > `Manage Libraries`.
- In the **Library Manager** window, enter **"EspSoftwareSerial"** into the search bar.
- Locate the **EspSoftwareSerial** library in the search results.
- Click the **Install** button to add the library to your Arduino environment.

2. **Install DRA818:**

> **Note:** The version of the DRA818 library available through the Arduino Library Manager is currently broken. To ensure proper functionality, you need to install it manually from the official GitHub release.
- **Download the DRA818 Library ZIP:**
Expand All @@ -80,8 +75,7 @@
- A confirmation message should appear indicating that the library was added successfully.

3. **Confirm All Libraries Are Installed:**

- After completing the above steps, ensure that both **EspSoftwareSerial** and **DRA818** are listed under `Sketch` > `Include Library`.
- After completing the above steps, ensure that **EspSoftwareSerial** and **DRA818** are listed under `Sketch` > `Include Library`.
- If any libraries are missing, revisit the installation steps to ensure they were added correctly.

### Opening the Project (Arduino IDE)
Expand Down Expand Up @@ -111,7 +105,6 @@
- The Arduino IDE will compile (if not already done) and upload the firmware to the ESP32.
- Monitor the output pane for upload progress and confirmation of success.


## Option 2: PlatformIO

### Install PlatformIO
Expand Down Expand Up @@ -174,13 +167,12 @@
### PlatformIO Specific Notes

- **Renaming the Main File for IntelliSense:**
- **For IntelliSense to work properly in PlatformIO**, rename the main project file from `.ino` to `.cpp`. For example, rename `kv4p_ht_esp32_wroom_32.ino` to `kv4p_ht_esp32_wroom_32.cpp`.
- **Important:** Do **not** commit this renamed file to the repository. Keeping the file as `.ino` in the repository ensures compatibility with the Arduino IDE build process.
- **Workflow Suggestion:**
- When working locally in PlatformIO, perform the rename to benefit from IntelliSense.
- Before committing changes, revert the file extension back to `.ino` to maintain Arduino IDE compatibility.


- ~~**For IntelliSense to work properly in PlatformIO**, rename the main project file from `.ino` to `.cpp`. For example, rename `kv4p_ht_esp32_wroom_32.ino` to `kv4p_ht_esp32_wroom_32.cpp`.~~
- I don't think this is necessary anymore. My (@SmittyHalibut) PlatformIO does all the right things just editing a .ino file now (Jan 2025).
- **Important:** Do **not** commit this renamed file to the repository. Keeping the file as `.ino` in the repository ensures compatibility with the Arduino IDE build process.
- **Workflow Suggestion:**
- When working locally in PlatformIO, perform the rename to benefit from IntelliSense.
- Before committing changes, revert the file extension back to `.ino` to maintain Arduino IDE compatibility.

## Additional Notes

Expand All @@ -194,7 +186,3 @@

- **Consistent Project Structure:**
- Ensure that any changes made in one environment (e.g., library installations, code modifications) are compatible with the other to maintain consistency across both build systems.




Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ int matchedDelimiterTokens = 0;
int matchedDelimiterTokensRx = 0;

// Mode of the app, which is essentially a state machine
#define MODE_TX 0
#define MODE_RX 1
#define MODE_STOPPED 2
int mode = MODE_STOPPED;
enum Mode {
MODE_TX,
MODE_RX,
MODE_STOPPED
};
Mode mode = MODE_STOPPED;

// Audio sampling rate, must match what Android app expects (and sends).
#define AUDIO_SAMPLE_RATE 22050
Expand Down Expand Up @@ -116,6 +118,9 @@ char radioModuleStatus = RADIO_MODULE_NOT_FOUND;
// Have we installed an I2S driver at least once?
bool i2sStarted = false;

// Current SQ status
bool squelched = false;

// I2S audio sampling stuff
#define I2S_READ_LEN 1024/4
#define I2S_WRITE_LEN 1024/4
Expand All @@ -138,6 +143,22 @@ typedef uint8_t hw_ver_t; // This allows us to do a lot more in the future if w
// #define HW_VER_?? (0x0F) // Unused
hw_ver_t hardware_version = HW_VER_V1; // lowest common denominator

// NeoPixel support
#define PIXELS_PIN 13
#define NUM_PIXELS 1

struct RGBColor {
uint8_t red;
uint8_t green;
uint8_t blue;
};
const RGBColor COLOR_STOPPED = {0, 0, 0};
const RGBColor COLOR_RX_SQL_CLOSED = {0, 0, 32};
const RGBColor COLOR_RX_SQL_OPEN = {0, 32, 0};
const RGBColor COLOR_TX = {16, 16, 0};
const RGBColor COLOR_BLACK = {0, 0, 0};
RGBColor COLOR_HW_VER = COLOR_BLACK;

#define _LOGE(fmt, ...) \
{ \
debug_log_printf(COMMAND_DEBUG_ERROR, ARDUHAL_LOG_FORMAT(E, fmt), ##__VA_ARGS__); \
Expand Down Expand Up @@ -166,11 +187,13 @@ hw_ver_t hardware_version = HW_VER_V1; // lowest common denominator
void initI2SRx();
void initI2STx();
void tuneTo(float freqTx, float freqRx, int tone, int squelch, String bandwidth);
void setMode(int newMode);
void setMode(Mode newMode);
void processTxAudio(uint8_t tempBuffer[], int bytesRead);
void iir_lowpass_reset();
hw_ver_t get_hardware_version();
void reportPhysPttState();
void show_LEDs();
void neopixelColor(RGBColor C, uint8_t bright);

void sendCmdToAndroid(byte cmdByte, const byte *params, size_t paramsLen);

Expand Down Expand Up @@ -211,16 +234,20 @@ void setup() {
// Hardware dependent pin assignments.
switch (hardware_version) {
case HW_VER_V1:
COLOR_HW_VER = {0, 32, 0};
SQ_PIN = 32;
break;
case HW_VER_V2_0C:
COLOR_HW_VER = {32, 0, 0};
SQ_PIN = 4;
break;
case HW_VER_V2_0D:
COLOR_HW_VER = {0, 0, 32};
SQ_PIN = 4;
break;
default:
// Unknown version detected. Indicate this some way?
COLOR_HW_VER = {16, 16, 16};
break;
}

Expand Down Expand Up @@ -249,7 +276,9 @@ void setup() {

printEnvironment();
// Begin in STOPPED mode
squelched = (digitalRead(SQ_PIN) == HIGH);
setMode(MODE_STOPPED);
show_LEDs();
_LOGI("Setup is finished");
}

Expand Down Expand Up @@ -424,7 +453,7 @@ void loop() {

if (hardware_version == HW_VER_V2_0C) {
// v2.0c has a lower input ADC range.
result = dra->volume(4);
result = dra->volume(6);
} else {
result = dra->volume(8);
}
Expand Down Expand Up @@ -654,7 +683,7 @@ void loop() {
ESP_ERROR_CHECK(i2s_read(I2S_NUM_0, &buffer16, sizeof(buffer16), &bytesRead, 0));
size_t samplesRead = bytesRead / 2;

bool squelched = (digitalRead(SQ_PIN) == HIGH);
squelched = (digitalRead(SQ_PIN) == HIGH);

for (int i = 0; i < samplesRead; i++) {
int16_t sample = remove_dc(2048 - (int16_t)(buffer16[i] & 0xfff));
Expand Down Expand Up @@ -734,6 +763,7 @@ void loop() {
// Disregard, we don't want to crash. Just pick up at next loop().)
// Serial.println("Exception in loop(), skipping cycle.");
}
show_LEDs();
}

/**
Expand Down Expand Up @@ -775,25 +805,22 @@ void tuneTo(float freqTx, float freqRx, int txTone, int rxTone, int squelch, Str
// Serial.println("tuneTo: " + String(result));
}

void setMode(int newMode) {
void setMode(Mode newMode) {
mode = newMode;
switch (mode) {
case MODE_STOPPED:
_LOGI("MODE_STOPPED");
digitalWrite(LED_PIN, LOW);
digitalWrite(PTT_PIN, HIGH);
break;
case MODE_RX:
_LOGI("MODE_RX");
digitalWrite(LED_PIN, LOW);
digitalWrite(PTT_PIN, HIGH);
initI2SRx();
matchedDelimiterTokensRx = 0;
break;
case MODE_TX:
_LOGI("MODE_TX");
txStartTime = micros();
digitalWrite(LED_PIN, HIGH);
digitalWrite(PTT_PIN, LOW);
initI2STx();
txCachedAudioBytes = 0;
Expand Down Expand Up @@ -874,3 +901,51 @@ void measureLoopFrequency() {
lastTime = now;
}
}

void neopixelColor(RGBColor C, uint8_t bright = 255) {
uint8_t red = (uint16_t(C.red) * bright) / 255;
uint8_t green = (uint16_t(C.green) * bright) / 255;
uint8_t blue = (uint16_t(C.blue) * bright) / 255;
neopixelWrite(PIXELS_PIN, red, green, blue);
}

// Calculate a float between min and max, that ramps from min to max in half of breath_every,
// and from max to min the second half of breath_every.
// now and breath_every are arbitrary units, but usually are milliseconds from millis().
uint8_t calc_breath(uint32_t now, uint32_t breath_every, uint8_t min, uint8_t max) {
uint16_t amplitude = max - min; // Ensure enough range for calculations
uint16_t bright = ((now % breath_every) * 512) / breath_every; // Scale to 0-512
if (bright > 255) bright = 512 - bright; // Fold >255 back down to 255-0
return ((bright * amplitude) / 255) + min;
}

void show_LEDs() {
// When to actually act?
static uint32_t next_time = 0;
const static uint32_t update_every = 50; // in milliseconds
uint32_t now = millis();
// Only change LEDs if:
// * it's been more than update_every ms since we've last set the LEDs.
if (now >= next_time) {
next_time = now + update_every;
switch (mode) {
case MODE_STOPPED:
digitalWrite(LED_PIN, LOW);
neopixelColor(COLOR_HW_VER);
break;
case MODE_RX:
digitalWrite(LED_PIN, LOW);
if (squelched) {
neopixelColor(COLOR_RX_SQL_CLOSED, calc_breath(now, 2000, 127, 255));
} else {
neopixelColor(COLOR_RX_SQL_OPEN);
}
break;
case MODE_TX:
digitalWrite(LED_PIN, HIGH);
neopixelColor(COLOR_TX);
break;
}
}
}

2 changes: 1 addition & 1 deletion website-src/contribute.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ <h1>Overview docs</h1>
</main>
</div>
</body>
</html>
</html>

0 comments on commit 1b8b5c0

Please sign in to comment.