initialize wifi two-way
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
target
|
target
|
||||||
result*
|
result*
|
||||||
|
hook.sh
|
||||||
|
|||||||
@@ -39,6 +39,11 @@
|
|||||||
rustc = toolchain;
|
rustc = toolchain;
|
||||||
}).buildPackage {
|
}).buildPackage {
|
||||||
src = ./.;
|
src = ./.;
|
||||||
|
|
||||||
|
# set these to build the project
|
||||||
|
ACCESS_POINT_SSID = null;
|
||||||
|
ACCESS_POINT_PASS = null;
|
||||||
|
|
||||||
# UPDATE .cargo/config.toml to reflect changes here in the runner
|
# UPDATE .cargo/config.toml to reflect changes here in the runner
|
||||||
postInstall = ''
|
postInstall = ''
|
||||||
mv $out/bin/esp32c6-play $out/bin/.esp32c6-play
|
mv $out/bin/esp32c6-play $out/bin/.esp32c6-play
|
||||||
|
|||||||
31
src/lib.rs
31
src/lib.rs
@@ -1,10 +1,14 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
use alloc::string::String;
|
||||||
|
use core::env;
|
||||||
use esp_hal::{
|
use esp_hal::{
|
||||||
clock::CpuClock,
|
clock::CpuClock,
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
use esp_hal_smartled::SmartLedsAdapter;
|
use esp_hal_smartled::SmartLedsAdapter;
|
||||||
|
use esp_radio::wifi;
|
||||||
use smart_leds::{RGB, SmartLedsWrite as _, brightness};
|
use smart_leds::{RGB, SmartLedsWrite as _, brightness};
|
||||||
|
|
||||||
// default brightness level of RGB led, used by write_rgb
|
// default brightness level of RGB led, used by write_rgb
|
||||||
@@ -38,6 +42,33 @@ pub fn init() -> esp_hal::peripherals::Peripherals {
|
|||||||
peripherals
|
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
|
// used the site https://deepbluembedded.com/sine-lookup-table-generator-calculator/ to generate
|
||||||
const SIN_LOOKUP_U8: [u8; 256] = [
|
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,
|
128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, 173, 176, 179, 182,
|
||||||
|
|||||||
48
src/main.rs
48
src/main.rs
@@ -1,8 +1,7 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
extern crate alloc;
|
use core::env;
|
||||||
use alloc::string::String;
|
|
||||||
use esp_backtrace as _; // panic_handler
|
use esp_backtrace as _; // panic_handler
|
||||||
use esp_hal::{
|
use esp_hal::{
|
||||||
interrupt::software::SoftwareInterruptControl, rmt::Rmt, time::Rate, timer::timg::TimerGroup,
|
interrupt::software::SoftwareInterruptControl, rmt::Rmt, time::Rate, timer::timg::TimerGroup,
|
||||||
@@ -11,7 +10,7 @@ use esp_hal_smartled::{SmartLedsAdapter, smart_led_buffer};
|
|||||||
use esp_radio::wifi;
|
use esp_radio::wifi;
|
||||||
use smart_leds::RGB;
|
use smart_leds::RGB;
|
||||||
|
|
||||||
use esp32c6_play::{init, sin, wait_ms, write_rgb};
|
use esp32c6_play::{connect_wifi, init, mk_wifi_config, sin, wait_ms, write_rgb};
|
||||||
|
|
||||||
// This creates a default app-descriptor required by the esp-idf bootloader.
|
// This creates a default app-descriptor required by the esp-idf bootloader.
|
||||||
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
|
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
|
||||||
@@ -25,40 +24,39 @@ fn main() -> ! {
|
|||||||
// initialize heap
|
// initialize heap
|
||||||
esp_alloc::heap_allocator!(size: 64 * 1024);
|
esp_alloc::heap_allocator!(size: 64 * 1024);
|
||||||
|
|
||||||
// initialize wifi
|
// initialize esp-rtos as scheduler for esp-radio
|
||||||
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
||||||
let software_interrupt = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
|
let software_interrupt = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
|
||||||
esp_rtos::start(timg0.timer0, software_interrupt.software_interrupt0);
|
esp_rtos::start(timg0.timer0, software_interrupt.software_interrupt0);
|
||||||
let esp_radio_controller = esp_radio::init().expect("failed to init radio controller");
|
|
||||||
let (mut wifi_controller, _) = wifi::new(
|
|
||||||
&esp_radio_controller,
|
|
||||||
peripherals.WIFI,
|
|
||||||
wifi::Config::default(),
|
|
||||||
)
|
|
||||||
.expect("failed to create wifi controller");
|
|
||||||
|
|
||||||
let wifi_config = wifi::ModeConfig::AccessPoint(
|
// initialize wifi
|
||||||
wifi::AccessPointConfig::default()
|
let radio_controller = esp_radio::init().expect("failed to init radio controller");
|
||||||
.with_auth_method(wifi::AuthMethod::WpaWpa2Personal)
|
let (mut wifi_controller, _) =
|
||||||
.with_ssid(String::from("ssid-test"))
|
wifi::new(&radio_controller, peripherals.WIFI, wifi::Config::default())
|
||||||
.with_password(String::from("password")),
|
.expect("failed to init wifi controller");
|
||||||
);
|
|
||||||
wifi_controller
|
|
||||||
.set_config(&wifi_config)
|
|
||||||
.expect("failed to set wifi config");
|
|
||||||
|
|
||||||
wifi_controller
|
let wifi_config = mk_wifi_config();
|
||||||
.start()
|
connect_wifi(&mut wifi_controller, &wifi_config);
|
||||||
.expect("failed to start wifi controller");
|
|
||||||
|
|
||||||
// set up rgb led
|
// initialize rgb led
|
||||||
let mut led_buffer = smart_led_buffer!(1 /* # of leds */);
|
let mut led_buffer = smart_led_buffer!(1 /* # of leds */);
|
||||||
let mut rgb_led = {
|
let mut rgb_led = {
|
||||||
let frequency = Rate::from_mhz(80); // max frequency of RMT
|
let frequency = Rate::from_mhz(80); // max frequency of RMT
|
||||||
let rmt = Rmt::new(peripherals.RMT, frequency).expect("failed to initialize RMT0");
|
let rmt = Rmt::new(peripherals.RMT, frequency).expect("failed to initialize rmt");
|
||||||
SmartLedsAdapter::new(rmt.channel0, peripherals.GPIO8, &mut led_buffer)
|
SmartLedsAdapter::new(rmt.channel0, peripherals.GPIO8, &mut led_buffer)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// red led and waiting message
|
||||||
|
write_rgb(&mut rgb_led, RGB::new(255, 20, 20));
|
||||||
|
log::info!("connecting...");
|
||||||
|
while !wifi_controller
|
||||||
|
.is_connected()
|
||||||
|
.expect("failed to check connection status")
|
||||||
|
{
|
||||||
|
wait_ms(100);
|
||||||
|
}
|
||||||
|
log::info!("connected to the access point");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
for i in 0..255 {
|
for i in 0..255 {
|
||||||
let r: u8 = i;
|
let r: u8 = i;
|
||||||
|
|||||||
Reference in New Issue
Block a user