refactor: structure pico project

This commit is contained in:
2025-09-04 23:00:48 +02:00
parent db44b15717
commit 0a376487df
8 changed files with 117 additions and 463 deletions

View File

@@ -1,46 +0,0 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into the executable file.
The source code of each library should be placed in a separate directory
("lib/your_library_name/[Code]").
For example, see the structure of the following example libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
Example contents of `src/main.c` using Foo and Bar:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
The PlatformIO Library Dependency Finder will find automatically dependent
libraries by scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

View File

@@ -0,0 +1,22 @@
#pragma once
class Stepper {
protected:
int steps_per_rev;
virtual void step(int steps, bool direction) = 0;
public:
void step_rev(double revs, bool direction){
if(revs == 0.0) return;
if(revs < 0.0){
direction = !direction;
revs = -revs;
}
long total_steps = (long)(revs * steps_per_rev + 0.5);
if(total_steps <= 0) return;
step((int)total_steps, direction);
}
virtual ~Stepper() {}
};

View File

@@ -0,0 +1,58 @@
#pragma once
#include <Arduino.h>
#include "Stepper.h"
#include <array>
class ULN2003Stepper : public Stepper {
private:
std::array<uint8_t, 4> pins;
static constexpr int steps_per_seq = 8;
const uint8_t sequence[8][4] = {
{1, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1},
{1, 0, 0, 1}
};
int step_delay_us = 5000;
int current_seq_idx = 0;
void step(int steps, bool direction) override {
if (steps <= 0) return;
for (int i = 0; i < steps; ++i) {
// calculate next step index
current_seq_idx = direction
? (current_seq_idx + 1) % steps_per_seq
: (current_seq_idx - 1 + steps_per_seq) % steps_per_seq;
// set pins according to the current sequence
digitalWrite(pins[0], sequence[current_seq_idx][0]);
digitalWrite(pins[1], sequence[current_seq_idx][1]);
digitalWrite(pins[2], sequence[current_seq_idx][2]);
digitalWrite(pins[3], sequence[current_seq_idx][3]);
// wait for the specified delay
delayMicroseconds(step_delay_us);
}
}
public:
ULN2003Stepper(std::array<uint8_t, 4> pins, int rev_steps) : pins(pins) {
steps_per_rev = rev_steps;
for (auto pin : pins) {
pinMode(pin, OUTPUT);
}
}
void setStepDelay(int delay_us) { step_delay_us = delay_us; }
void resetPhase() { current_seq_idx = 0; }
};