2024-02-10 23:51:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "pico/stdlib.h"
|
|
|
|
|
2024-02-09 12:20:30 +01:00
|
|
|
#include "keyer.h"
|
2024-02-12 15:57:50 +01:00
|
|
|
#include "sidetone.h"
|
2024-02-09 12:20:30 +01:00
|
|
|
|
2024-02-12 13:48:58 +01:00
|
|
|
extern const uint LED_PIN;
|
2024-02-10 23:51:55 +01:00
|
|
|
extern const uint LEFT_PADDLE_PIN;
|
2024-02-12 13:48:58 +01:00
|
|
|
extern const uint RIGHT_PADDLE_PIN;
|
2024-02-10 23:51:55 +01:00
|
|
|
|
2024-02-12 15:57:50 +01:00
|
|
|
const uint SIDETONE_FREQ = 622;
|
|
|
|
|
2024-02-10 23:51:55 +01:00
|
|
|
bool left_paddle_pressed()
|
|
|
|
{
|
2024-02-11 15:45:12 +01:00
|
|
|
if (!gpio_get(LEFT_PADDLE_PIN))
|
2024-02-10 23:51:55 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-12 13:48:58 +01:00
|
|
|
bool right_paddle_pressed()
|
|
|
|
{
|
|
|
|
if (!gpio_get(RIGHT_PADDLE_PIN))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t calcElementDurationUs(uint8_t wpm)
|
|
|
|
{
|
|
|
|
uint64_t duration = static_cast<uint64_t>(1.2 / static_cast<uint64_t>(wpm) * 1000 * 1000);
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
2024-02-13 09:00:39 +01:00
|
|
|
Keyer::Keyer(uint8_t wpm, Mode mode) : m_wpm(wpm), m_mode(mode)
|
2024-02-12 13:48:58 +01:00
|
|
|
{
|
|
|
|
m_elementDuration = calcElementDurationUs(m_wpm);
|
|
|
|
}
|
|
|
|
|
2024-02-14 11:05:33 +01:00
|
|
|
void Keyer::setSpeed(uint8_t wpm)
|
|
|
|
{
|
|
|
|
m_wpm = wpm;
|
|
|
|
m_elementDuration = calcElementDurationUs(wpm);
|
|
|
|
}
|
|
|
|
|
2024-02-09 12:20:30 +01:00
|
|
|
void Keyer::run()
|
|
|
|
{
|
2024-02-12 13:48:58 +01:00
|
|
|
auto timestamp = get_absolute_time();
|
|
|
|
|
2024-02-16 09:46:46 +01:00
|
|
|
switch (m_state)
|
2024-02-09 12:20:30 +01:00
|
|
|
{
|
2024-02-10 23:51:55 +01:00
|
|
|
case State::Wait:
|
2024-02-16 09:46:46 +01:00
|
|
|
if (left_paddle_pressed())
|
2024-02-12 14:10:04 +01:00
|
|
|
{
|
2024-02-13 10:46:11 +01:00
|
|
|
m_keyNextIambicB = false;
|
2024-02-16 09:46:46 +01:00
|
|
|
m_state = State::Dit;
|
2024-02-12 14:10:04 +01:00
|
|
|
}
|
|
|
|
else if (right_paddle_pressed())
|
|
|
|
{
|
2024-02-13 10:46:11 +01:00
|
|
|
m_keyNextIambicB = false;
|
2024-02-16 09:46:46 +01:00
|
|
|
m_state = State::Dah;
|
2024-02-12 13:48:58 +01:00
|
|
|
}
|
2024-02-13 10:46:11 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_mode == Mode::IAMBIC_B && m_keyNextIambicB)
|
|
|
|
{
|
|
|
|
if (m_previousState == State::Dit)
|
2024-02-16 09:46:46 +01:00
|
|
|
m_state = State::Dah;
|
2024-02-13 10:46:11 +01:00
|
|
|
else
|
2024-02-16 09:46:46 +01:00
|
|
|
m_state = State::Dit;
|
2024-02-13 10:46:11 +01:00
|
|
|
|
|
|
|
m_keyNextIambicB = false;
|
|
|
|
}
|
|
|
|
}
|
2024-02-10 23:51:55 +01:00
|
|
|
break;
|
2024-02-11 16:26:55 +01:00
|
|
|
case State::Dit:
|
2024-02-12 13:48:58 +01:00
|
|
|
if (!m_currentlyKeying)
|
|
|
|
{
|
|
|
|
m_currentlyKeying = true;
|
|
|
|
m_keying_until = make_timeout_time_us(m_elementDuration);
|
|
|
|
gpio_put(LED_PIN, 1);
|
2024-02-12 15:57:50 +01:00
|
|
|
m_Sidetone.on(SIDETONE_FREQ);
|
2024-02-12 13:48:58 +01:00
|
|
|
}
|
2024-02-11 15:45:12 +01:00
|
|
|
else
|
2024-02-11 16:26:55 +01:00
|
|
|
{
|
2024-02-16 20:05:03 +01:00
|
|
|
// If right paddle üressed -> note for Iambic B
|
|
|
|
if (right_paddle_pressed() && !m_keyNextIambicB)
|
2024-02-16 09:46:46 +01:00
|
|
|
{
|
2024-02-16 20:05:03 +01:00
|
|
|
printf("Iambic B -> lang\n");
|
2024-02-13 09:00:39 +01:00
|
|
|
m_keyNextIambicB = true;
|
2024-02-16 09:46:46 +01:00
|
|
|
}
|
2024-02-13 09:00:39 +01:00
|
|
|
|
2024-02-12 13:48:58 +01:00
|
|
|
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
|
|
|
|
{
|
|
|
|
m_currentlyKeying = false;
|
|
|
|
gpio_put(LED_PIN, 0);
|
2024-02-12 15:57:50 +01:00
|
|
|
m_Sidetone.off();
|
2024-02-12 13:48:58 +01:00
|
|
|
m_previousState = State::Dit;
|
2024-02-16 09:46:46 +01:00
|
|
|
m_state = State::DitPause;
|
2024-02-12 13:48:58 +01:00
|
|
|
}
|
2024-02-11 16:26:55 +01:00
|
|
|
}
|
|
|
|
break;
|
2024-02-12 13:48:58 +01:00
|
|
|
case State::Dah:
|
2024-02-12 14:10:04 +01:00
|
|
|
if (!m_currentlyKeying)
|
|
|
|
{
|
2024-02-12 13:48:58 +01:00
|
|
|
m_currentlyKeying = true;
|
|
|
|
m_keying_until = make_timeout_time_us(m_elementDuration * 3);
|
|
|
|
gpio_put(LED_PIN, 1);
|
2024-02-12 15:57:50 +01:00
|
|
|
m_Sidetone.on(SIDETONE_FREQ);
|
2024-02-12 14:10:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-16 20:05:03 +01:00
|
|
|
// If left paddle pressed -> Note for Iambic B
|
|
|
|
if (left_paddle_pressed() && !m_keyNextIambicB)
|
2024-02-16 09:46:46 +01:00
|
|
|
{
|
2024-02-16 20:05:03 +01:00
|
|
|
printf("Iambic B -> kurz\n");
|
2024-02-13 09:00:39 +01:00
|
|
|
m_keyNextIambicB = true;
|
2024-02-16 09:46:46 +01:00
|
|
|
}
|
2024-02-13 09:00:39 +01:00
|
|
|
|
2024-02-12 13:48:58 +01:00
|
|
|
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
|
|
|
|
{
|
|
|
|
m_currentlyKeying = false;
|
|
|
|
gpio_put(LED_PIN, 0);
|
2024-02-12 15:57:50 +01:00
|
|
|
m_Sidetone.off();
|
2024-02-12 13:48:58 +01:00
|
|
|
m_previousState = State::Dah;
|
2024-02-16 09:46:46 +01:00
|
|
|
m_state = State::DahPause;
|
2024-02-12 13:48:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2024-02-16 09:46:46 +01:00
|
|
|
case State::DitPause:
|
2024-02-13 09:00:39 +01:00
|
|
|
if (!m_currentlyPausing)
|
2024-02-12 13:48:58 +01:00
|
|
|
{
|
|
|
|
m_pausing_until = make_timeout_time_us(m_elementDuration);
|
2024-02-13 09:00:39 +01:00
|
|
|
m_currentlyPausing = true;
|
2024-02-12 13:48:58 +01:00
|
|
|
}
|
2024-02-16 09:46:46 +01:00
|
|
|
else
|
2024-02-15 10:03:29 +01:00
|
|
|
{
|
2024-02-16 09:46:46 +01:00
|
|
|
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0)
|
2024-02-15 10:03:29 +01:00
|
|
|
{
|
2024-02-16 09:46:46 +01:00
|
|
|
m_currentlyPausing = false;
|
2024-02-15 10:03:29 +01:00
|
|
|
|
2024-02-16 09:46:46 +01:00
|
|
|
if (right_paddle_pressed())
|
|
|
|
{
|
|
|
|
m_state = State::Dah;
|
|
|
|
m_keyNextIambicB = false;
|
|
|
|
}
|
|
|
|
else if (left_paddle_pressed())
|
|
|
|
{
|
|
|
|
m_state = State::Dit;
|
|
|
|
m_keyNextIambicB = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_state = State::Wait;
|
|
|
|
}
|
2024-02-15 10:03:29 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-16 09:46:46 +01:00
|
|
|
break;
|
|
|
|
case State::DahPause:
|
|
|
|
if (!m_currentlyPausing)
|
|
|
|
{
|
|
|
|
m_pausing_until = make_timeout_time_us(m_elementDuration);
|
|
|
|
m_currentlyPausing = true;
|
|
|
|
}
|
2024-02-15 10:03:29 +01:00
|
|
|
else
|
2024-02-12 14:10:04 +01:00
|
|
|
{
|
2024-02-15 10:03:29 +01:00
|
|
|
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0)
|
|
|
|
{
|
|
|
|
m_currentlyPausing = false;
|
2024-02-16 09:46:46 +01:00
|
|
|
|
|
|
|
if (left_paddle_pressed())
|
|
|
|
{
|
|
|
|
m_state = State::Dit;
|
|
|
|
m_keyNextIambicB = false;
|
|
|
|
}
|
|
|
|
else if (right_paddle_pressed())
|
|
|
|
{
|
|
|
|
m_state = State::Dah;
|
|
|
|
m_keyNextIambicB = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_state = State::Wait;
|
|
|
|
}
|
2024-02-15 10:03:29 +01:00
|
|
|
}
|
2024-02-12 13:48:58 +01:00
|
|
|
}
|
2024-02-09 12:20:30 +01:00
|
|
|
break;
|
2024-02-14 11:32:09 +01:00
|
|
|
case State::Abort:
|
|
|
|
gpio_put(LED_PIN, 0);
|
|
|
|
m_Sidetone.off();
|
|
|
|
m_keyNextIambicB = false;
|
|
|
|
m_currentlyPausing = false;
|
|
|
|
m_currentlyKeying = false;
|
|
|
|
m_previousState = State::Abort;
|
2024-02-16 09:46:46 +01:00
|
|
|
m_state = State::Wait;
|
2024-02-14 11:32:09 +01:00
|
|
|
break;
|
2024-02-09 12:20:30 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2024-02-14 11:32:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Keyer::stop()
|
|
|
|
{
|
2024-02-16 09:46:46 +01:00
|
|
|
m_state = State::Abort;
|
2024-02-09 12:20:30 +01:00
|
|
|
}
|