152 lines
4.1 KiB
C++
152 lines
4.1 KiB
C++
#include <cmath>
|
|
#include <stdio.h>
|
|
|
|
#include "bsp/board.h"
|
|
#include "hardware/adc.h"
|
|
#include "pico/binary_info/code.h"
|
|
#include "pico/multicore.h"
|
|
#include "pico/stdlib.h"
|
|
#include "pico/util/queue.h"
|
|
#include "tusb.h"
|
|
|
|
#include "keyer.h"
|
|
#include "settings.h"
|
|
#include "tm1637.h"
|
|
#include "tusb_config.h"
|
|
#include "utils.h"
|
|
#include "winkeyer.h"
|
|
#include <config.h>
|
|
|
|
extern constexpr uint LED_PIN = PICO_DEFAULT_LED_PIN;
|
|
extern constexpr uint DIT_PADDLE_PIN = 14;
|
|
extern constexpr uint DAH_PADDLE_PIN = 15;
|
|
// extern const uint AUDIO_OUT_PIN = 16;
|
|
extern constexpr uint CW_OUT_PIN = 17;
|
|
extern constexpr uint BUZZER_PIN = 18;
|
|
constexpr uint DISPLAY_DIO_PIN = 20;
|
|
constexpr uint DISPLAY_CLK_PIN = 21;
|
|
extern constexpr uint ADC_PIN = 26;
|
|
|
|
queue_t keyerQueue;
|
|
|
|
void setup()
|
|
{
|
|
stdio_init_all();
|
|
gpio_init(LED_PIN);
|
|
gpio_set_dir(LED_PIN, GPIO_OUT);
|
|
gpio_put(LED_PIN, 0);
|
|
|
|
// Setup pins for left and right paddles
|
|
gpio_init(DIT_PADDLE_PIN);
|
|
gpio_set_dir(DIT_PADDLE_PIN, GPIO_IN);
|
|
gpio_pull_up(DIT_PADDLE_PIN);
|
|
gpio_init(DAH_PADDLE_PIN);
|
|
gpio_set_dir(DAH_PADDLE_PIN, GPIO_IN);
|
|
gpio_pull_up(DAH_PADDLE_PIN);
|
|
gpio_init(CW_OUT_PIN);
|
|
gpio_set_dir(CW_OUT_PIN, GPIO_OUT);
|
|
gpio_put(CW_OUT_PIN, 0);
|
|
|
|
// Setup ADC
|
|
adc_init();
|
|
gpio_init(ADC_PIN);
|
|
adc_select_input(0);
|
|
|
|
// Setup USB
|
|
board_init();
|
|
tud_init(BOARD_TUD_RHPORT);
|
|
}
|
|
|
|
/* Let's do all the keying stuff in the second core, so there are no timing problems. */
|
|
void core1_main()
|
|
{
|
|
flash_safe_execute_core_init();
|
|
|
|
KeyerQueueData data;
|
|
queue_remove_blocking(&keyerQueue, &data);
|
|
|
|
Keyer keyer(data.wpm, data.mode);
|
|
|
|
while (true) {
|
|
queue_try_remove(&keyerQueue, &data);
|
|
|
|
switch (data.cmd) {
|
|
case KeyerQueueCommand::Run:
|
|
keyer.run();
|
|
break;
|
|
case KeyerQueueCommand::Stop:
|
|
keyer.stop();
|
|
data.cmd = KeyerQueueCommand::Run;
|
|
break;
|
|
case KeyerQueueCommand::Config:
|
|
keyer.setSpeed(data.wpm);
|
|
keyer.setMode(data.mode);
|
|
data.cmd = KeyerQueueCommand::Run;
|
|
break;
|
|
case KeyerQueueCommand::SendMessage:
|
|
keyer.sendCharacter(data.message);
|
|
data.cmd = KeyerQueueCommand::Run;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
timer_hw->dbgpause = 0; // workaround for problem with debug and sleep_ms
|
|
// https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639
|
|
|
|
setup();
|
|
|
|
printf("RaspiKeyer Version %s\n", PROJECT_VERSION);
|
|
|
|
queue_init(&keyerQueue, sizeof(KeyerQueueData), 2);
|
|
multicore_reset_core1();
|
|
multicore_launch_core1(core1_main);
|
|
|
|
Settings settings {read_settings()};
|
|
|
|
uint8_t currentWpm {0};
|
|
uint8_t lastWpm {0};
|
|
|
|
// If WPM in settings is set to 0 -> take speed from poti
|
|
if (settings.wpm == 0) {
|
|
currentWpm = calcWPM(potiRead(), settings.wpmPotiMin, settings.wpmPotiMax);
|
|
} else {
|
|
currentWpm = settings.wpm;
|
|
}
|
|
lastWpm = currentWpm;
|
|
|
|
KeyerQueueData keyerQueueData {KeyerQueueCommand::Run, currentWpm, settings.mode, 0};
|
|
queue_add_blocking(&keyerQueue, &keyerQueueData);
|
|
|
|
WinKeyer winKeyer;
|
|
|
|
pio_hw_t *pio = pio0;
|
|
TM1637 display {DISPLAY_DIO_PIN, DISPLAY_CLK_PIN, pio};
|
|
bi_decl(bi_1pin_with_name(DISPLAY_DIO_PIN, "[DIO] LED segments data pin"));
|
|
bi_decl(bi_1pin_with_name(DISPLAY_CLK_PIN, "[CLK] LED segments clock pin"));
|
|
display.displayIambicMode(settings.mode);
|
|
display.displaySpeed(currentWpm);
|
|
|
|
while (true) {
|
|
currentWpm = calcWPM(potiRead(), settings.wpmPotiMin, settings.wpmPotiMax);
|
|
|
|
// If WPM in settings is set to 0 -> take speed from poti
|
|
if (settings.wpm == 0 && (currentWpm != lastWpm)) {
|
|
KeyerQueueData keyerQueueData {KeyerQueueCommand::Config, currentWpm, settings.mode, 0};
|
|
queue_add_blocking(&keyerQueue, &keyerQueueData);
|
|
printf("WPM has changed to: %d\n", currentWpm);
|
|
lastWpm = currentWpm;
|
|
display.displaySpeed(currentWpm);
|
|
}
|
|
|
|
tud_task(); // Internal PICO purposes
|
|
|
|
winKeyer.run(keyerQueue);
|
|
}
|
|
|
|
return 0;
|
|
}
|