Skip to content

Commit 6321991

Browse files
committed
Initial Commit! It's something
0 parents  commit 6321991

File tree

9 files changed

+221
-0
lines changed

9 files changed

+221
-0
lines changed

.cargo/config.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[build]
2+
target = "xtensa-esp32-espidf"
3+
4+
[target.xtensa-esp32-espidf]
5+
linker = "ldproxy"
6+
runner = "espflash flash --monitor" # Select this runner for espflash v3.x.x
7+
rustflags = [
8+
"--cfg",
9+
"espidf_time64",
10+
] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110
11+
12+
[unstable]
13+
build-std = ["std", "panic_abort"]
14+
15+
[env]
16+
MCU = "esp32"
17+
# Note: this variable is not used by the pio builder (`cargo build --features pio`)
18+
ESP_IDF_VERSION = "v5.2.3"
19+
20+
# Workaround for https://github.com/esp-rs/esp-idf-template/issues/174
21+
CRATE_CC_NO_DEFAULTS = "1"

.github/workflows/rust_ci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Continuous Integration
2+
3+
on:
4+
push:
5+
paths-ignore:
6+
- "**/README.md"
7+
pull_request:
8+
workflow_dispatch:
9+
10+
env:
11+
CARGO_TERM_COLOR: always
12+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13+
14+
jobs:
15+
rust-checks:
16+
name: Rust Checks
17+
runs-on: ubuntu-latest
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
action:
22+
- command: build
23+
args: --release
24+
- command: fmt
25+
args: --all -- --check --color always
26+
- command: clippy
27+
args: --all-targets --all-features --workspace -- -D warnings
28+
steps:
29+
- name: Checkout repository
30+
uses: actions/checkout@v4
31+
- name: Setup Rust
32+
uses: esp-rs/[email protected]
33+
with:
34+
default: true
35+
buildtargets: esp32
36+
ldproxy: true
37+
- name: Enable caching
38+
uses: Swatinem/rust-cache@v2
39+
- name: Run command
40+
run: cargo ${{ matrix.action.command }} ${{ matrix.action.args }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.vscode
2+
/.embuild
3+
/target
4+
/Cargo.lock

.zed/settings.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Folder-specific settings
2+
//
3+
// For a full list of overridable settings, and general information on folder-specific settings,
4+
// see the documentation: https://zed.dev/docs/configuring-zed#settings-files
5+
{
6+
"lsp": {
7+
"rust-analyzer": {
8+
"initialization_options": {
9+
// To disable the checking entirely
10+
// (ignores all cargo and check settings below)
11+
// "checkOnSave": false
12+
}
13+
}
14+
}
15+
}

Cargo.toml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[package]
2+
name = "mff-hr-v1"
3+
version = "0.1.0"
4+
authors = ["nullstalgia <[email protected]>"]
5+
edition = "2021"
6+
resolver = "2"
7+
rust-version = "1.77"
8+
9+
[[bin]]
10+
name = "mff-hr-v1"
11+
harness = false # do not use the built in cargo test harness -> resolve rust-analyzer errors
12+
13+
[profile.release]
14+
opt-level = "s"
15+
16+
[profile.dev]
17+
debug = true # Symbols are nice and they don't increase the size on Flash
18+
opt-level = "z"
19+
20+
[features]
21+
default = ["std", "embassy", "esp-idf-svc/native"]
22+
23+
pio = ["esp-idf-svc/pio"]
24+
std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"]
25+
alloc = ["esp-idf-svc/alloc"]
26+
nightly = ["esp-idf-svc/nightly"]
27+
experimental = ["esp-idf-svc/experimental"]
28+
embassy = [
29+
"esp-idf-svc/embassy-sync",
30+
"esp-idf-svc/critical-section",
31+
"esp-idf-svc/embassy-time-driver",
32+
]
33+
34+
35+
[dependencies]
36+
log = "0.4"
37+
esp-idf-svc = { version = "0.49", default-features = false }
38+
esp-idf-hal = { version = "0.44", default-features = false }
39+
esp-idf-sys = { version = "0.35", default-features = false }
40+
esp32-nimble = "0.8.2"
41+
anyhow = "1.0.93"
42+
bstr = "1.11.0"
43+
44+
[build-dependencies]
45+
embuild = { version = "0.32.0", features = ["espidf"] }
46+
cc = "=1.1.30" # Necessary until a new version of `esp-idf-sys` is released

build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
embuild::espidf::sysenv::output();
3+
}

