Remove pw_spin_delay calls

- Remove Common::EndOfFrameCallback call every 10 seconds.
- Switch snake to use pw::chrono::SystemClock

Change-Id: Ic8a016f7b0bb2386a602f60cd261aea7d16810e8
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/kudzu/+/193391
Commit-Queue: Anthony DiGirolamo <tonymd@google.com>
Reviewed-by: Carlos Chinchilla <cachinchilla@google.com>
diff --git a/applications/32blit_demo/BUILD.gn b/applications/32blit_demo/BUILD.gn
index 8c2202c..842e161 100644
--- a/applications/32blit_demo/BUILD.gn
+++ b/applications/32blit_demo/BUILD.gn
@@ -33,7 +33,6 @@
     "$dir_pw_framebuffer",
     "$dir_pw_log",
     "$dir_pw_random",
-    "$dir_pw_spin_delay",
     "$dir_pw_string",
     "$dir_pw_sys_io",
     "$dir_pw_system:target_hooks",
diff --git a/applications/32blit_demo/main.cc b/applications/32blit_demo/main.cc
index 3502489..63be86c 100644
--- a/applications/32blit_demo/main.cc
+++ b/applications/32blit_demo/main.cc
@@ -29,7 +29,6 @@
 #include "pw_display/display.h"
 #include "pw_framebuffer/framebuffer.h"
 #include "pw_log/log.h"
-#include "pw_spin_delay/delay.h"
 #include "pw_string/string_builder.h"
 #include "pw_sys_io/sys_io.h"
 #include "pw_system/target_hooks.h"
diff --git a/applications/badge/BUILD.gn b/applications/badge/BUILD.gn
index 49e8030..3623d6b 100644
--- a/applications/badge/BUILD.gn
+++ b/applications/badge/BUILD.gn
@@ -42,7 +42,6 @@
     "$dir_pw_log",
     "$dir_pw_random",
     "$dir_pw_ring_buffer",
-    "$dir_pw_spin_delay",
     "$dir_pw_string",
     "$dir_pw_sys_io",
     "$dir_pw_system:target_hooks",
diff --git a/applications/badge/main.cc b/applications/badge/main.cc
index b94bfc1..e4b288b 100644
--- a/applications/badge/main.cc
+++ b/applications/badge/main.cc
@@ -40,7 +40,6 @@
 #include "pw_log/log.h"
 #include "pw_logo5x7.h"
 #include "pw_math/vector2.h"
-#include "pw_spin_delay/delay.h"
 #include "pw_string/string_builder.h"
 #include "pw_sys_io/sys_io.h"
 #include "pw_system/target_hooks.h"
@@ -256,8 +255,6 @@
 
   Buttons& kudzu_buttons = Common::GetButtons();
 
-  uint32_t frame_start_millis = pw::spin_delay::Millis();
-
   float x_scale_offset = 0.0;
   float y_scale_offset = 0.0;
   const float x_scale_increment = 0.7;
@@ -364,11 +361,6 @@
 
     // Every second make a log message.
     frame_counter.LogTiming();
-
-    if (pw::spin_delay::Millis() > frame_start_millis + 10000) {
-      Common::EndOfFrameCallback();
-      frame_start_millis = pw::spin_delay::Millis();
-    }
   }
 }
 
diff --git a/applications/snake/BUILD.gn b/applications/snake/BUILD.gn
index 0d22759..ed14b1a 100644
--- a/applications/snake/BUILD.gn
+++ b/applications/snake/BUILD.gn
@@ -34,6 +34,7 @@
   sources = [ "snake.cc" ]
   public_deps = [
     "$dir_pw_assert",
+    "$dir_pw_chrono:system_clock",
     "$dir_pw_containers:inline_deque",
     "$dir_pw_function",
   ]
