nixie.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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 {
  55. address: PCA9685_ADDR_1,
  56. pin: 8,
  57. }, // Tube 0 Digit 0
  58. DigitToPin {
  59. address: PCA9685_ADDR_1,
  60. pin: 9,
  61. }, // Tube 0 Digit 1
  62. DigitToPin {
  63. address: PCA9685_ADDR_1,
  64. pin: 10,
  65. }, // Tube 0 Digit 2
  66. DigitToPin {
  67. address: PCA9685_ADDR_1,
  68. pin: 12,
  69. }, // Tube 0 Digit 3
  70. DigitToPin {
  71. address: PCA9685_ADDR_1,
  72. pin: 15,
  73. }, // Tube 0 Digit 4
  74. DigitToPin {
  75. address: PCA9685_ADDR_1,
  76. pin: 14,
  77. }, // Tube 0 Digit 5
  78. DigitToPin {
  79. address: PCA9685_ADDR_1,
  80. pin: 11,
  81. }, // Tube 0 Digit 6
  82. DigitToPin {
  83. address: PCA9685_ADDR_1,
  84. pin: 0,
  85. }, // Tube 0 Digit 7
  86. DigitToPin {
  87. address: PCA9685_ADDR_1,
  88. pin: 1,
  89. }, // Tube 0 Digit 8
  90. DigitToPin {
  91. address: PCA9685_ADDR_1,
  92. pin: 13,
  93. }, // Tube 0 Digit 9
  94. ],
  95. },
  96. PwmDriver {
  97. digit: [
  98. DigitToPin {
  99. address: PCA9685_ADDR_1,
  100. pin: 5,
  101. }, // Tube 1 Digit 0
  102. DigitToPin {
  103. address: PCA9685_ADDR_1,
  104. pin: 6,
  105. }, // Tube 1 Digit 1
  106. DigitToPin {
  107. address: PCA9685_ADDR_1,
  108. pin: 7,
  109. }, // Tube 1 Digit 2
  110. DigitToPin {
  111. address: PCA9685_ADDR_1,
  112. pin: 2,
  113. }, // Tube 1 Digit 3
  114. DigitToPin {
  115. address: PCA9685_ADDR_2,
  116. pin: 4,
  117. }, // Tube 1 Digit 4
  118. DigitToPin {
  119. address: PCA9685_ADDR_2,
  120. pin: 1,
  121. }, // Tube 1 Digit 5
  122. DigitToPin {
  123. address: PCA9685_ADDR_1,
  124. pin: 4,
  125. }, // Tube 1 Digit 6
  126. DigitToPin {
  127. address: PCA9685_ADDR_2,
  128. pin: 2,
  129. }, // Tube 1 Digit 7
  130. DigitToPin {
  131. address: PCA9685_ADDR_2,
  132. pin: 3,
  133. }, // Tube 1 Digit 8
  134. DigitToPin {
  135. address: PCA9685_ADDR_1,
  136. pin: 3,
  137. }, // Tube 1 Digit 9
  138. ],
  139. },
  140. PwmDriver {
  141. digit: [
  142. DigitToPin {
  143. address: PCA9685_ADDR_3,
  144. pin: 8,
  145. }, // Tube 2 Digit 0
  146. DigitToPin {
  147. address: PCA9685_ADDR_3,
  148. pin: 9,
  149. }, // Tube 2 Digit 1
  150. DigitToPin {
  151. address: PCA9685_ADDR_3,
  152. pin: 10,
  153. }, // Tube 2 Digit 2
  154. DigitToPin {
  155. address: PCA9685_ADDR_3,
  156. pin: 12,
  157. }, // Tube 2 Digit 3
  158. DigitToPin {
  159. address: PCA9685_ADDR_2,
  160. pin: 12,
  161. }, // Tube 2 Digit 4
  162. DigitToPin {
  163. address: PCA9685_ADDR_2,
  164. pin: 13,
  165. }, // Tube 2 Digit 5
  166. DigitToPin {
  167. address: PCA9685_ADDR_3,
  168. pin: 11,
  169. }, // Tube 2 Digit 6
  170. DigitToPin {
  171. address: PCA9685_ADDR_2,
  172. pin: 14,
  173. }, // Tube 2 Digit 7
  174. DigitToPin {
  175. address: PCA9685_ADDR_2,
  176. pin: 11,
  177. }, // Tube 2 Digit 8
  178. DigitToPin {
  179. address: PCA9685_ADDR_3,
  180. pin: 13,
  181. }, // Tube 2 Digit 9
  182. ],
  183. },
  184. PwmDriver {
  185. digit: [
  186. DigitToPin {
  187. address: PCA9685_ADDR_3,
  188. pin: 5,
  189. }, // Tube 3 Digit 0
  190. DigitToPin {
  191. address: PCA9685_ADDR_3,
  192. pin: 6,
  193. }, // Tube 3 Digit 1
  194. DigitToPin {
  195. address: PCA9685_ADDR_3,
  196. pin: 7,
  197. }, // Tube 3 Digit 2
  198. DigitToPin {
  199. address: PCA9685_ADDR_3,
  200. pin: 2,
  201. }, // Tube 3 Digit 3
  202. DigitToPin {
  203. address: PCA9685_ADDR_3,
  204. pin: 14,
  205. }, // Tube 3 Digit 4
  206. DigitToPin {
  207. address: PCA9685_ADDR_3,
  208. pin: 15,
  209. }, // Tube 3 Digit 5
  210. DigitToPin {
  211. address: PCA9685_ADDR_3,
  212. pin: 4,
  213. }, // Tube 3 Digit 6
  214. DigitToPin {
  215. address: PCA9685_ADDR_3,
  216. pin: 1,
  217. }, // Tube 3 Digit 7
  218. DigitToPin {
  219. address: PCA9685_ADDR_3,
  220. pin: 0,
  221. }, // Tube 3 Digit 8
  222. DigitToPin {
  223. address: PCA9685_ADDR_3,
  224. pin: 3,
  225. }, // Tube 3 Digit 9
  226. ],
  227. },
  228. ],
  229. dot_address: PCA9685_ADDR_2,
  230. dot_pin: 15,
  231. }
  232. };
  233. #[derive(Debug, PartialEq)]
  234. enum State {
  235. Idle,
  236. Incrementing,
  237. Decrementing,
  238. }
  239. struct Digit {
  240. state: State,
  241. value: u32,
  242. pwm_start: u32,
  243. pwm_end: u32,
  244. fade_duration: Option<u32>,
  245. updated: bool,
  246. }
  247. impl Digit {
  248. const fn default() -> Self {
  249. Self {
  250. state: State::Idle,
  251. value: 0,
  252. pwm_start: 0,
  253. pwm_end: 0,
  254. fade_duration: None,
  255. updated: false,
  256. }
  257. }
  258. }
  259. struct CycleSettings {
  260. last_digit: Option<u32>,
  261. next_digit: u32,
  262. iteration: usize,
  263. last_fade_duration: u32,
  264. }
  265. struct Tube {
  266. digits: [Digit; NUM_DIGITS],
  267. last_digit: Option<u32>,
  268. cycle: Option<CycleSettings>,
  269. }
  270. impl Tube {
  271. const fn default() -> Self {
  272. const DIGIT_INIT: Digit = Digit::default();
  273. Self {
  274. digits: [DIGIT_INIT; 10],
  275. last_digit: None,
  276. cycle: None,
  277. }
  278. }
  279. fn fade_in_out_digit(
  280. &mut self,
  281. digit: Option<u32>,
  282. fade_duration: u32,
  283. cycle_cmd: bool,
  284. ) {
  285. // If the tube is in the middle of a cycle sequence and a call comes
  286. // in to update the tube digit (for time), override the last value of
  287. // the cycle sequence with the new digit.
  288. if let Some(ref mut cycle) = self.cycle {
  289. if !cycle_cmd {
  290. cycle.last_digit = digit;
  291. cycle.last_fade_duration = fade_duration;
  292. }
  293. }
  294. // Dont update if actively cycling tube unless cycle_cmd is set
  295. if (self.cycle.is_none() && !cycle_cmd) || cycle_cmd {
  296. // Fade out all digits
  297. for digit in 0..NUM_DIGITS {
  298. if self.digits[digit].value != DIGIT_MIN_BRIGHTNESS {
  299. self.digits[digit].state = State::Decrementing;
  300. self.digits[digit].fade_duration = Some(fade_duration);
  301. }
  302. }
  303. // Fade in the specified digit
  304. if let Some(digit) = digit {
  305. if self.digits[digit as usize].value != DIGIT_MAX_BRIGHTNESS {
  306. self.digits[digit as usize].state = State::Incrementing;
  307. self.digits[digit as usize].fade_duration = Some(fade_duration);
  308. }
  309. }
  310. self.last_digit = digit;
  311. }
  312. }
  313. }
  314. static CLOCK: Mutex<RefCell<Clock>> = Mutex::new(RefCell::new(Clock::default()));
  315. struct Clock {
  316. tubes: [Tube; NUM_TUBES],
  317. dot: Digit,
  318. minute: Option<u32>,
  319. hour: Option<u32>,
  320. }
  321. impl Clock {
  322. const fn default() -> Self {
  323. const TUBE_INIT: Tube = Tube::default();
  324. Self {
  325. tubes: [TUBE_INIT; NUM_TUBES],
  326. dot: Digit::default(),
  327. minute: None,
  328. hour: None,
  329. }
  330. }
  331. pub fn rtc_tick(&mut self, second: u32, minute: u32, hour: u32) {
  332. match self.hour {
  333. Some(prev_hour) if prev_hour / 10 == hour / 10 => {
  334. if hour / 10 == 0 {
  335. self.tubes[0].fade_in_out_digit(None, DIGIT_FADE_DURATION_MS, false);
  336. }
  337. }
  338. _ => {
  339. self.tubes[0].fade_in_out_digit(Some(hour / 10), DIGIT_FADE_DURATION_MS, false);
  340. }
  341. }
  342. match self.hour {
  343. Some(prev_hour) if prev_hour % 10 == hour % 10 => {}
  344. _ => {
  345. self.tubes[1].fade_in_out_digit(Some(hour % 10), DIGIT_FADE_DURATION_MS, false);
  346. }
  347. }
  348. match self.minute {
  349. Some(prev_minute) if prev_minute / 10 == minute / 10 => {}
  350. _ => {
  351. self.tubes[2].fade_in_out_digit(Some(minute / 10), DIGIT_FADE_DURATION_MS, false);
  352. }
  353. }
  354. match self.minute {
  355. Some(prev_minute) if prev_minute % 10 == minute % 10 => {}
  356. _ => {
  357. self.tubes[3].fade_in_out_digit(Some(minute % 10), DIGIT_FADE_DURATION_MS, false);
  358. }
  359. }
  360. #[cfg(test)]
  361. println!(
  362. "RTC tick: {}{}:{}{}",
  363. hour / 10,
  364. hour % 10,
  365. minute / 10,
  366. minute % 10
  367. );
  368. self.dot.state = match second % 2 {
  369. 0 => State::Incrementing,
  370. 1 => State::Decrementing,
  371. _ => State::Idle,
  372. };
  373. self.dot.fade_duration = Some(DIGIT_FADE_DURATION_MS);
  374. #[cfg(test)]
  375. println!("RTC tick: dot state is {:?}", self.dot.state);
  376. self.hour = Some(hour);
  377. self.minute = Some(minute);
  378. #[cfg(not(test))]
  379. free(|cs| {
  380. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  381. if let Some(ref mut timer) = timer_ref.deref_mut() {
  382. timer.listen(Event::TimeOut);
  383. }
  384. });
  385. }
  386. pub fn fps_tick(&mut self) -> bool {
  387. let mut pending_refresh: bool = false;
  388. let mut update_fn = |digit: &mut Digit, min: u32, max: u32, steps: u32| {
  389. match digit.state {
  390. State::Incrementing => {
  391. if digit.value >= max {
  392. digit.value = max;
  393. digit.state = State::Idle;
  394. } else {
  395. digit.value = digit.value.saturating_add(steps).clamp(min, max);
  396. digit.updated = true;
  397. pending_refresh = true;
  398. }
  399. },
  400. State::Decrementing => {
  401. if digit.value <= min {
  402. digit.value = min;
  403. digit.state = State::Idle;
  404. } else {
  405. digit.value = digit.value.saturating_sub(steps).clamp(min, max);
  406. digit.updated = true;
  407. pending_refresh = true;
  408. }
  409. },
  410. State::Idle => {
  411. digit.fade_duration = None;
  412. },
  413. };
  414. };
  415. #[cfg(not(test))]
  416. self.tubes.iter_mut().for_each(|tube| {
  417. tube.digits.iter_mut().for_each(|digit| {
  418. if let Some(fade_duration) = digit.fade_duration {
  419. let ticks = fade_duration * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
  420. let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  421. update_fn(digit, DIGIT_MIN_BRIGHTNESS, DIGIT_MAX_BRIGHTNESS, steps);
  422. }
  423. });
  424. });
  425. #[cfg(test)]
  426. for (t, tube) in self.tubes.iter_mut().enumerate() {
  427. for (d, digit) in tube.digits.iter_mut().enumerate() {
  428. if let Some(fade_duration) = digit.fade_duration {
  429. let ticks = fade_duration * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
  430. let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  431. update_fn(digit, DIGIT_MIN_BRIGHTNESS, DIGIT_MAX_BRIGHTNESS, steps);
  432. }
  433. if digit.updated {
  434. println!(
  435. "Refresh tick: updated tube {} digit {} to value {}",
  436. t, d, digit.value
  437. );
  438. }
  439. }
  440. }
  441. // Handle dot
  442. if let Some(fade_duration) = self.dot.fade_duration {
  443. let ticks = fade_duration * 1000 / (1000 / DISPLAY_REFRESH_FPS * 1000);
  444. let steps = ((DOT_MAX_BRIGHTNESS - DOT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  445. update_fn(&mut self.dot, DOT_MIN_BRIGHTNESS, DOT_MAX_BRIGHTNESS, steps);
  446. }
  447. #[cfg(test)]
  448. if self.dot.updated {
  449. println!("Refresh tick: updated dot to value {}", self.dot.value);
  450. }
  451. if pending_refresh {
  452. self.distribute_pwm();
  453. }
  454. pending_refresh
  455. }
  456. pub fn cycle_tick(&mut self) -> bool {
  457. let mut cycle_ended = true;
  458. self.tubes.iter_mut().for_each(|tube| {
  459. if let Some(cycle) = tube.cycle.as_mut() {
  460. #[cfg(test)]
  461. println!("Cycle tick: iteration {}", cycle.iteration);
  462. if cycle.iteration > 0 {
  463. let next_digit = cycle.next_digit;
  464. cycle.next_digit = if cycle.next_digit == 9 { 0 } else { cycle.next_digit + 1 };
  465. cycle.iteration = cycle.iteration - 1;
  466. tube.fade_in_out_digit(Some(next_digit), CYCLE_FADE_DURATION_MS, true);
  467. cycle_ended = false;
  468. } else {
  469. let last_digit = cycle.last_digit;
  470. let last_fade = cycle.last_fade_duration;
  471. tube.cycle = None;
  472. tube.fade_in_out_digit(last_digit, last_fade, false);
  473. }
  474. }
  475. });
  476. #[cfg(not(test))]
  477. free(|cs| {
  478. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  479. if let Some(ref mut timer) = timer_ref.deref_mut() {
  480. timer.listen(Event::TimeOut);
  481. }
  482. });
  483. cycle_ended
  484. }
  485. pub fn write_i2c<T>(&mut self, i2c: &mut T)
  486. where
  487. T: _embedded_hal_blocking_i2c_WriteRead
  488. + _embedded_hal_blocking_i2c_Read
  489. + _embedded_hal_blocking_i2c_Write,
  490. {
  491. for (t, tube) in self.tubes.iter_mut().enumerate() {
  492. for (d, digit) in tube.digits.iter_mut().enumerate() {
  493. if digit.updated {
  494. pca9685::set_digit(
  495. i2c,
  496. TUBE_MAPPING.driver[t].digit[d].address,
  497. TUBE_MAPPING.driver[t].digit[d].pin,
  498. digit.pwm_start,
  499. digit.pwm_end,
  500. );
  501. digit.updated = false;
  502. }
  503. }
  504. }
  505. if self.dot.updated {
  506. pca9685::set_digit(
  507. i2c,
  508. TUBE_MAPPING.dot_address,
  509. TUBE_MAPPING.dot_pin,
  510. self.dot.pwm_start,
  511. self.dot.pwm_end,
  512. );
  513. self.dot.updated = false;
  514. }
  515. }
  516. // In the event that there are multiple PWM outputs at less than 100% duty cycle,
  517. // stagger the start time of each PWM to reduce the switch on surge current. If the
  518. // duty cycle is greater than 100%, distribute the PWM outputs as much as possible
  519. // to keep the current consumption at a minimum.
  520. fn distribute_pwm(&mut self) {
  521. let mut last_pwm: u32 = 0;
  522. let mut incrementing: bool = true;
  523. // Closure to avoid duplicate code
  524. let mut update_digit = |digit: &mut Digit| {
  525. if digit.value == DIGIT_MIN_BRIGHTNESS {
  526. digit.pwm_start = 0;
  527. digit.pwm_end = 0;
  528. } else if digit.value == DIGIT_MAX_BRIGHTNESS {
  529. digit.pwm_start = 0;
  530. digit.pwm_end = DIGIT_MAX_BRIGHTNESS;
  531. } else {
  532. if incrementing {
  533. if last_pwm + digit.value > DIGIT_MAX_BRIGHTNESS {
  534. digit.pwm_start = DIGIT_MAX_BRIGHTNESS - digit.value;
  535. digit.pwm_end = DIGIT_MAX_BRIGHTNESS;
  536. last_pwm = digit.pwm_start;
  537. incrementing = false;
  538. } else {
  539. digit.pwm_start = last_pwm;
  540. digit.pwm_end = digit.pwm_start + digit.value;
  541. last_pwm = digit.pwm_end;
  542. }
  543. } else {
  544. if last_pwm - DIGIT_MIN_BRIGHTNESS < digit.value {
  545. digit.pwm_start = DIGIT_MIN_BRIGHTNESS;
  546. digit.pwm_end = digit.pwm_start + digit.value;
  547. last_pwm = digit.pwm_end;
  548. incrementing = true;
  549. } else {
  550. digit.pwm_end = last_pwm;
  551. digit.pwm_start = digit.pwm_end - digit.value;
  552. last_pwm = digit.pwm_start;
  553. }
  554. }
  555. digit.updated = true;
  556. }
  557. };
  558. #[cfg(not(test))]
  559. self.tubes.iter_mut().for_each(|tube| {
  560. tube.digits.iter_mut().for_each(|digit| {
  561. update_digit(digit);
  562. });
  563. });
  564. #[cfg(test)]
  565. for (t, tube) in self.tubes.iter_mut().enumerate() {
  566. for (d, digit) in tube.digits.iter_mut().enumerate() {
  567. update_digit(digit);
  568. if digit.updated {
  569. println!(
  570. "Distribute PWM: tube {} digit {} start {} end {}",
  571. t, d, digit.pwm_start, digit.pwm_end
  572. );
  573. }
  574. }
  575. }
  576. update_digit(&mut self.dot);
  577. #[cfg(test)]
  578. println!(
  579. "Distribute PWM: dot start {} end {}",
  580. self.dot.pwm_start, self.dot.pwm_end
  581. );
  582. }
  583. }
  584. pub fn rtc_interrupt<T>(i2c: &mut T)
  585. where
  586. T: _embedded_hal_blocking_i2c_WriteRead
  587. + _embedded_hal_blocking_i2c_Read
  588. + _embedded_hal_blocking_i2c_Write,
  589. {
  590. let (second, minute, hour) = ds3231::get_time(DS3231_ADDR, i2c);
  591. let (weekday, day, month, _, _) = ds3231::get_date(DS3231_ADDR, i2c);
  592. let hour = if ds3231::in_dst(weekday, day, month, hour) {
  593. (hour + 1) % 12
  594. } else {
  595. hour % 12
  596. };
  597. let hour = if hour == 0 { 12 } else { hour };
  598. free(|cs| {
  599. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  600. let clock = clock_ref.deref_mut();
  601. clock.rtc_tick(second, minute, hour);
  602. });
  603. }
  604. // This function is called by an interrupt that is triggered every
  605. // DISPLAY_REFRESH_FPS to update the display with a new brightness value.
  606. pub fn fps_interrupt<T>(i2c: &mut T)
  607. where
  608. T: _embedded_hal_blocking_i2c_WriteRead
  609. + _embedded_hal_blocking_i2c_Read
  610. + _embedded_hal_blocking_i2c_Write,
  611. {
  612. free(|cs| {
  613. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  614. let clock = clock_ref.deref_mut();
  615. let updated = clock.fps_tick();
  616. if updated {
  617. clock.write_i2c(i2c);
  618. free(|cs| {
  619. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  620. if let Some(ref mut timer) = timer_ref.deref_mut() {
  621. timer.clear_interrupt(Event::TimeOut);
  622. }
  623. })
  624. } else {
  625. free(|cs| {
  626. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  627. if let Some(ref mut timer) = timer_ref.deref_mut() {
  628. timer.unlisten(Event::TimeOut);
  629. }
  630. })
  631. }
  632. });
  633. }
  634. // This function is called by an interrupt that is triggered every
  635. // DIGIT_CYCLE_FADE_DURATION_HZ to update the digit being refreshed.
  636. pub fn cycle_interrupt() {
  637. free(|cs| {
  638. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  639. let clock = clock_ref.deref_mut();
  640. let cycle_ended = clock.cycle_tick();
  641. free(|cs| {
  642. let mut cycle_timer_ref = super::CYCLE_TIMER.borrow(cs).borrow_mut();
  643. if let Some(ref mut cycle_timer) = cycle_timer_ref.deref_mut() {
  644. if cycle_ended {
  645. cycle_timer.unlisten(Event::TimeOut);
  646. } else {
  647. cycle_timer.clear_interrupt(Event::TimeOut);
  648. }
  649. }
  650. });
  651. });
  652. }
  653. // This function is called to start cycling through all digits for a
  654. // tube to prevent damage to the nixie tube due to cathode poisoning.
  655. pub fn cycle_start(tube: usize) {
  656. free(|cs| {
  657. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  658. let clock = clock_ref.deref_mut();
  659. clock.tubes[tube].cycle = Some(CycleSettings {
  660. last_digit: clock.tubes[tube].last_digit,
  661. next_digit: 0,
  662. iteration: CYCLE_ITERATIONS,
  663. last_fade_duration: DIGIT_FADE_DURATION_MS,
  664. });
  665. });
  666. // Start the timer to cycle through all digits
  667. free(|cs| {
  668. let mut cycle_timer_ref = super::CYCLE_TIMER.borrow(cs).borrow_mut();
  669. if let Some(ref mut cycle_timer) = cycle_timer_ref.deref_mut() {
  670. cycle_timer.listen(Event::TimeOut);
  671. }
  672. });
  673. }
  674. #[cfg(test)]
  675. mod test {
  676. use super::*;
  677. use std::println;
  678. #[test]
  679. fn pwm_calc_test() {
  680. let mut clock: Clock = Clock::default();
  681. clock.rtc_tick(10, 23, 12);
  682. for tick in 0..1005 {
  683. println!("\nRefresh tick: {}", tick);
  684. if !clock.fps_tick() {
  685. println!("Refresh halted");
  686. break;
  687. }
  688. }
  689. }
  690. }