nixie.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. use core::{cell::RefCell, ops::DerefMut, panic};
  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 REFRESH_RATE_HZ: u32 = 1000;
  21. pub const DIGIT_FADE_DURATION_MS: u32 = 1_000_000;
  22. pub const CYCLE_FADE_DURATION_MS: u32 = 200_000;
  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. }
  264. struct Tube {
  265. digits: [Digit; NUM_DIGITS],
  266. last_active_digit: Option<u32>,
  267. cycle: Option<CycleSettings>,
  268. }
  269. impl Tube {
  270. const fn default() -> Self {
  271. const DIGIT_INIT: Digit = Digit::default();
  272. Self {
  273. digits: [DIGIT_INIT; 10],
  274. last_active_digit: None,
  275. cycle: None,
  276. }
  277. }
  278. fn fade_in_out_digit(
  279. &mut self,
  280. digit: Option<u32>,
  281. fade_duration: u32,
  282. cycle_cmd: bool,
  283. ) {
  284. // If the tube is in the middle of a cycle sequence and a call comes
  285. // in to update the tube digit (for time), override the last value of
  286. // the cycle sequence with the new digit.
  287. if let Some(ref mut cycle) = self.cycle {
  288. if !cycle_cmd {
  289. cycle.last_digit = digit;
  290. }
  291. }
  292. // Dont update if actively cycling tube unless cycle_cmd is set
  293. if (self.cycle.is_none() && !cycle_cmd) || cycle_cmd {
  294. // Fade out all digits
  295. for digit in 0..NUM_DIGITS {
  296. if self.digits[digit].value != DIGIT_MIN_BRIGHTNESS {
  297. self.digits[digit].state = State::Decrementing;
  298. self.digits[digit].fade_duration = Some(fade_duration);
  299. }
  300. }
  301. // Fade in the specified digit
  302. if let Some(digit) = digit {
  303. if self.digits[digit as usize].value != DIGIT_MAX_BRIGHTNESS {
  304. self.digits[digit as usize].state = State::Incrementing;
  305. self.digits[digit as usize].fade_duration = Some(fade_duration);
  306. }
  307. }
  308. self.last_active_digit = digit;
  309. }
  310. }
  311. }
  312. static CLOCK: Mutex<RefCell<Clock>> = Mutex::new(RefCell::new(Clock::default()));
  313. struct Clock {
  314. tubes: [Tube; NUM_TUBES],
  315. dot: Digit,
  316. minute: Option<u32>,
  317. hour: Option<u32>,
  318. }
  319. impl Clock {
  320. const fn default() -> Self {
  321. const TUBE_INIT: Tube = Tube::default();
  322. Self {
  323. tubes: [TUBE_INIT; NUM_TUBES],
  324. dot: Digit::default(),
  325. minute: None,
  326. hour: None,
  327. }
  328. }
  329. pub fn rtc_tick(&mut self, second: u32, minute: u32, hour: u32) {
  330. match self.hour {
  331. Some(prev_hour) if prev_hour / 10 == hour / 10 => {
  332. if hour / 10 == 0 {
  333. self.tubes[0].fade_in_out_digit(None, DIGIT_FADE_DURATION_MS, false);
  334. }
  335. }
  336. _ => {
  337. self.tubes[0].fade_in_out_digit(Some(hour / 10), DIGIT_FADE_DURATION_MS, false);
  338. }
  339. }
  340. match self.hour {
  341. Some(prev_hour) if prev_hour % 10 == hour % 10 => {}
  342. _ => {
  343. self.tubes[1].fade_in_out_digit(Some(hour % 10), DIGIT_FADE_DURATION_MS, false);
  344. }
  345. }
  346. match self.minute {
  347. Some(prev_minute) if prev_minute / 10 == minute / 10 => {}
  348. _ => {
  349. self.tubes[2].fade_in_out_digit(Some(minute / 10), DIGIT_FADE_DURATION_MS, false);
  350. }
  351. }
  352. match self.minute {
  353. Some(prev_minute) if prev_minute % 10 == minute % 10 => {}
  354. _ => {
  355. self.tubes[3].fade_in_out_digit(Some(minute % 10), DIGIT_FADE_DURATION_MS, false);
  356. }
  357. }
  358. #[cfg(test)]
  359. println!(
  360. "RTC tick: {}{}:{}{}",
  361. hour / 10,
  362. hour % 10,
  363. minute / 10,
  364. minute % 10
  365. );
  366. self.dot.state = match second % 2 {
  367. 0 => State::Incrementing,
  368. 1 => State::Decrementing,
  369. _ => State::Idle,
  370. };
  371. self.dot.fade_duration = Some(DIGIT_FADE_DURATION_MS);
  372. #[cfg(test)]
  373. println!("RTC tick: dot state is {:?}", self.dot.state);
  374. self.hour = Some(hour);
  375. self.minute = Some(minute);
  376. #[cfg(not(test))]
  377. free(|cs| {
  378. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  379. if let Some(ref mut timer) = timer_ref.deref_mut() {
  380. timer.listen(Event::TimeOut);
  381. }
  382. });
  383. }
  384. pub fn fps_tick(&mut self) -> bool {
  385. let mut pending_refresh: bool = false;
  386. let mut update_fn = |digit: &mut Digit, min: u32, max: u32, steps: u32| {
  387. match digit.state {
  388. State::Incrementing => {
  389. if digit.value >= max {
  390. digit.value = max;
  391. digit.state = State::Idle;
  392. } else {
  393. digit.value = digit.value.saturating_add(steps).clamp(min, max);
  394. digit.updated = true;
  395. pending_refresh = true;
  396. }
  397. },
  398. State::Decrementing => {
  399. if digit.value <= min {
  400. digit.value = min;
  401. digit.state = State::Idle;
  402. } else {
  403. digit.value = digit.value.saturating_sub(steps).clamp(min, max);
  404. digit.updated = true;
  405. pending_refresh = true;
  406. }
  407. },
  408. State::Idle => {
  409. digit.fade_duration = None;
  410. },
  411. };
  412. };
  413. #[cfg(not(test))]
  414. self.tubes.iter_mut().for_each(|tube| {
  415. tube.digits.iter_mut().for_each(|digit| {
  416. if let Some(fade_duration) = digit.fade_duration {
  417. let ticks = fade_duration / REFRESH_RATE_HZ;
  418. let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  419. update_fn(digit, DIGIT_MIN_BRIGHTNESS, DIGIT_MAX_BRIGHTNESS, steps);
  420. }
  421. });
  422. });
  423. #[cfg(test)]
  424. for (t, tube) in self.tubes.iter_mut().enumerate() {
  425. for (d, digit) in tube.digits.iter_mut().enumerate() {
  426. if let Some(fade_duration) = digit.fade_duration {
  427. let ticks = fade_duration / REFRESH_RATE_HZ;
  428. let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  429. update_fn(digit, DIGIT_MIN_BRIGHTNESS, DIGIT_MAX_BRIGHTNESS, steps);
  430. }
  431. if digit.updated {
  432. println!(
  433. "Refresh tick: updated tube {} digit {} to value {}",
  434. t, d, digit.value
  435. );
  436. }
  437. }
  438. }
  439. // Handle dot
  440. if let Some(fade_duration) = self.dot.fade_duration {
  441. let ticks = fade_duration / REFRESH_RATE_HZ;
  442. let steps = ((DOT_MAX_BRIGHTNESS - DOT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  443. update_fn(&mut self.dot, DOT_MIN_BRIGHTNESS, DOT_MAX_BRIGHTNESS, steps);
  444. }
  445. #[cfg(test)]
  446. if self.dot.updated {
  447. println!("Refresh tick: updated dot to value {}", self.dot.value);
  448. }
  449. if pending_refresh {
  450. self.distribute_pwm();
  451. }
  452. pending_refresh
  453. }
  454. pub fn cycle_tick(&mut self) -> bool {
  455. let mut cycle_ended = true;
  456. self.tubes.iter_mut().for_each(|tube| {
  457. if let Some(cycle) = tube.cycle.as_mut() {
  458. #[cfg(test)]
  459. println!("Cycle tick: iteration {}", cycle.iteration);
  460. if cycle.iteration > 0 {
  461. let next_digit = cycle.next_digit;
  462. cycle.next_digit = if cycle.next_digit == 9 { 0 } else { cycle.next_digit + 1 };
  463. cycle.iteration = cycle.iteration - 1;
  464. tube.fade_in_out_digit(Some(next_digit), CYCLE_FADE_DURATION_MS, true);
  465. cycle_ended = false;
  466. } else {
  467. let last_digit = cycle.last_digit;
  468. tube.cycle = None;
  469. tube.fade_in_out_digit(last_digit, CYCLE_FADE_DURATION_MS, true);
  470. }
  471. }
  472. });
  473. // self.tubes.iter_mut().for_each(|tube| {
  474. // tube.fade_in_out_digit(Some(0), CYCLE_FADE_DURATION_US, true);
  475. // });
  476. // self.tubes[3].fade_in_out_digit(Some(0), CYCLE_FADE_DURATION_US, true);
  477. #[cfg(not(test))]
  478. free(|cs| {
  479. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  480. if let Some(ref mut timer) = timer_ref.deref_mut() {
  481. timer.listen(Event::TimeOut);
  482. }
  483. });
  484. cycle_ended
  485. }
  486. pub fn write_i2c<T>(&mut self, i2c: &mut T)
  487. where
  488. T: _embedded_hal_blocking_i2c_WriteRead
  489. + _embedded_hal_blocking_i2c_Read
  490. + _embedded_hal_blocking_i2c_Write,
  491. {
  492. for (t, tube) in self.tubes.iter_mut().enumerate() {
  493. for (d, digit) in tube.digits.iter_mut().enumerate() {
  494. if digit.updated {
  495. pca9685::set_digit(
  496. i2c,
  497. TUBE_MAPPING.driver[t].digit[d].address,
  498. TUBE_MAPPING.driver[t].digit[d].pin,
  499. digit.pwm_start,
  500. digit.pwm_end,
  501. );
  502. digit.updated = false;
  503. }
  504. }
  505. }
  506. if self.dot.updated {
  507. pca9685::set_digit(
  508. i2c,
  509. TUBE_MAPPING.dot_address,
  510. TUBE_MAPPING.dot_pin,
  511. self.dot.pwm_start,
  512. self.dot.pwm_end,
  513. );
  514. self.dot.updated = false;
  515. }
  516. }
  517. // In the event that there are multiple PWM outputs at less than 100% duty cycle,
  518. // stagger the start time of each PWM to reduce the switch on surge current. If the
  519. // duty cycle is greater than 100%, distribute the PWM outputs as much as possible
  520. // to keep the current consumption at a minimum.
  521. fn distribute_pwm(&mut self) {
  522. let mut last_pwm: u32 = 0;
  523. let mut incrementing: bool = true;
  524. // Closure to avoid duplicate code
  525. let mut update_digit = |digit: &mut Digit| {
  526. if digit.value == DIGIT_MIN_BRIGHTNESS {
  527. digit.pwm_start = 0;
  528. digit.pwm_end = 0;
  529. } else if digit.value == DIGIT_MAX_BRIGHTNESS {
  530. digit.pwm_start = 0;
  531. digit.pwm_end = DIGIT_MAX_BRIGHTNESS;
  532. } else {
  533. if incrementing {
  534. if last_pwm + digit.value > DIGIT_MAX_BRIGHTNESS {
  535. digit.pwm_start = DIGIT_MAX_BRIGHTNESS - digit.value;
  536. digit.pwm_end = DIGIT_MAX_BRIGHTNESS;
  537. last_pwm = digit.pwm_start;
  538. incrementing = false;
  539. } else {
  540. digit.pwm_start = last_pwm;
  541. digit.pwm_end = digit.pwm_start + digit.value;
  542. last_pwm = digit.pwm_end;
  543. }
  544. } else {
  545. if last_pwm - DIGIT_MIN_BRIGHTNESS < digit.value {
  546. digit.pwm_start = DIGIT_MIN_BRIGHTNESS;
  547. digit.pwm_end = digit.pwm_start + digit.value;
  548. last_pwm = digit.pwm_end;
  549. incrementing = true;
  550. } else {
  551. digit.pwm_end = last_pwm;
  552. digit.pwm_start = digit.pwm_end - digit.value;
  553. last_pwm = digit.pwm_start;
  554. }
  555. }
  556. digit.updated = true;
  557. }
  558. };
  559. #[cfg(not(test))]
  560. self.tubes.iter_mut().for_each(|tube| {
  561. tube.digits.iter_mut().for_each(|digit| {
  562. update_digit(digit);
  563. });
  564. });
  565. #[cfg(test)]
  566. for (t, tube) in self.tubes.iter_mut().enumerate() {
  567. for (d, digit) in tube.digits.iter_mut().enumerate() {
  568. update_digit(digit);
  569. if digit.updated {
  570. println!(
  571. "Distribute PWM: tube {} digit {} start {} end {}",
  572. t, d, digit.pwm_start, digit.pwm_end
  573. );
  574. }
  575. }
  576. }
  577. update_digit(&mut self.dot);
  578. #[cfg(test)]
  579. println!(
  580. "Distribute PWM: dot start {} end {}",
  581. self.dot.pwm_start, self.dot.pwm_end
  582. );
  583. }
  584. }
  585. pub fn rtc_interrupt<T>(i2c: &mut T)
  586. where
  587. T: _embedded_hal_blocking_i2c_WriteRead
  588. + _embedded_hal_blocking_i2c_Read
  589. + _embedded_hal_blocking_i2c_Write,
  590. {
  591. let (second, minute, hour) = ds3231::get_time(DS3231_ADDR, i2c);
  592. let (weekday, day, month, _, _) = ds3231::get_date(DS3231_ADDR, i2c);
  593. let hour = if ds3231::in_dst(weekday, day, month, hour) {
  594. (hour + 1) % 12
  595. } else {
  596. hour % 12
  597. };
  598. let hour = if hour == 0 { 12 } else { hour };
  599. free(|cs| {
  600. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  601. let clock = clock_ref.deref_mut();
  602. clock.rtc_tick(second, minute, hour);
  603. });
  604. }
  605. // This function is called by an interrupt that is triggered every
  606. // REFRESH_RATE_HZ to update the display with a new brightness value.
  607. pub fn fps_interrupt<T>(i2c: &mut T)
  608. where
  609. T: _embedded_hal_blocking_i2c_WriteRead
  610. + _embedded_hal_blocking_i2c_Read
  611. + _embedded_hal_blocking_i2c_Write,
  612. {
  613. free(|cs| {
  614. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  615. let clock = clock_ref.deref_mut();
  616. let updated = clock.fps_tick();
  617. if updated {
  618. clock.write_i2c(i2c);
  619. free(|cs| {
  620. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  621. if let Some(ref mut timer) = timer_ref.deref_mut() {
  622. timer.clear_interrupt(Event::TimeOut);
  623. }
  624. })
  625. } else {
  626. free(|cs| {
  627. let mut timer_ref = super::FPS_TIMER.borrow(cs).borrow_mut();
  628. if let Some(ref mut timer) = timer_ref.deref_mut() {
  629. timer.unlisten(Event::TimeOut);
  630. }
  631. })
  632. }
  633. });
  634. }
  635. // This function is called by an interrupt that is triggered every
  636. // DIGIT_CYCLE_FADE_DURATION_HZ to update the digit being refreshed.
  637. pub fn cycle_interrupt() {
  638. free(|cs| {
  639. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  640. let clock = clock_ref.deref_mut();
  641. let cycle_ended = clock.cycle_tick();
  642. free(|cs| {
  643. let mut cycle_timer_ref = super::CYCLE_TIMER.borrow(cs).borrow_mut();
  644. if let Some(ref mut cycle_timer) = cycle_timer_ref.deref_mut() {
  645. if cycle_ended {
  646. cycle_timer.unlisten(Event::TimeOut);
  647. } else {
  648. cycle_timer.clear_interrupt(Event::TimeOut);
  649. }
  650. }
  651. });
  652. });
  653. }
  654. // This function is called to start cycling through all digits for a
  655. // tube to prevent damage to the nixie tube due to cathode poisoning.
  656. pub fn cycle_start(tube: usize) {
  657. free(|cs| {
  658. let mut clock_ref = CLOCK.borrow(cs).borrow_mut();
  659. let clock = clock_ref.deref_mut();
  660. clock.tubes[tube].cycle = Some(CycleSettings {
  661. last_digit: clock.tubes[tube].last_active_digit,
  662. next_digit: 0,
  663. iteration: CYCLE_ITERATIONS,
  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. }