@@ -52,6 +53,7 @@
   ]
   deps = [
     "$dir_pw_assert",
+    "$dir_pw_chrono:system_clock",
     "$dir_pw_draw",
     "$dir_pw_log",
     "$dir_pw_sync:lock_annotations",
@@ -65,10 +67,10 @@
   deps = [
     ":game",
     "$dir_pw_board_led",
+    "$dir_pw_chrono:system_clock",
     "$dir_pw_display",
     "$dir_pw_framebuffer",
     "$dir_pw_log",
-    "$dir_pw_spin_delay",
     "$dir_pw_system:target_hooks",
     "$dir_pw_system:work_queue",
     "$dir_pw_thread:thread",
diff --git a/applications/snake/game.cc b/applications/snake/game.cc
index a14406e..484b5f5 100644
--- a/applications/snake/game.cc
+++ b/applications/snake/game.cc
@@ -19,12 +19,18 @@
 
 #include "libkudzu/random.h"
 #include "pw_assert/check.h"
+#include "pw_chrono/system_clock.h"
 #include "pw_color/colors_pico8.h"
 #include "pw_draw/draw.h"
 #include "pw_log/log.h"
 
+using namespace std::chrono_literals;
+
 namespace snake {
 
+constexpr auto kDefaultTimePerAdvance =
+    pw::chrono::SystemClock::for_at_least(60ms);
+
 Game::Game(int32_t screen_width, int32_t screen_height)
     : screen_width_(screen_width / kPixelBoxRatio),
       screen_height_(screen_height / kPixelBoxRatio),
@@ -34,7 +40,9 @@
              Snake::Direction::kLeft),
       fruit_({.x = 0, .y = 0}),
       fruit_color_(pw::color::colors_pico8_rgb565[COLOR_BLUE]),
-      run_(false) {
+      run_(false),
+      time_per_advance_(kDefaultTimePerAdvance),
+      last_advance_time_(pw::chrono::SystemClock::now()) {
   SetNextFruitCoordinates();
 }
 
@@ -51,9 +59,11 @@
   }
   bool crashed = false;
   bool ate_fruit = false;
-  {
+  auto current_time = pw::chrono::SystemClock::now();
+  if (current_time > last_advance_time_ + time_per_advance_) {
     std::lock_guard lock(lock_);
     snake_.Advance(fruit_, ate_fruit, crashed);
+    last_advance_time_ = current_time;
   }
   if (crashed) {
     // TODO(cachinchilla): draw a crash.
@@ -62,6 +72,10 @@
   }
 
   if (ate_fruit) {
+    time_per_advance_ -= pw::chrono::SystemClock::for_at_least(5ms);
+    if (time_per_advance_.count() <= 0) {
+      time_per_advance_ = pw::chrono::SystemClock::for_at_least(0ms);
+    }
     SetNextFruitCoordinates();
   }
   Draw(framebuffer);
diff --git a/applications/snake/main.cc b/applications/snake/main.cc
index 909fe32..d7a9f86 100644
--- a/applications/snake/main.cc
+++ b/applications/snake/main.cc
@@ -24,7 +24,6 @@
 #include "pw_display/display.h"
 #include "pw_framebuffer/framebuffer.h"
 #include "pw_log/log.h"
-#include "pw_spin_delay/delay.h"
 #include "pw_system/target_hooks.h"
 #include "pw_system/work_queue.h"
 #include "pw_thread/detached_thread.h"
@@ -35,9 +34,6 @@
 
 namespace {
 
-constexpr uint32_t kFramesPerSecond = 16;
-constexpr uint32_t kWaitMillis = 1000 / kFramesPerSecond;
-
 class PollingTouchButtonsThread : public pw::thread::ThreadCore {
  public:
   PollingTouchButtonsThread(
@@ -52,7 +48,6 @@
     while (true) {
       pw::touchscreen::TouchEvent touch_event = touchscreen_.GetTouchPoint();
       buttons_.OnTouchEvent(touch_event);
-      pw::spin_delay::WaitMillis(kWaitMillis);
     }
   }
 
@@ -90,7 +85,6 @@
 
   // Display and app loop.
   kudzu::FrameCounter frame_counter = kudzu::FrameCounter();
-  uint32_t last_report_time = pw::spin_delay::Millis();
   while (true) {
     frame_counter.StartFrame();
 
@@ -112,14 +106,6 @@
     frame_counter.EndFlush();
 
     frame_counter.LogTiming();
-
-    pw::spin_delay::WaitMillis(kWaitMillis);
-
-    // Periodically make a log message.
-    if (pw::spin_delay::Millis() > last_report_time + 10000) {
-      Common::EndOfFrameCallback();
-      last_report_time = pw::spin_delay::Millis();
-    }
   }
 }
 
diff --git a/applications/snake/public/snake/game.h b/applications/snake/public/snake/game.h
index e450e77..1f69f39 100644
--- a/applications/snake/public/snake/game.h
+++ b/applications/snake/public/snake/game.h
@@ -16,6 +16,7 @@
 #include <cstddef>
 #include <mutex>
 
+#include "pw_chrono/system_clock.h"
 #include "pw_color/colors_pico8.h"
 #include "pw_framebuffer/framebuffer.h"
 #include "pw_sync/lock_annotations.h"
@@ -83,6 +84,8 @@
   Block fruit_;
   pw::color::color_rgb565_t fruit_color_;
   bool run_ = false;
+  pw::chrono::SystemClock::duration time_per_advance_;
+  pw::chrono::SystemClock::time_point last_advance_time_;
   pw::sync::Mutex lock_;
 };
 
diff --git a/applications/terminal_display/BUILD.gn b/applications/terminal_display/BUILD.gn
index 1fe897b..3b52fe0 100644
--- a/applications/terminal_display/BUILD.gn
+++ b/applications/terminal_display/BUILD.gn
@@ -47,7 +47,6 @@
     "$dir_pw_log",
     "$dir_pw_math",
     "$dir_pw_random",
-    "$dir_pw_spin_delay",
     "$dir_pw_string",
     "$dir_pw_sys_io",
     "$dir_pw_system:target_hooks",
diff --git a/applications/terminal_display/main.cc b/applications/terminal_display/main.cc
index bda7cb2..8d6818a 100644
--- a/applications/terminal_display/main.cc
+++ b/applications/terminal_display/main.cc
@@ -38,7 +38,6 @@
 #include "pw_math/vector2.h"
 #include "pw_math/vector3.h"
 #include "pw_ring_buffer/prefixed_entry_ring_buffer.h"
-#include "pw_spin_delay/delay.h"
 #include "pw_string/string_builder.h"
 #include "pw_sys_io/sys_io.h"
 #include "pw_system/target_hooks.h"