speed poti implemented

This commit is contained in:
Martin Brodbeck 2024-02-21 10:45:46 +01:00
parent a4e0e26076
commit 9eac552772
2 changed files with 58 additions and 15 deletions

View file

@ -58,6 +58,7 @@ target_link_libraries(raspi_keyer
hardware_pwm hardware_pwm
pico_flash pico_flash
pico_multicore pico_multicore
hardware_adc
tinyusb_device tinyusb_device
tinyusb_board tinyusb_board
hardware_pio hardware_pio

View file

@ -1,6 +1,8 @@
#include <cmath>
#include <stdio.h> #include <stdio.h>
#include "bsp/board.h" #include "bsp/board.h"
#include "hardware/adc.h"
#include "pico/multicore.h" #include "pico/multicore.h"
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "pico/util/queue.h" #include "pico/util/queue.h"
@ -21,6 +23,7 @@ extern const uint RIGHT_PADDLE_PIN = 15;
extern const uint BUZZER_PIN = 18; extern const uint BUZZER_PIN = 18;
extern const uint CW_OUT_PIN = 17; extern const uint CW_OUT_PIN = 17;
extern const uint AUDIO_OUT_PIN = 16; extern const uint AUDIO_OUT_PIN = 16;
extern const uint ADC_PIN = 26;
// Stuff for communicating between cores // Stuff for communicating between cores
enum class KeyerQueueCommand { enum class KeyerQueueCommand {
@ -54,11 +57,17 @@ void setup()
gpio_init(CW_OUT_PIN); gpio_init(CW_OUT_PIN);
gpio_set_dir(CW_OUT_PIN, GPIO_OUT); gpio_set_dir(CW_OUT_PIN, GPIO_OUT);
gpio_put(CW_OUT_PIN, 0); gpio_put(CW_OUT_PIN, 0);
// Setup ADC
adc_init();
gpio_init(ADC_PIN);
adc_select_input(0);
} }
/* Let's do all the keying stuff in the second core, so there are no timing problems. */ /* Let's do all the keying stuff in the second core, so there are no timing problems. */
void core1_main() void core1_main()
{ {
flash_safe_execute_core_init();
printf("Hello from core1!\n"); printf("Hello from core1!\n");
KeyerQueueData data; KeyerQueueData data;
queue_remove_blocking(&keyerQueue, &data); queue_remove_blocking(&keyerQueue, &data);
@ -89,15 +98,13 @@ void core1_main()
} }
} }
[[maybe_unused]] [[maybe_unused]] static void echo_serial_port(uint8_t itf, uint8_t buf[], uint32_t count)
static void echo_serial_port(uint8_t itf, uint8_t buf[], uint32_t count)
{ {
for(uint32_t i=0; i<count; i++) for (uint32_t i = 0; i < count; i++) {
{ tud_cdc_n_write_char(itf, buf[i]);
tud_cdc_n_write_char(itf, buf[i]); }
}
tud_cdc_n_write_flush(itf); tud_cdc_n_write_flush(itf);
} }
void cdc_task() void cdc_task()
@ -111,11 +118,11 @@ void cdc_task()
if (count > 1) { if (count > 1) {
printf("Command received …\n"); printf("Command received …\n");
switch (buf[0]) { switch (buf[0]) {
case 0: // ADMIN COMMAND case 0: // ADMIN COMMAND
switch (buf[1]) { switch (buf[1]) {
case 2: // Host Open case 2: // Host Open
// TODO: send back revision code // TODO: send back revision code
default: default:
printf("Unknown admin command.\n"); printf("Unknown admin command.\n");
} }
break; break;
@ -126,9 +133,27 @@ void cdc_task()
} }
} }
/* Returns the voltage level in percent (3,3V == 100%) */
float potiRead()
{
// 12-bit conversion, assume max value == ADC_VREF == 3.3 V
const float conversion_factor = 3.3f / (1 << 12);
float voltage = adc_read() * conversion_factor;
return voltage * 100 / 3.3;
}
/* Calculates the WPM speed from the volt percentage */
uint8_t calcWPM(float percent, uint8_t wpmMin, uint8_t wpmMax)
{
auto wpm = (percent * (wpmMax - wpmMin) / 100) + wpmMin;
uint8_t result = static_cast<uint8_t>(std::round(wpm));
return result;
}
int main() int main()
{ {
// timer_hw->dbgpause = 2; // workaround for problem with debug and sleep_ms timer_hw->dbgpause = 0; // workaround for problem with debug and sleep_ms
// https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639 // https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639
setup(); setup();
@ -137,19 +162,36 @@ int main()
printf("Hello from core0!\n"); printf("Hello from core0!\n");
Settings settings {read_settings()}; //Settings settings {read_settings()};
settings.wpm = 25; // TODO: remove! Settings settings;
uint8_t currentWpm, lastWpm {0};
if (settings.wpm == 0) {
currentWpm = calcWPM(potiRead(), settings.wpmPotiMin, settings.wpmPotiMax);
} else {
currentWpm = settings.wpm;
}
lastWpm = currentWpm;
queue_init(&keyerQueue, sizeof(KeyerQueueData), 2); queue_init(&keyerQueue, sizeof(KeyerQueueData), 2);
multicore_reset_core1(); multicore_reset_core1();
multicore_launch_core1(core1_main); multicore_launch_core1(core1_main);
KeyerQueueData keyerQueueData {KeyerQueueCommand::Run, settings.wpm, settings.mode}; KeyerQueueData keyerQueueData {KeyerQueueCommand::Run, currentWpm, settings.mode};
queue_add_blocking(&keyerQueue, &keyerQueueData); queue_add_blocking(&keyerQueue, &keyerQueueData);
while (true) { while (true) {
tud_task(); tud_task();
cdc_task(); cdc_task();
currentWpm = calcWPM(potiRead(), settings.wpmPotiMin, settings.wpmPotiMax);
if (currentWpm != lastWpm) {
KeyerQueueData keyerQueueData {KeyerQueueCommand::Config, currentWpm, settings.mode};
queue_add_blocking(&keyerQueue, &keyerQueueData);
printf("WPM has changed to: %d\n", currentWpm);
lastWpm = currentWpm;
}
busy_wait_ms(1000);
} }
return 0; return 0;