Skip to content

Commit ea0b100

Browse files
committed
update
1 parent 5775bdc commit ea0b100

File tree

8 files changed

+85
-31
lines changed

8 files changed

+85
-31
lines changed

.github/workflows/cpp.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ jobs:
2020
# cd cpp
2121
# chmod a+x *.sh
2222
# ./build_extlibs.sh
23-
- name: Test gecko
24-
run: |
25-
mkdir -p build
26-
cd build
27-
cmake ..
28-
make
29-
./test/test-all
23+
# - name: Test gecko
24+
# run: |
25+
# mkdir -p build
26+
# cd build
27+
# cmake ..
28+
# make
29+
# ./test/test-all

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ set(CMAKE_CXX_STANDARD 20)
44
set(CMAKE_C_STANDARD 17)
55

66
set(PROJECT mv)
7-
if (1)
7+
option(BUILD_ID "what to build for: apple or pico" "apple")
8+
if (BUILD_ID STREQUAL "apple")
89
# PROJECT_VERSION_MAJOR year
910
# PROJECT_VERSION_MINOR month
1011
# PROJECT_VERSION_PATCH day
@@ -15,7 +16,7 @@ if (1)
1516
find_package(Threads REQUIRED)
1617
string(PREPEND CMAKE_EXE_LINKER_FLAGS " -Wl ")
1718
set(PROJECT_TARGET_PLATFORM "Unix")
18-
else()
19+
elseif(BUILD_ID STREQUAL "pico")
1920
set(PICO_BOARD pico) # pico or pico_w or pico2
2021
set(PICO_SDK_PATH "$ENV{HOME}/github/pico-sdk")
2122
set(ENV{PATH} "$ENV{HOME}/github/gcc-arm/bin:$ENV{PATH}")

