more experiments

This commit is contained in:
Martin Brodbeck 2024-02-12 13:48:58 +01:00
parent e6c02baf95
commit 59c348a2e4
3 changed files with 89 additions and 43 deletions

View file

@ -4,7 +4,9 @@
#include "keyer.h" #include "keyer.h"
extern const uint LED_PIN;
extern const uint LEFT_PADDLE_PIN; extern const uint LEFT_PADDLE_PIN;
extern const uint RIGHT_PADDLE_PIN;
bool left_paddle_pressed() bool left_paddle_pressed()
{ {
@ -15,42 +17,84 @@ bool left_paddle_pressed()
return false; return false;
} }
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;
}
Keyer::Keyer(uint8_t wpm) : m_wpm(wpm)
{
m_elementDuration = calcElementDurationUs(m_wpm);
}
void Keyer::run() void Keyer::run()
{ {
auto timestamp = get_absolute_time();
switch (state) switch (state)
{ {
case State::Wait: case State::Wait:
if (left_paddle_pressed()) if (left_paddle_pressed()) {
state = State::Dit; state = State::Dit;
break; } else if (right_paddle_pressed()) {
case State::Dit: state = State::Dah;
printf(".");
if (left_paddle_pressed())
state = State::InterCharSpace;
else
state = State::Wait;
break;
case State::Dah:
printf("-");
++numElements;
if (numElements == 3)
{
numElements = 0;
if (left_paddle_pressed())
state = State::InterCharSpace;
else
state = State::Wait;
} }
break; break;
case State::Dit:
case State::InterCharSpace: if (!m_currentlyKeying)
printf("(.)"); {
m_currentlyKeying = true;
if (left_paddle_pressed()) m_keying_until = make_timeout_time_us(m_elementDuration);
state = State::Dit; gpio_put(LED_PIN, 1);
}
else else
{
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
{
m_currentlyKeying = false;
gpio_put(LED_PIN, 0);
m_previousState = State::Dit;
state = State::InterCharSpace;
}
}
break;
case State::Dah:
if(!m_currentlyKeying) {
m_currentlyKeying = true;
m_keying_until = make_timeout_time_us(m_elementDuration * 3);
gpio_put(LED_PIN, 1);
} else {
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
{
m_currentlyKeying = false;
gpio_put(LED_PIN, 0);
m_previousState = State::Dah;
state = State::InterCharSpace;
}
}
break;
case State::InterCharSpace:
if (m_previousState != State::InterCharSpace)
{
m_pausing_until = make_timeout_time_us(m_elementDuration);
m_previousState = State::InterCharSpace;
}
if(absolute_time_diff_us(timestamp, m_pausing_until) <= 0) {
state = State::Wait; state = State::Wait;
}
break; break;
default: default:

View file

@ -12,18 +12,20 @@ public:
InterCharSpace, InterCharSpace,
}; };
Keyer(uint8_t wpm);
void run(); void run();
private: private:
Keyer() {};
State state{State::Wait}; State state{State::Wait};
uint8_t numElements{0}; State m_previousState{State::Wait};
uint8_t m_wpm{18};
uint64_t m_elementDuration{0};
bool m_currentlyKeying {false};
absolute_time_t m_keying_until{0};
absolute_time_t m_pausing_until{0};
}; };
inline uint64_t element_duration_us(uint8_t wpm)
{
uint64_t duration = static_cast<uint64_t>(1.2 / static_cast<uint64_t>(wpm) * 1000 * 1000);
return duration;
}
#endif #endif

View file

@ -8,15 +8,16 @@
namespace namespace
{ {
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
const uint RIGHT_PADDLE_PIN = 15;
} }
extern const uint LED_PIN = PICO_DEFAULT_LED_PIN;
extern const uint LEFT_PADDLE_PIN = 14; extern const uint LEFT_PADDLE_PIN = 14;
extern const uint RIGHT_PADDLE_PIN = 15;
void setup() void setup()
{ {
stdio_init_all();
gpio_init(LED_PIN); gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT); gpio_set_dir(LED_PIN, GPIO_OUT);
gpio_put(LED_PIN, 0); gpio_put(LED_PIN, 0);
@ -28,27 +29,26 @@ void setup()
gpio_init(RIGHT_PADDLE_PIN); gpio_init(RIGHT_PADDLE_PIN);
gpio_set_dir(RIGHT_PADDLE_PIN, GPIO_IN); gpio_set_dir(RIGHT_PADDLE_PIN, GPIO_IN);
gpio_pull_up(RIGHT_PADDLE_PIN); gpio_pull_up(RIGHT_PADDLE_PIN);
sleep_ms(1000);
} }
int main() int main()
{ {
stdio_init_all();
timer_hw->dbgpause = 0; // fix problem with debug and sleep_ms https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639 timer_hw->dbgpause = 0; // fix problem with debug and sleep_ms https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639
sleep_ms(1000);
setup(); setup();
Settings settings{read_settings()}; Settings settings{read_settings()};
printf("\nIambic mode (loaded): %d\n", static_cast<int>(settings.mode)); // printf("\nIambic mode (loaded): %d\n", static_cast<int>(settings.mode));
printf("WPM (loaded): %d\n", settings.wpm); // printf("WPM (loaded): %d\n", settings.wpm);
printf("Element duration (u_sec): %" PRIu64 "\n", element_duration_us(settings.wpm)); // printf("Element duration (u_sec): %" PRIu64 "\n", element_duration_us(settings.wpm));
printf("\n");
Keyer keyer; Keyer keyer(settings.wpm);
while (true) while (true)
{ {
sleep_us(element_duration_us(settings.wpm));
keyer.run(); keyer.run();
} }