Browse code

Added time device and example.

Andrew Alderwick authored on 26/03/2021 21:01:51
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,20 @@
1
+;current { second 1 }
2
+
3
+( devices )
4
+
5
+|0100 ;Console { pad 8 char 1 byte 1 short 2 }
6
+|0190 ;Time { year 2 month 1 day 1 hour 1 minute 1 second 1 dow 1 doy 2 isdst 1 get 1 }
7
+|01F0 .RESET .FRAME .ERROR ( vectors )
8
+|01F8 [ 13fd 1ef3 1bf2 ] ( palette )
9
+
10
+( program )
11
+
12
+|0200 @RESET BRK
13
+
14
+@FRAME
15
+	#00 =Time.get
16
+	~Time.second ~current.second NEQ #01 JNZ BRK
17
+	~Time.second DUP =current.second =Console.byte
18
+
19
+@ERROR BRK
20
+
... ...
@@ -1,5 +1,6 @@
1 1
 #include <SDL2/SDL.h>
2 2
 #include <stdio.h>
3
+#include <time.h>
3 4
 
4 5
 /*
5 6
 Copyright (c) 2021 Devine Lu Linvega
... ...
@@ -417,6 +418,26 @@ midi_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
417 418
 	return b1;
418 419
 }
419 420
 
421
+Uint8
422
+time_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
423
+{
424
+	Uint8 *m = u->ram.dat;
425
+	time_t seconds = time(NULL);
426
+	struct tm *t = localtime(&seconds);
427
+	m[ptr +  0] = (t->tm_year & 0xff00) >> 8;
428
+	m[ptr +  1] = t->tm_year & 0xff;
429
+	m[ptr +  2] = t->tm_mon;
430
+	m[ptr +  3] = t->tm_mday;
431
+	m[ptr +  4] = t->tm_hour;
432
+	m[ptr +  5] = t->tm_min;
433
+	m[ptr +  6] = t->tm_sec;
434
+	m[ptr +  7] = t->tm_wday;
435
+	m[ptr +  8] = (t->tm_yday & 0x100) >> 8;
436
+	m[ptr +  9] = t->tm_yday && 0xff;
437
+	m[ptr + 10] = t->tm_isdst;
438
+	return b1;
439
+}
440
+
420 441
 Uint8
421 442
 system_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1)
422 443
 {
... ...
@@ -496,7 +517,7 @@ main(int argc, char **argv)
496 517
 	portuxn(&u, "file", file_poke);
497 518
 	portuxn(&u, "audio", audio_poke);
498 519
 	portuxn(&u, "midi", ppnil);
499
-	portuxn(&u, "---", ppnil);
520
+	portuxn(&u, "time", time_poke);
500 521
 	portuxn(&u, "---", ppnil);
501 522
 	portuxn(&u, "---", ppnil);
502 523
 	portuxn(&u, "---", ppnil);