mirror of
https://github.com/laanwj/k210-sdk-stuff.git
synced 2024-11-22 01:16:20 +04:00
rust: Add game-of-life
This commit is contained in:
parent
0572038b07
commit
927a21229e
@ -70,6 +70,14 @@ Mandelbrot fractal zoom.
|
|||||||
|
|
||||||
[README](rust/mandelbrot/README.md)
|
[README](rust/mandelbrot/README.md)
|
||||||
|
|
||||||
|
rust/game-of-life
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
"Game of life" cellular automata simulation. The state can be manipulated through the touch screen.
|
||||||
|
The amount of pressure applied determines the radius of the state change.
|
||||||
|
|
||||||
|
[README](rust/game-of-life/README.md)
|
||||||
|
|
||||||
ROM re'ing
|
ROM re'ing
|
||||||
===========
|
===========
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
members = [
|
members = [
|
||||||
"k210-console",
|
"k210-console",
|
||||||
"mandelbrot",
|
"mandelbrot",
|
||||||
|
"game-of-life",
|
||||||
]
|
]
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
|
9
rust/game-of-life/.cargo/config
Normal file
9
rust/game-of-life/.cargo/config
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[target.riscv64gc-unknown-none-elf]
|
||||||
|
runner = "riscv64-unknown-elf-gdb -x gdb_init"
|
||||||
|
rustflags = [
|
||||||
|
"-C", "link-arg=-Tmemory.x",
|
||||||
|
"-C", "link-arg=-Tlink.x",
|
||||||
|
]
|
||||||
|
|
||||||
|
[build]
|
||||||
|
target = "riscv64gc-unknown-none-elf"
|
2
rust/game-of-life/.gitignore
vendored
Normal file
2
rust/game-of-life/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
**/*.rs.bk
|
37
rust/game-of-life/CODE_OF_CONDUCT.md
Normal file
37
rust/game-of-life/CODE_OF_CONDUCT.md
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
# The Rust Code of Conduct
|
||||||
|
|
||||||
|
## Conduct
|
||||||
|
|
||||||
|
**Contact**: [RISC-V team](https://github.com/rust-embedded/wg#the-riscv-team)
|
||||||
|
|
||||||
|
* We are committed to providing a friendly, safe and welcoming environment for all, regardless of level of experience, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, nationality, or other similar characteristic.
|
||||||
|
* On IRC, please avoid using overtly sexual nicknames or other nicknames that might detract from a friendly, safe and welcoming environment for all.
|
||||||
|
* Please be kind and courteous. There's no need to be mean or rude.
|
||||||
|
* Respect that people have differences of opinion and that every design or implementation choice carries a trade-off and numerous costs. There is seldom a right answer.
|
||||||
|
* Please keep unstructured critique to a minimum. If you have solid ideas you want to experiment with, make a fork and see how it works.
|
||||||
|
* We will exclude you from interaction if you insult, demean or harass anyone. That is not welcome behavior. We interpret the term "harassment" as including the definition in the [Citizen Code of Conduct](http://citizencodeofconduct.org/); if you have any lack of clarity about what might be included in that concept, please read their definition. In particular, we don't tolerate behavior that excludes people in socially marginalized groups.
|
||||||
|
* Private harassment is also unacceptable. No matter who you are, if you feel you have been or are being harassed or made uncomfortable by a community member, please contact one of the channel ops or any of the [RISC-V team][team] immediately. Whether you're a regular contributor or a newcomer, we care about making this community a safe place for you and we've got your back.
|
||||||
|
* Likewise any spamming, trolling, flaming, baiting or other attention-stealing behavior is not welcome.
|
||||||
|
|
||||||
|
## Moderation
|
||||||
|
|
||||||
|
These are the policies for upholding our community's standards of conduct.
|
||||||
|
|
||||||
|
1. Remarks that violate the Rust standards of conduct, including hateful, hurtful, oppressive, or exclusionary remarks, are not allowed. (Cursing is allowed, but never targeting another user, and never in a hateful manner.)
|
||||||
|
2. Remarks that moderators find inappropriate, whether listed in the code of conduct or not, are also not allowed.
|
||||||
|
3. Moderators will first respond to such remarks with a warning.
|
||||||
|
4. If the warning is unheeded, the user will be "kicked," i.e., kicked out of the communication channel to cool off.
|
||||||
|
5. If the user comes back and continues to make trouble, they will be banned, i.e., indefinitely excluded.
|
||||||
|
6. Moderators may choose at their discretion to un-ban the user if it was a first offense and they offer the offended party a genuine apology.
|
||||||
|
7. If a moderator bans someone and you think it was unjustified, please take it up with that moderator, or with a different moderator, **in private**. Complaints about bans in-channel are not allowed.
|
||||||
|
8. Moderators are held to a higher standard than other community members. If a moderator creates an inappropriate situation, they should expect less leeway than others.
|
||||||
|
|
||||||
|
In the Rust community we strive to go the extra step to look out for each other. Don't just aim to be technically unimpeachable, try to be your best self. In particular, avoid flirting with offensive or sensitive issues, particularly if they're off-topic; this all too often leads to unnecessary fights, hurt feelings, and damaged trust; worse, it can drive people away from the community entirely.
|
||||||
|
|
||||||
|
And if someone takes issue with something you said or did, resist the urge to be defensive. Just stop doing what it was they complained about and apologize. Even if you feel you were misinterpreted or unfairly accused, chances are good there was something you could've communicated better — remember that it's your responsibility to make your fellow Rustaceans comfortable. Everyone wants to get along and we are all here first and foremost because we want to talk about cool technology. You will find that people will be eager to assume good intent and forgive as long as you earn their trust.
|
||||||
|
|
||||||
|
The enforcement policies listed above apply to all official embedded WG venues; including official IRC channels (#rust-embedded); GitHub repositories under rust-embedded; and all forums under rust-embedded.org (forum.rust-embedded.org).
|
||||||
|
|
||||||
|
*Adapted from the [Node.js Policy on Trolling](http://blog.izs.me/post/30036893703/policy-on-trolling) as well as the [Contributor Covenant v1.3.0](https://www.contributor-covenant.org/version/1/3/0/).*
|
||||||
|
|
||||||
|
[team]: https://github.com/rust-embedded/wg#the-riscv-team
|
218
rust/game-of-life/Cargo.lock
generated
Normal file
218
rust/game-of-life/Cargo.lock
generated
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "bare-metal"
|
||||||
|
version = "0.2.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bit_field"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "embedded-hal"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "k210-hal"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "git+https://github.com/riscv-rust/k210-hal.git?rev=725418b922d9bb6d8593ba4087b5d90f49e1b8e7#725418b922d9bb6d8593ba4087b5d90f49e1b8e7"
|
||||||
|
dependencies = [
|
||||||
|
"embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"k210-pac 0.1.0 (git+https://github.com/riscv-rust/k210-pac.git?rev=c24d654f5ad39eecf9758b852d0f54a88cefad3f)",
|
||||||
|
"nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "k210-pac"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "git+https://github.com/riscv-rust/k210-pac.git?rev=c24d654f5ad39eecf9758b852d0f54a88cefad3f#c24d654f5ad39eecf9758b852d0f54a88cefad3f"
|
||||||
|
dependencies = [
|
||||||
|
"bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"riscv 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"riscv-rt 0.5.0 (git+https://github.com/rust-embedded/riscv-rt.git?rev=ecc1344ffc9af1c88b3dd76b83ad56284672f888)",
|
||||||
|
"vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "k210-shared"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"k210-hal 0.1.0 (git+https://github.com/riscv-rust/k210-hal.git?rev=725418b922d9bb6d8593ba4087b5d90f49e1b8e7)",
|
||||||
|
"riscv 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"riscv-rt 0.5.0 (git+https://github.com/rust-embedded/riscv-rt.git?rev=ecc1344ffc9af1c88b3dd76b83ad56284672f888)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mandelbrot"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"k210-hal 0.1.0 (git+https://github.com/riscv-rust/k210-hal.git?rev=725418b922d9bb6d8593ba4087b5d90f49e1b8e7)",
|
||||||
|
"k210-shared 0.1.0",
|
||||||
|
"panic-halt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"riscv 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"riscv-rt 0.5.0 (git+https://github.com/rust-embedded/riscv-rt.git?rev=ecc1344ffc9af1c88b3dd76b83ad56284672f888)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nb"
|
||||||
|
version = "0.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "panic-halt"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "0.4.30"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "0.6.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "r0"
|
||||||
|
version = "0.2.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.5.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "riscv"
|
||||||
|
version = "0.5.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "riscv-rt"
|
||||||
|
version = "0.5.0"
|
||||||
|
source = "git+https://github.com/rust-embedded/riscv-rt.git?rev=ecc1344ffc9af1c88b3dd76b83ad56284672f888#ecc1344ffc9af1c88b3dd76b83ad56284672f888"
|
||||||
|
dependencies = [
|
||||||
|
"r0 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"riscv 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"riscv-rt-macros 0.1.5 (git+https://github.com/rust-embedded/riscv-rt.git?rev=ecc1344ffc9af1c88b3dd76b83ad56284672f888)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "riscv-rt-macros"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "git+https://github.com/rust-embedded/riscv-rt.git?rev=ecc1344ffc9af1c88b3dd76b83ad56284672f888#ecc1344ffc9af1c88b3dd76b83ad56284672f888"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"syn 0.15.34 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc_version"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "semver"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "semver-parser"
|
||||||
|
version = "0.7.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "0.15.34"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-xid"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vcell"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "void"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
|
[metadata]
|
||||||
|
"checksum bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a3caf393d93b2d453e80638d0674597020cef3382ada454faacd43d1a55a735a"
|
||||||
|
"checksum bit_field 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ed8765909f9009617974ab6b7d332625b320b33c326b1e9321382ef1999b5d56"
|
||||||
|
"checksum embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ee4908a155094da7723c2d60d617b820061e3b4efcc3d9e293d206a5a76c170b"
|
||||||
|
"checksum k210-hal 0.1.0 (git+https://github.com/riscv-rust/k210-hal.git?rev=725418b922d9bb6d8593ba4087b5d90f49e1b8e7)" = "<none>"
|
||||||
|
"checksum k210-pac 0.1.0 (git+https://github.com/riscv-rust/k210-pac.git?rev=c24d654f5ad39eecf9758b852d0f54a88cefad3f)" = "<none>"
|
||||||
|
"checksum nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b1411551beb3c11dedfb0a90a0fa256b47d28b9ec2cdff34c25a2fa59e45dbdc"
|
||||||
|
"checksum panic-halt 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812"
|
||||||
|
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
|
||||||
|
"checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db"
|
||||||
|
"checksum r0 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f"
|
||||||
|
"checksum rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9"
|
||||||
|
"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
|
||||||
|
"checksum rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0"
|
||||||
|
"checksum riscv 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "00f3ec73803f144a6474819070dfcf0b0d1662e1bd8845956850ee8f8cfbc69b"
|
||||||
|
"checksum riscv-rt 0.5.0 (git+https://github.com/rust-embedded/riscv-rt.git?rev=ecc1344ffc9af1c88b3dd76b83ad56284672f888)" = "<none>"
|
||||||
|
"checksum riscv-rt-macros 0.1.5 (git+https://github.com/rust-embedded/riscv-rt.git?rev=ecc1344ffc9af1c88b3dd76b83ad56284672f888)" = "<none>"
|
||||||
|
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
|
||||||
|
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
|
||||||
|
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
|
||||||
|
"checksum syn 0.15.34 (registry+https://github.com/rust-lang/crates.io-index)" = "a1393e4a97a19c01e900df2aec855a29f71cf02c402e2f443b8d2747c25c5dbe"
|
||||||
|
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
|
||||||
|
"checksum vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "45c297f0afb6928cd08ab1ff9d95e99392595ea25ae1b5ecf822ff8764e57a0d"
|
||||||
|
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
|
12
rust/game-of-life/Cargo.toml
Normal file
12
rust/game-of-life/Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "game-of-life"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["W.J. van der Laan <laanwj@protonmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
panic-halt = "0.2.0"
|
||||||
|
riscv-rt = "0.5.0"
|
||||||
|
k210-hal = "0.1.0"
|
||||||
|
riscv = "0.5"
|
||||||
|
k210-shared = { path = "../k210-shared" }
|
39
rust/game-of-life/README.md
Normal file
39
rust/game-of-life/README.md
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
# `game-of-life`
|
||||||
|
|
||||||
|
"Game of life" cellular automata simulation. The state can be manipulated through the touch screen.
|
||||||
|
The amount of pressure applied determines the radius of the state change.
|
||||||
|
|
||||||
|
## Getting started
|
||||||
|
|
||||||
|
Start openocd:
|
||||||
|
|
||||||
|
openocd -f dp_busblaster.cfg -f openocd.cfg
|
||||||
|
|
||||||
|
Run the example:
|
||||||
|
|
||||||
|
cargo run --release
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Copyright 2019 W. J. van der Laan
|
||||||
|
|
||||||
|
Permission to use, copy, modify, and/or distribute this software for any
|
||||||
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
copyright notice and this permission notice appear in all copies.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
## Code of Conduct
|
||||||
|
|
||||||
|
Contribution to this crate is organized under the terms of the [Rust Code of
|
||||||
|
Conduct][CoC], the maintainer of this crate, the [RISC-V team][team], promises
|
||||||
|
to intervene to uphold that code of conduct.
|
||||||
|
|
||||||
|
[CoC]: CODE_OF_CONDUCT.md
|
||||||
|
[team]: https://github.com/rust-embedded/wg#the-riscv-team
|
3
rust/game-of-life/gdb_init
Normal file
3
rust/game-of-life/gdb_init
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
target remote :3333
|
||||||
|
load
|
||||||
|
c
|
27
rust/game-of-life/gen-block-sprite.py
Executable file
27
rust/game-of-life/gen-block-sprite.py
Executable file
@ -0,0 +1,27 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
'''Script to generate BLOCK_SPRITE constant'''
|
||||||
|
BLK_SIZE = 8
|
||||||
|
|
||||||
|
def rgb565(r, g, b):
|
||||||
|
return ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
|
||||||
|
|
||||||
|
image = [[(0, 0, 0)] * BLK_SIZE for _ in range(BLK_SIZE)]
|
||||||
|
|
||||||
|
mm = BLK_SIZE//2
|
||||||
|
for y in range(BLK_SIZE//2):
|
||||||
|
for x in range(BLK_SIZE//2):
|
||||||
|
l = min(x,y)
|
||||||
|
col = (0xa8*(l+1)//mm,0x48*(l+1)//mm,0xa8*(l+1)//mm)
|
||||||
|
image[y][x] = col
|
||||||
|
image[BLK_SIZE-y-1][x] = col
|
||||||
|
image[y][BLK_SIZE-x-1] = col
|
||||||
|
image[BLK_SIZE-y-1][BLK_SIZE-x-1] = col
|
||||||
|
|
||||||
|
outb = []
|
||||||
|
for y in range(BLK_SIZE):
|
||||||
|
outb.append([((rgb565(*image[y][x*2+0])<<16) | rgb565(*image[y][x*2+1])) for x in range(BLK_SIZE//2)])
|
||||||
|
|
||||||
|
print('pub static BLOCK_SPRITE: [[u32; 4];8] = [')
|
||||||
|
for y in outb:
|
||||||
|
print(' [%s],' % (', '.join(('0x%08x' % i) for i in y)))
|
||||||
|
print(']')
|
11
rust/game-of-life/openocd.cfg
Normal file
11
rust/game-of-life/openocd.cfg
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
transport select jtag
|
||||||
|
adapter_khz 1000
|
||||||
|
|
||||||
|
set _CHIPNAME riscv
|
||||||
|
jtag newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x04e4796b
|
||||||
|
|
||||||
|
set _TARGETNAME $_CHIPNAME.cpu
|
||||||
|
target create $_TARGETNAME riscv -chain-position $_TARGETNAME
|
||||||
|
|
||||||
|
init
|
||||||
|
halt
|
205
rust/game-of-life/src/main.rs
Normal file
205
rust/game-of-life/src/main.rs
Normal file
@ -0,0 +1,205 @@
|
|||||||
|
#![allow(dead_code)]
|
||||||
|
#![allow(non_snake_case)]
|
||||||
|
#![allow(non_camel_case_types)]
|
||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
extern crate panic_halt;
|
||||||
|
|
||||||
|
use k210_hal::pac;
|
||||||
|
use k210_hal::prelude::*;
|
||||||
|
use k210_hal::stdout::Stdout;
|
||||||
|
use k210_shared::board::def::{DISP_WIDTH,DISP_HEIGHT,NS2009_SLV_ADDR,NS2009_CAL,NS2009_ADDR_BITS,NS2009_CLK};
|
||||||
|
use k210_shared::board::lcd;
|
||||||
|
use k210_shared::board::lcd_colors;
|
||||||
|
use k210_shared::board::ns2009::TouchScreen;
|
||||||
|
use k210_shared::soc::fpioa;
|
||||||
|
use k210_shared::soc::i2c;
|
||||||
|
use k210_shared::soc::sleep::usleep;
|
||||||
|
use k210_shared::soc::sysctl;
|
||||||
|
use riscv_rt::entry;
|
||||||
|
|
||||||
|
pub const BLK_SIZE: usize = 8;
|
||||||
|
pub const GRID_WIDTH: usize = DISP_WIDTH / BLK_SIZE;
|
||||||
|
pub const GRID_HEIGHT: usize = DISP_HEIGHT / BLK_SIZE;
|
||||||
|
|
||||||
|
pub type ScreenImage = [u32; DISP_WIDTH * DISP_HEIGHT / 2];
|
||||||
|
|
||||||
|
struct Universe {
|
||||||
|
state: [[bool; GRID_WIDTH*GRID_HEIGHT]; 2],
|
||||||
|
cur: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Universe {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
state: [[false; GRID_WIDTH*GRID_HEIGHT]; 2],
|
||||||
|
cur: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, x: usize, y: usize) -> bool {
|
||||||
|
self.state[self.cur][y * GRID_WIDTH + x]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, x: usize, y: usize, state: bool) {
|
||||||
|
self.state[self.cur][y * GRID_WIDTH + x] = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn toggle(&mut self, x: usize, y: usize) {
|
||||||
|
self.state[self.cur][y * GRID_WIDTH + x] ^= true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Run cellular automaton */
|
||||||
|
pub fn iterate(&mut self) {
|
||||||
|
for y in 0..GRID_HEIGHT {
|
||||||
|
// wrap around y
|
||||||
|
let ypi = if y == 0 { GRID_HEIGHT-1 } else { y-1 } * GRID_WIDTH;
|
||||||
|
let yi = y * GRID_WIDTH;
|
||||||
|
let yni = if y == GRID_HEIGHT-1 { 0 } else { y+1 } * GRID_WIDTH;
|
||||||
|
for x in 0..GRID_WIDTH {
|
||||||
|
// wrap around x
|
||||||
|
let xp = if x == 0 { GRID_WIDTH-1 } else { x-1 };
|
||||||
|
let xn = if x == GRID_WIDTH-1 { 0 } else { x+1 };
|
||||||
|
|
||||||
|
let count = self.state[self.cur][ypi + xp] as u32 +
|
||||||
|
self.state[self.cur][ypi + x] as u32 +
|
||||||
|
self.state[self.cur][ypi + xn] as u32 +
|
||||||
|
self.state[self.cur][yi + xp] as u32 +
|
||||||
|
self.state[self.cur][yi + xn] as u32 +
|
||||||
|
self.state[self.cur][yni + xp] as u32 +
|
||||||
|
self.state[self.cur][yni + x] as u32 +
|
||||||
|
self.state[self.cur][yni + xn] as u32;
|
||||||
|
|
||||||
|
self.state[1-self.cur][yi + x] = match (self.state[self.cur][yi + x], count) {
|
||||||
|
// Rule 1: Any live cell with fewer than two live neighbours
|
||||||
|
// dies, as if caused by underpopulation.
|
||||||
|
(true, x) if x < 2 => false,
|
||||||
|
// Rule 2: Any live cell with two or three live neighbours
|
||||||
|
// lives on to the next generation.
|
||||||
|
(true, 2) | (true, 3) => true,
|
||||||
|
// Rule 3: Any live cell with more than three live
|
||||||
|
// neighbours dies, as if by overpopulation.
|
||||||
|
(true, x) if x > 3 => false,
|
||||||
|
// Rule 4: Any dead cell with exactly three live neighbours
|
||||||
|
// becomes a live cell, as if by reproduction.
|
||||||
|
(false, 3) => true,
|
||||||
|
// All other cells remain in the same state.
|
||||||
|
(otherwise, _) => otherwise,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.cur = 1-self.cur;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Connect pins to internal functions */
|
||||||
|
fn io_mux_init() {
|
||||||
|
/* Init SPI IO map and function settings */
|
||||||
|
fpioa::set_function(37, fpioa::function::gpiohs(lcd::RST_GPIONUM));
|
||||||
|
fpioa::set_io_pull(37, fpioa::pull::DOWN); // outputs must be pull-down
|
||||||
|
fpioa::set_function(38, fpioa::function::gpiohs(lcd::DCX_GPIONUM));
|
||||||
|
fpioa::set_io_pull(38, fpioa::pull::DOWN);
|
||||||
|
fpioa::set_function(36, fpioa::function::SPI0_SS3);
|
||||||
|
fpioa::set_function(39, fpioa::function::SPI0_SCLK);
|
||||||
|
|
||||||
|
/* I2C0 for touch-screen */
|
||||||
|
fpioa::set_function(30, fpioa::function::I2C0_SCLK);
|
||||||
|
fpioa::set_function(31, fpioa::function::I2C0_SDA);
|
||||||
|
|
||||||
|
sysctl::set_spi0_dvp_data(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Set correct voltage for pins */
|
||||||
|
fn io_set_power() {
|
||||||
|
/* Set dvp and spi pin to 1.8V */
|
||||||
|
sysctl::set_power_mode(sysctl::power_bank::BANK6, sysctl::io_power_mode::V18);
|
||||||
|
sysctl::set_power_mode(sysctl::power_bank::BANK7, sysctl::io_power_mode::V18);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** How to show a block */
|
||||||
|
pub static BLOCK_SPRITE: [[u32; 4];8] = [
|
||||||
|
[0x28852885, 0x28852885, 0x28852885, 0x28852885],
|
||||||
|
[0x2885512a, 0x512a512a, 0x512a512a, 0x512a2885],
|
||||||
|
[0x2885512a, 0x79af79af, 0x79af79af, 0x512a2885],
|
||||||
|
[0x2885512a, 0x79afaa55, 0xaa5579af, 0x512a2885],
|
||||||
|
[0x2885512a, 0x79afaa55, 0xaa5579af, 0x512a2885],
|
||||||
|
[0x2885512a, 0x79af79af, 0x79af79af, 0x512a2885],
|
||||||
|
[0x2885512a, 0x512a512a, 0x512a512a, 0x512a2885],
|
||||||
|
[0x28852885, 0x28852885, 0x28852885, 0x28852885],
|
||||||
|
];
|
||||||
|
|
||||||
|
#[entry]
|
||||||
|
fn main() -> ! {
|
||||||
|
let p = pac::Peripherals::take().unwrap();
|
||||||
|
|
||||||
|
let clocks = k210_hal::clock::Clocks::new();
|
||||||
|
|
||||||
|
usleep(200000);
|
||||||
|
|
||||||
|
// Configure UART
|
||||||
|
let serial = p.UARTHS.constrain(115_200.bps(), &clocks);
|
||||||
|
let (mut tx, _) = serial.split();
|
||||||
|
|
||||||
|
let mut stdout = Stdout(&mut tx);
|
||||||
|
|
||||||
|
io_mux_init();
|
||||||
|
io_set_power();
|
||||||
|
|
||||||
|
lcd::init();
|
||||||
|
lcd::set_direction(lcd::direction::YX_LRUD);
|
||||||
|
lcd::clear(lcd_colors::PURPLE);
|
||||||
|
|
||||||
|
let mut image: ScreenImage = [0; DISP_WIDTH * DISP_HEIGHT / 2];
|
||||||
|
|
||||||
|
writeln!(stdout, "NS2009 init").unwrap();
|
||||||
|
i2c::init(NS2009_SLV_ADDR, NS2009_ADDR_BITS, NS2009_CLK);
|
||||||
|
|
||||||
|
let mut filter = if let Some(filter) = TouchScreen::init(NS2009_CAL) {
|
||||||
|
filter
|
||||||
|
} else {
|
||||||
|
writeln!(stdout, "NS2009 init failure").unwrap();
|
||||||
|
panic!("Fatal error");
|
||||||
|
};
|
||||||
|
let mut universe = Universe::new();
|
||||||
|
/* glider:
|
||||||
|
010
|
||||||
|
001
|
||||||
|
111
|
||||||
|
*/
|
||||||
|
universe.set(GRID_WIDTH/2+0, GRID_HEIGHT/2-1, true);
|
||||||
|
universe.set(GRID_WIDTH/2+1, GRID_HEIGHT/2+0, true);
|
||||||
|
universe.set(GRID_WIDTH/2-1, GRID_HEIGHT/2+1, true);
|
||||||
|
universe.set(GRID_WIDTH/2+0, GRID_HEIGHT/2+1, true);
|
||||||
|
universe.set(GRID_WIDTH/2+1, GRID_HEIGHT/2+1, true);
|
||||||
|
loop {
|
||||||
|
if let Some(ev) = filter.poll() {
|
||||||
|
// writeln!(stdout, "{:?}", ev).unwrap();
|
||||||
|
let x = ev.x / (BLK_SIZE as i32);
|
||||||
|
let y = ev.y / (BLK_SIZE as i32);
|
||||||
|
// Determine radius of changed area from pressure
|
||||||
|
let r = ev.z / 300;
|
||||||
|
for yi in y-r..y+r+1 {
|
||||||
|
for xi in x-r..x+r+1 {
|
||||||
|
if (xi as usize) < DISP_WIDTH && (yi as usize) < DISP_HEIGHT {
|
||||||
|
universe.toggle(xi as usize, yi as usize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for y in 0..GRID_HEIGHT {
|
||||||
|
for x in 0..GRID_WIDTH {
|
||||||
|
let state = universe.get(x, y);
|
||||||
|
for yi in 0..BLK_SIZE {
|
||||||
|
for xi in 0..BLK_SIZE/2 {
|
||||||
|
let idx = (y * BLK_SIZE + yi) * DISP_WIDTH/2 + x * BLK_SIZE/2 + xi;
|
||||||
|
image[idx] = if state { BLOCK_SPRITE[yi][xi] } else { 0 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lcd::draw_picture(0, 0, DISP_WIDTH as u16, DISP_HEIGHT as u16, &image);
|
||||||
|
|
||||||
|
universe.iterate();
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,4 @@
|
|||||||
|
pub mod def;
|
||||||
pub mod lcd;
|
pub mod lcd;
|
||||||
pub mod lcd_colors;
|
pub mod lcd_colors;
|
||||||
|
pub mod ns2009;
|
||||||
|
18
rust/k210-shared/src/board/def.rs
Normal file
18
rust/k210-shared/src/board/def.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/** Global board definitions */
|
||||||
|
|
||||||
|
/** Display width in pixels */
|
||||||
|
pub const DISP_WIDTH: usize = 320;
|
||||||
|
/** Display height in pixels */
|
||||||
|
pub const DISP_HEIGHT: usize = 240;
|
||||||
|
|
||||||
|
/** I2C address of NS2009 */
|
||||||
|
pub const NS2009_SLV_ADDR: u16 = 0x48;
|
||||||
|
|
||||||
|
/** I2C address bits for NS2009 */
|
||||||
|
pub const NS2009_ADDR_BITS: u32 = 7;
|
||||||
|
|
||||||
|
/** I2C clock speed for NS2009 */
|
||||||
|
pub const NS2009_CLK: u32 = 100000;
|
||||||
|
|
||||||
|
/** Calibration matrix for touch screen */
|
||||||
|
pub const NS2009_CAL: [i32; 7] = [65, 5853, -1083592, -4292, -15, 16450115, 65536];
|
134
rust/k210-shared/src/board/ns2009.rs
Normal file
134
rust/k210-shared/src/board/ns2009.rs
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
use core::result::Result;
|
||||||
|
|
||||||
|
use crate::soc::i2c;
|
||||||
|
use crate::util::filters;
|
||||||
|
|
||||||
|
/* low level functions */
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub enum command {
|
||||||
|
LOW_POWER_READ_X = 0xc0,
|
||||||
|
LOW_POWER_READ_Y = 0xd0,
|
||||||
|
LOW_POWER_READ_Z1 = 0xe0,
|
||||||
|
LOW_POWER_READ_Z2 = 0xf0,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(cmd: command) -> Result<u16, ()>
|
||||||
|
{
|
||||||
|
let mut buf = [0u8; 2];
|
||||||
|
if i2c::recv_data(&[cmd as u8], &mut buf).is_ok() {
|
||||||
|
Ok(((buf[0] as u16) << 4) | ((buf[1] as u16) >> 4))
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Position filter */
|
||||||
|
pub struct TSFilter {
|
||||||
|
mx: filters::Median<i32>,
|
||||||
|
my: filters::Median<i32>,
|
||||||
|
nx: filters::Mean<i32>,
|
||||||
|
ny: filters::Mean<i32>,
|
||||||
|
cal: [i32; 7],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TSFilter {
|
||||||
|
/* input: calibration matrix */
|
||||||
|
pub fn new(cal: [i32; 7]) -> Self {
|
||||||
|
Self {
|
||||||
|
mx: filters::Median::<i32>::new(),
|
||||||
|
my: filters::Median::<i32>::new(),
|
||||||
|
nx: filters::Mean::<i32>::new(),
|
||||||
|
ny: filters::Mean::<i32>::new(),
|
||||||
|
cal,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, x: u16, y: u16) -> (i32, i32) {
|
||||||
|
let tx = self.mx.update(x as i32);
|
||||||
|
let ty = self.my.update(y as i32);
|
||||||
|
let tx = self.nx.update(tx);
|
||||||
|
let ty = self.ny.update(ty);
|
||||||
|
((self.cal[2] + self.cal[0] * tx + self.cal[1] * ty) / self.cal[6],
|
||||||
|
(self.cal[5] + self.cal[3] * tx + self.cal[4] * ty) / self.cal[6])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.mx.clear();
|
||||||
|
self.my.clear();
|
||||||
|
self.nx.clear();
|
||||||
|
self.ny.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Event kind */
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub enum EventKind {
|
||||||
|
Begin,
|
||||||
|
Move,
|
||||||
|
End,
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Touch screen event */
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Event {
|
||||||
|
pub kind: EventKind,
|
||||||
|
pub x: i32,
|
||||||
|
pub y: i32,
|
||||||
|
pub z: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/** High-level touch screen abstraction */
|
||||||
|
pub struct TouchScreen {
|
||||||
|
filter: TSFilter,
|
||||||
|
press: bool,
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TouchScreen {
|
||||||
|
/* input: calibration matrix */
|
||||||
|
pub fn init(cal: [i32; 7]) -> Option<Self> {
|
||||||
|
// Do a test read
|
||||||
|
if let Ok(_) = read(command::LOW_POWER_READ_Z1) {
|
||||||
|
Some(Self {
|
||||||
|
filter: TSFilter::new(cal),
|
||||||
|
press: false,
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Poll for touch screen event, return the current event (Begin, Move, End) or None */
|
||||||
|
pub fn poll(&mut self) -> Option<Event> {
|
||||||
|
let mut ev: Option<Event> = None;
|
||||||
|
if let Ok(z1) = read(command::LOW_POWER_READ_Z1) {
|
||||||
|
if z1 > 80 && z1 < 2000 {
|
||||||
|
if let (Ok(x), Ok(y)) = (read(command::LOW_POWER_READ_X), read(command::LOW_POWER_READ_Y)) {
|
||||||
|
let (x, y) = self.filter.update(x, y);
|
||||||
|
if !self.press
|
||||||
|
{
|
||||||
|
self.press = true;
|
||||||
|
ev = Some(Event { kind: EventKind::Begin, x, y, z: z1 as i32 });
|
||||||
|
}
|
||||||
|
else if self.x != x || self.y != y {
|
||||||
|
ev = Some(Event { kind: EventKind::Move, x, y, z: z1 as i32 });
|
||||||
|
}
|
||||||
|
self.x = x;
|
||||||
|
self.y = y;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if self.press
|
||||||
|
{
|
||||||
|
self.filter.clear();
|
||||||
|
self.press = false;
|
||||||
|
ev = Some(Event { kind: EventKind::End, x: self.x, y: self.y, z: 0 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ev
|
||||||
|
}
|
||||||
|
}
|
@ -5,3 +5,4 @@
|
|||||||
|
|
||||||
pub mod board;
|
pub mod board;
|
||||||
pub mod soc;
|
pub mod soc;
|
||||||
|
mod util;
|
||||||
|
1
rust/k210-shared/src/util.rs
Normal file
1
rust/k210-shared/src/util.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod filters;
|
178
rust/k210-shared/src/util/filters.rs
Normal file
178
rust/k210-shared/src/util/filters.rs
Normal file
@ -0,0 +1,178 @@
|
|||||||
|
/** Moving filters for touch screen noise filtering */
|
||||||
|
use core::ops::{AddAssign, SubAssign, Div};
|
||||||
|
use core::cmp::PartialOrd;
|
||||||
|
|
||||||
|
/* TODO: really want const_generics here */
|
||||||
|
/* must be smaller than 256 */
|
||||||
|
const S: usize = 5;
|
||||||
|
|
||||||
|
/** Moving average filter */
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Mean<T> {
|
||||||
|
buffer: [T; S],
|
||||||
|
index: usize,
|
||||||
|
count: usize,
|
||||||
|
sum: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Copy + From<u8> + AddAssign + SubAssign + Div> Mean<T>
|
||||||
|
where T: Div<Output = T> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
buffer: [T::from(0); S],
|
||||||
|
index: 0,
|
||||||
|
count: 0,
|
||||||
|
sum: T::from(0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, value: T) -> T {
|
||||||
|
self.sum -= self.buffer[self.index];
|
||||||
|
self.sum += value;
|
||||||
|
self.buffer[self.index] = value;
|
||||||
|
self.index = if self.index == S - 1 { 0 } else { self.index + 1 };
|
||||||
|
if self.count < S {
|
||||||
|
self.count += 1;
|
||||||
|
}
|
||||||
|
return self.sum / T::from(self.count as u8);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.buffer = [T::from(0); S];
|
||||||
|
self.index = 0;
|
||||||
|
self.count = 0;
|
||||||
|
self.sum = T::from(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Moving median filter */
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Median<T> {
|
||||||
|
buffer: [T; S],
|
||||||
|
index: [usize; S],
|
||||||
|
position: usize,
|
||||||
|
count: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Copy + From<u8> + PartialOrd> Median<T>
|
||||||
|
where T: Div<Output = T> {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
buffer: [T::from(0); S],
|
||||||
|
index: [0; S],
|
||||||
|
position: 0,
|
||||||
|
count: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, value: T) -> T {
|
||||||
|
let pos = self.position;
|
||||||
|
let cnt = self.count;
|
||||||
|
let result: T;
|
||||||
|
|
||||||
|
if cnt > 0 {
|
||||||
|
let oval;
|
||||||
|
let mut oidx;
|
||||||
|
let dummy_oval;
|
||||||
|
if cnt == S {
|
||||||
|
oidx = 0;
|
||||||
|
while self.index[oidx] != pos {
|
||||||
|
oidx += 1;
|
||||||
|
}
|
||||||
|
oval = self.buffer[pos];
|
||||||
|
dummy_oval = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self.index[pos] = pos;
|
||||||
|
oidx = pos;
|
||||||
|
oval = T::from(0);
|
||||||
|
dummy_oval = true;
|
||||||
|
}
|
||||||
|
self.buffer[pos] = value;
|
||||||
|
|
||||||
|
if !dummy_oval && oval < value {
|
||||||
|
while (oidx + 1) != cnt
|
||||||
|
{
|
||||||
|
oidx += 1;
|
||||||
|
let cidx = self.index[oidx];
|
||||||
|
if self.buffer[cidx] < value {
|
||||||
|
self.index[oidx] = self.index[oidx - 1];
|
||||||
|
self.index[oidx - 1] = cidx;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if dummy_oval || oval > value {
|
||||||
|
while oidx != 0
|
||||||
|
{
|
||||||
|
oidx -= 1;
|
||||||
|
let cidx = self.index[oidx];
|
||||||
|
if self.buffer[cidx] > value {
|
||||||
|
self.index[oidx] = self.index[oidx + 1];
|
||||||
|
self.index[oidx + 1] = cidx;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = self.buffer[self.index[cnt / 2]];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self.buffer[0] = value;
|
||||||
|
self.index[0] = 0;
|
||||||
|
self.position = 0;
|
||||||
|
self.count = 0;
|
||||||
|
result = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.position = if pos == S - 1 { 0 } else { pos + 1 };
|
||||||
|
if cnt < S {
|
||||||
|
self.count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.buffer = [T::from(0); S];
|
||||||
|
self.index = [0; S];
|
||||||
|
self.position = 0;
|
||||||
|
self.count = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mean() {
|
||||||
|
let mut t = Mean::<i32>::new();
|
||||||
|
assert_eq!(t.update(10), 10);
|
||||||
|
assert_eq!(t.update(20), 15);
|
||||||
|
assert_eq!(t.update(0), 10);
|
||||||
|
assert_eq!(t.update(10), 10);
|
||||||
|
assert_eq!(t.update(60), 20);
|
||||||
|
assert_eq!(t.update(10), 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_median() {
|
||||||
|
let mut t = Median::<i32>::new();
|
||||||
|
assert_eq!(t.update(10), 10); // 10
|
||||||
|
assert_eq!(t.update(20), 10); // 10 20
|
||||||
|
assert_eq!(t.update(30), 20); // 10 20 30
|
||||||
|
assert_eq!(t.update(40), 20); // 10 20 30 40
|
||||||
|
assert_eq!(t.update(50), 30); // 10 20 30 40 50
|
||||||
|
assert_eq!(t.update(0), 30); // 0 20 30 40 50
|
||||||
|
assert_eq!(t.update(60), 40); // 0 30 40 50 60
|
||||||
|
assert_eq!(t.update(10), 40); // 0 10 40 50 60
|
||||||
|
assert_eq!(t.update(10), 10); // 0 10 10 50 60
|
||||||
|
assert_eq!(t.update(15), 10); // 0 10 10 15 60
|
||||||
|
assert_eq!(t.update(70), 15); // 10 10 15 60 70
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user