@@ -106,24 +106,35 @@ fn rave(comptime count: u8) !void {
}
const uS_TO_SEC = 1000 * 1000 * 1000;
const DAY = 86400;
fn timeMode(on_time: u32, off_time: u32, offset: i32) !void {
const DAY = 86400;
fn sleepSeconds(s: u64) void {
std.time.sleep(s * uS_TO_SEC);
}
fn timeModeCore(current_time: i64, on_time: u32, off_time: u32) !u64 {
var sleep: i64 = DAY;
if (current_time == off_time) {
inline for (PINS) |pin| try pinLow(pin);
sleep += on_time - off_time;
} else if (current_time == on_time) {
inline for (.{PINS[3]}) |pin| try pinHigh(pin);
sleep += off_time - on_time;
} else {
return 30;
}
return @abs(@mod(sleep, DAY));
}
fn timeMode(on: u32, off: u32, offset: i32) !void {
const mask: i64 = ~@as(i64, 3);
while (true) {
const current_time = std.time.timestamp();
const current_day = @mod(current_time + offset, DAY) & mask;
log.debug("current time {} mod DAY {}", .{ current_time + offset, current_day });
if (current_day == off_time) {
inline for (PINS) |pin| try pinLow(pin);
std.time.sleep(6 * 60 * 60 * uS_TO_SEC);
} else if (current_day == on_time) {
inline for (.{PINS[3]}) |pin| try pinLow(pin);
std.time.sleep(6 * 60 * 60 * uS_TO_SEC);
} else {
std.time.sleep(uS_TO_SEC);
}
const instant = std.time.timestamp();
const cal_time = @mod(instant + offset, DAY) & mask;
log.debug("current time {} mod DAY {}", .{ instant + offset, cal_time });
const sleep_for = try timeModeCore(cal_time, on, off);
sleepSeconds(sleep_for);
}
}