code beautifying

This commit is contained in:
Martin Brodbeck 2024-02-16 20:56:03 +01:00
parent 9a07c24d8f
commit 08885a3aac
8 changed files with 292 additions and 113 deletions

View file

@ -13,8 +13,7 @@ const uint SIDETONE_FREQ = 622;
bool left_paddle_pressed()
{
if (!gpio_get(LEFT_PADDLE_PIN))
{
if (!gpio_get(LEFT_PADDLE_PIN)) {
return true;
}
return false;
@ -22,8 +21,7 @@ bool left_paddle_pressed()
bool right_paddle_pressed()
{
if (!gpio_get(RIGHT_PADDLE_PIN))
{
if (!gpio_get(RIGHT_PADDLE_PIN)) {
return true;
}
return false;
@ -35,10 +33,7 @@ uint64_t calcElementDurationUs(uint8_t wpm)
return duration;
}
Keyer::Keyer(uint8_t wpm, Mode mode) : m_wpm(wpm), m_mode(mode)
{
m_elementDuration = calcElementDurationUs(m_wpm);
}
Keyer::Keyer(uint8_t wpm, Mode mode) : m_wpm(wpm), m_mode(mode) { m_elementDuration = calcElementDurationUs(m_wpm); }
void Keyer::setSpeed(uint8_t wpm)
{
@ -50,23 +45,16 @@ void Keyer::run()
{
auto timestamp = get_absolute_time();
switch (m_state)
{
switch (m_state) {
case State::Wait:
if (left_paddle_pressed())
{
if (left_paddle_pressed()) {
m_keyNextIambicB = false;
m_state = State::Dit;
}
else if (right_paddle_pressed())
{
} else if (right_paddle_pressed()) {
m_keyNextIambicB = false;
m_state = State::Dah;
}
else
{
if (m_mode == Mode::IAMBIC_B && m_keyNextIambicB)
{
} else {
if (m_mode == Mode::IAMBIC_B && m_keyNextIambicB) {
if (m_previousState == State::Dit)
m_state = State::Dah;
else
@ -77,24 +65,19 @@ void Keyer::run()
}
break;
case State::Dit:
if (!m_currentlyKeying)
{
if (!m_currentlyKeying) {
m_currentlyKeying = true;
m_keying_until = make_timeout_time_us(m_elementDuration);
gpio_put(LED_PIN, 1);
m_Sidetone.on(SIDETONE_FREQ);
}
else
{
} else {
// If right paddle üressed -> note for Iambic B
if (right_paddle_pressed() && !m_keyNextIambicB)
{
if (right_paddle_pressed() && !m_keyNextIambicB) {
printf("Iambic B -> lang\n");
m_keyNextIambicB = true;
}
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
{
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0) {
m_currentlyKeying = false;
gpio_put(LED_PIN, 0);
m_Sidetone.off();
@ -104,24 +87,19 @@ void Keyer::run()
}
break;
case State::Dah:
if (!m_currentlyKeying)
{
if (!m_currentlyKeying) {
m_currentlyKeying = true;
m_keying_until = make_timeout_time_us(m_elementDuration * 3);
gpio_put(LED_PIN, 1);
m_Sidetone.on(SIDETONE_FREQ);
}
else
{
} else {
// If left paddle pressed -> Note for Iambic B
if (left_paddle_pressed() && !m_keyNextIambicB)
{
if (left_paddle_pressed() && !m_keyNextIambicB) {
printf("Iambic B -> kurz\n");
m_keyNextIambicB = true;
}
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0)
{
if (absolute_time_diff_us(timestamp, m_keying_until) <= 0) {
m_currentlyKeying = false;
gpio_put(LED_PIN, 0);
m_Sidetone.off();
@ -131,58 +109,40 @@ void Keyer::run()
}
break;
case State::DitPause:
if (!m_currentlyPausing)
{
if (!m_currentlyPausing) {
m_pausing_until = make_timeout_time_us(m_elementDuration);
m_currentlyPausing = true;
}
else
{
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0)
{
} else {
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0) {
m_currentlyPausing = false;
if (right_paddle_pressed())
{
if (right_paddle_pressed()) {
m_state = State::Dah;
m_keyNextIambicB = false;
}
else if (left_paddle_pressed())
{
} else if (left_paddle_pressed()) {
m_state = State::Dit;
m_keyNextIambicB = false;
}
else
{
} else {
m_state = State::Wait;
}
}
}
break;
case State::DahPause:
if (!m_currentlyPausing)
{
if (!m_currentlyPausing) {
m_pausing_until = make_timeout_time_us(m_elementDuration);
m_currentlyPausing = true;
}
else
{
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0)
{
} else {
if (absolute_time_diff_us(timestamp, m_pausing_until) <= 0) {
m_currentlyPausing = false;
if (left_paddle_pressed())
{
if (left_paddle_pressed()) {
m_state = State::Dit;
m_keyNextIambicB = false;
}
else if (right_paddle_pressed())
{
} else if (right_paddle_pressed()) {
m_state = State::Dah;
m_keyNextIambicB = false;
}
else
{
} else {
m_state = State::Wait;
}
}
@ -202,7 +162,4 @@ void Keyer::run()
}
}
void Keyer::stop()
{
m_state = State::Abort;
}
void Keyer::stop() { m_state = State::Abort; }

View file

@ -1,14 +1,14 @@
#ifndef KEYER_H
#define KEYER_H
#include "sidetone.h"
#include "settings.h"
#include "sidetone.h"
extern const uint BUZZER_PIN;
class Keyer final
{
public:
public:
Keyer() = delete;
Keyer(uint8_t wpm, Mode mode);
@ -18,9 +18,8 @@ public:
void run();
void stop();
private:
enum class State
{
private:
enum class State {
Wait,
Dit,
Dah,

View file

@ -1,11 +1,11 @@
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/stdlib.h"
#include "pico/util/queue.h"
#include "settings.h"
#include "keyer.h"
#include "settings.h"
namespace
{
@ -18,15 +18,13 @@ extern const uint RIGHT_PADDLE_PIN = 15;
extern const uint BUZZER_PIN = 18;
// Stuff for communicating between cores
enum class KeyerQueueCommand
{
enum class KeyerQueueCommand {
Run,
Stop,
Config,
Wait,
};
struct KeyerQueueData
{
struct KeyerQueueData {
KeyerQueueCommand cmd;
uint8_t wpm;
Mode mode;
@ -49,7 +47,7 @@ void setup()
gpio_set_dir(RIGHT_PADDLE_PIN, GPIO_IN);
gpio_pull_up(RIGHT_PADDLE_PIN);
//sleep_ms(1000);
// sleep_ms(1000);
}
/* Let's do all the keying stuff in the second core, so there are no timing problems. */
@ -61,12 +59,10 @@ void core1_main()
Keyer keyer(data.wpm, data.mode);
while (true)
{
while (true) {
queue_try_remove(&keyerQueue, &data);
switch (data.cmd)
{
switch (data.cmd) {
case KeyerQueueCommand::Run:
keyer.run();
break;
@ -89,8 +85,8 @@ void core1_main()
int main()
{
//timer_hw->dbgpause = 2; // workaround for problem with debug and sleep_ms
// https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639
// timer_hw->dbgpause = 2; // workaround for problem with debug and sleep_ms
// https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639
setup();
@ -106,8 +102,7 @@ int main()
KeyerQueueData keyerQueueData{KeyerQueueCommand::Run, settings.wpm, settings.mode};
queue_add_blocking(&keyerQueue, &keyerQueueData);
while (true)
{
while (true) {
// Currently there's nothing to do on core0
sleep_ms(1000);
}

View file

@ -6,14 +6,14 @@
namespace
{
constexpr const uint32_t FLASH_TARGET_OFFSET = PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE;
const uint8_t *flash_target_contents = (const uint8_t *)(XIP_BASE + FLASH_TARGET_OFFSET);
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
}
constexpr const uint32_t FLASH_TARGET_OFFSET = PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE;
const uint8_t *flash_target_contents = (const uint8_t *)(XIP_BASE + FLASH_TARGET_OFFSET);
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
} // namespace
void flash_store_callback(void *settings)
{
//int writeSize = (sizeof(struct Settings) / FLASH_PAGE_SIZE) + 1;
// int writeSize = (sizeof(struct Settings) / FLASH_PAGE_SIZE) + 1;
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, (uint8_t *)settings, FLASH_PAGE_SIZE);
@ -24,8 +24,7 @@ void store_settings(Settings &settings)
uint8_t *settingsAsBytes = (uint8_t *)&settings;
int result = flash_safe_execute(flash_store_callback, settingsAsBytes, 1000);
if (result == PICO_OK)
{
if (result == PICO_OK) {
gpio_put(LED_PIN, 1);
sleep_ms(250);
gpio_put(LED_PIN, 0);
@ -47,8 +46,7 @@ Settings read_settings()
memcpy(&settings, flash_target_contents, sizeof(struct Settings));
if (settings.magic_number != MAGIC_NUMBER)
{
if (settings.magic_number != MAGIC_NUMBER) {
settings = Settings();
gpio_put(LED_PIN, 1);

View file

@ -5,15 +5,12 @@
const uint16_t MAGIC_NUMBER = 2;
enum class Mode : uint8_t
{
enum class Mode : uint8_t {
IAMBIC_A = 0,
IAMBIC_B,
// ULTIMATE
};
struct Settings
{
struct Settings {
uint16_t magic_number{MAGIC_NUMBER}; // Bytes: 2
Mode mode{Mode::IAMBIC_B}; // Bytes: 1
uint8_t wpm{18}; // Bytes: 1

View file

@ -1,5 +1,5 @@
#include "pico/stdlib.h"
#include "hardware/pwm.h"
#include "pico/stdlib.h"
#include "sidetone.h"
@ -23,7 +23,4 @@ void Sidetone::on(uint freq)
pwm_set_gpio_level(m_gpio, 32768U);
}
void Sidetone::off()
{
pwm_set_gpio_level(m_gpio, 0);
}
void Sidetone::off() { pwm_set_gpio_level(m_gpio, 0); }

View file

@ -3,12 +3,12 @@
class Sidetone
{
public:
public:
Sidetone(uint gpio);
void on(uint freq);
void off();
private:
private:
Sidetone(){};
uint m_gpio{0};
};