Added some code snippets
All checks were successful
Build & Publish / build (push) Successful in 8m21s

This commit is contained in:
Florian Kaiser 2023-08-03 08:20:34 +02:00
parent 7916bc84d6
commit 22d036515c
Signed by: H4CK3R-01
SSH Key Fingerprint: SHA256:Zh0ZE/S6X5mfivz9uLnbQizfkJfEQTjL8FlWbPqhqk0
6 changed files with 221 additions and 40 deletions

View File

@ -4,6 +4,7 @@ RUN sudo apt update && sudo apt install dialog -y
COPY script.sh /opt
COPY data /opt/data
COPY snippets /opt/snippets
RUN sudo chmod +x /opt/script.sh

147
script.sh
View File

@ -20,6 +20,19 @@ display_result() {
--msgbox "$2" -1 -1
}
display_code() {
string="----- Imports -----
${2}
----- Code -----
${3}"
dialog --title "$1" \
--backtitle "Configure Zephyr RTOS" \
--no-collapse \
--msgbox "$string" -1 -1
}
main_dialog() {
selection=$(dialog --stdout \
--backtitle "Configure Zephyr RTOS" \
@ -110,71 +123,125 @@ plattform_dialog() {
}
connectivity_dialog() {
# TODO
connectivity=$( dialog --stdout \
--backtitle "Connectivity" \
--checklist "Select connectivity:" $HEIGHT $WIDTH 4 \
1 "WiFi" $(in_array "$CONNECTIVITY" 1) \
2 "Bluetooth" $(in_array "$CONNECTIVITY" 2) \
3 "LoRaWAN" $(in_array "$CONNECTIVITY" 3) \
4 "NB-IoT" $(in_array "$CONNECTIVITY" 4) )
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "WiFi" \
2 "Bluetooth" \
3 "LoRaWAN" \
4 "NB-IoT")
if [ ! -z "$connectivity" ]; then
CONNECTIVITY=$connectivity
fi
case $connectivity in
1 ) # WiFi
# TODO
;;
2 ) # Bluetooth
# TODO
;;
3 ) # LoRaWAN
# TODO
;;
4 ) # NB-IoT
# TODO
;;
esac
}
protocols_dialog() {
# TODO
protocols=$( dialog --stdout \
--backtitle "Protocols" \
--checklist "Select protocols:" $HEIGHT $WIDTH 4 \
1 "MQTT" $(in_array "$PROTOCOLS" 1) \
2 "REST" $(in_array "$PROTOCOLS" 2) \
3 "CoAP" $(in_array "$PROTOCOLS" 3) )
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "MQTT" \
2 "REST" \
3 "CoAP")
if [ ! -z "$protocols" ]; then
PROTOCOLS=$protocols
fi
case $protocols in
1 ) # MQTT
# TODO
;;
2 ) # REST
# TODO
;;
3 ) # CoAP
# TODO
;;
esac
}
interfaces_dialog() {
# TODO
interfaces=$( dialog --stdout \
--backtitle "Interfaces" \
--checklist "Select interfaces:" $HEIGHT $WIDTH 4 \
1 "I2C" $(in_array "$INTERFACES" 1) \
2 "SPI" $(in_array "$INTERFACES" 2) \
3 "UART" $(in_array "$INTERFACES" 3) )
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "I2C" \
2 "SPI" \
3 "UART")
if [ ! -z "$interfaces" ]; then
INTERFACES=$interfaces
fi
case $interfaces in
1 ) # I2C
# TODO
;;
2 ) # SPI
# TODO
;;
esac
}
sensors_dialog() {
# TODO
sensors=$( dialog --stdout \
--backtitle "Sensors" \
--checklist "Select sensors:" $HEIGHT $WIDTH 4 \
1 "Sensor 1" $(in_array "$SENSORS" 1) \
2 "Sensor 2" $(in_array "$SENSORS" 2) \
3 "Sensor 3" $(in_array "$SENSORS" 3) \
4 "Sensor 4" $(in_array "$SENSORS" 4) )
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "Button" \
2 "Sensor 2" \
3 "Sensor 3" \
4 "Sensor 4" )
if [ ! -z "$sensors" ]; then
SENSORS=$sensors
fi
case $sensors in
1 ) # Button
imports=$(cat snippets/button/imports.txt)
code=$(cat snippets/led/code.txt)
display_code "Use this code snippet" "$imports" "$code"
;;
2 )
# TODO
;;
3 )
# TODO
;;
esac
}
actuators_dialog() {
# TODO
actuators=$( dialog --stdout \
--backtitle "Actuators" \
--checklist "Select actuators:" $HEIGHT $WIDTH 4 \
1 "Actuators 1" $(in_array "$ACTUATORS" 1) \
2 "Actuators 2" $(in_array "$ACTUATORS" 2) \
3 "Actuators 3" $(in_array "$ACTUATORS" 3) \
4 "Actuators 4" $(in_array "$ACTUATORS" 4) )
--menu "Please select:" $HEIGHT $WIDTH 4 \
1 "LED" \
2 "Actuators 2" \
3 "Actuators 3" \
4 "Actuators 4" )
if [ ! -z "$actuators" ]; then
ACTUATORS=$actuators
fi
case $actuators in
1 ) # LED
imports=$(cat snippets/led/imports.txt)
code=$(cat snippets/led/code.txt)
display_code "Use this code snippet" "$imports" "$code"
;;
2 )
# TODO
;;
3 )
# TODO
;;
4 )
# TODO
;;
esac
}
init_workspace() {
@ -203,7 +270,7 @@ build_project() {
flash_project() {
plattform_dialog
case "$PLATTFORM" in
"nrf52840dongle_nrf52840")
display_result 'Flash project' "Please run those commands in an environment where nrfutil and the compiled hex files are available:\\n\\n\\nnrfutil pkg generate --hw-version 52 --sd-req=0x00 --application build/zephyr/zephyr.hex --application-version 1 mcuboot.zip\\n\\nnrfutil dfu usb-serial -pkg mcuboot.zip -p /dev/ttyACM0"
@ -216,7 +283,7 @@ flash_project() {
clean_build_dir() {
rm -rf /workdir/app/build/*
display_result "Cleaned build" "Cleaned build directory successfully"
}

65
snippets/button/code.txt Normal file
View File

@ -0,0 +1,65 @@
void button_pressed(const struct device *dev, struct gpio_callback *cb,
uint32_t pins)
{
printk("Button pressed at %" PRIu32 "\n", k_cycle_get_32());
}
int button(void)
{
int ret;
if (!gpio_is_ready_dt(&button)) {
printk("Error: button device %s is not ready\n",
button.port->name);
return 0;
}
ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
if (ret != 0) {
printk("Error %d: failed to configure %s pin %d\n",
ret, button.port->name, button.pin);
return 0;
}
ret = gpio_pin_interrupt_configure_dt(&button,
GPIO_INT_EDGE_TO_ACTIVE);
if (ret != 0) {
printk("Error %d: failed to configure interrupt on %s pin %d\n",
ret, button.port->name, button.pin);
return 0;
}
gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin));
gpio_add_callback(button.port, &button_cb_data);
printk("Set up button at %s pin %d\n", button.port->name, button.pin);
if (led.port && !device_is_ready(led.port)) {
printk("Error %d: LED device %s is not ready; ignoring it\n",
ret, led.port->name);
led.port = NULL;
}
if (led.port) {
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT);
if (ret != 0) {
printk("Error %d: failed to configure LED device %s pin %d\n",
ret, led.port->name, led.pin);
led.port = NULL;
} else {
printk("Set up LED at %s pin %d\n", led.port->name, led.pin);
}
}
printk("Press the button\n");
if (led.port) {
while (1) {
/* If we have an LED, match its state to the button's. */
int val = gpio_pin_get_dt(&button);
if (val >= 0) {
gpio_pin_set_dt(&led, val);
}
k_msleep(SLEEP_TIME_MS);
}
}
return 0;
}

View File

@ -0,0 +1,25 @@
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/sys/util.h>
#include <zephyr/sys/printk.h>
#include <inttypes.h>
#define SLEEP_TIME_MS 1
/*
* Get button configuration from the devicetree sw0 alias. This is mandatory.
*/
#define SW0_NODE DT_ALIAS(sw0)
#if !DT_NODE_HAS_STATUS(SW0_NODE, okay)
#error "Unsupported board: sw0 devicetree alias is not defined"
#endif
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios,
{0});
static struct gpio_callback button_cb_data;
/*
* The led0 devicetree alias is optional. If present, we'll use it
* to turn on the LED whenever the button is pressed.
*/
static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios,
{0});

21
snippets/led/code.txt Normal file
View File

@ -0,0 +1,21 @@
int blink_led(void) {
int ret;
if (!gpio_is_ready_dt(&led)) {
return 0;
}
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
if (ret < 0) {
return 0;
}
while (1) {
ret = gpio_pin_toggle_dt(&led);
if (ret < 0) {
return 0;
}
k_msleep(1000);
}
return 0;
}

2
snippets/led/imports.txt Normal file
View File

@ -0,0 +1,2 @@
#include <zephyr/drivers/gpio.h>
#define LED0_NODE DT_ALIAS(led0)