Skip to content

Commit

Permalink
Merge pull request #30 from ramonaoptics/startup_commands
Browse files Browse the repository at this point in the history
Try to add startup commands
  • Loading branch information
hmaarrfk authored Nov 10, 2024
2 parents 467cbf8 + b55c9fc commit 2747289
Show file tree
Hide file tree
Showing 11 changed files with 404 additions and 165 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
build:
strategy:
matrix:
teensy: ['32', '40']
teensy: ['32', '40', '40_blinker', '40_fan_hub', '32_fan_hub']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -25,7 +25,7 @@ jobs:
pip install platformio
- name: Build firmware
run: |
make teensy${{ matrix.teensy }}
platformio run --environment teensy${{ matrix.teensy }}
git_describe=$(git describe --tags --dirty)
cp .pio/build/teensy${{ matrix.teensy }}/firmware.hex firmware_teensy${{ matrix.teensy }}-${git_describe}.hex
cp .pio/build/teensy${{ matrix.teensy }}/firmware.hex firmware_teensy${{ matrix.teensy }}.hex
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This program allows you to use the Teensy as a USB to protocol converter.

The design is centered around exposing simple building blocks to a USB UART interface.
In this case, the Teensy is not designed as a standalone device. Rather, it should always be used with
In this case, the Teensy is not designed as a standalone device. Rather, it should always be used with
a host computer that sends requests to accomplish different actions.

Currently, we support:
Expand Down Expand Up @@ -50,5 +50,3 @@ To upload a precompiled build:
```
teensy_loader_cli -s --mcu=TEENSY40 build_version_number.hex
```


