Switch demos to run on a thread

Change-Id: Ibbf904ba3e036548893267c489d4ddb4fb631cf8
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/kudzu/+/174849
Reviewed-by: Anthony DiGirolamo <tonymd@google.com>
diff --git a/applications/32blit_demo/BUILD.gn b/applications/32blit_demo/BUILD.gn
index 58cc2e6..d028cc6 100644
--- a/applications/32blit_demo/BUILD.gn
+++ b/applications/32blit_demo/BUILD.gn
@@ -30,7 +30,7 @@
     "random.h",
   ]
   deps = [
-    "$dir_app_common",
+    "//applications/app_common",
     "$dir_pw_board_led",
     "$dir_pw_color",
     "$dir_pw_display",
diff --git a/applications/32blit_demo/main.cc b/applications/32blit_demo/main.cc
index 4284fce..b313c9c 100644
--- a/applications/32blit_demo/main.cc
+++ b/applications/32blit_demo/main.cc
@@ -137,9 +137,7 @@
   return count == 0 ? 0 : sum / count;
 }
 
-}  // namespace
-
-void MainTask() {
+void MainTask(void*) {
   // Timing variables
   uint32_t frame_start_millis = pw::spin_delay::Millis();
   uint32_t frames = 0;
@@ -229,6 +227,8 @@
   }
 }
 
+}  // namespace
+
 namespace pw::system {
 
 void UserAppInit() {
@@ -236,7 +236,7 @@
 
   pw::thread::DetachedThread(pw::system::WorkQueueThreadOptions(),
                              pw::system::GetWorkQueue());
-  pw::system::GetWorkQueue().CheckPushWork(MainTask);
+  pw::thread::DetachedThread(Common::DisplayDrawThreadOptions(), MainTask);
 }
 
 }  // namespace pw::system
diff --git a/applications/app_common/BUILD.gn b/applications/app_common/BUILD.gn
index ec34126..0a0d66e 100644
--- a/applications/app_common/BUILD.gn
+++ b/applications/app_common/BUILD.gn
@@ -29,6 +29,7 @@
   public_deps = [
     "$dir_pw_display",
     "$dir_pw_status",
+    "$dir_pw_thread:thread",
   ]
   public = [ "public/app_common/common.h" ]
 }
diff --git a/applications/app_common/public/app_common/common.h b/applications/app_common/public/app_common/common.h
index 9b8652c..abef17f 100644
--- a/applications/app_common/public/app_common/common.h
+++ b/applications/app_common/public/app_common/common.h
@@ -15,6 +15,7 @@
 
 #include "pw_display/display.h"
 #include "pw_status/status.h"
+#include "pw_thread/thread.h"
 
 // This class is used for initialization and to create the objects which
 // are common to the test applications.
@@ -28,4 +29,7 @@
 
   // Return an initialized display.
   static pw::display::Display& GetDisplay();
+
+  // Provides thread options for the display thread.
+  static const pw::thread::Options& DisplayDrawThreadOptions();
 };
