nixie.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. use core::ops::DerefMut;
  2. use cortex_m::interrupt::free;
  3. use stm32l4xx_hal::{
  4. prelude::{
  5. _embedded_hal_blocking_i2c_Read, _embedded_hal_blocking_i2c_Write,
  6. _embedded_hal_blocking_i2c_WriteRead,
  7. },
  8. timer::Event,
  9. };
  10. use crate::pca9685;
  11. pub const DS3231_ADDR: u8 = 0x68;
  12. pub const TUSB322_ADDR: u8 = 0x47;
  13. pub const PCA9685_ADDR_1: u8 = 0x41;
  14. pub const PCA9685_ADDR_2: u8 = 0x42;
  15. pub const PCA9685_ADDR_3: u8 = 0x43;
  16. pub const PCA9685_ALL_CALL: u8 = 0x70; // Default enabled
  17. pub const PCA9685_SUB_CALL_1: u8 = 0x71; // Default disabled
  18. pub const PCA9685_SUB_CALL_2: u8 = 0x72; // Default disabled
  19. pub const PCA9685_SUB_CALL_3: u8 = 0x73; // Default disabled
  20. pub const DISPLAY_REFRESH_FPS: u32 = 500;
  21. pub const DIGIT_FADE_DURATION_MS: u32 = 1000;
  22. pub const CYCLE_FADE_DURATION_MS: u32 = 200;
  23. pub const CYCLE_ITERATIONS: usize = 20;
  24. pub const CYCLE_REFRESH_INTERVAL: u32 = 60;
  25. pub const CYCLE_REFRESH_VARIANCE: u32 = 30;
  26. const DOT_MIN_BRIGHTNESS: u32 = 256;
  27. const DOT_MAX_BRIGHTNESS: u32 = 640;
  28. const DOT_FADE_DURATION_US: u32 = 1_000_000;
  29. const DIGIT_MAX_BRIGHTNESS: u32 = 4096;
  30. const DIGIT_MIN_BRIGHTNESS: u32 = 0;
  31. const NUM_TUBES: usize = 4;
  32. const NUM_DIGITS: usize = 10;
  33. const MAP_DOT_ADDR: u8 = PCA9685_ADDR_2;
  34. const MAP_DOT_PIN: u8 = 15;
  35. const MAP_ADDR: usize = 0;
  36. const MAP_PIN: usize = 1;
  37. struct DigitToPin {
  38. address: u8,
  39. pin: usize,
  40. }
  41. struct PwmDriver {
  42. digit: [DigitToPin; 10],
  43. }
  44. struct PwmOutputMap {
  45. driver: [PwmDriver; 4],
  46. dot_address: u8,
  47. dot_pin: usize,
  48. }
  49. static TUBE_MAPPING: PwmOutputMap = {
  50. PwmOutputMap {
  51. driver: [
  52. PwmDriver {
  53. digit: [
  54. DigitToPin { address: PCA9685_ADDR_1, pin: 8, }, // Tube 0 Digit 0
  55. DigitToPin { address: PCA9685_ADDR_1, pin: 9, }, // Tube 0 Digit 1
  56. DigitToPin { address: PCA9685_ADDR_1, pin: 10, }, // Tube 0 Digit 2
  57. DigitToPin { address: PCA9685_ADDR_1, pin: 12, }, // Tube 0 Digit 3
  58. DigitToPin { address: PCA9685_ADDR_1, pin: 15, }, // Tube 0 Digit 4
  59. DigitToPin { address: PCA9685_ADDR_1, pin: 14, }, // Tube 0 Digit 5
  60. DigitToPin { address: PCA9685_ADDR_1, pin: 11, }, // Tube 0 Digit 6
  61. DigitToPin { address: PCA9685_ADDR_1, pin: 0, }, // Tube 0 Digit 7
  62. DigitToPin { address: PCA9685_ADDR_1, pin: 1, }, // Tube 0 Digit 8
  63. DigitToPin { address: PCA9685_ADDR_1, pin: 13, }, // Tube 0 Digit 9
  64. ],
  65. },
  66. PwmDriver {
  67. digit: [
  68. DigitToPin { address: PCA9685_ADDR_1, pin: 5, }, // Tube 1 Digit 0
  69. DigitToPin { address: PCA9685_ADDR_1, pin: 6, }, // Tube 1 Digit 1
  70. DigitToPin { address: PCA9685_ADDR_1, pin: 7, }, // Tube 1 Digit 2
  71. DigitToPin { address: PCA9685_ADDR_1, pin: 2, }, // Tube 1 Digit 3
  72. DigitToPin { address: PCA9685_ADDR_2, pin: 4, }, // Tube 1 Digit 4
  73. DigitToPin { address: PCA9685_ADDR_2, pin: 1, }, // Tube 1 Digit 5
  74. DigitToPin { address: PCA9685_ADDR_1, pin: 4, }, // Tube 1 Digit 6
  75. DigitToPin { address: PCA9685_ADDR_2, pin: 2, }, // Tube 1 Digit 7
  76. DigitToPin { address: PCA9685_ADDR_2, pin: 3, }, // Tube 1 Digit 8
  77. DigitToPin { address: PCA9685_ADDR_1, pin: 3, }, // Tube 1 Digit 9
  78. ],
  79. },
  80. PwmDriver {
  81. digit: [
  82. DigitToPin { address: PCA9685_ADDR_3, pin: 8, }, // Tube 2 Digit 0
  83. DigitToPin { address: PCA9685_ADDR_3, pin: 9, }, // Tube 2 Digit 1
  84. DigitToPin { address: PCA9685_ADDR_3, pin: 10, }, // Tube 2 Digit 2
  85. DigitToPin { address: PCA9685_ADDR_3, pin: 12, }, // Tube 2 Digit 3
  86. DigitToPin { address: PCA9685_ADDR_2, pin: 12, }, // Tube 2 Digit 4
  87. DigitToPin { address: PCA9685_ADDR_2, pin: 13, }, // Tube 2 Digit 5
  88. DigitToPin { address: PCA9685_ADDR_3, pin: 11, }, // Tube 2 Digit 6
  89. DigitToPin { address: PCA9685_ADDR_2, pin: 14, }, // Tube 2 Digit 7
  90. DigitToPin { address: PCA9685_ADDR_2, pin: 11, }, // Tube 2 Digit 8
  91. DigitToPin { address: PCA9685_ADDR_3, pin: 13, }, // Tube 2 Digit 9
  92. ],
  93. },
  94. PwmDriver {
  95. digit: [
  96. DigitToPin { address: PCA9685_ADDR_3, pin: 5, }, // Tube 3 Digit 0
  97. DigitToPin { address: PCA9685_ADDR_3, pin: 6, }, // Tube 3 Digit 1
  98. DigitToPin { address: PCA9685_ADDR_3, pin: 7, }, // Tube 3 Digit 2
  99. DigitToPin { address: PCA9685_ADDR_3, pin: 2, }, // Tube 3 Digit 3
  100. DigitToPin { address: PCA9685_ADDR_3, pin: 14, }, // Tube 3 Digit 4
  101. DigitToPin { address: PCA9685_ADDR_3, pin: 15, }, // Tube 3 Digit 5
  102. DigitToPin { address: PCA9685_ADDR_3, pin: 4, }, // Tube 3 Digit 6
  103. DigitToPin { address: PCA9685_ADDR_3, pin: 1, }, // Tube 3 Digit 7
  104. DigitToPin { address: PCA9685_ADDR_3, pin: 0, }, // Tube 3 Digit 8
  105. DigitToPin { address: PCA9685_ADDR_3, pin: 3, }, // Tube 3 Digit 9
  106. ],
  107. },
  108. ],
  109. dot_address: PCA9685_ADDR_2,
  110. dot_pin: 15,
  111. }
  112. };
  113. #[derive(Debug, PartialEq)]
  114. enum State {
  115. Idle,
  116. Incrementing,
  117. Decrementing,
  118. }
  119. struct Digit {
  120. state: State,
  121. value: u32,
  122. pwm_start: u32,
  123. pwm_end: u32,
  124. fade_duration: Option<u32>,
  125. updated: bool,
  126. }
  127. impl Digit {
  128. const fn default() -> Self {
  129. Self {
  130. state: State::Idle,
  131. value: 0,
  132. pwm_start: 0,
  133. pwm_end: 0,
  134. fade_duration: None,
  135. updated: false,
  136. }
  137. }
  138. }
  139. struct CycleSettings {
  140. last_digit: Option<u32>,
  141. next_digit: u32,
  142. iteration: usize,
  143. last_fade_duration: u32,
  144. }
  145. struct Tube {
  146. digits: [Digit; NUM_DIGITS],
  147. last_digit: Option<u32>,
  148. cycle: Option<CycleSettings>,
  149. }
  150. impl Tube {
  151. const fn default() -> Self {
  152. const DIGIT_INIT: Digit = Digit::default();
  153. Self {
  154. digits: [DIGIT_INIT; 10],
  155. last_digit: None,
  156. cycle: None,
  157. }
  158. }
  159. fn fade_in_out_digit(&mut self, digit: Option<u32>, fade_duration: u32, cycle_cmd: bool) {
  160. // If the tube is in the middle of a cycle sequence and a call comes
  161. // in to update the tube digit (for time), override the last value of
  162. // the cycle sequence with the new digit.
  163. if let Some(ref mut cycle) = self.cycle {
  164. if !cycle_cmd {
  165. cycle.last_digit = digit;
  166. cycle.last_fade_duration = fade_duration;
  167. }
  168. }
  169. // Dont update if actively cycling tube unless cycle_cmd is set
  170. if (self.cycle.is_none() && !cycle_cmd) || cycle_cmd {
  171. // Fade out all digits
  172. for digit in 0..NUM_DIGITS {
  173. self.digits[digit].state = State::Decrementing;
  174. self.digits[digit].fade_duration = Some(fade_duration);
  175. }
  176. // Fade in the specified digit
  177. if let Some(digit) = digit {
  178. self.digits[digit as usize].state = State::Incrementing;
  179. self.digits[digit as usize].fade_duration = Some(fade_duration);
  180. }
  181. self.last_digit = digit;
  182. }
  183. }
  184. }
  185. pub struct Clock {
  186. tubes: [Tube; NUM_TUBES],
  187. dot: Digit,
  188. minute: Option<u32>,
  189. hour: Option<u32>,
  190. }
  191. impl Clock {
  192. pub const fn default() -> Self {
  193. const TUBE_INIT: Tube = Tube::default();
  194. Self {
  195. tubes: [TUBE_INIT; NUM_TUBES],
  196. dot: Digit::default(),
  197. minute: None,
  198. hour: None,
  199. }
  200. }
  201. // Sets a new time to be displayed
  202. pub fn rtc_tick(&mut self, second: u32, minute: u32, hour: u32) {
  203. // Update digit for each tube if value has changed
  204. match self.hour {
  205. Some(prev_hour) if prev_hour / 10 == hour / 10 => {
  206. if hour / 10 == 0 {
  207. self.tubes[0].fade_in_out_digit(None, DIGIT_FADE_DURATION_MS, false);
  208. }
  209. }
  210. _ => {
  211. self.tubes[0].fade_in_out_digit(Some(hour / 10), DIGIT_FADE_DURATION_MS, false);
  212. }
  213. }
  214. match self.hour {
  215. Some(prev_hour) if prev_hour % 10 == hour % 10 => {}
  216. _ => {
  217. self.tubes[1].fade_in_out_digit(Some(hour % 10), DIGIT_FADE_DURATION_MS, false);
  218. }
  219. }
  220. match self.minute {
  221. Some(prev_minute) if prev_minute / 10 == minute / 10 => {}
  222. _ => {
  223. self.tubes[2].fade_in_out_digit(Some(minute / 10), DIGIT_FADE_DURATION_MS, false);
  224. }
  225. }
  226. match self.minute {
  227. Some(prev_minute) if prev_minute % 10 == minute % 10 => {}
  228. _ => {
  229. self.tubes[3].fade_in_out_digit(Some(minute % 10), DIGIT_FADE_DURATION_MS, false);
  230. }
  231. }
  232. #[cfg(test)]
  233. println!(
  234. "RTC tick: {}{}:{}{}",
  235. hour / 10,
  236. hour % 10,
  237. minute / 10,
  238. minute % 10
  239. );
  240. // Set fade direction for dot
  241. self.dot.state = match second % 2 {
  242. 0 => State::Incrementing,
  243. 1 => State::Decrementing,
  244. _ => State::Idle,
  245. };
  246. self.dot.fade_duration = Some(DIGIT_FADE_DURATION_MS);
  247. #[cfg(test)]
  248. println!("RTC tick: dot state is {:?}", self.dot.state);
  249. // Store the last set value for the next update
  250. self.hour = Some(hour);
  251. self.minute = Some(minute);
  252. // Start the display refresh timer to update the display
  253. #[cfg(not(test))]
  254. free(|cs| {
  255. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  256. if let Some(ref mut timer) = timer_ref.deref_mut() {
  257. timer.listen(Event::TimeOut);
  258. }
  259. });
  260. }
  261. // Updates the display with values due to fade in/out
  262. // Returns true if values have changed
  263. pub fn fps_tick(&mut self) -> bool {
  264. let mut pending_refresh: bool = false;
  265. let mut update_fn = |digit: &mut Digit, min: u32, max: u32, steps: u32| {
  266. match digit.state {
  267. State::Incrementing => {
  268. if digit.value >= max {
  269. digit.value = max;
  270. digit.state = State::Idle;
  271. } else {
  272. digit.value = digit.value.saturating_add(steps).clamp(min, max);
  273. digit.updated = true;
  274. pending_refresh = true;
  275. }
  276. }
  277. State::Decrementing => {
  278. if digit.value <= min {
  279. digit.value = min;
  280. digit.state = State::Idle;
  281. } else {
  282. digit.value = digit.value.saturating_sub(steps).clamp(min, max);
  283. digit.updated = true;
  284. pending_refresh = true;
  285. }
  286. }
  287. State::Idle => {
  288. digit.fade_duration = None;
  289. }
  290. };
  291. };
  292. #[cfg(not(test))]
  293. self.tubes.iter_mut().for_each(|tube| {
  294. tube.digits.iter_mut().for_each(|digit| {
  295. if let Some(fade_duration) = digit.fade_duration {
  296. let ticks = fade_duration * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
  297. let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  298. update_fn(digit, DIGIT_MIN_BRIGHTNESS, DIGIT_MAX_BRIGHTNESS, steps);
  299. }
  300. });
  301. });
  302. #[cfg(test)]
  303. for (t, tube) in self.tubes.iter_mut().enumerate() {
  304. for (d, digit) in tube.digits.iter_mut().enumerate() {
  305. if let Some(fade_duration) = digit.fade_duration {
  306. let ticks = fade_duration * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
  307. let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  308. update_fn(digit, DIGIT_MIN_BRIGHTNESS, DIGIT_MAX_BRIGHTNESS, steps);
  309. }
  310. if digit.updated {
  311. println!(
  312. "Refresh tick: updated tube {} digit {} to value {}",
  313. t, d, digit.value
  314. );
  315. }
  316. }
  317. }
  318. // Update dot values
  319. if let Some(fade_duration) = self.dot.fade_duration {
  320. let ticks = fade_duration * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
  321. let steps = ((DOT_MAX_BRIGHTNESS - DOT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  322. update_fn(&mut self.dot, DOT_MIN_BRIGHTNESS, DOT_MAX_BRIGHTNESS, steps);
  323. }
  324. #[cfg(test)]
  325. if self.dot.updated {
  326. println!("Refresh tick: updated dot to value {}", self.dot.value);
  327. }
  328. // Compute actual PWM values if display values have changed
  329. if pending_refresh {
  330. self.distribute_pwm();
  331. }
  332. pending_refresh
  333. }
  334. // Updates the digit displayed during a cycle sequence
  335. // Returns true if the cycle sequence has completed
  336. pub fn cycle_tick(&mut self) -> bool {
  337. let mut cycle_ended = true;
  338. self.tubes.iter_mut().for_each(|tube| {
  339. if let Some(cycle) = tube.cycle.as_mut() {
  340. #[cfg(test)]
  341. println!("Cycle tick: iteration {}", cycle.iteration);
  342. if cycle.iteration > 0 {
  343. let next_digit = cycle.next_digit;
  344. cycle.next_digit = if cycle.next_digit == 9 { 0 } else { cycle.next_digit + 1 };
  345. cycle.iteration = cycle.iteration - 1;
  346. tube.fade_in_out_digit(Some(next_digit), CYCLE_FADE_DURATION_MS, true);
  347. cycle_ended = false;
  348. } else {
  349. let last_digit = cycle.last_digit;
  350. let last_fade = cycle.last_fade_duration;
  351. tube.cycle = None;
  352. tube.fade_in_out_digit(last_digit, last_fade, false);
  353. }
  354. }
  355. });
  356. #[cfg(not(test))]
  357. free(|cs| {
  358. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  359. if let Some(ref mut timer) = timer_ref.deref_mut() {
  360. timer.listen(Event::TimeOut);
  361. }
  362. });
  363. cycle_ended
  364. }
  365. // Start cycling sequence for the given tube to prevent long term damage from cathode poisoning
  366. pub fn cycle_start(&mut self, tube: usize) {
  367. self.tubes[tube].cycle = Some(CycleSettings {
  368. last_digit: self.tubes[tube].last_digit,
  369. next_digit: 0,
  370. iteration: CYCLE_ITERATIONS,
  371. last_fade_duration: DIGIT_FADE_DURATION_MS,
  372. });
  373. }
  374. // Writes updated PWM values to each PCA9685
  375. pub fn write_i2c<T>(&mut self, i2c: &mut T)
  376. where
  377. T: _embedded_hal_blocking_i2c_WriteRead
  378. + _embedded_hal_blocking_i2c_Read
  379. + _embedded_hal_blocking_i2c_Write,
  380. {
  381. for (t, tube) in self.tubes.iter_mut().enumerate() {
  382. for (d, digit) in tube.digits.iter_mut().enumerate() {
  383. if digit.updated {
  384. pca9685::set_digit(
  385. i2c,
  386. TUBE_MAPPING.driver[t].digit[d].address,
  387. TUBE_MAPPING.driver[t].digit[d].pin,
  388. digit.pwm_start,
  389. digit.pwm_end,
  390. );
  391. digit.updated = false;
  392. }
  393. }
  394. }
  395. if self.dot.updated {
  396. pca9685::set_digit(
  397. i2c,
  398. TUBE_MAPPING.dot_address,
  399. TUBE_MAPPING.dot_pin,
  400. self.dot.pwm_start,
  401. self.dot.pwm_end,
  402. );
  403. self.dot.updated = false;
  404. }
  405. }
  406. // In the event that there are multiple PWM outputs at less than 100% duty cycle,
  407. // stagger the start time of each PWM to reduce the switch on surge current. If the
  408. // duty cycle is greater than 100%, distribute the PWM outputs as much as possible
  409. // to keep the current consumption at a minimum.
  410. fn distribute_pwm(&mut self) {
  411. let mut last_pwm: u32 = 0;
  412. let mut incrementing: bool = true;
  413. // Closure to avoid duplicate code
  414. let mut update_digit = |digit: &mut Digit| {
  415. if digit.value == DIGIT_MIN_BRIGHTNESS {
  416. digit.pwm_start = 0;
  417. digit.pwm_end = 0;
  418. } else if digit.value == DIGIT_MAX_BRIGHTNESS {
  419. digit.pwm_start = 0;
  420. digit.pwm_end = DIGIT_MAX_BRIGHTNESS;
  421. } else {
  422. if incrementing {
  423. if last_pwm + digit.value > DIGIT_MAX_BRIGHTNESS {
  424. digit.pwm_start = DIGIT_MAX_BRIGHTNESS - digit.value;
  425. digit.pwm_end = DIGIT_MAX_BRIGHTNESS;
  426. last_pwm = digit.pwm_start;
  427. incrementing = false;
  428. } else {
  429. digit.pwm_start = last_pwm;
  430. digit.pwm_end = digit.pwm_start + digit.value;
  431. last_pwm = digit.pwm_end;
  432. }
  433. } else {
  434. if last_pwm - DIGIT_MIN_BRIGHTNESS < digit.value {
  435. digit.pwm_start = DIGIT_MIN_BRIGHTNESS;
  436. digit.pwm_end = digit.pwm_start + digit.value;
  437. last_pwm = digit.pwm_end;
  438. incrementing = true;
  439. } else {
  440. digit.pwm_end = last_pwm;
  441. digit.pwm_start = digit.pwm_end - digit.value;
  442. last_pwm = digit.pwm_start;
  443. }
  444. }
  445. digit.updated = true;
  446. }
  447. };
  448. #[cfg(not(test))]
  449. self.tubes.iter_mut().for_each(|tube| {
  450. tube.digits.iter_mut().for_each(|digit| {
  451. update_digit(digit);
  452. });
  453. });
  454. #[cfg(test)]
  455. for (t, tube) in self.tubes.iter_mut().enumerate() {
  456. for (d, digit) in tube.digits.iter_mut().enumerate() {
  457. update_digit(digit);
  458. if digit.updated {
  459. println!(
  460. "Distribute PWM: tube {} digit {} start {} end {}",
  461. t, d, digit.pwm_start, digit.pwm_end
  462. );
  463. }
  464. }
  465. }
  466. // The dot is somewhat sensitive to changes to PWM signal, so for the sake
  467. // of consistency, keep the signal on time at the end of the PWM period.
  468. // Otherwise the distribution algorithm will sometimes place the on time at
  469. // the end of the PWM period in one cycle and at the start of the period for
  470. // the next, which results in visual flickers.
  471. // update_digit(&mut self.dot);
  472. self.dot.pwm_start = DIGIT_MAX_BRIGHTNESS - self.dot.value;
  473. self.dot.pwm_end = DIGIT_MAX_BRIGHTNESS;
  474. #[cfg(test)]
  475. println!(
  476. "Distribute PWM: dot start {} end {}",
  477. self.dot.pwm_start, self.dot.pwm_end
  478. );
  479. }
  480. }
  481. #[cfg(test)]
  482. mod test {
  483. use super::*;
  484. use std::println;
  485. #[test]
  486. fn pwm_calc_test() {
  487. let mut clock: Clock = Clock::default();
  488. // Initialize clock to arbitrary time
  489. clock.rtc_tick(10, 8, 12);
  490. // Iterate and print output values for each display refresh tick
  491. for tick in 0..1000 {
  492. println!("\nRefresh tick: {}", tick);
  493. if !clock.fps_tick() {
  494. println!("Refresh halted");
  495. break;
  496. }
  497. // Reset the updated field for each digit
  498. clock.tubes.iter_mut().for_each(|tube| {
  499. tube.digits.iter_mut().for_each(|digit| {
  500. digit.updated = false;
  501. });
  502. });
  503. clock.dot.updated = false;
  504. }
  505. }
  506. #[test]
  507. fn cycle_test() {
  508. let mut clock: Clock = Clock::default();
  509. // Initialize clock to arbitrary time
  510. clock.rtc_tick(00, 27, 8);
  511. // Simulate a cycle refresh sequence on tube 0
  512. clock.tubes[0].cycle = Some(CycleSettings {
  513. last_digit: clock.tubes[0].last_digit,
  514. next_digit: 0,
  515. iteration: CYCLE_ITERATIONS,
  516. last_fade_duration: DIGIT_FADE_DURATION_MS,
  517. });
  518. // Iterate and print debug values for each cycle
  519. for cycle in 0..1000 {
  520. println!("\nCycle tick: {}", cycle);
  521. if clock.cycle_tick() {
  522. println!("Cycle halted");
  523. break;
  524. }
  525. // Iterate and print output values for each display refresh tick
  526. for tick in 0..1000 {
  527. println!("\nRefresh tick: {}", tick);
  528. if !clock.fps_tick() {
  529. println!("Refresh halted");
  530. break;
  531. }
  532. // Reset the updated field for each digit
  533. clock.tubes.iter_mut().for_each(|tube| {
  534. tube.digits.iter_mut().for_each(|digit| {
  535. digit.updated = false;
  536. });
  537. });
  538. clock.dot.updated = false;
  539. }
  540. }
  541. }
  542. }