rust-toolchain.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[toolchain]
2+
channel = "esp"

sdkconfig.defaults

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
2+
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000
3+
4+
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
5+
# This allows to use 1 ms granularity for thread sleeps (10 ms by default).
6+
#CONFIG_FREERTOS_HZ=1000
7+
8+
# Workaround for https://github.com/espressif/esp-idf/issues/7631
9+
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
10+
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n
11+
CONFIG_BT_ENABLED=y
12+
CONFIG_BT_BLE_ENABLED=y
13+
CONFIG_BT_BLUEDROID_ENABLED=n
14+
CONFIG_BT_NIMBLE_ENABLED=y

src/main.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use bstr::ByteSlice;
2+
use esp32_nimble::{uuid128, BLEClient, BLEDevice, BLEScan};
3+
use esp_idf_svc::hal::{
4+
prelude::Peripherals,
5+
task::block_on,
6+
timer::{TimerConfig, TimerDriver},
7+
};
8+
9+
fn main() -> anyhow::Result<()> {
10+
esp_idf_svc::sys::link_patches();
11+
esp_idf_svc::log::EspLogger::initialize_default();
12+
13+
let peripherals = Peripherals::take()?;
14+
let mut timer = TimerDriver::new(peripherals.timer00, &TimerConfig::new())?;
15+
16+
block_on(async {
17+
let ble_device = BLEDevice::take();
18+
let mut ble_scan = BLEScan::new();
19+
let device = ble_scan
20+
.active_scan(true)
21+
.interval(100)
22+
.window(99)
23+
.start(ble_device, 10000, |device, data| {
24+
if let Some(name) = data.name() {
25+
if name.contains_str("Polar H10") {
26+
return Some(*device);
27+
}
28+
}
29+
None
30+
})
31+
.await?;
32+
33+
if let Some(device) = device {
34+
let mut client = BLEClient::new();
35+
client.on_connect(|client| {
36+
client.update_conn_params(120, 120, 0, 60).unwrap();
37+
});
38+
client.connect(&device.addr()).await?;
39+
40+
let service = client
41+
.get_service(uuid128!("0000180f-0000-1000-8000-00805f9b34fb"))
42+
.await?;
43+
44+
let uuid = uuid128!("00002a19-0000-1000-8000-00805f9b34fb");
45+
let characteristic = service.get_characteristic(uuid).await?;
46+
let value = characteristic.read_value().await?;
47+
::log::info!("{} value: {:?}", characteristic, value);
48+
49+
let service = client
50+
.get_service(uuid128!("0000180d-0000-1000-8000-00805f9b34fb"))
51+
.await?;
52+
53+
let uuid = uuid128!("00002a37-0000-1000-8000-00805f9b34fb");
54+
let characteristic = service.get_characteristic(uuid).await?;
55+
56+
if !characteristic.can_notify() {
57+
::log::error!("characteristic can't notify: {}", characteristic);
58+
return anyhow::Ok(());
59+
}
60+
61+
::log::info!("subscribe to {}", characteristic);
62+
characteristic
63+
.on_notify(|data| {
64+
::log::info!("{:?}", data);
65+
})
66+
.subscribe_notify(false)
67+
.await?;
68+
69+
timer.delay(timer.tick_hz() * 10).await?;
70+
71+
client.disconnect()?;
72+
}
73+
74+
anyhow::Ok(())
75+
})
76+
}

0 commit comments

Comments
 (0)