diff --git a/applications/app_common_impl/BUILD.gn b/applications/app_common_impl/BUILD.gn
index 8951b2d..d0c8411 100644
--- a/applications/app_common_impl/BUILD.gn
+++ b/applications/app_common_impl/BUILD.gn
@@ -72,7 +72,7 @@
     ":spi_stm32_flags",
   ]
   deps = [
-    "$dir_pigweed_experimental/applications/app_common:app_common.facade",
+    "//applications/app_common:app_common.facade",
     "$dir_pw_digital_io_stm32cube",
     "$dir_pw_display",
     "$dir_pw_display_driver_ili9341",
@@ -86,7 +86,7 @@
 
 pw_source_set("stm32") {
   deps = [
-    "$dir_pigweed_experimental/applications/app_common:app_common.facade",
+    "//applications/app_common:app_common.facade",
     "$dir_pw_display_driver_null",
   ]
   sources = [ "common_stm32.cc" ]
@@ -98,7 +98,7 @@
     ":spi_flags",
   ]
   deps = [
-    "$dir_pigweed_experimental/applications/app_common:app_common.facade",
+    "//applications/app_common:app_common.facade",
     "$dir_pw_digital_io_arduino",
     "$dir_pw_display",
     "$dir_pw_display_driver_ili9341",
@@ -115,7 +115,7 @@
   pw_source_set("mimxrt595") {
     public_configs = [ ":common_flags" ]
     deps = [
-      "$dir_pigweed_experimental/applications/app_common:app_common.facade",
+      "//applications/app_common:app_common.facade",
       "$dir_pw_display",
       "$dir_pw_display_driver_mipi_dsi",
       "$dir_pw_framebuffer_pool_mcuxpresso",
@@ -132,7 +132,7 @@
   "$PICO_ROOT/src/rp2_common/hardware_pwm",
   "$PICO_ROOT/src/rp2_common/hardware_spi",
   "$PICO_ROOT/src/rp2_common/hardware_vreg",
-  "$dir_pigweed_experimental/applications/app_common:app_common.facade",
+  "//applications/app_common:app_common.facade",
   "$dir_pw_digital_io_rp2040",
   "$dir_pw_display",
   "$dir_pw_framebuffer_pool",
@@ -142,6 +142,8 @@
   "$dir_pw_spi_rp2040",
   "$dir_pw_sync:borrow",
   "$dir_pw_sync:mutex",
+  "$dir_pw_thread:thread",
+  "$dir_pw_thread_freertos:thread",
 ]
 
 pw_source_set("pico_ili9341") {
@@ -199,10 +201,12 @@
 pw_source_set("host_imgui") {
   public_configs = [ ":common_flags" ]
   deps = [
-    "$dir_pigweed_experimental/applications/app_common:app_common.facade",
+    "//applications/app_common:app_common.facade",
     "$dir_pw_display_driver_imgui",
     "$dir_pw_display_imgui",
     "$dir_pw_framebuffer_pool",
+    "$dir_pw_thread:thread",
+    "$dir_pw_thread_stl:thread",
   ]
   sources = [ "common_host_imgui.cc" ]
   remove_configs = []
@@ -214,9 +218,11 @@
 pw_source_set("host_null") {
   public_configs = [ ":common_flags" ]
   deps = [
-    "$dir_pigweed_experimental/applications/app_common:app_common.facade",
+    "//applications/app_common:app_common.facade",
     "$dir_pw_display",
     "$dir_pw_display_driver_null",
+    "$dir_pw_thread:thread",
+    "$dir_pw_thread_stl:thread",
   ]
   sources = [ "common_host_null.cc" ]
 }
diff --git a/applications/app_common_impl/common_host_imgui.cc b/applications/app_common_impl/common_host_imgui.cc
index bdc099a..4036f79 100644
--- a/applications/app_common_impl/common_host_imgui.cc
+++ b/applications/app_common_impl/common_host_imgui.cc
@@ -17,6 +17,8 @@
 #include "pw_display_imgui/display.h"
 #include "pw_framebuffer_pool/framebuffer_pool.h"
 #include "pw_status/try.h"
+#include "pw_thread/thread.h"
+#include "pw_thread_stl/options.h"
 
 using pw::Status;
 using pw::color::color_rgb565_t;
@@ -55,3 +57,8 @@
 
 // static
 pw::display::Display& Common::GetDisplay() { return s_display; }
+
+const pw::thread::Options& Common::DisplayDrawThreadOptions() {
+  static pw::thread::stl::Options display_draw_thread_options;
+  return display_draw_thread_options;
+}
diff --git a/applications/app_common_impl/common_host_null.cc b/applications/app_common_impl/common_host_null.cc
index 89461a3..504faa4 100644
--- a/applications/app_common_impl/common_host_null.cc
+++ b/applications/app_common_impl/common_host_null.cc
@@ -15,6 +15,8 @@
 #include "pw_display/display.h"
 #include "pw_display_driver_null/display_driver.h"
 #include "pw_status/try.h"
+#include "pw_thread/thread.h"
+#include "pw_thread_stl/options.h"
 
 using pw::Status;
 using pw::framebuffer::PixelFormat;
@@ -42,3 +44,8 @@
 
 // static
 pw::display::Display& Common::GetDisplay() { return s_display; }
+
+const pw::thread::Options& Common::DisplayDrawThreadOptions() {
+  static pw::thread::stl::Options display_draw_thread_options;
+  return display_draw_thread_options;
+}
diff --git a/applications/app_common_impl/common_pico.cc b/applications/app_common_impl/common_pico.cc
index 56409ad..3b80b09 100644
--- a/applications/app_common_impl/common_pico.cc
+++ b/applications/app_common_impl/common_pico.cc
@@ -18,6 +18,7 @@
 #define LIB_CMSIS_CORE 0
 #define LIB_PICO_STDIO_SEMIHOSTING 0
 
+#include "FreeRTOS.h"
 #include "hardware/gpio.h"
 #include "hardware/pwm.h"
 #include "hardware/vreg.h"
@@ -32,6 +33,10 @@
 #include "pw_status/status.h"
 #include "pw_sync/borrow.h"
 #include "pw_sync/mutex.h"
+#include "pw_thread/detached_thread.h"
+#include "pw_thread/thread.h"
+#include "pw_thread_freertos/context.h"
+#include "pw_thread_freertos/options.h"
 
 #if defined(DISPLAY_TYPE_ILI9341)
 #include "pw_display_driver_ili9341/display_driver.h"
@@ -191,6 +196,10 @@
 
 pw::i2c::PicoInitiator i2c_bus(ki2cConfig);
 
+static constexpr size_t kDisplayDrawThreadStackWords = 512;
+static pw::thread::freertos::StaticContextWithStack<kDisplayDrawThreadStackWords>
+    display_draw_thread_context;
+
 }  // namespace
 
 // static
@@ -251,3 +260,13 @@
 
 // static
 pw::display::Display& Common::GetDisplay() { return s_display; }
+
+const pw::thread::Options& Common::DisplayDrawThreadOptions() {
+  static constexpr auto options =
+      pw::thread::freertos::Options()
+          .set_name("DisplayDrawThread")
+          .set_static_context(display_draw_thread_context)
+          // TODO: amontanez - Find a way to better manage priorities.
+          .set_priority(static_cast<UBaseType_t>(tskIDLE_PRIORITY + 1));
+  return options;
+}
diff --git a/applications/terminal_display/BUILD.gn b/applications/terminal_display/BUILD.gn
index 8f40c63..c229863 100644
--- a/applications/terminal_display/BUILD.gn
+++ b/applications/terminal_display/BUILD.gn
@@ -38,7 +38,7 @@
   sources = [ "main.cc" ]
   deps = [
     ":text_buffer",
-    "$dir_app_common",
+    "//applications/app_common",
     "$dir_pw_board_led",
     "$dir_pw_color",
     "$dir_pw_containers:vector",
diff --git a/applications/terminal_display/main.cc b/applications/terminal_display/main.cc
index a86ba7e..3373972 100644
--- a/applications/terminal_display/main.cc
+++ b/applications/terminal_display/main.cc
@@ -430,9 +430,7 @@
   return count == 0 ? 0 : sum / count;
 }
 
-}  // namespace
-
-void MainTask() {
+void MainTask(void*) {
   // Timing variables
   uint32_t frame_start_millis = pw::spin_delay::Millis();
   uint32_t frames = 0;
@@ -530,6 +528,8 @@
   }
 }
 
+}  // namespace
+
 namespace pw::system {
 
 void UserAppInit() {
@@ -537,7 +537,7 @@
 
   pw::thread::DetachedThread(pw::system::WorkQueueThreadOptions(),
                              pw::system::GetWorkQueue());
-  pw::system::GetWorkQueue().CheckPushWork(MainTask);
+  pw::thread::DetachedThread(Common::DisplayDrawThreadOptions(), MainTask);
 }
 
 }  // namespace pw::system