nixie.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. use stm32l4xx_hal::prelude::{
  2. _embedded_hal_blocking_i2c_Read, _embedded_hal_blocking_i2c_Write,
  3. _embedded_hal_blocking_i2c_WriteRead,
  4. };
  5. use crate::ds3231;
  6. pub const DS3231_ADDR: u8 = 0x68;
  7. pub const TUSB322_ADDR: u8 = 0x47;
  8. pub const PCA9685_ADDR_1: u8 = 0x41;
  9. pub const PCA9685_ADDR_2: u8 = 0x42;
  10. pub const PCA9685_ADDR_3: u8 = 0x43;
  11. pub const PCA9685_ALL_CALL: u8 = 0x70; // Default enabled
  12. pub const PCA9685_SUB_CALL_1: u8 = 0x71; // Default disabled
  13. pub const PCA9685_SUB_CALL_2: u8 = 0x72; // Default disabled
  14. pub const PCA9685_SUB_CALL_3: u8 = 0x73; // Default disabled
  15. const MAP_DOT_ADDR: u8 = PCA9685_ADDR_2;
  16. const MAP_DOT_PIN: u8 = 15;
  17. const MAP_ADDR: usize = 0;
  18. const MAP_PIN: usize = 1;
  19. const DIGIT_FADE_DURATION_US: u32 = 1_000_000;
  20. const REFRESH_RATE_US: u32 = 1000;
  21. const DIGIT_RNG_FADE_DURATION_US: u32 = 200_000;
  22. const DIGIT_RNG_FADE_ITERATIONS: usize = 20;
  23. const DIGIT_RNG_REFRESH_INTERVAL: usize = 60;
  24. const DIGIT_RNG_REFRESH_VARIANCE: usize = 30;
  25. const DOT_MIN_BRIGHTNESS: u32 = 256;
  26. const DOT_MAX_BRIGHTNESS: u32 = 640;
  27. const DOT_FADE_DURATION_US: u32 = 1_000_000;
  28. const DIGIT_MAX_BRIGHTNESS: u32 = 4096;
  29. const DIGIT_MIN_BRIGHTNESS: u32 = 0;
  30. const NUM_TUBES: usize = 4;
  31. const NUM_DIGITS: usize = 10;
  32. struct DigitToPin {
  33. address: u8,
  34. pin: usize,
  35. }
  36. struct PwmDriver {
  37. digit: [DigitToPin; 10],
  38. }
  39. struct PwmOutputMap {
  40. driver: [PwmDriver; 4],
  41. dot_address: u8,
  42. dot_pin: usize,
  43. }
  44. static TUBE_MAPPING: PwmOutputMap = {
  45. PwmOutputMap {
  46. driver: [
  47. PwmDriver {
  48. digit: [
  49. DigitToPin {
  50. address: PCA9685_ADDR_1,
  51. pin: 8,
  52. }, // Tube 0 Digit 0
  53. DigitToPin {
  54. address: PCA9685_ADDR_1,
  55. pin: 9,
  56. }, // Tube 0 Digit 1
  57. DigitToPin {
  58. address: PCA9685_ADDR_1,
  59. pin: 10,
  60. }, // Tube 0 Digit 2
  61. DigitToPin {
  62. address: PCA9685_ADDR_1,
  63. pin: 12,
  64. }, // Tube 0 Digit 3
  65. DigitToPin {
  66. address: PCA9685_ADDR_1,
  67. pin: 15,
  68. }, // Tube 0 Digit 4
  69. DigitToPin {
  70. address: PCA9685_ADDR_1,
  71. pin: 14,
  72. }, // Tube 0 Digit 5
  73. DigitToPin {
  74. address: PCA9685_ADDR_1,
  75. pin: 11,
  76. }, // Tube 0 Digit 6
  77. DigitToPin {
  78. address: PCA9685_ADDR_1,
  79. pin: 0,
  80. }, // Tube 0 Digit 7
  81. DigitToPin {
  82. address: PCA9685_ADDR_1,
  83. pin: 1,
  84. }, // Tube 0 Digit 8
  85. DigitToPin {
  86. address: PCA9685_ADDR_1,
  87. pin: 13,
  88. }, // Tube 0 Digit 9
  89. ],
  90. },
  91. PwmDriver {
  92. digit: [
  93. DigitToPin {
  94. address: PCA9685_ADDR_1,
  95. pin: 5,
  96. }, // Tube 1 Digit 0
  97. DigitToPin {
  98. address: PCA9685_ADDR_1,
  99. pin: 6,
  100. }, // Tube 1 Digit 1
  101. DigitToPin {
  102. address: PCA9685_ADDR_1,
  103. pin: 7,
  104. }, // Tube 1 Digit 2
  105. DigitToPin {
  106. address: PCA9685_ADDR_1,
  107. pin: 2,
  108. }, // Tube 1 Digit 3
  109. DigitToPin {
  110. address: PCA9685_ADDR_2,
  111. pin: 4,
  112. }, // Tube 1 Digit 4
  113. DigitToPin {
  114. address: PCA9685_ADDR_2,
  115. pin: 1,
  116. }, // Tube 1 Digit 5
  117. DigitToPin {
  118. address: PCA9685_ADDR_1,
  119. pin: 4,
  120. }, // Tube 1 Digit 6
  121. DigitToPin {
  122. address: PCA9685_ADDR_2,
  123. pin: 2,
  124. }, // Tube 1 Digit 7
  125. DigitToPin {
  126. address: PCA9685_ADDR_2,
  127. pin: 3,
  128. }, // Tube 1 Digit 8
  129. DigitToPin {
  130. address: PCA9685_ADDR_1,
  131. pin: 3,
  132. }, // Tube 1 Digit 9
  133. ],
  134. },
  135. PwmDriver {
  136. digit: [
  137. DigitToPin {
  138. address: PCA9685_ADDR_3,
  139. pin: 8,
  140. }, // Tube 2 Digit 0
  141. DigitToPin {
  142. address: PCA9685_ADDR_3,
  143. pin: 9,
  144. }, // Tube 2 Digit 1
  145. DigitToPin {
  146. address: PCA9685_ADDR_3,
  147. pin: 10,
  148. }, // Tube 2 Digit 2
  149. DigitToPin {
  150. address: PCA9685_ADDR_3,
  151. pin: 12,
  152. }, // Tube 2 Digit 3
  153. DigitToPin {
  154. address: PCA9685_ADDR_2,
  155. pin: 12,
  156. }, // Tube 2 Digit 4
  157. DigitToPin {
  158. address: PCA9685_ADDR_2,
  159. pin: 13,
  160. }, // Tube 2 Digit 5
  161. DigitToPin {
  162. address: PCA9685_ADDR_3,
  163. pin: 11,
  164. }, // Tube 2 Digit 6
  165. DigitToPin {
  166. address: PCA9685_ADDR_2,
  167. pin: 14,
  168. }, // Tube 2 Digit 7
  169. DigitToPin {
  170. address: PCA9685_ADDR_2,
  171. pin: 11,
  172. }, // Tube 2 Digit 8
  173. DigitToPin {
  174. address: PCA9685_ADDR_3,
  175. pin: 13,
  176. }, // Tube 2 Digit 9
  177. ],
  178. },
  179. PwmDriver {
  180. digit: [
  181. DigitToPin {
  182. address: PCA9685_ADDR_3,
  183. pin: 5,
  184. }, // Tube 3 Digit 0
  185. DigitToPin {
  186. address: PCA9685_ADDR_3,
  187. pin: 6,
  188. }, // Tube 3 Digit 1
  189. DigitToPin {
  190. address: PCA9685_ADDR_3,
  191. pin: 7,
  192. }, // Tube 3 Digit 2
  193. DigitToPin {
  194. address: PCA9685_ADDR_3,
  195. pin: 2,
  196. }, // Tube 3 Digit 3
  197. DigitToPin {
  198. address: PCA9685_ADDR_3,
  199. pin: 14,
  200. }, // Tube 3 Digit 4
  201. DigitToPin {
  202. address: PCA9685_ADDR_3,
  203. pin: 15,
  204. }, // Tube 3 Digit 5
  205. DigitToPin {
  206. address: PCA9685_ADDR_3,
  207. pin: 4,
  208. }, // Tube 3 Digit 6
  209. DigitToPin {
  210. address: PCA9685_ADDR_3,
  211. pin: 1,
  212. }, // Tube 3 Digit 7
  213. DigitToPin {
  214. address: PCA9685_ADDR_3,
  215. pin: 0,
  216. }, // Tube 3 Digit 8
  217. DigitToPin {
  218. address: PCA9685_ADDR_3,
  219. pin: 3,
  220. }, // Tube 3 Digit 9
  221. ],
  222. },
  223. ],
  224. dot_address: PCA9685_ADDR_2,
  225. dot_pin: 15,
  226. }
  227. };
  228. #[derive(PartialEq)]
  229. enum DigitState {
  230. Idle,
  231. Incrementing,
  232. Decrementing,
  233. }
  234. struct Digit {
  235. current_state: DigitState,
  236. value: u32,
  237. pwm_start: u32,
  238. pwm_end: u32,
  239. updated: bool,
  240. }
  241. impl Digit {
  242. const fn default() -> Self {
  243. Self {
  244. current_state: DigitState::Idle,
  245. value: 0,
  246. pwm_start: 0,
  247. pwm_end: 0,
  248. updated: false,
  249. }
  250. }
  251. }
  252. struct Tube {
  253. digits: [Digit; NUM_DIGITS],
  254. last_active_digit: Option<usize>,
  255. refresh_last_digit: Option<usize>,
  256. refresh_active: bool,
  257. }
  258. impl Tube {
  259. const fn default() -> Self {
  260. const DIGIT_INIT: Digit = Digit::default();
  261. Self {
  262. digits: [DIGIT_INIT; 10],
  263. last_active_digit: None,
  264. refresh_last_digit: None,
  265. refresh_active: false,
  266. }
  267. }
  268. }
  269. struct Clock {
  270. tubes: [Tube; NUM_TUBES],
  271. dot: Digit,
  272. fade_duration: u32,
  273. }
  274. impl Clock {
  275. const fn default() -> Self {
  276. const TUBE_INIT: Tube = Tube::default();
  277. Self {
  278. tubes: [TUBE_INIT; NUM_TUBES],
  279. dot: Digit::default(),
  280. fade_duration: 0,
  281. }
  282. }
  283. }
  284. static mut CLOCK: Clock = Clock::default();
  285. // pub fn set_digit(tube: usize, digit: usize, pwm_start: u32, pwm_end: u32) {
  286. // assert!(pwm_end >= pwm_start);
  287. // assert!(tube < NUM_TUBES);
  288. // assert!(digit < NUM_DIGITS);
  289. // unsafe {
  290. // CLOCK.tubes[tube].digits[digit].current_state = DigitState::Incrementing;
  291. // CLOCK.tubes[tube].digits[digit].pwm_start = pwm_start;
  292. // CLOCK.tubes[tube].digits[digit].pwm_end = pwm_end;
  293. // CLOCK.tubes[tube].digits[digit].value = pwm_end - pwm_start;
  294. // CLOCK.tubes[tube].digits[digit].updated = true;
  295. // }
  296. // }
  297. pub fn fade_in_out_digit(tube: usize, digit: Option<usize>, fade_duration: u32, refresh_cmd: bool) {
  298. // assert!(tube < NUM_TUBES);
  299. // assert!(Some(digit) < NUM_DIGITS);
  300. unsafe {
  301. // If the tube is in the middle of a refresh sequence and a call comes
  302. // in to update the tube digit (for time), override the last value of
  303. // the refresh sequence with the new digit.
  304. if CLOCK.tubes[tube].refresh_active && !refresh_cmd {
  305. CLOCK.tubes[tube].refresh_last_digit = digit;
  306. }
  307. // Dont update if actively refreshing tube unless RngUpdate is set
  308. if (!CLOCK.tubes[tube].refresh_active && !refresh_cmd) || refresh_cmd {
  309. // Fade out all digits
  310. for digit in 0..NUM_DIGITS {
  311. CLOCK.tubes[tube].digits[digit].current_state = DigitState::Decrementing;
  312. }
  313. // Fade in the specified digit
  314. if let Some(digit) = digit {
  315. CLOCK.tubes[tube].digits[digit].current_state = DigitState::Incrementing;
  316. }
  317. CLOCK.tubes[tube].last_active_digit = digit;
  318. CLOCK.fade_duration = fade_duration;
  319. }
  320. }
  321. // TODO! trigger refresh timer
  322. }
  323. pub fn refresh_frame() {
  324. let mut pending_refresh: bool = false;
  325. unsafe {
  326. let ticks = CLOCK.fade_duration / REFRESH_RATE_US;
  327. for tube in 0..NUM_TUBES {
  328. let steps = ((DIGIT_MAX_BRIGHTNESS - DIGIT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  329. for digit in 0..NUM_DIGITS {
  330. let mut d = &mut CLOCK.tubes[tube].digits[digit];
  331. match d.current_state {
  332. DigitState::Incrementing => {
  333. d.value = d.value.saturating_add(steps);
  334. if d.value >= DIGIT_MAX_BRIGHTNESS {
  335. d.value = DIGIT_MAX_BRIGHTNESS;
  336. d.current_state = DigitState::Idle;
  337. }
  338. d.updated = true;
  339. pending_refresh = true;
  340. }
  341. DigitState::Decrementing => {
  342. d.value = d.value.saturating_sub(steps);
  343. if d.value <= DIGIT_MIN_BRIGHTNESS {
  344. d.value = DIGIT_MIN_BRIGHTNESS;
  345. d.current_state = DigitState::Idle;
  346. }
  347. d.updated = true;
  348. pending_refresh = true;
  349. }
  350. DigitState::Idle => (),
  351. }
  352. }
  353. }
  354. // Handle dot
  355. let steps = ((DOT_MAX_BRIGHTNESS - DOT_MIN_BRIGHTNESS) + ticks - 1) / ticks;
  356. match CLOCK.dot.current_state {
  357. DigitState::Incrementing => {
  358. CLOCK.dot.value = CLOCK.dot.value.saturating_add(steps);
  359. if CLOCK.dot.value >= DIGIT_MAX_BRIGHTNESS {
  360. CLOCK.dot.value = DIGIT_MAX_BRIGHTNESS;
  361. CLOCK.dot.current_state = DigitState::Idle;
  362. }
  363. CLOCK.dot.updated = true;
  364. pending_refresh = true;
  365. }
  366. DigitState::Decrementing => {
  367. CLOCK.dot.value = CLOCK.dot.value.saturating_sub(steps);
  368. if CLOCK.dot.value >= DIGIT_MIN_BRIGHTNESS {
  369. CLOCK.dot.value = DIGIT_MIN_BRIGHTNESS;
  370. CLOCK.dot.current_state = DigitState::Idle;
  371. }
  372. CLOCK.dot.updated = true;
  373. pending_refresh = true;
  374. }
  375. DigitState::Idle => (),
  376. }
  377. }
  378. if pending_refresh {
  379. // TODO! trigger refresh timer
  380. }
  381. }
  382. pub fn rtc_tick<T>(i2c: &mut T)
  383. where
  384. T: _embedded_hal_blocking_i2c_WriteRead
  385. + _embedded_hal_blocking_i2c_Read
  386. + _embedded_hal_blocking_i2c_Write,
  387. {
  388. static mut STARTUP: bool = true;
  389. static mut PREV_MINUTE: u32 = 0;
  390. static mut PREV_HOUR: u32 = 0;
  391. let (second, minute, hour) = ds3231::get_time(DS3231_ADDR, i2c);
  392. let (weekday, day, month, _, _) = ds3231::get_date(DS3231_ADDR, i2c);
  393. let hour = if ds3231::in_dst(weekday, day, month, hour) {
  394. (hour + 1) % 12
  395. } else {
  396. hour % 12
  397. };
  398. let hour = if hour == 0 { 12 } else { hour };
  399. unsafe {
  400. if STARTUP || PREV_HOUR / 10 != hour / 10 {
  401. fade_in_out_digit(
  402. 0,
  403. if hour / 10 != 0 {
  404. Some((hour / 10) as usize)
  405. } else {
  406. None
  407. },
  408. DIGIT_FADE_DURATION_US,
  409. false,
  410. )
  411. }
  412. if STARTUP || PREV_HOUR % 10 != hour % 10 {
  413. fade_in_out_digit(1, Some((hour % 10) as usize), DIGIT_FADE_DURATION_US, false);
  414. }
  415. if STARTUP || PREV_MINUTE / 10 != minute / 10 {
  416. fade_in_out_digit(
  417. 2,
  418. Some((minute / 10) as usize),
  419. DIGIT_FADE_DURATION_US,
  420. false,
  421. );
  422. }
  423. if STARTUP || PREV_MINUTE % 10 != minute % 10 {
  424. fade_in_out_digit(
  425. 3,
  426. Some((minute % 10) as usize),
  427. DIGIT_FADE_DURATION_US,
  428. false,
  429. );
  430. }
  431. CLOCK.dot.current_state = match second % 2 {
  432. 0 => DigitState::Incrementing,
  433. 1 => DigitState::Decrementing,
  434. _ => DigitState::Idle,
  435. };
  436. PREV_MINUTE = minute;
  437. PREV_HOUR = hour;
  438. STARTUP = false;
  439. }
  440. }
  441. pub fn refresh_tick<T>(i2c: &mut T)
  442. where
  443. T: _embedded_hal_blocking_i2c_WriteRead
  444. + _embedded_hal_blocking_i2c_Read
  445. + _embedded_hal_blocking_i2c_Write,
  446. {
  447. }
  448. pub fn rng_tick<T>(i2c: &mut T)
  449. where
  450. T: _embedded_hal_blocking_i2c_WriteRead
  451. + _embedded_hal_blocking_i2c_Read
  452. + _embedded_hal_blocking_i2c_Write,
  453. {
  454. }