nixie.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. use core::{cell::RefCell, ops::DerefMut};
  2. use cortex_m::interrupt::{free, Mutex};
  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::{ds3231, 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. if self.digits[digit].value != DIGIT_MIN_BRIGHTNESS {
  174. self.digits[digit].state = State::Decrementing;
  175. self.digits[digit].fade_duration = Some(fade_duration);
  176. }
  177. }
  178. // Fade in the specified digit
  179. if let Some(digit) = digit {
  180. if self.digits[digit as usize].value != DIGIT_MAX_BRIGHTNESS {
  181. self.digits[digit as usize].state = State::Incrementing;
  182. self.digits[digit as usize].fade_duration = Some(fade_duration);
  183. }
  184. }
  185. self.last_digit = digit;
  186. }
  187. }
  188. }
  189. static CLOCK: Mutex<RefCell<Clock>> = Mutex::new(RefCell::new(Clock::default()));
  190. struct Clock {
  191. tubes: [Tube; NUM_TUBES],
  192. dot: Digit,
  193. minute: Option<u32>,
  194. hour: Option<u32>,
  195. }
  196. impl Clock {
  197. const fn default() -> Self {
  198. const TUBE_INIT: Tube = Tube::default();
  199. Self {
  200. tubes: [TUBE_INIT; NUM_TUBES],
  201. dot: Digit::default(),
  202. minute: None,
  203. hour: None,
  204. }
  205. }
  206. // Sets a new time to be displayed
  207. pub fn rtc_tick(&mut self, second: u32, minute: u32, hour: u32) {
  208. // Update digit for each tube if value has changed
  209. match self.hour {
  210. Some(prev_hour) if prev_hour / 10 == hour / 10 => {
  211. if hour / 10 == 0 {
  212. self.tubes[0].fade_in_out_digit(None, DIGIT_FADE_DURATION_MS, false);
  213. }
  214. }
  215. _ => {
  216. self.tubes[0].fade_in_out_digit(Some(hour / 10), DIGIT_FADE_DURATION_MS, false);
  217. }
  218. }
  219. match self.hour {
  220. Some(prev_hour) if prev_hour % 10 == hour % 10 => {}
  221. _ => {
  222. self.tubes[1].fade_in_out_digit(Some(hour % 10), DIGIT_FADE_DURATION_MS, false);
  223. }
  224. }
  225. match self.minute {
  226. Some(prev_minute) if prev_minute / 10 == minute / 10 => {}
  227. _ => {
  228. self.tubes[2].fade_in_out_digit(Some(minute / 10), DIGIT_FADE_DURATION_MS, false);
  229. }
  230. }
  231. match self.minute {
  232. Some(prev_minute) if prev_minute % 10 == minute % 10 => {}
  233. _ => {
  234. self.tubes[3].fade_in_out_digit(Some(minute % 10), DIGIT_FADE_DURATION_MS, false);
  235. }
  236. }
  237. #[cfg(test)]
  238. println!(
  239. "RTC tick: {}{}:{}{}",
  240. hour / 10,
  241. hour % 10,
  242. minute / 10,
  243. minute % 10
  244. );
  245. // Set fade direction for dot
  246. self.dot.state = match second % 2 {
  247. 0 => State::Incrementing,
  248. 1 => State::Decrementing,
  249. _ => State::Idle,
  250. };
  251. self.dot.fade_duration = Some(DIGIT_FADE_DURATION_MS);
  252. #[cfg(test)]
  253. println!("RTC tick: dot state is {:?}", self.dot.state);
  254. // Store the last set value for the next update
  255. self.hour = Some(hour);
  256. self.minute = Some(minute);
  257. // Start the display refresh timer to update the display
  258. #[cfg(not(test))]
  259. free(|cs| {
  260. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  261. if let Some(ref mut timer) = timer_ref.deref_mut() {
  262. timer.listen(Event::TimeOut);
  263. }
  264. });
  265. }
  266. // Updates the display with values due to fade in/out
  267. // Returns true if values have changed
  268. pub fn fps_tick(&mut self) -> bool {
  269. let mut pending_refresh: bool = false;
  270. let mut update_fn = |digit: &mut Digit, min: u32, max: u32, steps: u32| {
  271. match digit.state {
  272. State::Incrementing => {
  273. if digit.value >= max {
  274. digit.value = max;
  275. digit.state = State::Idle;
  276. } else {
  277. digit.value = digit.value.saturating_add(steps).clamp(min, max);
  278. digit.updated = true;
  279. pending_refresh = true;
  280. }
  281. }
  282. State::Decrementing => {
  283. if digit.value <= min {
  284. digit.value = min;
  285. digit.state = State::Idle;
  286. } else {
  287. digit.value = digit.value.saturating_sub(steps).clamp(min, max);
  288. digit.updated = true;
  289. pending_refresh = true;
  290. }
  291. }
  292. State::Idle => {
  293. digit.fade_duration = None;
  294. }
  295. };
  296. };
  297. #[cfg(not(test))]
  298. self.tubes.iter_mut().for_each(|tube| {
  299. tube.digits.iter_mut().for_each(|digit| {
  300. if let Some(fade_duration) = digit.fade_duration {
  301. let ticks = fade_duration * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
  302. let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  303. update_fn(digit, DIGIT_MIN_BRIGHTNESS, DIGIT_MAX_BRIGHTNESS, steps);
  304. }
  305. });
  306. });
  307. #[cfg(test)]
  308. for (t, tube) in self.tubes.iter_mut().enumerate() {
  309. for (d, digit) in tube.digits.iter_mut().enumerate() {
  310. if let Some(fade_duration) = digit.fade_duration {
  311. let ticks = fade_duration * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
  312. let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  313. update_fn(digit, DIGIT_MIN_BRIGHTNESS, DIGIT_MAX_BRIGHTNESS, steps);
  314. }
  315. if digit.updated {
  316. println!(
  317. "Refresh tick: updated tube {} digit {} to value {}",
  318. t, d, digit.value
  319. );
  320. }
  321. }
  322. }
  323. // Update dot values
  324. if let Some(fade_duration) = self.dot.fade_duration {
  325. let ticks = fade_duration * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
  326. let steps = ((DOT_MAX_BRIGHTNESS - DOT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  327. update_fn(&mut self.dot, DOT_MIN_BRIGHTNESS, DOT_MAX_BRIGHTNESS, steps);
  328. }
  329. #[cfg(test)]
  330. if self.dot.updated {
  331. println!("Refresh tick: updated dot to value {}", self.dot.value);
  332. }
  333. // Compute actual PWM values if display values have changed
  334. if pending_refresh {
  335. self.distribute_pwm();
  336. }
  337. pending_refresh
  338. }
  339. // Updates the digit displayed during a cycle sequence
  340. // Returns true if the cycle sequence has completed
  341. pub fn cycle_tick(&mut self) -> bool {
  342. let mut cycle_ended = true;
  343. self.tubes.iter_mut().for_each(|tube| {
  344. if let Some(cycle) = tube.cycle.as_mut() {
  345. #[cfg(test)]
  346. println!("Cycle tick: iteration {}", cycle.iteration);
  347. if cycle.iteration > 0 {
  348. let next_digit = cycle.next_digit;
  349. cycle.next_digit = if cycle.next_digit == 9 { 0 } else { cycle.next_digit + 1 };
  350. cycle.iteration = cycle.iteration - 1;
  351. tube.fade_in_out_digit(Some(next_digit), CYCLE_FADE_DURATION_MS, true);
  352. cycle_ended = false;
  353. } else {
  354. let last_digit = cycle.last_digit;
  355. let last_fade = cycle.last_fade_duration;
  356. tube.cycle = None;
  357. tube.fade_in_out_digit(last_digit, last_fade, false);
  358. }
  359. }
  360. });
  361. #[cfg(not(test))]
  362. free(|cs| {
  363. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  364. if let Some(ref mut timer) = timer_ref.deref_mut() {
  365. timer.listen(Event::TimeOut);
  366. }
  367. });
  368. cycle_ended
  369. }
  370. // Writes updated PWM values to each PCA9685
  371. pub fn write_i2c<T>(&mut self, i2c: &mut T)
  372. where
  373. T: _embedded_hal_blocking_i2c_WriteRead
  374. + _embedded_hal_blocking_i2c_Read
  375. + _embedded_hal_blocking_i2c_Write,
  376. {
  377. for (t, tube) in self.tubes.iter_mut().enumerate() {
  378. for (d, digit) in tube.digits.iter_mut().enumerate() {
  379. if digit.updated {
  380. pca9685::set_digit(
  381. i2c,
  382. TUBE_MAPPING.driver[t].digit[d].address,
  383. TUBE_MAPPING.driver[t].digit[d].pin,
  384. digit.pwm_start,
  385. digit.pwm_end,
  386. );
  387. digit.updated = false;
  388. }
  389. }
  390. }
  391. if self.dot.updated {
  392. pca9685::set_digit(
  393. i2c,
  394. TUBE_MAPPING.dot_address,
  395. TUBE_MAPPING.dot_pin,
  396. self.dot.pwm_start,
  397. self.dot.pwm_end,
  398. );
  399. self.dot.updated = false;
  400. }
  401. }
  402. // In the event that there are multiple PWM outputs at less than 100% duty cycle,
  403. // stagger the start time of each PWM to reduce the switch on surge current. If the
  404. // duty cycle is greater than 100%, distribute the PWM outputs as much as possible
  405. // to keep the current consumption at a minimum.
  406. fn distribute_pwm(&mut self) {
  407. let mut last_pwm: u32 = 0;
  408. let mut incrementing: bool = true;
  409. // Closure to avoid duplicate code
  410. let mut update_digit = |digit: &mut Digit| {
  411. if digit.value == DIGIT_MIN_BRIGHTNESS {
  412. digit.pwm_start = 0;
  413. digit.pwm_end = 0;
  414. } else if digit.value == DIGIT_MAX_BRIGHTNESS {
  415. digit.pwm_start = 0;
  416. digit.pwm_end = DIGIT_MAX_BRIGHTNESS;
  417. } else {
  418. if incrementing {
  419. if last_pwm + digit.value > DIGIT_MAX_BRIGHTNESS {
  420. digit.pwm_start = DIGIT_MAX_BRIGHTNESS - digit.value;
  421. digit.pwm_end = DIGIT_MAX_BRIGHTNESS;
  422. last_pwm = digit.pwm_start;
  423. incrementing = false;
  424. } else {
  425. digit.pwm_start = last_pwm;
  426. digit.pwm_end = digit.pwm_start + digit.value;
  427. last_pwm = digit.pwm_end;
  428. }
  429. } else {
  430. if last_pwm - DIGIT_MIN_BRIGHTNESS < digit.value {
  431. digit.pwm_start = DIGIT_MIN_BRIGHTNESS;
  432. digit.pwm_end = digit.pwm_start + digit.value;
  433. last_pwm = digit.pwm_end;
  434. incrementing = true;
  435. } else {
  436. digit.pwm_end = last_pwm;
  437. digit.pwm_start = digit.pwm_end - digit.value;
  438. last_pwm = digit.pwm_start;
  439. }
  440. }
  441. digit.updated = true;
  442. }
  443. };
  444. #[cfg(not(test))]
  445. self.tubes.iter_mut().for_each(|tube| {
  446. tube.digits.iter_mut().for_each(|digit| {
  447. update_digit(digit);
  448. });
  449. });
  450. #[cfg(test)]
  451. for (t, tube) in self.tubes.iter_mut().enumerate() {
  452. for (d, digit) in tube.digits.iter_mut().enumerate() {
  453. update_digit(digit);
  454. if digit.updated {
  455. println!(
  456. "Distribute PWM: tube {} digit {} start {} end {}",
  457. t, d, digit.pwm_start, digit.pwm_end
  458. );
  459. }
  460. }
  461. }
  462. // The dot is somewhat sensitive to changes to PWM signal, so for the sake
  463. // of consistency, keep the signal on time at the end of the PWM period.
  464. // Otherwise the distribution algorithm will sometimes place the on time at
  465. // the end of the PWM period in one cycle and at the start of the period for
  466. // the next, which results in visual flickers.
  467. // update_digit(&mut self.dot);
  468. self.dot.pwm_start = DIGIT_MAX_BRIGHTNESS - self.dot.value;
  469. self.dot.pwm_end = DIGIT_MAX_BRIGHTNESS;
  470. #[cfg(test)]
  471. println!(
  472. "Distribute PWM: dot start {} end {}",
  473. self.dot.pwm_start, self.dot.pwm_end
  474. );
  475. }
  476. }
  477. // Periodically called to update the display with a new time
  478. pub fn rtc_interrupt<T>(i2c: &mut T)
  479. where
  480. T: _embedded_hal_blocking_i2c_WriteRead
  481. + _embedded_hal_blocking_i2c_Read
  482. + _embedded_hal_blocking_i2c_Write,
  483. {
  484. // Read new time from DS3231
  485. let (second, minute, hour) = ds3231::get_time(DS3231_ADDR, i2c);
  486. let (weekday, day, month, _, _) = ds3231::get_date(DS3231_ADDR, i2c);
  487. // Calculate new values and account for DST
  488. let hour = if ds3231::in_dst(weekday, day, month, hour) { (hour + 1) % 12 } else { hour % 12 };
  489. let hour = if hour == 0 { 12 } else { hour };
  490. free(|cs| {
  491. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  492. let clock = clock_ref.deref_mut();
  493. clock.rtc_tick(second, minute, hour);
  494. });
  495. }
  496. // Periodically called to trigger a display refresh
  497. pub fn fps_interrupt<T>(i2c: &mut T)
  498. where
  499. T: _embedded_hal_blocking_i2c_WriteRead
  500. + _embedded_hal_blocking_i2c_Read
  501. + _embedded_hal_blocking_i2c_Write,
  502. {
  503. free(|cs| {
  504. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  505. let clock = clock_ref.deref_mut();
  506. // Compute updates for non-static digits
  507. let updated = clock.fps_tick();
  508. // Write new values if values have changed, otherwise disable the refresh timer
  509. if updated {
  510. clock.write_i2c(i2c);
  511. free(|cs| {
  512. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  513. if let Some(ref mut timer) = timer_ref.deref_mut() {
  514. timer.clear_interrupt(Event::TimeOut);
  515. }
  516. })
  517. } else {
  518. free(|cs| {
  519. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  520. if let Some(ref mut timer) = timer_ref.deref_mut() {
  521. timer.unlisten(Event::TimeOut);
  522. }
  523. })
  524. }
  525. });
  526. }
  527. // Periodically called during a cycle sequence to increment through digits
  528. // Returns true if the cycle sequence has completed
  529. pub fn cycle_interrupt() -> bool {
  530. free(|cs| {
  531. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  532. let clock = clock_ref.deref_mut();
  533. clock.cycle_tick()
  534. })
  535. }
  536. // Start cycling sequence for the given tube to prevent long term damage from cathode poisoning
  537. pub fn cycle_start(tube: usize) {
  538. free(|cs| {
  539. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  540. let clock = clock_ref.deref_mut();
  541. clock.tubes[tube].cycle = Some(CycleSettings {
  542. last_digit: clock.tubes[tube].last_digit,
  543. next_digit: 0,
  544. iteration: CYCLE_ITERATIONS,
  545. last_fade_duration: DIGIT_FADE_DURATION_MS,
  546. });
  547. });
  548. // Start the timer to cycle through individual digits
  549. free(|cs| {
  550. let mut cycle_timer_ref = super::CYCLE_TIMER.borrow(cs).borrow_mut();
  551. if let Some(ref mut cycle_timer) = cycle_timer_ref.deref_mut() {
  552. cycle_timer.listen(Event::TimeOut);
  553. }
  554. });
  555. }
  556. #[cfg(test)]
  557. mod test {
  558. use super::*;
  559. use std::println;
  560. #[test]
  561. fn pwm_calc_test() {
  562. let mut clock: Clock = Clock::default();
  563. // Initialize clock to arbitrary time
  564. clock.rtc_tick(10, 23, 12);
  565. // Iterate and print output values for each display refresh tick
  566. for tick in 0..1000 {
  567. println!("\nRefresh tick: {}", tick);
  568. if !clock.fps_tick() {
  569. println!("Refresh halted");
  570. break;
  571. }
  572. // Reset the updated field for each digit
  573. clock.tubes.iter_mut().for_each(|tube| {
  574. tube.digits.iter_mut().for_each(|digit| {
  575. digit.updated = false;
  576. });
  577. });
  578. clock.dot.updated = false;
  579. }
  580. }
  581. #[test]
  582. fn cycle_test() {
  583. let mut clock: Clock = Clock::default();
  584. // Initialize clock to arbitrary time
  585. clock.rtc_tick(10, 23, 12);
  586. // Simulate a cycle refresh sequence on tube 0
  587. clock.tubes[0].cycle = Some(CycleSettings {
  588. last_digit: clock.tubes[0].last_digit,
  589. next_digit: 0,
  590. iteration: CYCLE_ITERATIONS,
  591. last_fade_duration: DIGIT_FADE_DURATION_MS,
  592. });
  593. // Iterate and print debug values for each cycle
  594. for cycle in 0..1000 {
  595. println!("\nCycle tick: {}", cycle);
  596. if clock.cycle_tick() {
  597. println!("Cycle halted");
  598. break;
  599. }
  600. // Iterate and print output values for each display refresh tick
  601. for tick in 0..1000 {
  602. println!("\nRefresh tick: {}", tick);
  603. if !clock.fps_tick() {
  604. println!("Refresh halted");
  605. break;
  606. }
  607. // Reset the updated field for each digit
  608. clock.tubes.iter_mut().for_each(|tube| {
  609. tube.digits.iter_mut().for_each(|digit| {
  610. digit.updated = false;
  611. });
  612. });
  613. clock.dot.updated = false;
  614. }
  615. }
  616. }
  617. }