diff --git a/Dockerfile b/Dockerfile index e4ec61d..1a7cb2b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/script.sh b/script.sh index 04e797b..14711a3 100755 --- a/script.sh +++ b/script.sh @@ -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" } diff --git a/snippets/button/code.txt b/snippets/button/code.txt new file mode 100644 index 0000000..2e9bf00 --- /dev/null +++ b/snippets/button/code.txt @@ -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; +} diff --git a/snippets/button/imports.txt b/snippets/button/imports.txt new file mode 100644 index 0000000..3795edf --- /dev/null +++ b/snippets/button/imports.txt @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +#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}); diff --git a/snippets/led/code.txt b/snippets/led/code.txt new file mode 100644 index 0000000..7b91bfb --- /dev/null +++ b/snippets/led/code.txt @@ -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; +} \ No newline at end of file diff --git a/snippets/led/imports.txt b/snippets/led/imports.txt new file mode 100644 index 0000000..40066b9 --- /dev/null +++ b/snippets/led/imports.txt @@ -0,0 +1,2 @@ +#include +#define LED0_NODE DT_ALIAS(led0) \ No newline at end of file