Add blinky logic and pw_string
Change-Id: Ia762593a241d14b796250f36ae0f4f50a214617b
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/quickstart/zephyr/+/201812
Reviewed-by: Yuval Peress <peress@google.com>
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index effefbd..bf3c10b 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -13,9 +13,7 @@
# the License.
cmake_minimum_required(VERSION 3.20)
-
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
-
project(zephyr-quickstart LANGUAGES C CXX)
target_sources(app PRIVATE src/main.cc)
diff --git a/app/prj.conf b/app/prj.conf
index b32e2c2..9460409 100644
--- a/app/prj.conf
+++ b/app/prj.conf
@@ -12,10 +12,17 @@
# License for the specific language governing permissions and limitations under
# the License.
-# C++ configuration.
+# C++ config
CONFIG_CPP=y
CONFIG_STD_CPP17=y
CONFIG_REQUIRES_FULL_LIBCPP=y
-# Logging configuration.
+# Peripherals config
+CONFIG_GPIO=y
+
+# Logging config
CONFIG_LOG=y
+
+# Pigweed config
+# https://pigweed.dev/docs/os/zephyr/kconfig.html
+CONFIG_PIGWEED_STRING=y
diff --git a/app/src/main.cc b/app/src/main.cc
index 1a996c4..6c015df 100644
--- a/app/src/main.cc
+++ b/app/src/main.cc
@@ -12,9 +12,44 @@
// License for the specific language governing permissions and limitations under
// the License.
-#include "pw_log/log.h"
+#include <stdio.h>
+#include <zephyr/kernel.h>
+#include <zephyr/drivers/gpio.h>
+
+#include "pw_log/log.h"
+#include "pw_string/string.h"
+
+#define LED0_NODE DT_ALIAS(led0)
+#define SLEEP_TIME_MS 1000
+
+static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
+
+// Logs messages with pw_log and pw_string and blinks an LED using Zephyr's
+// GPIO Driver API. Blinky logic is adapted from Zephyr's basic blinky sample:
+// https://github.com/zephyrproject-rtos/zephyr/tree/main/samples/basic/blinky
int main() {
- PW_LOG_INFO("Hello, world!");
+ pw::InlineString<32> greetings = "Hello, world!";
+ PW_LOG_INFO("%s", greetings.c_str());
+ int ret;
+ bool led_state = true;
+ if (!gpio_is_ready_dt(&led)) {
+ PW_LOG_ERROR("LED unavailable");
+ return 0;
+ }
+ ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
+ if (ret < 0) {
+ PW_LOG_ERROR("LED configuration failed");
+ return 0;
+ }
+ while (true) {
+ ret = gpio_pin_toggle_dt(&led);
+ if (ret < 0) {
+ return 0;
+ }
+ led_state = !led_state;
+ PW_LOG_INFO("LED state: %s", led_state ? "ON" : "OFF");
+ k_msleep(SLEEP_TIME_MS);
+ }
return 0;
}