Browse Source

Draw time of day in the mazes.

master
Juergen Stuber 2 years ago
parent
commit
c4d45a9016
3 changed files with 47 additions and 22 deletions
  1. 1
    0
      Cargo.toml
  2. 23
    11
      src/bin/dualmaze/main.rs
  3. 23
    11
      src/bin/maze/main.rs

+ 1
- 0
Cargo.toml View File

@@ -6,3 +6,4 @@ edition = "2018"
6 6
 
7 7
 [dependencies]
8 8
 rand = "0.6.0"
9
+chrono = "0.4.6"

+ 23
- 11
src/bin/dualmaze/main.rs View File

@@ -4,6 +4,9 @@ use std::io::Write;
4 4
 use std::thread::sleep;
5 5
 use std::time::Duration;
6 6
 
7
+use chrono::Local;
8
+use chrono::Timelike;
9
+
7 10
 use rand::thread_rng;
8 11
 use rand::Rng;
9 12
 
@@ -179,6 +182,11 @@ impl Board {
179 182
             self.draw_horizontal_segment(pos + v2d(1, delta), hsize);
180 183
         }
181 184
     }
185
+    fn draw_digit(&mut self, pos: Point2d, size: Vec2d, digit: u8) {
186
+        let segment_table = vec![0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f];
187
+        let segments = segment_table[digit as usize];
188
+        self.draw_7_segments(pos, size, segments);
189
+    }
182 190
 }
183 191
 
184 192
 struct Move {
@@ -215,31 +223,35 @@ fn main() -> std::io::Result<()> {
215 223
 
216 224
     let t_frame = 0.040; // s
217 225
     let delay = Duration::new(0, (1_000_000_000.0 * t_frame) as u32);
218
-    let mut t_wait = 0.0; // s
219 226
 
220 227
     let board_size = v2d(x_size as i32, y_size as i32);
221 228
     // round down to odd size for maze
222 229
     let maze_size = v2d(
223 230
         (board_size.x - 1) / 2 * 2 + 1,
224
-        (board_size.y - 1) / 2 * 2 + 1
231
+        (board_size.y - 1) / 2 * 2 + 1,
225 232
     );
226 233
 
227 234
     let mut board = Board(Vec::new());
228 235
     let mut open = Vec::new();
236
+    let mut last_drawn_minute = 99;
229 237
     loop {
230 238
         if open.is_empty() {
231
-            if t_wait > 0.0 {
232
-                t_wait -= t_frame;
233
-            } else {
234
-                t_wait = 60.0; // s
239
+            // get time
240
+            let dt = Local::now();
241
+            let h = dt.hour();
242
+            let m = dt.minute();
243
+
244
+            if m != last_drawn_minute {
245
+                last_drawn_minute = m;
246
+
235 247
                 board = Board::new(board_size, maze_size);
236 248
 
237
-                // draw stuff in prios
249
+                // draw time in prios
238 250
                 let segment_size = v2d(11, 5);
239
-                board.draw_7_segments(p2d(6, 8), segment_size, 0x06);
240
-                board.draw_7_segments(p2d(24, 8), segment_size, 0x4f);
241
-                board.draw_7_segments(p2d(42, 8), segment_size, 0x4f);
242
-                board.draw_7_segments(p2d(60, 8), segment_size, 0x07);
251
+                board.draw_digit(p2d(6, 8), segment_size, (h / 10) as u8);
252
+                board.draw_digit(p2d(24, 8), segment_size, (h % 10) as u8);
253
+                board.draw_digit(p2d(42, 8), segment_size, (m / 10) as u8);
254
+                board.draw_digit(p2d(60, 8), segment_size, (m % 10) as u8);
243 255
 
244 256
                 // start building walls from the border
245 257
                 for x in (2..(maze_size.x - 2)).step_by(2) {

+ 23
- 11
src/bin/maze/main.rs View File

@@ -4,6 +4,9 @@ use std::io::Write;
4 4
 use std::thread::sleep;
5 5
 use std::time::Duration;
6 6
 
7
+use chrono::Local;
8
+use chrono::Timelike;
9
+
7 10
 use rand::thread_rng;
8 11
 use rand::Rng;
9 12
 
@@ -181,6 +184,11 @@ impl Board {
181 184
             self.draw_horizontal_segment(pos + v2d(1, delta), hsize);
182 185
         }
183 186
     }
187
+    fn draw_digit(&mut self, pos: Point2d, size: Vec2d, digit: u8) {
188
+        let segment_table = vec![0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f];
189
+        let segments = segment_table[digit as usize];
190
+        self.draw_7_segments(pos, size, segments);
191
+    }
184 192
 }
185 193
 
186 194
 struct Move {
@@ -217,33 +225,37 @@ fn main() -> std::io::Result<()> {
217 225
 
218 226
     let t_frame = 0.040; // s
219 227
     let delay = Duration::new(0, (1_000_000_000.0 * t_frame) as u32);
220
-    let mut t_wait = 0.0; // s
221 228
 
222 229
     let board_size = v2d(x_size as i32, y_size as i32);
223 230
     // round down to odd size for maze
224 231
     let maze_size = v2d(
225 232
         (board_size.x - 1) / 2 * 2 + 1,
226
-        (board_size.y - 1) / 2 * 2 + 1
233
+        (board_size.y - 1) / 2 * 2 + 1,
227 234
     );
228 235
     // pick odd position at or near the middle
229 236
     let maze_mid = p2d((maze_size.x - 1) / 4 * 2 + 1, (maze_size.y - 1) / 4 * 2 + 1);
230 237
 
231 238
     let mut board = Board(Vec::new());
232 239
     let mut open = Vec::new();
240
+    let mut last_drawn_minute = 99;
233 241
     loop {
234 242
         if open.is_empty() {
235
-            if t_wait > 0.0 {
236
-                t_wait -= t_frame;
237
-            } else {
238
-                t_wait = 60.0; // s
243
+            // get time
244
+            let dt = Local::now();
245
+            let h = dt.hour();
246
+            let m = dt.minute();
247
+
248
+            if m != last_drawn_minute {
249
+                last_drawn_minute = m;
250
+
239 251
                 board = Board::new(board_size, maze_size);
240 252
 
241
-                // draw stuff in prios
253
+                // draw time in prios
242 254
                 let segment_size = v2d(11, 5);
243
-                board.draw_7_segments(p2d(6, 8), segment_size, 0x06);
244
-                board.draw_7_segments(p2d(24, 8), segment_size, 0x4f);
245
-                board.draw_7_segments(p2d(42, 8), segment_size, 0x4f);
246
-                board.draw_7_segments(p2d(60, 8), segment_size, 0x07);
255
+                board.draw_digit(p2d(6, 8), segment_size, (h / 10) as u8);
256
+                board.draw_digit(p2d(24, 8), segment_size, (h % 10) as u8);
257
+                board.draw_digit(p2d(42, 8), segment_size, (m / 10) as u8);
258
+                board.draw_digit(p2d(60, 8), segment_size, (m % 10) as u8);
247 259
 
248 260
                 // start in the middle
249 261
                 board.set(maze_mid, Square::Corridor);

Loading…
Cancel
Save