summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: a7006ad0e8041685b1a7f74f800bcba69e3804be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#![no_std]

extern crate alloc;
use alloc::string::String;
use core::env;
use esp_hal::{
    clock::CpuClock,
    time::{Duration, Instant},
};
use esp_hal_smartled::SmartLedsAdapter;
use esp_radio::wifi;
use smart_leds::{RGB, SmartLedsWrite as _, brightness};

// default brightness level of RGB led, used by write_rgb
const RGB_LED_LEVEL: u8 = u8::MAX;

// writes color at RGB_LED_LEVEL brightness to rgb_led
pub fn write_rgb(rgb_led: &mut SmartLedsAdapter<25>, color: RGB<u8>) {
    rgb_led
        .write(brightness([color].into_iter(), RGB_LED_LEVEL))
        .unwrap();
}

// chugs for millis milliseconds
pub fn wait_ms(millis: u64) {
    let delay_start = Instant::now();
    while delay_start.elapsed() < Duration::from_millis(millis) {}
}

// initializes peripherals and logging
pub fn init() -> esp_hal::peripherals::Peripherals {
    esp_println::logger::init_logger_from_env();

    let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());

    let peripherals = esp_hal::init(config);

    log::info!("test info log");
    log::warn!("test warn log");
    log::error!("test error log");

    peripherals
}

pub fn mk_wifi_config() -> wifi::ModeConfig {
    let access_point_config = wifi::AccessPointConfig::default()
        .with_auth_method(wifi::AuthMethod::WpaWpa2Personal)
        .with_ssid(String::from("esp32c6"))
        .with_password(String::from("password"));
    let client_config = wifi::ClientConfig::default()
        .with_auth_method(wifi::AuthMethod::WpaWpa2Personal)
        .with_ssid(String::from(env!("ACCESS_POINT_SSID")))
        .with_password(String::from(env!("ACCESS_POINT_PASS")));
    wifi::ModeConfig::ApSta(client_config, access_point_config)
}

pub fn connect_wifi(
    wifi_controller: &mut wifi::WifiController,
    wifi_config: &wifi::ModeConfig,
) -> () {
    wifi_controller
        .set_config(wifi_config)
        .expect("failed to set wifi config");
    wifi_controller
        .start()
        .expect("failed to start wifi controller");
    wifi_controller
        .connect()
        .expect("failed to attempt to connect wifi controller to access point");
}

// used the site https://deepbluembedded.com/sine-lookup-table-generator-calculator/ to generate
const SIN_LOOKUP_U8: [u8; 256] = [
    128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, 173, 176, 179, 182,
    185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211, 213, 215, 218, 220, 222, 224, 226, 228,
    230, 232, 234, 235, 237, 238, 240, 241, 243, 244, 245, 246, 248, 249, 250, 250, 251, 252, 253,
    253, 254, 254, 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251, 250,
    250, 249, 248, 246, 245, 244, 243, 241, 240, 238, 237, 235, 234, 232, 230, 228, 226, 224, 222,
    220, 218, 215, 213, 211, 208, 206, 203, 201, 198, 196, 193, 190, 188, 185, 182, 179, 176, 173,
    170, 167, 165, 162, 158, 155, 152, 149, 146, 143, 140, 137, 134, 131, 128, 124, 121, 118, 115,
    112, 109, 106, 103, 100, 97, 93, 90, 88, 85, 82, 79, 76, 73, 70, 67, 65, 62, 59, 57, 54, 52,
    49, 47, 44, 42, 40, 37, 35, 33, 31, 29, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, 10, 9, 7,
    6, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, 10, 11,
    12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, 40, 42, 44, 47, 49, 52, 54, 57, 59,
    62, 65, 67, 70, 73, 76, 79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121,
    124,
];

// looks up the sin of a value where the domain and range are both scaled to fit 0..255
pub fn sin(i: u8) -> u8 {
    SIN_LOOKUP_U8[usize::from(i)]
}

// looks up the cos of a value where the domain and range are both scaled to fit 0..255
pub fn cos(i: u8) -> u8 {
    SIN_LOOKUP_U8[usize::from(i + 64)]
}

// looks up the tan of a value where the domain and range are both scaled to fit 0..255, given that cos(i) /= 0
pub fn tan(i: u8) -> Option<u8> {
    match cos(i) {
        0 => None,
        cosofi => Some(sin(i) / cosofi),
    }
}