Skip to content

Commit

Permalink
Merge pull request #34 from Strosel/notification
Browse files Browse the repository at this point in the history
feat: Wrapper over notification API
  • Loading branch information
str4d authored May 10, 2023
2 parents 39f8ffd + 16c3f4b commit 684484d
Show file tree
Hide file tree
Showing 11 changed files with 698 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
1 change: 1 addition & 0 deletions crates/flipperzero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ rand_core = "0.6"

# Toolbox
digest = "0.10"
bitflags = "1.0"

[dev-dependencies]
flipperzero-alloc.workspace = true
Expand Down
46 changes: 16 additions & 30 deletions crates/flipperzero/examples/notification.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
//! Demonstrates use of the Flipper Zero notification API.
//! This currently uses the `flipperzero-sys` crate as it is not currently
//! exposed in the high-level `flipperzero` crate.
//!
//! See https://github.com/flipperdevices/flipperzero-firmware/blob/0.74.2/applications/services/notification/notification_messages.h#L66
//! for possible message sequences.
//! Notification example for Flipper Zero
#![no_main]
#![no_std]
Expand All @@ -12,18 +7,15 @@
extern crate flipperzero_rt;

// Required for allocator
#[cfg(feature = "alloc")]
extern crate flipperzero_alloc;

use core::ffi::c_char;
use core::time::Duration;

use flipperzero::furi::thread::sleep;
use flipperzero::{
furi::thread::sleep,
notification::{feedback, led, NotificationService},
};
use flipperzero_rt::{entry, manifest};
use flipperzero_sys as sys;

/// Record for the notifications app
const RECORD_NOTIFICATION: *const c_char = sys::c_string!("notification");

// Define the FAP Manifest for this application
manifest!(name = "Rust notification example");
Expand All @@ -33,24 +25,18 @@ entry!(main);

