Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http-example): expose the internal temp sensor as a JSON endpoint #125

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/embassy-http-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ embassy-net = { workspace = true, features = ["tcp"] }
embassy-sync = { workspace = true }
embassy-time = { workspace = true, default-features = false }
embedded-io-async = "0.6.0"
fixed = "1.24.0"
heapless = { workspace = true }
httparse = { version = "1.8.0", default-features = false }
picoserve = { version = "0.8.0", default-features = false, features = ["embassy"] }
Expand All @@ -23,3 +24,4 @@ embassy-nrf = { workspace = true, optional = true }

[features]
button-readings = ["dep:embassy-nrf"]
internal-temp = ["riot-rs/internal-temp"]
9 changes: 9 additions & 0 deletions examples/embassy-http-server/laze.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ apps:
- ?release
- network
- ?button-readings
- ?internal-temp

modules:
- name: button-readings
Expand All @@ -17,3 +18,11 @@ modules:
global:
FEATURES:
- button-readings

- name: internal-temp
context:
- nrf52840
env:
global:
FEATURES:
- internal-temp
27 changes: 25 additions & 2 deletions examples/embassy-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod routes;

use riot_rs as _;

use riot_rs::embassy::network;
use riot_rs::embassy::{arch, network};
use riot_rs::rt::debug::println;

use embassy_net::tcp::TcpSocket;
Expand All @@ -23,6 +23,8 @@ use embassy_nrf::gpio::{Input, Pin, Pull};
struct AppState {
#[cfg(feature = "button-readings")]
buttons: ButtonInputs,
#[cfg(context = "nrf52840")]
temp: TempInput,
}

#[cfg(feature = "button-readings")]
Expand All @@ -44,6 +46,17 @@ impl picoserve::extract::FromRef<AppState> for ButtonInputs {
}
}

#[cfg(context = "nrf52840")]
#[derive(Copy, Clone)]
struct TempInput(&'static Mutex<CriticalSectionRawMutex, embassy_nrf::temp::Temp<'static>>);

#[cfg(context = "nrf52840")]
impl picoserve::extract::FromRef<AppState> for TempInput {
fn from_ref(state: &AppState) -> Self {
state.temp
}
}

type AppRouter = impl picoserve::routing::PathRouter<AppState>;

const WEB_TASK_POOL_SIZE: usize = 2;
Expand Down Expand Up @@ -89,7 +102,7 @@ async fn web_task(
use riot_rs::embassy::{arch::OptionalPeripherals, Spawner};
#[riot_rs::embassy::distributed_slice(riot_rs::embassy::EMBASSY_TASKS)]
#[linkme(crate = riot_rs::embassy::linkme)]
fn web_server_init(spawner: &Spawner, peripherals: &mut OptionalPeripherals) {
fn web_server_init(spawner: &Spawner, peripherals: &mut arch::OptionalPeripherals) {
#[cfg(feature = "button-readings")]
let button_inputs = {
let buttons = pins::Buttons::take_from(peripherals).unwrap();
Expand All @@ -104,10 +117,18 @@ fn web_server_init(spawner: &Spawner, peripherals: &mut OptionalPeripherals) {
ButtonInputs(make_static!(Mutex::new(buttons)))
};

#[cfg(context = "nrf52840")]
let temp = {
let temp = arch::internal_temp::sensor(peripherals.TEMP.take().unwrap());
TempInput(make_static!(Mutex::new(temp)))
};

fn make_app() -> picoserve::Router<AppRouter, AppState> {
let router = picoserve::Router::new().route("/", get(routes::index));
#[cfg(feature = "button-readings")]
let router = router.route("/buttons", get(routes::buttons));
#[cfg(context = "nrf52840")]
let router = router.route("/api/temp", get(routes::temp));
router
}

Expand All @@ -123,6 +144,8 @@ fn web_server_init(spawner: &Spawner, peripherals: &mut OptionalPeripherals) {
let app_state = AppState {
#[cfg(feature = "button-readings")]
buttons: button_inputs,
#[cfg(context = "nrf52840")]
temp,
};
spawner.spawn(web_task(id, app, config, app_state)).unwrap();
}
Expand Down
3 changes: 3 additions & 0 deletions examples/embassy-http-server/src/pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ define_peripherals!(Buttons {
btn3: P0_24,
btn4: P0_25,
});

#[cfg(context = "nrf52840")]
define_peripherals!(Temp { temp: TEMP });
6 changes: 6 additions & 0 deletions examples/embassy-http-server/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ pub mod index;
#[cfg(feature = "button-readings")]
pub mod buttons;

#[cfg(context = "nrf52840")]
pub mod temp;

pub use index::index;

#[cfg(feature = "button-readings")]
pub use buttons::buttons;

#[cfg(context = "nrf52840")]
pub use temp::temp;
19 changes: 19 additions & 0 deletions examples/embassy-http-server/src/routes/temp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use fixed::traits::LossyInto;
use picoserve::{
extract::State,
response::{IntoResponse, Json},
};

use crate::TempInput;

pub async fn temp(State(TempInput(temp)): State<TempInput>) -> impl IntoResponse {
let temp = (100 * temp.lock().await.read().await).lossy_into();

Json(JsonTemp { temp })
}

#[derive(serde::Serialize)]
struct JsonTemp {
// Temperature in hundredths of degrees Celsius
temp: i32,
}
1 change: 1 addition & 0 deletions src/riot-rs-embassy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ time = ["dep:embassy-time", "embassy-executor/integrated-timers"]
usb = ["dep:embassy-usb"]
net = ["dep:embassy-net"]
usb-ethernet = ["usb", "net"]
internal-temp = []

wifi-cyw43 = ["dep:cyw43", "dep:cyw43-pio", "dep:embassy-net-driver-channel", "net"]

Expand Down
13 changes: 13 additions & 0 deletions src/riot-rs-embassy/src/arch/nrf52.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ pub fn init(config: Config) -> OptionalPeripherals {
let peripherals = embassy_nrf::init(config);
OptionalPeripherals::from(peripherals)
}

#[cfg(feature = "internal-temp")]
pub mod internal_temp {
use embassy_nrf::{peripherals, temp};

embassy_nrf::bind_interrupts!(struct TempIrqs {
TEMP => embassy_nrf::temp::InterruptHandler;
});

pub fn sensor(temp: peripherals::TEMP) -> temp::Temp<'static> {
temp::Temp::new(temp, TempIrqs)
}
}
1 change: 1 addition & 0 deletions src/riot-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static_cell = { workspace = true }
default = ["riot-rs-rt/_panic-handler"]

debug-console = ["riot-rs-rt/debug-console"]
internal-temp = ["riot-rs-embassy/internal-temp"]
net = ["riot-rs-embassy/net"]
# Allows to have no boards selected, useful to run platform-independent tooling
no-boards = ["riot-rs-boards/no-boards"]
Expand Down
Loading