diff options
author | Marc Poulhiès <dkm@kataplop.net> | 2020-09-02 16:06:58 +0200 |
---|---|---|
committer | Marc Poulhiès <dkm@kataplop.net> | 2020-11-11 21:18:38 +0100 |
commit | 091566813df47450a6fca945339cd1e1ba90c915 (patch) | |
tree | a72489949e08ab67a9bd265ecdc3d6b24d43b1ff /firmware | |
parent | 5be1a9fbf95cbbc1300f24f77f049c93480b3854 (diff) |
Initial work for Pouetpouet board
Based on keyseebee but now very different.
Diffstat (limited to 'firmware')
-rw-r--r-- | firmware/Cargo.lock | 152 | ||||
-rw-r--r-- | firmware/Cargo.toml | 9 | ||||
-rw-r--r-- | firmware/src/main.rs | 285 |
3 files changed, 260 insertions, 186 deletions
diff --git a/firmware/Cargo.lock b/firmware/Cargo.lock index 6bdfa27..5648d78 100644 --- a/firmware/Cargo.lock +++ b/firmware/Cargo.lock @@ -17,20 +17,21 @@ checksum = "f0ffd3d69bd89910509a5d31d1f1353f38ccffdd116dd0099bbd6627f7bd8ad8" [[package]] name = "as-slice" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37dfb65bc03b2bc85ee827004f14a6817e04160e3b1a28931986a666a9290e70" +checksum = "bb4d1c23475b74e3672afa8c2be22040b8b7783ad9b461021144ed10a46bb0e6" dependencies = [ "generic-array 0.12.3", "generic-array 0.13.2", + "generic-array 0.14.4", "stable_deref_trait", ] [[package]] name = "autocfg" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" +checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "bare-metal" @@ -48,6 +49,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" [[package]] +name = "bytemuck" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41aa2ec95ca3b5c54cf73c91acf06d24f4495d5f1b1c12506ae3483d646177ac" + +[[package]] name = "byteorder" version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -64,9 +71,9 @@ dependencies = [ [[package]] name = "cortex-m" -version = "0.6.3" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2be99930c99669a74d986f7fd2162085498b322e6daae8ef63a97cc9ac1dc73c" +checksum = "88cdafeafba636c00c467ded7f1587210725a1adfab0c24028a7844b87738263" dependencies = [ "aligned", "bare-metal", @@ -76,9 +83,9 @@ dependencies = [ [[package]] name = "cortex-m-rt" -version = "0.6.12" +version = "0.6.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00d518da72bba39496024b62607c1d8e37bcece44b2536664f1132a73a499a28" +checksum = "980c9d0233a909f355ed297ef122f257942de5e0a2cb1c39f60684b65bcb90fb" dependencies = [ "cortex-m-rt-macros", "r0", @@ -97,9 +104,9 @@ dependencies = [ [[package]] name = "cortex-m-rtic" -version = "0.5.3" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cd388b154c7e7d212c5af7541ee1f174f29ccb0c22e9117f8d13a5aad233b6" +checksum = "b30efcb6b7920d9016182c485687f0012487032a14c415d2fce6e9862ef8260e" dependencies = [ "cortex-m", "cortex-m-rt", @@ -111,9 +118,9 @@ dependencies = [ [[package]] name = "cortex-m-rtic-macros" -version = "0.5.0" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29e29e01b3ec80d59bfd96aaf94d04008bebfde3ab7016e12bfbd6c0b466d22a" +checksum = "9a1a6a4c9550373038c0e21a78d44d529bd697c25bbf6b8004bddc6e63b119c7" dependencies = [ "proc-macro2", "quote", @@ -123,9 +130,9 @@ dependencies = [ [[package]] name = "either" -version = "1.5.3" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] name = "embedded-hal" @@ -156,6 +163,16 @@ dependencies = [ ] [[package]] +name = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] name = "hash32" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -166,18 +183,15 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.8.1" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34f595585f103464d8d2f6e9864682d74c1601fed5e07d62b1c9058dba8246fb" -dependencies = [ - "autocfg", -] +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" [[package]] name = "heapless" -version = "0.5.5" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73a8a2391a3bc70b31f60e7a90daa5755a360559c0b6b9c5cfc0fee482362dc0" +checksum = "74911a68a1658cfcfb61bc0ccfbd536e3b6e906f8c2f7883ee50157e3e2184f1" dependencies = [ "as-slice", "generic-array 0.13.2", @@ -187,9 +201,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.5.0" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b88cd59ee5f71fea89a62248fc8f387d44400cefe05ef548466d61ced9029a7" +checksum = "55e2e4c765aa53a0424761bf9f41aa7a6ac1efa87238f59560640e27fca028f2" dependencies = [ "autocfg", "hashbrown", @@ -197,8 +211,8 @@ dependencies = [ [[package]] name = "keyberon" -version = "0.1.0" -source = "git+https://github.com/TeXitoi/keyberon#1d5ddd00785ea612fe4beccbe2abb4097842facb" +version = "0.1.1" +source = "git+https://github.com/TeXitoi/keyberon#c1fd84fd9a6a55b51c42ccc67d642ac1fdca2e4e" dependencies = [ "arraydeque", "either", @@ -209,22 +223,6 @@ dependencies = [ ] [[package]] -name = "keyseebee" -version = "0.1.0" -dependencies = [ - "cortex-m", - "cortex-m-rt", - "cortex-m-rtic", - "embedded-hal", - "generic-array 0.13.2", - "keyberon", - "nb 1.0.0", - "panic-halt", - "stm32f0xx-hal", - "usb-device", -] - -[[package]] name = "nb" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -246,10 +244,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812" [[package]] +name = "pouetpouet" +version = "0.1.0" +dependencies = [ + "cortex-m", + "cortex-m-rt", + "cortex-m-rtic", + "embedded-hal", + "generic-array 0.13.2", + "keyberon", + "nb 1.0.0", + "panic-halt", + "smart-leds", + "stm32f0xx-hal", + "usb-device", + "ws2812-spi", +] + +[[package]] name = "proc-macro2" -version = "1.0.19" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" dependencies = [ "unicode-xid", ] @@ -270,6 +286,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f" [[package]] +name = "rgb" +version = "0.8.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "287f3c3f8236abb92d8b7e36797f19159df4b58f0a658cc3fb6dd3004b1f3bd3" +dependencies = [ + "bytemuck", +] + +[[package]] name = "rtic-core" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -311,6 +336,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] +name = "smart-leds" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38dd45fa275f70b4110eac5f5182611ad384f88bb22b68b9a9c3cafd7015290b" +dependencies = [ + "smart-leds-trait", +] + +[[package]] +name = "smart-leds-trait" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebf6d833fa93f16a1c1874e62c2aebe8567e5bdd436d59bf543ed258b6f7a8e3" +dependencies = [ + "rgb", +] + +[[package]] name = "stable_deref_trait" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -341,9 +384,7 @@ dependencies = [ [[package]] name = "stm32f0xx-hal" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "212a649a93c47b422e05dfab72dd45ef24b226d826654f393caeef729e110582" +version = "0.17.1" dependencies = [ "bare-metal", "cast", @@ -357,9 +398,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.37" +version = "1.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "239f255b9e3429350f188c27b807fc9920a15eb9145230ff1a7d054c08fec319" +checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac" dependencies = [ "proc-macro2", "quote", @@ -380,9 +421,9 @@ checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" [[package]] name = "usb-device" -version = "0.2.5" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e5e2b9ba23f0d9ef7a34e498b6581c9d67944a1916542bfc7238bf1dc0d6acd" +checksum = "849eed9b4dc61a1f17ba1d7a5078ceb095b9410caa38a506eb281ed5eff12fbd" [[package]] name = "vcell" @@ -410,3 +451,14 @@ checksum = "0d67cb4616d99b940db1d6bd28844ff97108b498a6ca850e5b6191a532063286" dependencies = [ "vcell", ] + +[[package]] +name = "ws2812-spi" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6c2ba0d6c0ea9c117487411e93dc5dacaafc2c17698677a03d1c67901d4c70a" +dependencies = [ + "embedded-hal", + "nb 0.1.3", + "smart-leds-trait", +] diff --git a/firmware/Cargo.toml b/firmware/Cargo.toml index 1da11be..2fd5d7b 100644 --- a/firmware/Cargo.toml +++ b/firmware/Cargo.toml @@ -1,11 +1,12 @@ [package] -name = "keyseebee" +name = "pouetpouet" version = "0.1.0" -authors = ["Guillaume Pinot <texitoi@texitoi.eu>"] +authors = ["Marc Poulhiès <dkm@kataplop.net>", "Guillaume Pinot <texitoi@texitoi.eu>"] edition = "2018" [dependencies] -stm32f0xx-hal = { version = "0.17.0", features = ["rt", "stm32f072", "stm32-usbd"] } +stm32f0xx-hal = { git = "https://github.com/dkm/stm32f0xx-hal/", features = ["rt", "stm32f072", "stm32-usbd"] } +##{ version = "0.17.0", features = ["rt", "stm32f072", "stm32-usbd"] } cortex-m = "0.6" cortex-m-rt = { version = "0.6.10", features = ["device"] } panic-halt = "0.2.0" @@ -15,6 +16,8 @@ generic-array = "0.13" embedded-hal = "0.2" usb-device = "0.2.0" nb = "1.0.0" +ws2812-spi = "0.3.0" +smart-leds = "0.3.0" [profile.release] lto = true diff --git a/firmware/src/main.rs b/firmware/src/main.rs index 8534bce..5e4e063 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -4,14 +4,26 @@ // set the panic handler use panic_halt as _; +extern crate smart_leds; +extern crate ws2812_spi; + +use smart_leds::{brightness, colors, SmartLedsWrite, RGB8}; + +use ws2812_spi as ws2812; + use core::convert::Infallible; use embedded_hal::digital::v2::{InputPin, OutputPin}; -use generic_array::typenum::{U4, U6}; -use hal::gpio::{gpioa, gpiob, Floating, Input, Output, PullUp, PushPull}; +use generic_array::typenum::{U12, U5}; +use hal::gpio::{gpioa, gpiob, Alternate, Input, Output, PullUp, PushPull, AF0}; use hal::prelude::*; -use hal::serial; + +use embedded_hal::spi::FullDuplex; + use hal::usb; -use hal::{stm32, timers}; +use hal::{ + spi::{EightBit, Mode, Phase, Polarity, Spi}, + stm32, timers, +}; use keyberon::action::{k, l, m, Action, Action::*}; use keyberon::debounce::Debouncer; use keyberon::impl_heterogenous_array; @@ -19,15 +31,28 @@ use keyberon::key_code::KbHidReport; use keyberon::key_code::KeyCode::*; use keyberon::layout::{Event, Layout}; use keyberon::matrix::{Matrix, PressedKeys}; -use nb::block; + use rtic::app; use stm32f0xx_hal as hal; use usb_device::bus::UsbBusAllocator; use usb_device::class::UsbClass as _; use usb_device::device::UsbDeviceState; -type UsbClass = keyberon::Class<'static, usb::UsbBusType, ()>; -type UsbDevice = keyberon::Device<'static, usb::UsbBusType>; +type UsbClass = keyberon::Class< + 'static, + usb::UsbBusType, + Leds< + Spi< + stm32::SPI1, + gpioa::PA5<Alternate<AF0>>, + gpioa::PA6<Alternate<AF0>>, + gpioa::PA7<Alternate<AF0>>, + EightBit, + >, + >, +>; + +type UsbDevice = usb_device::device::UsbDevice<'static, usb::UsbBusType>; trait ResultExt<T> { fn get(self) -> T; @@ -42,18 +67,24 @@ impl<T> ResultExt<T> for Result<T, Infallible> { } pub struct Cols( - gpioa::PA15<Input<PullUp>>, - gpiob::PB3<Input<PullUp>>, - gpiob::PB4<Input<PullUp>>, - gpiob::PB5<Input<PullUp>>, - gpiob::PB8<Input<PullUp>>, - gpiob::PB9<Input<PullUp>>, + gpioa::PA0<Input<PullUp>>, // 12 + gpioa::PA1<Input<PullUp>>, // 11 + gpiob::PB13<Input<PullUp>>, // 10 + gpiob::PB12<Input<PullUp>>, // 9 + gpiob::PB14<Input<PullUp>>, // 8 + gpiob::PB15<Input<PullUp>>, // 7 + gpioa::PA15<Input<PullUp>>, // 6 + gpiob::PB3<Input<PullUp>>, // 5 + gpiob::PB4<Input<PullUp>>, // 4 + gpiob::PB5<Input<PullUp>>, // 3 + gpiob::PB8<Input<PullUp>>, // 2 + gpiob::PB9<Input<PullUp>>, // 1 ); impl_heterogenous_array! { Cols, dyn InputPin<Error = Infallible>, - U6, - [0, 1, 2, 3, 4, 5] + U12, + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] } pub struct Rows( @@ -61,76 +92,85 @@ pub struct Rows( gpiob::PB1<Output<PushPull>>, gpiob::PB2<Output<PushPull>>, gpiob::PB10<Output<PushPull>>, + gpiob::PB11<Output<PushPull>>, ); impl_heterogenous_array! { Rows, dyn OutputPin<Error = Infallible>, - U4, - [0, 1, 2, 3] -} - -const CUT: Action = m(&[LShift, Delete]); -const COPY: Action = m(&[LCtrl, Insert]); -const PASTE: Action = m(&[LShift, Insert]); -const L2_ENTER: Action = HoldTap { - timeout: 160, - hold: &l(2), - tap: &k(Enter), -}; -const L1_SP: Action = HoldTap { - timeout: 160, - hold: &l(1), - tap: &k(Space), -}; -const CSPACE: Action = m(&[LCtrl, Space]); -macro_rules! s { - ($k:ident) => { - m(&[LShift, $k]) - }; -} -macro_rules! a { - ($k:ident) => { - m(&[RAlt, $k]) - }; + U5, + [0, 1, 2, 3, 4] } #[rustfmt::skip] pub static LAYERS: keyberon::layout::Layers = &[ &[ - &[k(Tab), k(Q), k(W), k(E), k(R), k(T), k(Y), k(U), k(I), k(O), k(P), k(LBracket)], - &[k(RBracket),k(A), k(S), k(D), k(F), k(G), k(H), k(J), k(K), k(L), k(SColon),k(Quote) ], - &[k(Equal), k(Z), k(X), k(C), k(V), k(B), k(N), k(M), k(Comma),k(Dot), k(Slash), k(Bslash) ], - &[Trans, Trans,k(LGui),k(LAlt),L1_SP,k(LCtrl),k(RShift),L2_ENTER,k(RAlt),k(BSpace),Trans, Trans ], - ], &[ - &[Trans, k(Pause),Trans, k(PScreen),Trans, Trans,Trans, Trans, k(Delete),Trans, Trans, Trans ], - &[Trans, Trans, k(NumLock),k(Insert), k(Escape),Trans,k(CapsLock),k(Left),k(Down), k(Up), k(Right),Trans ], - &[k(NonUsBslash),k(Undo), CUT, COPY, PASTE, Trans,Trans, k(Home),k(PgDown),k(PgUp),k(End), Trans ], - &[Trans, Trans, Trans, Trans, Trans, Trans,Trans, Trans, Trans, Trans, Trans, Trans ], - ], &[ - &[s!(Grave),s!(Kb1),s!(Kb2),s!(Kb3),s!(Kb4),s!(Kb5),s!(Kb6),s!(Kb7),s!(Kb8),s!(Kb9),s!(Kb0),s!(Minus)], - &[ k(Grave), k(Kb1), k(Kb2), k(Kb3), k(Kb4), k(Kb5), k(Kb6), k(Kb7), k(Kb8), k(Kb9), k(Kb0), k(Minus)], - &[a!(Grave),a!(Kb1),a!(Kb2),a!(Kb3),a!(Kb4),a!(Kb5),a!(Kb6),a!(Kb7),a!(Kb8),a!(Kb9),a!(Kb0),a!(Minus)], - &[Trans, Trans, Trans, Trans, CSPACE, Trans, Trans, Trans, Trans, Trans, Trans, Trans ], + &[k(Grave), k(Kb1),k(Kb2),k(Kb3), k(Kb4),k(Kb5), k(Kb6), k(Kb7), k(Kb8), k(Kb9), k(Kb0), k(Minus), k(Space)], + + &[k(Q), k(W), k(E), k(R), k(T), k(Tab), k(Y), k(U), k(I), k(O), k(P), k(LBracket)], + &[k(A), k(S), k(D), k(F), k(G), k(BSpace), k(H), k(J), k(K), k(L), k(SColon), k(Quote)], + &[k(Z), k(X), k(C), k(V), k(B), k(Enter), k(N), k(M), k(Comma), k(Dot), k(Slash), k(Bslash) ], + + &[k(LCtrl), k(LShift), k(LGui), l(1), k(LAlt), k(Space), k(Delete), k(RAlt), k(Equal), k(RBracket),k(RShift), k(RCtrl)], + ], &[ &[k(F1),k(F2),k(F3),k(F4),k(F5),k(F6),k(F7),k(F8),k(F9),k(F10),k(F11),k(F12)], - &[Trans,Trans,Trans,Trans,Trans,Trans,Trans,Trans,Trans,Trans, Trans, Trans ], - &[Trans,Trans,Trans,Trans,Trans,Trans,Trans,Trans,Trans,Trans, Trans, Trans ], - &[Trans,Trans,Trans,Trans,Trans,Trans,Trans,Trans,Trans,Trans, Trans, Trans ], + + &[k(SysReq), k(NumLock), Trans, Trans, Trans, k(Escape), k(Insert), k(PgUp), k(PgDown), Trans, Trans, Trans ], + &[Trans , Trans , Trans, Trans, Trans, Trans, k(Home), k(Up), k(End), Trans, Trans, Trans ], + &[Trans, Trans, Trans, Trans, Trans, Trans, k(Left), k(Down), k(Right), Trans, Trans, Trans ], + &[Trans, Trans, Trans, Trans, Trans, Trans, Trans, Trans, Trans, Trans, Trans, Trans ], ], ]; +pub struct Leds<SPI> { + ws: ws2812::Ws2812<SPI>, + leds: [RGB8; 10], +} + +impl<SPI, E> keyberon::keyboard::Leds for Leds<SPI> +where + SPI: FullDuplex<u8, Error = E>, +{ + fn caps_lock(&mut self, status: bool) { + if status { + self.leds[0] = colors::BLUE; + } else { + self.leds[0] = colors::BLACK; + } + self.ws + .write(brightness(self.leds.iter().cloned(), 10)); + } + + fn num_lock(&mut self, status: bool) { + if status { + self.leds[1] = colors::GREEN; + } else { + self.leds[1] = colors::BLACK; + } + self.ws + .write(brightness(self.leds.iter().cloned(), 10)); + } + + fn compose(&mut self, status: bool) { + if status { + self.leds[3] = colors::VIOLET; + } else { + self.leds[3] = colors::BLACK; + } + self.ws + .write(brightness(self.leds.iter().cloned(), 10)); + } +} + #[app(device = crate::hal::pac, peripherals = true)] const APP: () = { struct Resources { usb_dev: UsbDevice, usb_class: UsbClass, matrix: Matrix<Cols, Rows>, - debouncer: Debouncer<PressedKeys<U4, U6>>, + debouncer: Debouncer<PressedKeys<U5, U12>>, layout: Layout, timer: timers::Timer<stm32::TIM3>, - transpose: fn(Event) -> Event, - tx: serial::Tx<hal::pac::USART1>, - rx: serial::Rx<hal::pac::USART1>, } #[init] @@ -158,32 +198,61 @@ const APP: () = { *USB_BUS = Some(usb::UsbBusType::new(usb)); let usb_bus = USB_BUS.as_ref().unwrap(); - let usb_class = keyberon::new_class(usb_bus, ()); - let usb_dev = keyberon::new_device(usb_bus); + // Handling of ws2812 leds - let mut timer = timers::Timer::tim3(c.device.TIM3, 1.khz(), &mut rcc); - timer.listen(timers::Event::TimeOut); + let pa5 = gpioa.pa5; // sck + let pa6 = gpioa.pa6; // miso + let pa7 = gpioa.pa7; // mosi - let pb12: &gpiob::PB12<Input<Floating>> = &gpiob.pb12; - let is_left = pb12.is_low().get(); - let transpose = if is_left { - transpose_left - } else { - transpose_right + // Configure pins for SPI + let (sck, miso, mosi) = cortex_m::interrupt::free(move |cs| { + ( + pa5.into_alternate_af0(cs), + pa6.into_alternate_af0(cs), + pa7.into_alternate_af0(cs), + ) + }); + + const MODE: Mode = Mode { + polarity: Polarity::IdleHigh, + phase: Phase::CaptureOnSecondTransition, }; + let spi = Spi::spi1( + c.device.SPI1, + (sck, miso, mosi), + MODE, + 3_000_000.hz(), + &mut rcc, + ); - let (pa9, pa10) = (gpioa.pa9, gpioa.pa10); - let pins = cortex_m::interrupt::free(move |cs| { - (pa9.into_alternate_af1(cs), pa10.into_alternate_af1(cs)) - }); - let mut serial = serial::Serial::usart1(c.device.USART1, pins, 38_400.bps(), &mut rcc); - serial.listen(serial::Event::Rxne); - let (tx, rx) = serial.split(); + // ws2812 + let mut ws = ws2812::Ws2812::new(spi); + + let mut leds = Leds { + ws, + leds: [colors::BLACK; 10], + }; + leds.ws.write(leds.leds.iter().cloned()).unwrap(); + + let usb_class = keyberon::new_class(usb_bus, leds); + let usb_dev = keyberon::new_device(usb_bus); + + let mut timer = timers::Timer::tim3(c.device.TIM3, 1.khz(), &mut rcc); + timer.listen(timers::Event::TimeOut); let pa15 = gpioa.pa15; + let pa1 = gpioa.pa1; + let pa0 = gpioa.pa0; + let matrix = cortex_m::interrupt::free(move |cs| { Matrix::new( Cols( + pa0.into_pull_up_input(cs), + pa1.into_pull_up_input(cs), + gpiob.pb13.into_pull_up_input(cs), + gpiob.pb12.into_pull_up_input(cs), + gpiob.pb14.into_pull_up_input(cs), + gpiob.pb15.into_pull_up_input(cs), pa15.into_pull_up_input(cs), gpiob.pb3.into_pull_up_input(cs), gpiob.pb4.into_pull_up_input(cs), @@ -196,6 +265,7 @@ const APP: () = { gpiob.pb1.into_push_pull_output(cs), gpiob.pb2.into_push_pull_output(cs), gpiob.pb10.into_push_pull_output(cs), + gpiob.pb11.into_push_pull_output(cs), ), ) }); @@ -207,25 +277,6 @@ const APP: () = { debouncer: Debouncer::new(PressedKeys::default(), PressedKeys::default(), 5), matrix: matrix.get(), layout: Layout::new(LAYERS), - transpose, - tx, - rx, - } - } - - #[task(binds = USART1, priority = 5, spawn = [handle_event], resources = [rx])] - fn rx(c: rx::Context) { - static mut BUF: [u8; 4] = [0; 4]; - - if let Ok(b) = c.resources.rx.read() { - BUF.rotate_left(1); - BUF[3] = b; - - if BUF[3] == b'\n' { - if let Ok(event) = de(&BUF[..]) { - c.spawn.handle_event(Some(event)).unwrap(); - } - } } } @@ -259,20 +310,12 @@ const APP: () = { binds = TIM3, priority = 2, spawn = [handle_event], - resources = [matrix, debouncer, timer, &transpose, tx], + resources = [matrix, debouncer, timer], )] fn tick(c: tick::Context) { c.resources.timer.wait().ok(); - for event in c - .resources - .debouncer - .events(c.resources.matrix.get().get()) - .map(c.resources.transpose) - { - for &b in &ser(event) { - block!(c.resources.tx.write(b)).get(); - } + for event in c.resources.debouncer.events(c.resources.matrix.get().get()) { c.spawn.handle_event(Some(event)).unwrap(); } c.spawn.handle_event(None).unwrap(); @@ -282,27 +325,3 @@ const APP: () = { fn CEC_CAN(); } }; - -fn transpose_left(e: Event) -> Event { - e -} -fn transpose_right(e: Event) -> Event { - match e { - Event::Press(i, j) => Event::Press(i, 11 - j), - Event::Release(i, j) => Event::Release(i, 11 - j), - } -} - -fn de(bytes: &[u8]) -> Result<Event, ()> { - match *bytes { - [b'P', i, j, b'\n'] => Ok(Event::Press(i as usize, j as usize)), - [b'R', i, j, b'\n'] => Ok(Event::Release(i as usize, j as usize)), - _ => Err(()), - } -} -fn ser(e: Event) -> [u8; 4] { - match e { - Event::Press(i, j) => [b'P', i as u8, j as u8, b'\n'], - Event::Release(i, j) => [b'R', i as u8, j as u8, b'\n'], - } -} |