43 changes: 43 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ lib_deps =
build_flags =
-D TEENSY_OPT_FASTEST_LTO
!python versioneer.py
build_src_filter =
+<*.c>
+<*.h>
+<*.cpp>
+<*.hpp>
-<examples/*>

[env:teensy40]
platform = teensy
Expand All @@ -33,3 +39,40 @@ lib_deps =
https://github.com/ramonaoptics/teensy4_i2c.git#our_mods
build_flags =
!python versioneer.py
build_src_filter =
+<*.c>
+<*.h>
+<*.cpp>
+<*.hpp>
-<examples/*>

[env:teensy40_blinker]
extends = env:teensy40
build_src_filter =
+<*.c>
+<*.h>
+<*.cpp>
+<*.hpp>
-<examples/**>
+<examples/blinker/*>


[env:teensy32_fan_hub]
extends = env:teensy32
build_src_filter =
+<*.c>
+<*.h>
+<*.cpp>
+<*.hpp>
-<examples/**>
+<examples/fan_hub/*>

[env:teensy40_fan_hub]
extends = env:teensy40
build_src_filter =
+<*.c>
+<*.h>
+<*.cpp>
+<*.hpp>
-<examples/**>
+<examples/fan_hub/*>
39 changes: 27 additions & 12 deletions src/commandconstants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ int register_write_uint16(CommandRouter *cmd, int argc, const char **argv);
int register_read_uint32(CommandRouter *cmd, int argc, const char **argv);
int register_write_uint32(CommandRouter *cmd, int argc, const char **argv);

int eeprom_length(CommandRouter *cmd, int argc, const char **argv);
int eeprom_read_uint8(CommandRouter *cmd, int argc, const char **argv);
int eeprom_write_uint8(CommandRouter *cmd, int argc, const char **argv);
int eeprom_update_uint8(CommandRouter *cmd, int argc, const char **argv);
int eeprom_read_string(CommandRouter *cmd, int argc, const char **argv);
int eeprom_write_string(CommandRouter *cmd, int argc, const char **argv);

// Mostly for debugging and startup scripts
int sleep_seconds(CommandRouter *cmd, int argc, const char **argv);
int startup_commands_available(CommandRouter *cmd, int argc, const char **argv);
int read_startup_command(CommandRouter *cmd, int argc, const char **argv);
int demo_commands_available(CommandRouter *cmd, int argc, const char **argv);
int read_demo_command(CommandRouter *cmd, int argc, const char **argv);
int disable_demo_commands(CommandRouter *cmd, int argc, const char **argv);
int enable_demo_commands(CommandRouter *cmd, int argc, const char **argv);
int demo_commands_available(CommandRouter *cmd, int argc, const char **argv);
int demo_commands_enabled(CommandRouter *cmd, int argc, const char **argv);

// Syntax is: {short command, description, syntax}
const command_item_t command_list[] = {
Expand Down Expand Up @@ -172,17 +179,25 @@ const command_item_t command_list[] = {
"register_read_uint32 address", register_read_uint32},
{"register_write_uint32", "Write to an arbitrary hardware register.",
"register_write_uint32 address data", register_write_uint32},
{"eeprom_length", "Return the size of the of the EEPROM in bytes.",
"eeprom_length", eeprom_length},
{"eeprom_read_uint8", "Read data from a given EEPROM address.",
"eeprom_read_uint8 address", eeprom_read_uint8},
{"eeprom_write_uint8", "Write to an EEPROM address.",
"eeprom_write_uint8 address data", eeprom_write_uint8},
{"eeprom_update_uint8", "Write to an EEPROM address if the value has changed.",
"eeprom_update_uint8 address data", eeprom_update_uint8},
{"eeprom_read_string", "Read data from a given EEPROM address.",
"eeprom_read_string address", eeprom_read_string},
{"eeprom_write_string", "Write to an EEPROM address.",
"eeprom_write_string address data", eeprom_write_string},
{"sleep", "Sleep (and block) for the desired duration",
"sleep duration", sleep_seconds},
{"startup_commands_available", "Number of startup commands available",
"startup_commands_available", startup_commands_available},
{"read_startup_command", "Read a startup command",
"read_startup_command index", read_startup_command},
{"demo_commands_available", "Number of demo commands available",
"demo_commands_available", demo_commands_available},
{"read_demo_command", "Read a demo command",
"read_demo_command index", read_demo_command},
{"disable_demo_commands", "Disable the demo command loop on future startups.",
"disable_demo_commands", disable_demo_commands},
{"enable_demo_commands", "Enable the demo command loop on future startups.",
"enable_demo_commands", enable_demo_commands},
{"demo_commands_enabled", "Check if demo commands are enabled.",
"demo_commands_enabled", demo_commands_enabled},
{nullptr, nullptr, nullptr, nullptr},
};
61 changes: 36 additions & 25 deletions src/commandrouting.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "commandrouting.hpp"
#include <Arduino.h>
#include <errno.h>
#include <EEPROM.h>

#define LICENSE_TEXT \
"teensytoany: hardware debugger based on the Teensy platform\n" \
Expand Down Expand Up @@ -185,6 +186,32 @@ int command_help_func(CommandRouter *cmd, int argc, const char **argv) {
}
}

static void parseInputBuffer(char *input_buffer, int bytes_read, const char **argv, int &argc) {
// This could likely be replaced by strtok_r, but I really failed
// at using it on the teensy 4.0
// And as such, I'm simply ignoring using it.
int i = 0;
argc = 0;
while (i < bytes_read) {
char c = input_buffer[i];
if (c == '\0')
break;
if (input_buffer[i] == ' ') {
input_buffer[i] = '\0';
i++;
continue;
}
argv[argc] = &input_buffer[i];
argc++;
while (i < bytes_read) {
i += 1;
c = input_buffer[i];
if (c == ' ' || c == '\0')
break;
}
}
}

int CommandRouter::processSerialStream() {
int argc;
int bytes_read = 0;
Expand Down Expand Up @@ -230,31 +257,7 @@ int CommandRouter::processSerialStream() {
return 0;
}

// This could likely be replaced by strtok_r, but I really failed
// at using it on the teensy 4.0
// And as such, I'm simply ignoring using it.
int i;
i = 0;
argc = 0;
while (i < bytes_read){
char c;
c = input_buffer[i];
if (c == '\0')
break;
if (input_buffer[i] == ' ') {
input_buffer[i] = '\0';
i++;
continue;
}
argv[argc] = &input_buffer[i];
argc++;
while(i < bytes_read) {
i += 1;
c = input_buffer[i];
if (c == ' ' || c == '\0')
break;
}
}
parseInputBuffer(input_buffer, bytes_read, argv, argc);

#if 0
Serial.printf("argc == %d\n", argc);
Expand Down Expand Up @@ -283,3 +286,11 @@ int CommandRouter::processSerialStream() {

return result;
}

int CommandRouter::processString(const char *buffer) {
int argc;
this->buffer[0] = '\0';
int bytes_read = snprintf(this->buffer + 1, this->buffer_size - 1, "%s", buffer);
parseInputBuffer(this->buffer + 1, bytes_read, argv, argc);
return route(argc, argv);
}
1 change: 1 addition & 0 deletions src/commandrouting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CommandRouter {
const char **argv_buffer);
int help(const char *command_name = nullptr);
int processSerialStream();
int processString(const char *buffer);
void cleanup();
~CommandRouter();

Expand Down
20 changes: 20 additions & 0 deletions src/examples/blinker/blinker_commands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const char *teensy_to_any_startup_commands[] = {
"gpio_pin_mode 13 OUTPUT 1",
"gpio_digital_pulse 13 0 1 200E-3",
"gpio_digital_pulse 13 1 0 200E-3",
"gpio_digital_pulse 13 0 1 100E-3",
"gpio_digital_pulse 13 1 0 100E-3",
"gpio_digital_pulse 13 0 1 100E-3",
"gpio_digital_pulse 13 1 0 100E-3",
"gpio_digital_pulse 13 0 1 100E-3",
"gpio_digital_pulse 13 1 0 100E-3",
"gpio_digital_pulse 13 0 1 200E-3",
"gpio_digital_pulse 13 1 0 200E-3",
nullptr,
};

const char *teensy_to_any_demo_commands[] = {
"gpio_digital_pulse 13 0 1 50E-3",
"gpio_digital_pulse 13 1 0 50E-3",
nullptr,
};
64 changes: 64 additions & 0 deletions src/examples/fan_hub/fan_hub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* A small demo to show how the teensy-to-any can be used to control 4 pin fans.
* It is assumed that the fan PWM pin is connected to pin 2
* and that an indicator LED is connected to Pin 13 (this is the default board)
*
* The demo code will run the fan at
* * 100%
* * 75%
* * 50%
* * 25%
* * 0%
* For 3 seconds at a time and then continue in an infinite loop.
* The LED can be used as a visual guide of the expected speed of the fan.
*
* Not all fan will be able to operate at 25% duty cyle.
*
* Ramona Optics - 2024
*/
const char *teensy_to_any_startup_commands[] = {
// Setup the LED as an indicator pin
"gpio_pin_mode 13 OUTPUT 1",
// Setup pin 2 as the PWM control pin
"gpio_pin_mode 2 OUTPUT 1",
// Set the frequency to 25 kHz
"analog_write_frequency 2 25000",
// Also setup the LED to tell us what is going on.
"analog_write_frequency 13 25000",
nullptr,
};

// Change the fan speed
const char *teensy_to_any_demo_commands[] = {
"analog_write 2 255",
"analog_write 13 255",
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms

"analog_write 2 192",
// Brightness is a strange thing, and does not
// decrease so linearly according to human perception
"analog_write 13 128",
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms

"analog_write 2 128",
"analog_write 13 30",
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms

"analog_write 2 64",
"analog_write 13 10",
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms

"analog_write 2 0",
"analog_write 13 0",
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
"sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", "sleep 0.2", // 1000 ms
nullptr,
};
Loading

0 comments on commit 2747289

Please sign in to comment.