// Entry point
fn main(_args: *mut u8) -> i32 {
let notification_app =
unsafe { sys::furi::UnsafeRecord::<sys::NotificationApp>::open(RECORD_NOTIFICATION) };

unsafe {
// Set the notification LED to different colours
for sequence in [
&sys::sequence_set_only_red_255,
&sys::sequence_set_only_green_255,
&sys::sequence_set_only_blue_255,
] {
sys::notification_message(notification_app.as_ptr(), sequence);
sleep(Duration::from_secs(1));
}
sys::notification_message(notification_app.as_ptr(), &sys::sequence_reset_rgb);

// Success!
sys::notification_message(notification_app.as_ptr(), &sys::sequence_success);
let mut app = NotificationService::open();

// Set the notification LED to different colours
for sequences in [&led::ONLY_RED, &led::ONLY_GREEN, &led::ONLY_BLUE] {
app.notify(sequences);
sleep(Duration::from_secs(1));
}

app.notify(&led::RESET_RGB);

// Success!
app.notify_blocking(&feedback::SUCCESS);

0
}
1 change: 1 addition & 0 deletions crates/flipperzero/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod furi;
pub mod gui;
pub mod io;
pub mod macros;
pub mod notification;
pub mod storage;
pub mod toolbox;

Expand Down
18 changes: 18 additions & 0 deletions crates/flipperzero/src/notification/backlight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::{messages, NotificationSequence};
use crate::notification_sequence;

pub const RESET_DISPLAY: NotificationSequence =
notification_sequence![messages::DISPLAY_BACKLIGHT_OFF];

pub const DISPLAY_BACKLIGHT_ON: NotificationSequence =
notification_sequence![messages::DISPLAY_BACKLIGHT_ON];
pub const DISPLAY_BACKLIGHT_OFF: NotificationSequence =
notification_sequence![messages::DISPLAY_BACKLIGHT_OFF];

pub const DISPLAY_BACKLIGHT_ENFORCE_ON: NotificationSequence =
notification_sequence![messages::DISPLAY_BACKLIGHT_ENFORCE_ON];
pub const DISPLAY_BACKLIGHT_ENFORCE_AUTO: NotificationSequence =
notification_sequence![messages::DISPLAY_BACKLIGHT_ENFORCE_AUTO];

pub const DISPLAY_BACKLIGHT_OFF_DELAY_1000: NotificationSequence =
notification_sequence![messages::DELAY_1000, messages::DISPLAY_BACKLIGHT_OFF];
68 changes: 68 additions & 0 deletions crates/flipperzero/src/notification/feedback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use super::{messages, sounds, NotificationSequence};
use crate::notification_sequence;

pub const CHARGING: NotificationSequence =
notification_sequence![messages::RED_255, messages::GREEN_0];
pub const CHARGED: NotificationSequence =
notification_sequence![messages::GREEN_255, messages::RED_0];
pub const NOT_CHARGING: NotificationSequence =
notification_sequence![messages::RED_0, messages::GREEN_0];

pub const SUCCESS: NotificationSequence = notification_sequence![
messages::DISPLAY_BACKLIGHT_ON,
messages::GREEN_255,
messages::VIBRO_ON,
sounds::C5,
messages::DELAY_50,
messages::VIBRO_OFF,
sounds::E5,
messages::DELAY_50,
sounds::G5,
messages::DELAY_50,
sounds::C6,
messages::DELAY_50,
messages::SOUND_OFF,
];

pub const ERROR: NotificationSequence = notification_sequence![
messages::DISPLAY_BACKLIGHT_ON,
messages::RED_255,
messages::VIBRO_ON,
sounds::C5,
messages::DELAY_100,
messages::VIBRO_OFF,
messages::SOUND_OFF,
messages::DELAY_100,
messages::VIBRO_ON,
sounds::C5,
messages::DELAY_100,
messages::VIBRO_OFF,
messages::SOUND_OFF,
];

pub const AUDIO_VISUAL_ALERT: NotificationSequence = notification_sequence![
messages::FORCE_SPEAKER_VOLUME_SETTING_1,
messages::FORCE_VIBRO_SETTING_ON,
messages::FORCE_DISPLAY_BRIGHTNESS_SETTING_1,
messages::VIBRO_ON,
messages::DISPLAY_BACKLIGHT_ON,
sounds::C7,
messages::DELAY_250,
messages::DISPLAY_BACKLIGHT_OFF,
sounds::C4,
messages::DELAY_250,
messages::DISPLAY_BACKLIGHT_ON,
sounds::C7,
messages::DELAY_250,
messages::DISPLAY_BACKLIGHT_OFF,
sounds::C4,
messages::DELAY_250,
messages::DISPLAY_BACKLIGHT_ON,
sounds::C7,
messages::DELAY_250,
messages::DISPLAY_BACKLIGHT_OFF,
sounds::C4,
messages::DELAY_250,
messages::SOUND_OFF,
messages::VIBRO_OFF,
];
99 changes: 99 additions & 0 deletions crates/flipperzero/src/notification/led.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use super::{messages, NotificationSequence};
use crate::notification_sequence;

pub const RESET_RED: NotificationSequence = notification_sequence![messages::RED_0];
pub const RESET_GREEN: NotificationSequence = notification_sequence![messages::GREEN_0];
pub const RESET_BLUE: NotificationSequence = notification_sequence![messages::BLUE_0];
pub const RESET_RGB: NotificationSequence =
notification_sequence![messages::RED_0, messages::GREEN_0, messages::BLUE_0];

pub const ONLY_RED: NotificationSequence = notification_sequence![
messages::RED_255,
messages::GREEN_0,
messages::BLUE_0,
messages::DO_NOT_RESET,
];
pub const ONLY_GREEN: NotificationSequence = notification_sequence![
messages::RED_0,
messages::GREEN_255,
messages::BLUE_0,
messages::DO_NOT_RESET,
];
pub const ONLY_BLUE: NotificationSequence = notification_sequence![
messages::RED_0,
messages::GREEN_0,
messages::BLUE_255,
messages::DO_NOT_RESET,
];

pub const SET_RED: NotificationSequence =
notification_sequence![messages::RED_255, messages::DO_NOT_RESET];
pub const SET_GREEN: NotificationSequence =
notification_sequence![messages::GREEN_255, messages::DO_NOT_RESET];
pub const SET_BLUE: NotificationSequence =
notification_sequence![messages::BLUE_255, messages::DO_NOT_RESET];

pub const SOLID_YELLOW: NotificationSequence = notification_sequence![
messages::RED_255,
messages::GREEN_255,
messages::BLUE_0,
messages::DO_NOT_RESET,
];

pub const BLINK_RED_10: NotificationSequence =
notification_sequence![messages::RED_255, messages::DELAY_10];
pub const BLINK_GREEN_10: NotificationSequence =
notification_sequence![messages::GREEN_255, messages::DELAY_10];
pub const BLINK_BLUE_10: NotificationSequence =
notification_sequence![messages::BLUE_255, messages::DELAY_10];
pub const BLINK_YELLOW_10: NotificationSequence =
notification_sequence![messages::RED_255, messages::GREEN_255, messages::DELAY_10,];
pub const BLINK_CYAN_10: NotificationSequence =
notification_sequence![messages::GREEN_255, messages::BLUE_255, messages::DELAY_10,];
pub const BLINK_MAGNENTA_10: NotificationSequence =
notification_sequence![messages::RED_255, messages::BLUE_255, messages::DELAY_10];

pub const BLINK_RED_100: NotificationSequence =
notification_sequence![messages::RED_255, messages::DELAY_100];
pub const BLINK_GREEN_100: NotificationSequence =
notification_sequence![messages::GREEN_255, messages::DELAY_100];
pub const BLINK_BLUE_100: NotificationSequence =
notification_sequence![messages::BLUE_255, messages::DELAY_100];
pub const BLINK_YELLOW_100: NotificationSequence =
notification_sequence![messages::RED_255, messages::GREEN_255, messages::DELAY_100,];
pub const BLINK_CYAN_100: NotificationSequence =
notification_sequence![messages::GREEN_255, messages::BLUE_255, messages::DELAY_100,];
pub const BLINK_MAGNENTA_100: NotificationSequence =
notification_sequence![messages::RED_255, messages::BLUE_255, messages::DELAY_100,];

pub const BLINK_START_RED: NotificationSequence = notification_sequence![
messages::BLINK_START_10,
messages::BLINK_SET_COLOR_RED,
messages::DO_NOT_RESET,
];
pub const BLINK_START_GREEN: NotificationSequence = notification_sequence![
messages::BLINK_START_10,
messages::BLINK_SET_COLOR_GREEN,
messages::DO_NOT_RESET,
];
pub const BLINK_START_BLUE: NotificationSequence = notification_sequence![
messages::BLINK_START_10,
messages::BLINK_SET_COLOR_BLUE,
messages::DO_NOT_RESET,
];
pub const BLINK_START_YELLOW: NotificationSequence = notification_sequence![
messages::BLINK_START_10,
messages::BLINK_SET_COLOR_YELLOW,
messages::DO_NOT_RESET,
];
pub const BLINK_START_CYAN: NotificationSequence = notification_sequence![
messages::BLINK_START_10,
messages::BLINK_SET_COLOR_CYAN,
messages::DO_NOT_RESET,
];
pub const BLINK_START_MAGENTA: NotificationSequence = notification_sequence![
messages::BLINK_START_10,
messages::BLINK_SET_COLOR_MAGENTA,
messages::DO_NOT_RESET,
];
pub const BLINK_STOP: NotificationSequence = notification_sequence![messages::BLINK_STOP];
61 changes: 61 additions & 0 deletions crates/flipperzero/src/notification/messages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use super::{Light, NotificationMessage};

pub const DISPLAY_BACKLIGHT_ON: NotificationMessage = NotificationMessage::display_backlight(0xFF);
pub const DISPLAY_BACKLIGHT_OFF: NotificationMessage = NotificationMessage::display_backlight(0x00);
pub const DISPLAY_BACKLIGHT_ENFORCE_ON: NotificationMessage =
NotificationMessage::display_backlight_enforce_on();
pub const DISPLAY_BACKLIGHT_ENFORCE_AUTO: NotificationMessage =
NotificationMessage::display_backlight_enforce_auto();

pub const RED_255: NotificationMessage = NotificationMessage::led_red(255);
pub const GREEN_255: NotificationMessage = NotificationMessage::led_green(255);
pub const BLUE_255: NotificationMessage = NotificationMessage::led_blue(255);
pub const RED_0: NotificationMessage = NotificationMessage::led_red(0);
pub const GREEN_0: NotificationMessage = NotificationMessage::led_green(0);
pub const BLUE_0: NotificationMessage = NotificationMessage::led_blue(0);

pub const BLINK_START_10: NotificationMessage =
NotificationMessage::led_blink_start(10, 100, Light::OFF);
pub const BLINK_START_100: NotificationMessage =
NotificationMessage::led_blink_start(100, 1000, Light::OFF);
pub const BLINK_STOP: NotificationMessage = NotificationMessage::led_blink_stop();

pub const BLINK_SET_COLOR_RED: NotificationMessage =
NotificationMessage::led_blink_color(Light::RED);
pub const BLINK_SET_COLOR_GREEN: NotificationMessage =
NotificationMessage::led_blink_color(Light::GREEN);
pub const BLINK_SET_COLOR_BLUE: NotificationMessage =
NotificationMessage::led_blink_color(Light::BLUE);
pub const BLINK_SET_COLOR_CYAN: NotificationMessage =
NotificationMessage::led_blink_color(Light::CYAN);
pub const BLINK_SET_COLOR_MAGENTA: NotificationMessage =
NotificationMessage::led_blink_color(Light::MAGENTA);
pub const BLINK_SET_COLOR_YELLOW: NotificationMessage =
NotificationMessage::led_blink_color(Light::YELLOW);
pub const BLINK_SET_COLOR_WHITE: NotificationMessage =
NotificationMessage::led_blink_color(Light::WHITE);

pub const DELAY_1: NotificationMessage = NotificationMessage::delay(1);
pub const DELAY_10: NotificationMessage = NotificationMessage::delay(10);
pub const DELAY_25: NotificationMessage = NotificationMessage::delay(25);
pub const DELAY_50: NotificationMessage = NotificationMessage::delay(50);
pub const DELAY_100: NotificationMessage = NotificationMessage::delay(100);
pub const DELAY_250: NotificationMessage = NotificationMessage::delay(250);
pub const DELAY_500: NotificationMessage = NotificationMessage::delay(500);
pub const DELAY_1000: NotificationMessage = NotificationMessage::delay(1000);

pub const SOUND_OFF: NotificationMessage = NotificationMessage::sound_off();

pub const VIBRO_ON: NotificationMessage = NotificationMessage::vibro(true);
pub const VIBRO_OFF: NotificationMessage = NotificationMessage::vibro(false);

pub const DO_NOT_RESET: NotificationMessage = NotificationMessage::do_not_reset();

pub const FORCE_SPEAKER_VOLUME_SETTING_1: NotificationMessage =
NotificationMessage::force_speaker_volume_setting(1.0);
pub const FORCE_VIBRO_SETTING_ON: NotificationMessage =
NotificationMessage::force_vibro_setting(true);
pub const FORCE_VIBRO_SETTING_OFF: NotificationMessage =
NotificationMessage::force_vibro_setting(false);
pub const FORCE_DISPLAY_BRIGHTNESS_SETTING_1: NotificationMessage =
NotificationMessage::force_display_bightness_setting(1.0);
Loading

0 comments on commit 684484d

Please sign in to comment.