examples/pico/dummy.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ int main() {
1818

1919
ServoPort s;
2020

21+
uint8_t data[] = {1,2,3,4,5,6,7,8,9,0};
22+
s.write(data, 10);
23+
s.read(data, 10);
24+
s.write(data, 10);
25+
s.read(data, 10);
26+
s.write(data, 10);
27+
s.read(data, 10);
28+
s.write(data, 10);
29+
s.read(data, 10);
30+
s.write(data, 10);
31+
s.read(data, 10);
32+
s.write(data, 10);
33+
s.read(data, 10);
34+
2135
while (1) {
2236
printf(".");
2337
sleep_ms(1000);

examples/unix/move.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int main() {
2727
// pprint(mv);
2828
printf(">> Wrote: %d ", serial.write(mv));
2929
// pprint(mv);
30-
delay(1000);
30+
sleep_ms(1000);
3131
}
3232

3333
return 0;

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
Yet another Dynamexl library for smart servos. This is call `mv` after the
88
unix move command.
99

10+
## Command line
11+
12+
```bash
13+
$ mkdir build
14+
$ cd build
15+
$ rm -fr * && cmake -DBUILD_ID=pico .. && make
16+
```
17+
1018
## Cool
1119

1220
- [termcolor](https://github.com/ikalnytskyi/termcolor)

src/mv.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ union {
5353
// #include <Arduino.h>
5454
// #include <ports/arduino_port.hpp>
5555
#if defined(PICO_BOARD)
56-
// #if defined(PICO_SDK)
5756
#include <ports/pico_port.hpp>
5857
#elif defined(__APPLE__)
5958
#include <unistd.h> // usleep
60-
inline void delay(unsigned int msec) { usleep(1000 * msec); }
59+
inline void sleep_us(unsigned int usec) { usleep(usec); }
60+
inline void sleep_ms(unsigned int msec) { usleep(1000 * msec); }
61+
#include <ports/unix_port.hpp>
62+
#elif defined(__linux__)
63+
#include <unistd.h> // usleep
64+
inline void sleep_us(unsigned int usec) { usleep(usec); }
65+
inline void sleep_ms(unsigned int msec) { usleep(1000 * msec); }
6166
#include <ports/unix_port.hpp>
6267
#endif
6368

src/ports/pico_port.hpp

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <hardware/uart.h>
88
#include <stdint.h>
99

10+
///////////////////////////////////////////////////////////
1011

1112
/**
1213
This is a FIFO, but with a fixed size that will over write old
@@ -15,8 +16,8 @@
1516
template <uint16_t BUFFER_SIZE> class Fifo {
1617
// public:
1718
uint8_t buffer[BUFFER_SIZE];
18-
uint16_t head{0}; // read / pop
19-
uint16_t tail{0}; // write / push
19+
uint16_t head{0}; // read / pop / oldest data
20+
uint16_t tail{0}; // write / push / newest data
2021
uint16_t numElem{0};
2122

2223
public:
@@ -42,18 +43,46 @@ template <uint16_t BUFFER_SIZE> class Fifo {
4243
}
4344
buffer[tail] = b;
4445
tail = nextPos(tail);
45-
numElem = numElem + 1;
46+
numElem += 1;
4647
}
4748

4849
uint8_t pop() volatile {
4950
if (isEmpty()) return 0;
5051
uint8_t ret = buffer[head];
5152
head = nextPos(head);
5253
numElem = numElem - 1;
54+
if (numElem == 0) clear();
5355
return ret;
5456
}
57+
58+
// uint16_t copy(uint8_t *p, const uint16_t size) volatile {
59+
// if (tail > head) {
60+
// uint16_t n = tail - head;
61+
// n = n > size ? size : n;
62+
// memcpy(p, &buffer[head], n);
63+
// head += n;
64+
// return n;
65+
// }
66+
// else {
67+
// uint16_t n = BUFFER_SIZE - head;
68+
// n = n > size ? size : n;
69+
// uint16_t save = n;
70+
// memcpy(p, &buffer[head], n);
71+
// if (n == size) {
72+
// head += n;
73+
// return save;
74+
// }
75+
// n = size - n;
76+
// n = n > tail ? tail : n;
77+
// memcpy(&p[save], &buffer[n], n);
78+
// head = n;
79+
// return save + n;
80+
// }
81+
// }
5582
};
5683

84+
///////////////////////////////////////////////////////////
85+
5786
constexpr uint UART0_TX_PIN = 0;
5887
constexpr uint UART0_RX_PIN = 1;
5988
constexpr uint UART1_TX_PIN = 8;
@@ -65,15 +94,6 @@ constexpr uint32_t tx_valid[2] = { // uart0, uart1
6594
constexpr uint32_t rx_valid[2] = { // uart0, uart1
6695
(1 << 1) | (1 << 13) | (1 << 17) | (1 << 29),
6796
(1 << 5) | (1 << 9) | (1 << 21) | (1 << 25)};
68-
// constexpr uint32_t sda_valid[2] = { // i2c0, i2c1
69-
// (1 << 0) | (1 << 4) | (1 << 8) | (1 << 12) | (1 << 16) | (1 << 20) |
70-
// (1 << 24) | (1 << 28),
71-
// (1 << 2) | (1 << 6) | (1 << 10) | (1 << 14) | (1 << 18) | (1 << 22) |
72-
// (1 << 26)};
73-
// constexpr uint32_t scl_valid[2] = { // i2c0, i2c1
74-
// (1 << 1) | (1 << 5) | (1 << 9) | (1 << 13) | (1 << 17) | (1 << 21) | (1 <<
75-
// 25) | (1 << 29), (1 << 3) | (1 << 7) | (1 << 11) | (1 << 15) | (1 << 19) |
76-
// (1 << 23) | (1 << 27)};
7797

7898
constexpr uint16_t UART_BUFFER_SIZE = 128;
7999
static volatile Fifo<UART_BUFFER_SIZE> buffer_0;
@@ -114,15 +134,17 @@ static void uart1_irq_func(void) {
114134
irq_set_enabled(UART1_IRQ, true);
115135
}
116136

137+
117138
class ServoPort {
118139
uart_inst_t *uart;
140+
uint32_t dd{0};
119141
volatile Fifo<UART_BUFFER_SIZE> *buffer{nullptr};
120142

121143
public:
122144
ServoPort() {}
123145
~ServoPort() { uart_deinit(uart); }
124146

125-
uint init(uint baudrate, uint8_t port, uint8_t pin_tx, uint8_t pin_rx) {
147+
uint init(uint baudrate, uint8_t port, uint8_t pin_tx, uint8_t pin_rx, uint8_t pin_dd) {
126148
bool valid = (1 << pin_tx) & tx_valid[port];
127149
if (!valid) return 0;
128150
valid = (1 << pin_rx) & rx_valid[port];
@@ -156,17 +178,20 @@ class ServoPort {
156178
uart_set_fifo_enabled(uart, true);
157179
uart_set_translate_crlf(uart, false);
158180

181+
gpio_init(pin_dd);
182+
gpio_set_dir(pin_dd, GPIO_OUT);
183+
dd = pin_dd;
184+
gpio_put(dd, 0);
185+
159186
return baudrate;
160187
}
161188

162189
inline bool is_enabled() { return uart_is_enabled(uart); }
163-
164190
inline void flush() { buffer->clear(); }
165-
166191
inline uint set_baud(uint baud) { return uart_set_baudrate(uart, baud); }
167-
168192
inline size_t available() { return buffer->size(); }
169193

194+
inline uint8_t read() { return buffer->pop(); }
170195
size_t read(uint8_t *data, size_t size) {
171196
size_t cnt = 0;
172197
size_t bsize = buffer->size();
@@ -175,11 +200,12 @@ class ServoPort {
175200
}
176201

177202
return cnt;
203+
// return buffer->copy(data, size);
178204
}
179205

180-
inline uint8_t read() { return buffer->pop(); }
181-
182206
void write(const uint8_t *data, size_t size) {
207+
gpio_put(dd, 1);
183208
uart_write_blocking(uart, data, size);
209+
gpio_put(dd, 0);
184210
}
185211
};

src/ports/unix_port.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class SerialPort {
104104
perror("*** Couldn't set port attribute");
105105

106106
set_dir(DD_READ);
107-
delay(100);
107+
sleep_ms(100);
108108
set_dir(DD_WRITE);
109109
}
110110

0 commit comments

Comments
 (0)