nixie.rs 24 KB

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