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

236
.clang-format Normal file
View file

@ -0,0 +1,236 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: true
AlignConsecutiveBitFields:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveDeclarations:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveMacros:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveShortCaseStatements:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCaseColons: false
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments:
Kind: Always
OverEmptyLines: 0
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterExternBlock: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterAttributes: Never
BreakAfterJavaFieldAnnotations: false
BreakArrays: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Linux
BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: AfterExternBlock
IndentGotoLabels: true
IndentPPDirectives: None
IndentRequiresClause: true
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertBraces: false
InsertNewlineAtEOF: false
InsertTrailingCommas: None
IntegerLiteralSeparator:
Binary: 0
BinaryMinDigits: 0
Decimal: 0
DecimalMinDigits: 0
Hex: 0
HexMinDigits: 0
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
KeepEmptyLinesAtEOF: false
LambdaBodyIndentation: Signature
LineEnding: DeriveLF
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PackConstructorInitializers: BinPack
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
PPIndentWidth: -1
QualifierAlignment: Leave
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
RemoveParentheses: Leave
RemoveSemicolon: false
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: LexicographicNumeric
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeJsonColon: false
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
AfterRequiresInClause: false
AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInContainerLiterals: true
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParens: Never
SpacesInParensOptions:
InCStyleCasts: false
InConditionalStatements: false
InEmptyParentheses: false
Other: false
SpacesInSquareBrackets: false
Standard: Latest
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseTab: Never
VerilogBreakBetweenInstancePorts: true
WhitespaceSensitiveMacros:
- BOOST_PP_STRINGIZE
- CF_SWIFT_NAME
- NS_SWIFT_NAME
- PP_STRINGIZE
- STRINGIZE
...

View file

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

View file

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

View file

@ -1,11 +1,11 @@
#include <stdio.h> #include <stdio.h>
#include "pico/stdlib.h"
#include "pico/multicore.h" #include "pico/multicore.h"
#include "pico/stdlib.h"
#include "pico/util/queue.h" #include "pico/util/queue.h"
#include "settings.h"
#include "keyer.h" #include "keyer.h"
#include "settings.h"
namespace namespace
{ {
@ -18,15 +18,13 @@ extern const uint RIGHT_PADDLE_PIN = 15;
extern const uint BUZZER_PIN = 18; extern const uint BUZZER_PIN = 18;
// Stuff for communicating between cores // Stuff for communicating between cores
enum class KeyerQueueCommand enum class KeyerQueueCommand {
{
Run, Run,
Stop, Stop,
Config, Config,
Wait, Wait,
}; };
struct KeyerQueueData struct KeyerQueueData {
{
KeyerQueueCommand cmd; KeyerQueueCommand cmd;
uint8_t wpm; uint8_t wpm;
Mode mode; Mode mode;
@ -49,7 +47,7 @@ void setup()
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); // sleep_ms(1000);
} }
/* 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. */
@ -61,12 +59,10 @@ void core1_main()
Keyer keyer(data.wpm, data.mode); Keyer keyer(data.wpm, data.mode);
while (true) while (true) {
{
queue_try_remove(&keyerQueue, &data); queue_try_remove(&keyerQueue, &data);
switch (data.cmd) switch (data.cmd) {
{
case KeyerQueueCommand::Run: case KeyerQueueCommand::Run:
keyer.run(); keyer.run();
break; break;
@ -89,8 +85,8 @@ void core1_main()
int main() int main()
{ {
//timer_hw->dbgpause = 2; // workaround for problem with debug and sleep_ms // timer_hw->dbgpause = 2; // 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();
@ -106,8 +102,7 @@ int main()
KeyerQueueData keyerQueueData{KeyerQueueCommand::Run, settings.wpm, settings.mode}; KeyerQueueData keyerQueueData{KeyerQueueCommand::Run, settings.wpm, settings.mode};
queue_add_blocking(&keyerQueue, &keyerQueueData); queue_add_blocking(&keyerQueue, &keyerQueueData);
while (true) while (true) {
{
// Currently there's nothing to do on core0 // Currently there's nothing to do on core0
sleep_ms(1000); sleep_ms(1000);
} }

View file

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

View file

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

View file

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

View file

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