Skip to content

Commit c29a046

Browse files
1ms TIME/LAST fixes #144
1 parent 798b52d commit c29a046

File tree

10 files changed

+73
-16
lines changed

10 files changed

+73
-16
lines changed

module/flash.c

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// asf
66
#include "flashc.h"
77
#include "print_funcs.h"
8+
#include "init_teletype.h"
89

910
// this
1011
#include "teletype.h"
@@ -89,6 +90,11 @@ void flash_read(uint8_t preset_no, scene_state_t *scene,
8990
unpack_grid(scene);
9091
memcpy(text, &f.scenes[preset_no].text,
9192
SCENE_TEXT_LINES * SCENE_TEXT_CHARS);
93+
// need to reset timestamps
94+
uint32_t ticks = get_ticks();
95+
for (size_t i = 0; i < TEMP_SCRIPT; i++)
96+
scene->scripts[i].last_time = ticks;
97+
scene->variables.time = 0;
9298
}
9399

94100
uint8_t flash_last_saved_scene() {

module/main.c

+4
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,10 @@ void render_init(void) {
783783
////////////////////////////////////////////////////////////////////////////////
784784
// teletype_io.h
785785

786+
uint32_t tele_get_ticks() {
787+
return get_ticks();
788+
}
789+
786790
void tele_metro_updated() {
787791
uint32_t metro_time = scene_state.variables.m;
788792

simulator/tt.c

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include "util.h"
1212

1313

14+
uint32_t tele_get_ticks() {
15+
return 0;
16+
}
17+
1418
void tele_metro_updated() {
1519
printf("METRO UPDATED");
1620
printf("\n");

src/ops/init.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ static void op_INIT_TIME_get(const void *NOTUSED(data), scene_state_t *ss,
169169
command_state_t *NOTUSED(cs)) {
170170
clear_delays(ss);
171171
ss->variables.time = 0;
172-
for (uint8_t i = 0; i < TEMP_SCRIPT; i++) ss->scripts[i].last_time = 0;
172+
uint32_t ticks = tele_get_ticks();
173+
for (uint8_t i = 0; i < TEMP_SCRIPT; i++) ss->scripts[i].last_time = ticks;
174+
ss->variables.time = 0;
173175
ss_sync_every(ss, 0);
174176
}

src/ops/variables.c

+39-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "helpers.h"
66
#include "ops/op.h"
77
#include "teletype.h"
8+
#include "teletype_io.h"
89

910

1011
static void op_LAST_get(const void *data, scene_state_t *ss, exec_state_t *es,
@@ -25,6 +26,14 @@ static void op_I_get(const void *data, scene_state_t *ss, exec_state_t *es,
2526
command_state_t *cs);
2627
static void op_I_set(const void *data, scene_state_t *ss, exec_state_t *es,
2728
command_state_t *cs);
29+
static void op_TIME_get(const void *data, scene_state_t *ss, exec_state_t *es,
30+
command_state_t *cs);
31+
static void op_TIME_set(const void *data, scene_state_t *ss, exec_state_t *es,
32+
command_state_t *cs);
33+
static void op_TIME_ACT_get(const void *data, scene_state_t *ss, exec_state_t *es,
34+
command_state_t *cs);
35+
static void op_TIME_ACT_set(const void *data, scene_state_t *ss, exec_state_t *es,
36+
command_state_t *cs);
2837

2938
// clang-format off
3039
const tele_op_t op_A = MAKE_SIMPLE_VARIABLE_OP(A , variables.a );
@@ -39,8 +48,8 @@ const tele_op_t op_O_MAX = MAKE_SIMPLE_VARIABLE_OP(O.MAX , variables.o_
3948
const tele_op_t op_O_MIN = MAKE_SIMPLE_VARIABLE_OP(O.MIN , variables.o_min );
4049
const tele_op_t op_O_WRAP = MAKE_SIMPLE_VARIABLE_OP(O.WRAP , variables.o_wrap );
4150
const tele_op_t op_T = MAKE_SIMPLE_VARIABLE_OP(T , variables.t );
42-
const tele_op_t op_TIME = MAKE_SIMPLE_VARIABLE_OP(TIME , variables.time );
43-
const tele_op_t op_TIME_ACT = MAKE_SIMPLE_VARIABLE_OP(TIME.ACT , variables.time_act );
51+
const tele_op_t op_TIME = MAKE_GET_SET_OP(TIME, op_TIME_get, op_TIME_set, 0, true);
52+
const tele_op_t op_TIME_ACT = MAKE_GET_SET_OP(TIME.ACT, op_TIME_ACT_get, op_TIME_ACT_set, 0, true);
4453
const tele_op_t op_LAST = MAKE_GET_OP(LAST , op_LAST_get, 1, true);
4554
const tele_op_t op_X = MAKE_SIMPLE_VARIABLE_OP(X , variables.x );
4655
const tele_op_t op_Y = MAKE_SIMPLE_VARIABLE_OP(Y , variables.y );
@@ -52,6 +61,34 @@ const tele_op_t op_O = MAKE_GET_SET_OP(O , op_O_get , op_O_set , 0,
5261
const tele_op_t op_I = MAKE_GET_SET_OP(I , op_I_get, op_I_set, 0, true);
5362
// clang-format on
5463

64+
static void op_TIME_get(const void *NOTUSED(data), scene_state_t *ss,
65+
exec_state_t *NOTUSED(es), command_state_t *cs) {
66+
int64_t delta = ss->variables.time_act ?
67+
tele_get_ticks() - ss->variables.time : ss->variables.time;
68+
cs_push(cs, delta & 0x7fff);
69+
}
70+
71+
static void op_TIME_set(const void *NOTUSED(data), scene_state_t *ss,
72+
exec_state_t *NOTUSED(es), command_state_t *cs) {
73+
int16_t new_time = cs_pop(cs);
74+
ss->variables.time = ss->variables.time_act ?
75+
tele_get_ticks() - new_time : new_time;
76+
}
77+
78+
static void op_TIME_ACT_get(const void *NOTUSED(data), scene_state_t *ss,
79+
exec_state_t *NOTUSED(es), command_state_t *cs) {
80+
cs_push(cs, ss->variables.time_act ? 1 : 0);
81+
}
82+
83+
static void op_TIME_ACT_set(const void *NOTUSED(data), scene_state_t *ss,
84+
exec_state_t *NOTUSED(es), command_state_t *cs) {
85+
int16_t act = cs_pop(cs);
86+
if (act && ss->variables.time_act) return;
87+
if (!act && !ss->variables.time_act) return;
88+
ss->variables.time_act = act ? 1 : 0;
89+
ss->variables.time = tele_get_ticks() - ss->variables.time;
90+
}
91+
5592
static void op_LAST_get(const void *NOTUSED(data), scene_state_t *ss,
5693
exec_state_t *NOTUSED(es), command_state_t *cs) {
5794
int16_t script_number = cs_pop(cs) - 1;

src/state.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ void ss_init(scene_state_t *ss) {
1717
ss->stack_op.top = 0;
1818
memset(&ss->scripts, 0, ss_scripts_size());
1919
turtle_init(&ss->turtle);
20+
uint32_t ticks = tele_get_ticks();
21+
for (size_t i = 0; i < TEMP_SCRIPT; i++)
22+
ss->scripts[i].last_time = ticks;
23+
ss->variables.time = 0;
24+
ss->variables.time_act = 1;
2025
}
2126

2227
void ss_variables_init(scene_state_t *ss) {
@@ -331,17 +336,14 @@ size_t ss_scripts_size() {
331336
}
332337

333338
int16_t ss_get_script_last(scene_state_t *ss, script_number_t idx) {
334-
int16_t now = ss->variables.time;
335339
if (idx < TT_SCRIPT_1) return 0;
336340
if (idx > INIT_SCRIPT) return 0;
337-
int16_t last = ss->scripts[idx].last_time;
338-
if (now < last)
339-
return (INT16_MAX - last) + (now - INT16_MIN); // I must be dense?
340-
return now - last;
341+
uint32_t delta = (tele_get_ticks() - ss->scripts[idx].last_time) & 0x7fff;
342+
return delta;
341343
}
342344

343345
void ss_update_script_last(scene_state_t *ss, script_number_t idx) {
344-
ss->scripts[idx].last_time = ss->variables.time;
346+
ss->scripts[idx].last_time = tele_get_ticks();
345347
}
346348

347349
every_count_t *ss_get_every(scene_state_t *ss, script_number_t idx,

src/state.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ typedef struct {
9090
int16_t r_min;
9191
int16_t r_max;
9292
int16_t scene;
93-
int16_t time;
94-
int16_t time_act;
93+
int64_t time;
94+
uint8_t time_act;
9595
int16_t tr[TR_COUNT];
9696
int16_t tr_pol[TR_COUNT];
9797
int16_t tr_time[TR_COUNT];
@@ -129,7 +129,7 @@ typedef struct {
129129
uint8_t l;
130130
tele_command_t c[SCRIPT_MAX_COMMANDS];
131131
every_count_t every[SCRIPT_MAX_COMMANDS];
132-
int16_t last_time;
132+
uint32_t last_time;
133133
} scene_script_t;
134134

135135
typedef struct {

src/teletype.c

-4
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,6 @@ process_result_t process_command(scene_state_t *ss, exec_state_t *es,
299299
// TICK /////////////////////////////////////////////////////////
300300

301301
void tele_tick(scene_state_t *ss, uint8_t time) {
302-
// time is the basic resolution of all code henceforth called
303-
// hardware 2.0: get an RTC!
304-
if (ss->variables.time_act) ss->variables.time += time;
305-
306302
// could be a while() if there is reason to expect a user to cascade moves
307303
// with SCRIPTs without the tick delay
308304
if (ss->turtle.stepped && ss->turtle.script_number != TEMP_SCRIPT) {

src/teletype_io.h

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
// These functions are for interacting with the teletype hardware, each target
88
// must provide it's own implementation
99

10+
// used for TIME and LAST
11+
extern uint32_t tele_get_ticks(void);
12+
1013
// called when M or M.ACT are updated
1114
extern void tele_metro_updated(void);
1215

tests/main.c

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "process_tests.h"
1212
#include "turtle_tests.h"
1313

14+
uint32_t tele_get_ticks() {
15+
return 0;
16+
}
1417
void tele_metro_updated() {}
1518
void tele_metro_reset() {}
1619
void tele_tr(uint8_t i, int16_t v) {}

0 commit comments

Comments
 (0)