Skip to content

Commit

Permalink
add optional I2S and optional WM8960 support, envs for lora v3,wifi v…
Browse files Browse the repository at this point in the history
…3&vanilla (#108)
  • Loading branch information
haklein authored Dec 2, 2024
1 parent 48c0c6d commit bdf12a4
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 20 deletions.
113 changes: 99 additions & 14 deletions Software/src/Version 6/MorseOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@
#include "MorsePreferences.h"

#include "SSD1306Wire.h"
SSD1306Wire display(0x3c, OLED_SDA, OLED_SCL, GEOMETRY_128_64, I2C_ONE, 700000);
SSD1306Wire display(0x3c, OLED_SDA, OLED_SCL, GEOMETRY_128_64, I2C_TWO, 700000);

#ifdef CONFIG_SOUND_I2S
#include "I2S_Sidetone.hpp"
I2S_Sidetone sidetone;
#endif

#ifdef CONFIG_WM8960
#include <SparkFun_WM8960_Arduino_Library.h>
WM8960 codec;
#endif

using namespace MorseOutput;

Expand Down Expand Up @@ -601,22 +611,87 @@ void MorseOutput::resetTOT() { //// reset the Time Out Timer - we do this

void MorseOutput::soundSetup()
{
// set up PWMs for tone generation
ledcSetup(toneChannel, toneFreq, pwmResolution);
ledcAttachPin(LF_Pin, toneChannel);
#ifndef CONFIG_SOUND_I2S
// set up PWMs for tone generation
ledcSetup(toneChannel, toneFreq, pwmResolution);
ledcAttachPin(LF_Pin, toneChannel);

ledcSetup(lineOutChannel, toneFreq, pwmResolution);
ledcAttachPin(lineOutPin, lineOutChannel); ////// change this for real version - no line out currntly

ledcSetup(volChannel, volFreq, pwmResolution);
ledcAttachPin(HF_Pin, volChannel);

ledcWrite(toneChannel, 0);
ledcWrite(lineOutChannel, 0);
#else
#ifdef CONFIG_WM8960
#pragma message ("WM8960 CODEC")
Wire.begin(CONFIG_WM8960_SDA, CONFIG_WM8960_SCL);
if (codec.begin() == false) //Begin communication over I2C
{
DEBUG("The WM8960 did not respond. Please check wiring.");
} else {
DEBUG("WM8960 is connected properly.");
// General setup needed
codec.enableVREF();
codec.enableVMID();

// Connect from DAC outputs to output mixer
codec.enableLD2LO();
codec.enableRD2RO();

// Enable output mixers
codec.enableLOMIX();
codec.enableROMIX();

// CLOCK STUFF, These settings will get you 44.1KHz sample rate, and class-d
// freq at 705.6kHz
codec.enablePLL(); // Needed for class-d amp clock
codec.setPLLPRESCALE(WM8960_PLLPRESCALE_DIV_2);
codec.setSMD(WM8960_PLL_MODE_FRACTIONAL);
codec.setCLKSEL(WM8960_CLKSEL_PLL);
codec.setSYSCLKDIV(WM8960_SYSCLK_DIV_BY_2);
codec.setBCLKDIV(4);
codec.setDCLKDIV(WM8960_DCLKDIV_16);
codec.setPLLN(7);
codec.setPLLK(0x86, 0xC2, 0x26); // PLLK=86C226h
codec.setWL(WM8960_WL_16BIT);

codec.enablePeripheralMode();

codec.enableDacLeft();
codec.enableDacRight();

codec.disableDacMute(); // Default is "soft mute" on, so we must disable mute to make channels active
//codec.enableLoopBack(); // Loopback sends ADC data directly into DAC
codec.disableLoopBack();

codec.enableSpeakers();

codec.setDacLeftDigitalVolumeDB(20.0);
codec.setDacRightDigitalVolumeDB(20.0);

Serial.println("SPKR Volume set to 0dB");
codec.setSpeakerVolumeDB(6.00);

codec.enableHeadphones();
codec.setHeadphoneVolumeDB(6.00);

// headphone jack detection
codec.enableHeadphoneJackDetect();
codec.setHeadphoneJackDetectInput(WM8960_JACKDETECT_LINPUT3);
}

ledcSetup(lineOutChannel, toneFreq, pwmResolution);
ledcAttachPin(lineOutPin, lineOutChannel); ////// change this for real version - no line out currntly

ledcSetup(volChannel, volFreq, pwmResolution);
ledcAttachPin(HF_Pin, volChannel);

ledcWrite(toneChannel, 0);
ledcWrite(lineOutChannel, 0);
#endif
sidetone.begin(44100,16,2,128); // defaults to 44100, 16, 2, 32
sidetone.setFrequency(600.0);
#endif
}


void MorseOutput::pwmTone(unsigned int frequency, unsigned int volume, boolean lineOut) { // frequency in Hertz, volume in range 0 - 19; we use 10 bit resolution
#ifndef CONFIG_SOUND_I2S
const uint16_t vol[] = {0, 1, 2, 4, 6, 9, 14, 21, 31, 45, 70, 100, 140, 200, 280, 390, 512, 680, 840, 1023}; // 20 values
unsigned int i = constrain(volume, 0, 19);
unsigned int j = vol[i] >> 8; // experimental: soften the inital click
Expand Down Expand Up @@ -645,12 +720,16 @@ void MorseOutput::pwmTone(unsigned int frequency, unsigned int volume, boolean l
delay(3);
ledcWrite(volChannel, vol[i]);



#else
sidetone.setFrequency(frequency);
sidetone.setVolume(float(volume) / 19.0);
sidetone.on();
#endif
}


void MorseOutput::pwmNoTone(unsigned int volume) { // stop playing a tone by changing duty cycle of the tone to 0
#ifndef CONFIG_SOUND_I2S
const uint16_t vol[] = {0, 1, 2, 4, 6, 9, 14, 21, 31, 45, 70, 100, 140, 200, 280, 390, 512, 680, 840, 1023}; // 20 values
unsigned int i = constrain(volume, 0, 19);
unsigned int j = vol[i] >> 8; // experimental: soften the inital click
Expand All @@ -668,6 +747,9 @@ void MorseOutput::pwmNoTone(unsigned int volume) { // stop playing a tone b
ledcWrite(toneChannel, dutyCycleZero);
ledcWrite(lineOutChannel, dutyCycleZero);

#else
sidetone.off();
#endif
}


Expand All @@ -689,6 +771,9 @@ void MorseOutput::soundSignalOK() {
pwmTone(440, MorsePreferences::sidetoneVolume, false);
delay(97);
pwmNoTone(MorsePreferences::sidetoneVolume);
#ifdef CONFIG_SOUND_I2S
delay(20);
#endif
pwmTone(587, MorsePreferences::sidetoneVolume, false);
delay(193);
pwmNoTone(MorsePreferences::sidetoneVolume);
Expand Down
4 changes: 3 additions & 1 deletion Software/src/Version 6/m32_v6.ino
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,12 @@ void displayStartUp(uint16_t volt) {
MorseOutput::printOnScroll(1, REGULAR, 0, "© 2018-2024");

// uint16_t volt = batteryVoltage(); // has been measured early in setup()


#ifndef SKIP_BATTERY_PROTECT
if (volt > 1000 && volt < 2800)
MorseOutput::displayEmptyBattery(shutMeDown);
else
#endif
MorseOutput::displayBatteryStatus(volt);
//prepare board version, just in case we want to switch to M32protocol later on
if (MorsePreferences::boardVersion == 3)
Expand Down
144 changes: 139 additions & 5 deletions Software/src/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,153 @@

[platformio]
src_dir=Version 6
default_envs=heltec_wifi_lora_32_V2

[common]
lib_deps_external =
thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.6.1
bblanchon/[email protected]
madhephaestus/ESP32Encoder @ ^0.11.7

[env:heltec_wifi_lora_32_V2]
platform = espressif32
board = heltec_wifi_lora_32_V2
framework = arduino
monitor_speed = 115200
monitor_echo = yes
upload_speed = 921600
build_flags =
-D CORE_DEBUG_LEVEL=0
;-D LORA_DISABLED=1

lib_deps =
${common.lib_deps_external}
jgromes/RadioLib@^7.1.0

[env:heltec_wifi_kit_32_V3]
platform=espressif32
board=heltec_wifi_kit_32_V3
framework=arduino
monitor_speed = 115200
monitor_echo = yes
upload_speed = 921600

build_flags =
; -D CORE_DEBUG_LEVEL=0
-D INTERNAL_PULLUP=1
-D SKIP_BATTERY_PROTECT=1
-D LORA_DISABLED=1
-D OLED_SDA=17
-D OLED_SCL=18
-D OLED_RST=21
-D PIN_VEXT=36
-D PIN_ADC_CTRL=37
-D PIN_ROT_DT=33
-D PIN_ROT_CLK=47
-D PIN_ROT_BTN=34
-D PIN_PADDLE_LEFT=19
-D PIN_PADDLE_RIGHT=48
-D PIN_TOUCH_LEFT=4
-D PIN_TOUCH_RIGHT=5
-D PIN_BATTERY=1
-D PIN_KEYER=35
-D PIN_AUDIO_IN=-1
-D CONFIG_SOUND_I2S=1
-D CONFIG_I2S_BCK_PIN=41
-D CONFIG_I2S_LRCK_PIN=42
-D CONFIG_I2S_DATA_PIN=45
-D CONFIG_I2S_DATA_IN_PIN=46
-D CONFIG_WM8960=1
-D CONFIG_WM8960_SDA=7
-D CONFIG_WM8960_SCL=6

lib_deps =
${common.lib_deps_external}
https://github.com/haklein/cw-i2s-sidetone.git
WiFiClientSecure
sparkfun/SparkFun WM8960 Arduino Library@^1.0.4

[env:heltec_wifi_lora_32_V3]
platform=espressif32
board=heltec_wifi_lora_32_V3
framework=arduino
monitor_speed = 115200
monitor_echo = yes
upload_speed = 921600

build_flags =
; -D CORE_DEBUG_LEVEL=0
-D INTERNAL_PULLUP=1
-D SKIP_BATTERY_PROTECT=1
-D OLED_SDA=17
-D OLED_SCL=18
-D OLED_RST=21
-D PIN_VEXT=36
-D PIN_ADC_CTRL=37
-D PIN_ROT_DT=42
-D PIN_ROT_CLK=45
-D PIN_ROT_BTN=41
-D PIN_PADDLE_LEFT=46
-D PIN_PADDLE_RIGHT=34
-D PIN_TOUCH_LEFT=2
-D PIN_TOUCH_RIGHT=3
-D PIN_BATTERY=1
-D PIN_KEYER=40
-D PIN_KEYER_LED=35
-D PIN_AUDIO_IN=-1
-D CONFIG_SOUND_I2S=1
-D CONFIG_I2S_BCK_PIN=4
-D CONFIG_I2S_LRCK_PIN=5
-D CONFIG_I2S_DATA_PIN=6
-D CONFIG_I2S_DATA_IN_PIN=7
-D CONFIG_WM8960=1
-D CONFIG_WM8960_SDA=33
-D CONFIG_WM8960_SCL=47
-D LoRa_MOSI=10
-D LoRa_MISO=11
-D LoRa_SCK=9
-D LoRa_nss=8
-D LoRa_dio1=14
-D LoRa_nrst=12
-D LoRa_busy=13
-D RADIO_SX1262=1

lib_deps =
${common.lib_deps_external}
https://github.com/haklein/cw-i2s-sidetone.git
WiFiClientSecure
sparkfun/SparkFun WM8960 Arduino Library@^1.0.4
jgromes/RadioLib@^7.1.0

[env:vanilla]
platform=espressif32
board=heltec_wifi_lora_32_V3
framework=arduino
monitor_speed = 115200
monitor_echo = yes
upload_speed = 921600

build_flags =
; -D CORE_DEBUG_LEVEL=0
-D INTERNAL_PULLUP=1
-D SKIP_BATTERY_PROTECT=1
-D SKIP_LEGACY_PINDEFS=1
-D LORA_DISABLED=1
-D TOUCHPADDLES_DISABLED=1
-D OLED_SDA=18
-D OLED_SCL=17
-D PIN_ROT_DT=42
-D PIN_ROT_CLK=45
-D PIN_ROT_BTN=41
-D PIN_PADDLE_LEFT=38
-D PIN_PADDLE_RIGHT=39
-D PIN_KEYER=35
-D CONFIG_SOUND_I2S=1
-D CONFIG_I2S_BCK_PIN=6
-D CONFIG_I2S_LRCK_PIN=5
-D CONFIG_I2S_DATA_PIN=4
-D CONFIG_I2S_DATA_IN_PIN=7

lib_deps =
thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.6.1
bblanchon/[email protected]
madhephaestus/ESP32Encoder @ ^0.11.7
jgromes/RadioLib@^6.6.0
${common.lib_deps_external}
https://github.com/haklein/cw-i2s-sidetone.git
WiFiClientSecure

0 comments on commit bdf12a4

Please sign in to comment.