kopia lustrzana https://github.com/pimoroni/pimoroni-pico
Added latest local code for wireless pack
rodzic
d127f4d299
commit
e5545c1c02
|
@ -1,3 +1,4 @@
|
|||
add_subdirectory(esp32wireless)
|
||||
add_subdirectory(st7789)
|
||||
add_subdirectory(msa301)
|
||||
add_subdirectory(rv3028)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
include(esp32wireless.cmake)
|
|
@ -0,0 +1,12 @@
|
|||
set(DRIVER_NAME esp32wireless)
|
||||
add_library(${DRIVER_NAME} INTERFACE)
|
||||
|
||||
target_sources(${DRIVER_NAME} INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/${DRIVER_NAME}.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/spi_drv.cpp
|
||||
)
|
||||
|
||||
target_include_directories(${DRIVER_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
# Pull in pico libraries that we need
|
||||
target_link_libraries(${DRIVER_NAME} INTERFACE pico_stdlib hardware_spi)
|
Plik diff jest za duży
Load Diff
|
@ -0,0 +1,214 @@
|
|||
#pragma once
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "spi_drv.hpp"
|
||||
|
||||
|
||||
|
||||
|
||||
#define WL_FW_VER_LENGTH 6
|
||||
|
||||
#define WIFI_SPI_ACK 1
|
||||
#define WIFI_SPI_ERR 0xFF
|
||||
|
||||
// Maxmium number of socket
|
||||
#define WIFI_MAX_SOCK_NUM 10
|
||||
// Socket not available constant
|
||||
#define SOCK_NOT_AVAIL 255
|
||||
// Default state value for Wifi state field
|
||||
#define NA_STATE -1
|
||||
//Maximum number of attempts to establish wifi connection
|
||||
#define WL_MAX_ATTEMPT_CONNECTION 10
|
||||
|
||||
enum wl_ping_result_t {
|
||||
WL_PING_DEST_UNREACHABLE = -1,
|
||||
WL_PING_TIMEOUT = -2,
|
||||
WL_PING_UNKNOWN_HOST = -3,
|
||||
WL_PING_ERROR = -4
|
||||
};
|
||||
|
||||
enum wl_status_t {
|
||||
WL_NO_SHIELD = 255,
|
||||
WL_NO_MODULE = WL_NO_SHIELD,
|
||||
WL_IDLE_STATUS = 0,
|
||||
WL_NO_SSID_AVAIL,
|
||||
WL_SCAN_COMPLETED,
|
||||
WL_CONNECTED,
|
||||
WL_CONNECT_FAILED,
|
||||
WL_CONNECTION_LOST,
|
||||
WL_DISCONNECTED,
|
||||
WL_AP_LISTENING,
|
||||
WL_AP_CONNECTED,
|
||||
WL_AP_FAILED
|
||||
};
|
||||
|
||||
enum wl_enc_type { /* Values map to 802.11 encryption suites... */
|
||||
ENC_TYPE_WEP = 5,
|
||||
ENC_TYPE_TKIP = 2,
|
||||
ENC_TYPE_CCMP = 4,
|
||||
/* ... except these two, 7 and 8 are reserved in 802.11-2007 */
|
||||
ENC_TYPE_NONE = 7,
|
||||
ENC_TYPE_AUTO = 8,
|
||||
|
||||
ENC_TYPE_UNKNOWN = 255
|
||||
};
|
||||
|
||||
enum wl_error_code_t {
|
||||
WL_FAILURE = -1,
|
||||
WL_SUCCESS = 1,
|
||||
};
|
||||
|
||||
|
||||
|
||||
#define KEY_IDX_LEN 1
|
||||
|
||||
|
||||
|
||||
namespace pimoroni {
|
||||
|
||||
struct IPAddress {
|
||||
uint32_t a;
|
||||
};
|
||||
|
||||
class WifiNINA {
|
||||
//--------------------------------------------------
|
||||
// Constants
|
||||
//--------------------------------------------------
|
||||
public:
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Variables
|
||||
//--------------------------------------------------
|
||||
private:
|
||||
SpiDrv driver;
|
||||
|
||||
char network_ssid[WL_NETWORKS_LIST_MAXNUM][WL_SSID_MAX_LENGTH];
|
||||
|
||||
// firmware version string in the format a.b.c
|
||||
char fwVersion[WL_FW_VER_LENGTH];
|
||||
|
||||
// settings of current selected network
|
||||
char _ssid[WL_SSID_MAX_LENGTH];
|
||||
uint8_t _bssid[WL_MAC_ADDR_LENGTH];
|
||||
uint8_t _mac[WL_MAC_ADDR_LENGTH];
|
||||
uint8_t _localIp[WL_IPV4_LENGTH];
|
||||
uint8_t _subnetMask[WL_IPV4_LENGTH];
|
||||
uint8_t _gatewayIp[WL_IPV4_LENGTH];
|
||||
public:
|
||||
//PicoWireless();
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Methods
|
||||
//--------------------------------------------------
|
||||
public:
|
||||
virtual bool init();
|
||||
|
||||
//--------------------------------------------------
|
||||
//From https://github.com/adafruit/WiFiNINA/blob/master/src/utility/wifi_drv.cpp
|
||||
//--------------------------------------------------
|
||||
|
||||
void get_network_data(uint8_t *ip, uint8_t *mask, uint8_t *gwip);
|
||||
void get_remote_data(uint8_t sock, uint8_t *ip, uint8_t *port);
|
||||
|
||||
int8_t wifi_set_network(const char* ssid, uint8_t ssid_len);
|
||||
int8_t wifi_set_passphrase(const char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len);
|
||||
int8_t wifi_set_key(const char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len);
|
||||
|
||||
void config(uint8_t valid_params, uint32_t local_ip, uint32_t gateway, uint32_t subnet);
|
||||
|
||||
void set_dns(uint8_t valid_params, uint32_t dns_server1, uint32_t dns_server2);
|
||||
void set_hostname(const char* hostname);
|
||||
int8_t disconnect();
|
||||
|
||||
uint8_t get_connection_status();
|
||||
uint8_t* get_mac_address();
|
||||
|
||||
void get_ip_address(uint8_t *ip_out);
|
||||
void get_subnet_mask(uint8_t *mask_out);
|
||||
void get_gateway_ip(uint8_t *ip_out);
|
||||
|
||||
const char* get_current_ssid();
|
||||
uint8_t* get_current_bssid();
|
||||
int32_t get_current_rssi();
|
||||
uint8_t get_current_encryption_type();
|
||||
|
||||
int8_t start_scan_networks();
|
||||
uint8_t get_scan_networks();
|
||||
const char* get_ssid_networks(uint8_t network_item);
|
||||
|
||||
uint8_t get_enc_type_networks(uint8_t network_item);
|
||||
uint8_t* get_bssid_networks(uint8_t network_item, uint8_t* bssid_out);
|
||||
uint8_t get_channel_networks(uint8_t network_item);
|
||||
int32_t get_rssi_networks(uint8_t network_item);
|
||||
|
||||
uint8_t req_host_by_name(const char* hostname);
|
||||
int get_host_by_name(IPAddress& aResult); //TODO
|
||||
int get_host_by_name(const char* aHostname, IPAddress& aResult); //Calls req_host_by_name and get_host_by_name
|
||||
|
||||
const char* get_fw_version();
|
||||
uint32_t get_time();
|
||||
void set_power_mode(uint8_t mode);
|
||||
|
||||
int8_t wifi_set_ap_network(const char* ssid, uint8_t ssid_len, uint8_t channel);
|
||||
int8_t wifi_set_ap_passphrase(const char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len, uint8_t channel);
|
||||
|
||||
int16_t ping(uint32_t ip_address, uint8_t ttl);
|
||||
|
||||
void debug(uint8_t on);
|
||||
float get_temperature();
|
||||
void pin_mode(uint8_t pin, uint8_t mode);
|
||||
|
||||
void digital_write(uint8_t pin, uint8_t value);
|
||||
void analog_write(uint8_t pin, uint8_t value);
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
//From https://github.com/adafruit/WiFiNINA/blob/master/src/utility/server_drv.cpp
|
||||
//--------------------------------------------------
|
||||
|
||||
void start_server(uint16_t port, uint8_t sock, uint8_t prot_mode);
|
||||
void start_server(uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t prot_mode);
|
||||
void start_client(uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t prot_mode);
|
||||
void start_client(const char* host, uint8_t host_len, uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t prot_mode);
|
||||
void stop_client(uint8_t sock);
|
||||
|
||||
uint8_t get_server_state(uint8_t sock);
|
||||
uint8_t get_client_state(uint8_t sock);
|
||||
uint16_t avail_data(uint8_t sock);
|
||||
uint8_t avail_server(uint8_t sock);
|
||||
|
||||
bool get_data(uint8_t sock, uint8_t *data_out, uint8_t peek);
|
||||
bool get_data_buf(uint8_t sock, uint8_t *data_out, uint16_t *data_len_out);
|
||||
bool insert_data_buf(uint8_t sock, const uint8_t *data_in, uint16_t len);
|
||||
bool send_udp_data(uint8_t sock);
|
||||
|
||||
uint16_t send_data(uint8_t sock, const uint8_t *data_in, uint16_t len);
|
||||
uint8_t check_data_sent(uint8_t sock);
|
||||
uint8_t get_socket();
|
||||
|
||||
//--------------------------------------------------
|
||||
// TODO Sort below ones out, particularly wpa2s from CPy
|
||||
//--------------------------------------------------
|
||||
|
||||
//Unknown how to implement these
|
||||
// uint8_t digital_read(uint8_t pin);
|
||||
// uint8_t analog_read(uint8_t pin);
|
||||
|
||||
|
||||
// void wifi_set_entidentity(); //SET_ENT_IDENT
|
||||
// void wifi_set_entusername(); //SET_ENT_UNAME
|
||||
// void wifi_set_entpassword(); //SET_ENT_PASSWD
|
||||
|
||||
// void wifi_set_entenable(); //SET_ENT_ENABLE
|
||||
|
||||
// void is_connected(void); //Reads an internal status var for WL_CONNECTED
|
||||
|
||||
// void connect(void); //Calls connect_ap
|
||||
// void connect_ap(void); //Calls wifi_set_network and wifi_set_passphrase
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,316 @@
|
|||
#include "spi_drv.hpp"
|
||||
|
||||
namespace pimoroni {
|
||||
|
||||
void SpiDrv::init() {
|
||||
spi_init(spi, 8000000);
|
||||
gpio_set_function(miso, GPIO_FUNC_SPI);
|
||||
gpio_set_function(sck, GPIO_FUNC_SPI);
|
||||
gpio_set_function(mosi, GPIO_FUNC_SPI);
|
||||
|
||||
// Chip select is active-low, so we'll initialise it to a driven-high state
|
||||
gpio_init(cs);
|
||||
gpio_set_dir(cs, GPIO_OUT);
|
||||
gpio_put(cs, true);
|
||||
|
||||
gpio_init(gpio0);
|
||||
gpio_set_dir(gpio0, GPIO_OUT);
|
||||
|
||||
gpio_init(resetn);
|
||||
gpio_set_dir(resetn, GPIO_OUT);
|
||||
}
|
||||
|
||||
void SpiDrv::reset() {
|
||||
gpio_put(gpio0, true);
|
||||
gpio_put(cs, true);
|
||||
gpio_put(resetn, false);
|
||||
sleep_ms(10);
|
||||
gpio_put(resetn, true);
|
||||
sleep_ms(750);
|
||||
}
|
||||
|
||||
bool SpiDrv::available() {
|
||||
return gpio_get(gpio0);
|
||||
}
|
||||
|
||||
void SpiDrv::esp_select() {
|
||||
gpio_put(cs, false);
|
||||
}
|
||||
|
||||
void SpiDrv::esp_deselect() {
|
||||
gpio_put(cs, true);
|
||||
}
|
||||
|
||||
bool SpiDrv::get_esp_ready() {
|
||||
return !gpio_get(ack);
|
||||
}
|
||||
|
||||
bool SpiDrv::get_esp_ack() {
|
||||
return gpio_get(ack);
|
||||
}
|
||||
|
||||
void SpiDrv::wait_for_esp_ack() {
|
||||
while(!get_esp_ack()) {
|
||||
tight_loop_contents();
|
||||
}
|
||||
}
|
||||
|
||||
void SpiDrv::wait_for_esp_ready() {
|
||||
while(!get_esp_ready()) {
|
||||
tight_loop_contents();
|
||||
}
|
||||
}
|
||||
|
||||
void SpiDrv::wait_for_esp_select() {
|
||||
wait_for_esp_ready();
|
||||
esp_select();
|
||||
wait_for_esp_ack();
|
||||
}
|
||||
|
||||
int SpiDrv::wait_for_byte(uint8_t wait_byte) {
|
||||
int timeout = BYTE_TIMEOUT;
|
||||
uint8_t byte_read = 0;
|
||||
do{
|
||||
byte_read = read_byte(); //get data byte
|
||||
if (byte_read == ERR_CMD) {
|
||||
printf("Err cmd received\n");
|
||||
return -1;
|
||||
}
|
||||
} while((timeout-- > 0) && (byte_read != wait_byte));
|
||||
return (byte_read == wait_byte);
|
||||
}
|
||||
|
||||
bool SpiDrv::read_and_check_byte(uint8_t check_byte, uint8_t *byte_out) {
|
||||
get_param(byte_out);
|
||||
return (*byte_out == check_byte);
|
||||
}
|
||||
|
||||
uint8_t SpiDrv::read_byte() {
|
||||
uint8_t byte_read = 0;
|
||||
get_param(&byte_read);
|
||||
return byte_read;
|
||||
}
|
||||
|
||||
bool SpiDrv::wait_response_params(uint8_t cmd, uint8_t num_param, tParam *params_out) {
|
||||
uint8_t data = 0;
|
||||
int i = 0, ii = 0;
|
||||
|
||||
IF_CHECK_START_CMD() {
|
||||
CHECK_DATA(cmd | REPLY_FLAG, data){};
|
||||
|
||||
uint8_t num_param_read = read_byte();
|
||||
if(num_param_read != 0) {
|
||||
for(i = 0; i < num_param_read; ++i) {
|
||||
params_out[i].param_len = read_param_len8();
|
||||
spi_read_blocking(spi, DUMMY_DATA, params_out[i].param, params_out[i].param_len);
|
||||
}
|
||||
}
|
||||
else {
|
||||
printf("Error num_param == 0\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(num_param != num_param_read) {
|
||||
printf("Mismatch num_param\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
read_and_check_byte(END_CMD, &data);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SpiDrv::wait_response_cmd(uint8_t cmd, uint8_t num_param, uint8_t *param_out, uint8_t *param_len_out) {
|
||||
uint8_t data = 0;
|
||||
int ii = 0;
|
||||
|
||||
IF_CHECK_START_CMD() {
|
||||
CHECK_DATA(cmd | REPLY_FLAG, data){};
|
||||
|
||||
CHECK_DATA(num_param, data) {
|
||||
read_param_len8(param_len_out);
|
||||
for(ii = 0; ii < (*param_len_out); ++ii) {
|
||||
get_param(¶m_out[ii]);
|
||||
}
|
||||
}
|
||||
|
||||
read_and_check_byte(END_CMD, &data);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SpiDrv::wait_response_data8(uint8_t cmd, uint8_t *param_out, uint8_t *param_len_out) {
|
||||
uint8_t data = 0;
|
||||
int ii = 0;
|
||||
|
||||
IF_CHECK_START_CMD() {
|
||||
CHECK_DATA(cmd | REPLY_FLAG, data){};
|
||||
|
||||
uint8_t num_param_read = read_byte();
|
||||
if(num_param_read != 0) {
|
||||
read_param_len8(param_len_out);
|
||||
spi_read_blocking(spi, DUMMY_DATA, param_out, *param_len_out);
|
||||
}
|
||||
|
||||
read_and_check_byte(END_CMD, &data);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SpiDrv::wait_response_data16(uint8_t cmd, uint8_t* param_out, uint16_t *param_len_out) {
|
||||
uint8_t data = 0;
|
||||
uint16_t ii = 0;
|
||||
|
||||
IF_CHECK_START_CMD() {
|
||||
CHECK_DATA(cmd | REPLY_FLAG, data){};
|
||||
|
||||
uint8_t num_param_read = read_byte();
|
||||
if(num_param_read != 0) {
|
||||
read_param_len16(param_len_out);
|
||||
spi_read_blocking(spi, DUMMY_DATA, param_out, *param_len_out);
|
||||
}
|
||||
|
||||
read_and_check_byte(END_CMD, &data);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SpiDrv::wait_response(uint8_t cmd, uint8_t *num_param_out, uint8_t **params_out, uint8_t max_num_params) {
|
||||
uint8_t data = 0;
|
||||
int i = 0, ii = 0;
|
||||
|
||||
uint8_t* index[WL_SSID_MAX_LENGTH];
|
||||
|
||||
for(i = 0 ; i < WL_NETWORKS_LIST_MAXNUM; i++)
|
||||
index[i] = (uint8_t*)params_out + (WL_SSID_MAX_LENGTH * i);
|
||||
|
||||
IF_CHECK_START_CMD() {
|
||||
CHECK_DATA(cmd | REPLY_FLAG, data){};
|
||||
|
||||
uint8_t num_param_read = read_byte();
|
||||
|
||||
if(num_param_read > max_num_params) {
|
||||
num_param_read = max_num_params;
|
||||
}
|
||||
*num_param_out = num_param_read;
|
||||
if(num_param_read != 0) {
|
||||
for(i = 0; i < num_param_read; ++i) {
|
||||
uint8_t param_len = read_param_len8();
|
||||
spi_read_blocking(spi, DUMMY_DATA, index[i], param_len);
|
||||
index[i][param_len] = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
printf("Error numParams == 0\n");
|
||||
read_and_check_byte(END_CMD, &data);
|
||||
return false;
|
||||
}
|
||||
read_and_check_byte(END_CMD, &data);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void SpiDrv::send_param(const uint8_t *param, uint8_t param_len, lastParam last_param) {
|
||||
send_param_len8(param_len);
|
||||
|
||||
spi_write_blocking(spi, param, param_len);
|
||||
|
||||
if(last_param) {
|
||||
uint8_t buf = END_CMD;
|
||||
spi_write_blocking(spi, &buf, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void SpiDrv::send_param_len8(uint8_t param_len) {
|
||||
spi_write_blocking(spi, ¶m_len, 1);
|
||||
}
|
||||
|
||||
void SpiDrv::send_param_len16(uint16_t param_len) {
|
||||
uint8_t buf[2];
|
||||
buf[0] = (uint8_t)((param_len & 0xff00) >> 8);
|
||||
buf[1] = (uint8_t)(param_len & 0xff);
|
||||
spi_write_blocking(spi, buf, 2);
|
||||
}
|
||||
|
||||
uint8_t SpiDrv::read_param_len8(uint8_t *param_len_out) {
|
||||
uint8_t param_len;
|
||||
get_param(¶m_len);
|
||||
if(param_len_out != nullptr) {
|
||||
*param_len_out = param_len;
|
||||
}
|
||||
return param_len;
|
||||
}
|
||||
|
||||
uint16_t SpiDrv::read_param_len16(uint16_t *param_len_out) {
|
||||
uint8_t buf[2];
|
||||
spi_read_blocking(spi, DUMMY_DATA, buf, 2);
|
||||
uint16_t param_len = (buf[0] << 8) | (buf[1] & 0xff);
|
||||
if(param_len_out != nullptr) {
|
||||
*param_len_out = param_len;
|
||||
}
|
||||
return param_len;
|
||||
}
|
||||
|
||||
void SpiDrv::send_buffer(const uint8_t* param, uint16_t param_len, lastParam last_param) {
|
||||
send_param_len16(param_len);
|
||||
|
||||
spi_write_blocking(spi, param, param_len);
|
||||
|
||||
if(last_param) {
|
||||
uint8_t buf = END_CMD;
|
||||
spi_write_blocking(spi, &buf, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void SpiDrv::send_param(uint16_t param, lastParam last_param) {
|
||||
send_param_len8(2);
|
||||
|
||||
uint8_t buf[2];
|
||||
buf[0] = (uint8_t)((param & 0xff00) >> 8);
|
||||
buf[1] = (uint8_t)(param & 0xff);
|
||||
spi_write_blocking(spi, buf, 2);
|
||||
|
||||
if(last_param) {
|
||||
uint8_t buf = END_CMD;
|
||||
spi_write_blocking(spi, &buf, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void SpiDrv::send_byte_param(uint8_t param, lastParam last_param) {
|
||||
send_param_len8(1);
|
||||
|
||||
spi_write_blocking(spi, ¶m, 1);
|
||||
|
||||
if(last_param) {
|
||||
uint8_t buf = END_CMD;
|
||||
spi_write_blocking(spi, &buf, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void SpiDrv::send_cmd(uint8_t cmd, uint8_t num_param) {
|
||||
uint8_t buf[3];
|
||||
buf[0] = START_CMD;
|
||||
buf[1] = cmd & ~(REPLY_FLAG);
|
||||
buf[2] = num_param;
|
||||
spi_write_blocking(spi, buf, 3);
|
||||
|
||||
if(num_param == 0) {
|
||||
uint8_t buf = END_CMD;
|
||||
spi_write_blocking(spi, &buf, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void SpiDrv::pad_to_multiple_of_4(int command_size) {
|
||||
while(command_size % 4) {
|
||||
read_byte();
|
||||
command_size++;
|
||||
}
|
||||
}
|
||||
|
||||
void SpiDrv::get_param(uint8_t* param_out) {
|
||||
spi_read_blocking(spi, DUMMY_DATA, param_out, 1);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,171 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/spi.h"
|
||||
|
||||
// Maximum size of a SSID
|
||||
#define WL_SSID_MAX_LENGTH 32
|
||||
// Length of passphrase. Valid lengths are 8-63.
|
||||
#define WL_WPA_KEY_MAX_LENGTH 63
|
||||
// Length of key in bytes. Valid values are 5 and 13.
|
||||
#define WL_WEP_KEY_MAX_LENGTH 13
|
||||
// Size of a MAC-address or BSSID
|
||||
#define WL_MAC_ADDR_LENGTH 6
|
||||
// Size of a MAC-address or BSSID
|
||||
#define WL_IPV4_LENGTH 4
|
||||
// Maximum size of a SSID list
|
||||
#define WL_NETWORKS_LIST_MAXNUM 10
|
||||
|
||||
|
||||
#define IF_CHECK_START_CMD() \
|
||||
if(!wait_for_byte(START_CMD)) { \
|
||||
printf("Error waiting START_CMD\n"); \
|
||||
return false; \
|
||||
} \
|
||||
else \
|
||||
|
||||
|
||||
#define CHECK_DATA(check, x) \
|
||||
if(!read_and_check_byte(check, &x)) { \
|
||||
printf("Reply error\n"); \
|
||||
return false; \
|
||||
} \
|
||||
else \
|
||||
|
||||
namespace pimoroni {
|
||||
|
||||
class SpiDrv {
|
||||
//--------------------------------------------------
|
||||
// Constants
|
||||
//--------------------------------------------------
|
||||
public:
|
||||
static const uint8_t DEFAULT_CS_PIN = 7;
|
||||
static const uint8_t DEFAULT_SCK_PIN = 18;
|
||||
static const uint8_t DEFAULT_MOSI_PIN = 19;
|
||||
static const uint8_t DEFAULT_MISO_PIN = 16;
|
||||
static const uint8_t DEFAULT_RESETN_PIN = 11;
|
||||
static const uint8_t DEFAULT_GPIO0_PIN = 2;
|
||||
static const uint8_t DEFAULT_ACK_PIN = 10;
|
||||
|
||||
static const uint8_t DUMMY_DATA = 0xFF;
|
||||
|
||||
private:
|
||||
static const uint8_t START_CMD = 0xE0;
|
||||
static const uint8_t END_CMD = 0xEE;
|
||||
static const uint8_t ERR_CMD = 0xEF;
|
||||
|
||||
static const uint8_t CMD_FLAG = 0;
|
||||
static const uint8_t REPLY_FLAG = 1 << 7;
|
||||
static const uint8_t DATA_FLAG = 0x40;
|
||||
|
||||
static const uint8_t CMD_POS = 1; // Position of Command OpCode on SPI stream
|
||||
static const uint8_t PARAM_LEN_POS = 2; // Position of Param len on SPI stream
|
||||
|
||||
static const int BYTE_TIMEOUT = 1000;
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Enums
|
||||
//--------------------------------------------------
|
||||
public:
|
||||
enum numParams : uint8_t {
|
||||
PARAM_NUMS_0 = 0,
|
||||
PARAM_NUMS_1,
|
||||
PARAM_NUMS_2,
|
||||
PARAM_NUMS_3,
|
||||
PARAM_NUMS_4,
|
||||
PARAM_NUMS_5
|
||||
};
|
||||
|
||||
enum lastParam : bool {
|
||||
NO_LAST_PARAM = false,
|
||||
LAST_PARAM = true,
|
||||
};
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Substructures
|
||||
//--------------------------------------------------
|
||||
public:
|
||||
struct tParam {
|
||||
uint8_t param_len;
|
||||
uint8_t* param;
|
||||
};
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Variables
|
||||
//--------------------------------------------------
|
||||
private:
|
||||
spi_inst_t *spi = spi0;
|
||||
|
||||
int8_t cs = DEFAULT_CS_PIN;
|
||||
int8_t sck = DEFAULT_SCK_PIN;
|
||||
int8_t mosi = DEFAULT_MOSI_PIN;
|
||||
int8_t miso = DEFAULT_MISO_PIN;
|
||||
int8_t resetn = DEFAULT_RESETN_PIN;
|
||||
int8_t gpio0 = DEFAULT_GPIO0_PIN;
|
||||
int8_t ack = DEFAULT_ACK_PIN;
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Constructors/Destructor
|
||||
//--------------------------------------------------
|
||||
public:
|
||||
SpiDrv() {}
|
||||
|
||||
SpiDrv(spi_inst_t *spi,
|
||||
uint8_t cs, uint8_t sck, uint8_t mosi, uint8_t miso,
|
||||
uint8_t resetn, uint8_t gpio0, uint8_t ack) :
|
||||
spi(spi), cs(cs), sck(sck), mosi(mosi), miso(miso),
|
||||
resetn(resetn), gpio0(gpio0), ack(ack) {}
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Methods
|
||||
//--------------------------------------------------
|
||||
public:
|
||||
void init();
|
||||
void reset();
|
||||
|
||||
bool available();
|
||||
|
||||
void esp_select();
|
||||
void esp_deselect();
|
||||
|
||||
bool get_esp_ready();
|
||||
bool get_esp_ack();
|
||||
|
||||
void wait_for_esp_ack();
|
||||
void wait_for_esp_ready();
|
||||
void wait_for_esp_select();
|
||||
int wait_for_byte(uint8_t wait_byte);
|
||||
|
||||
bool read_and_check_byte(uint8_t check_byte, uint8_t *byte_out);
|
||||
uint8_t read_byte();
|
||||
|
||||
bool wait_response_params(uint8_t cmd, uint8_t num_param, tParam *params_out);
|
||||
bool wait_response_cmd(uint8_t cmd, uint8_t num_param, uint8_t *param_out, uint8_t *param_len_out);
|
||||
bool wait_response_data8(uint8_t cmd, uint8_t *param_out, uint8_t *param_len_out);
|
||||
bool wait_response_data16(uint8_t cmd, uint8_t *param_out, uint16_t *param_len_out);
|
||||
bool wait_response(uint8_t cmd, uint8_t *num_param_out, uint8_t **params_out, uint8_t max_num_params);
|
||||
|
||||
void send_param(const uint8_t* param, uint8_t param_len, lastParam last_param = NO_LAST_PARAM);
|
||||
void send_param_len8(uint8_t param_len);
|
||||
void send_param_len16(uint16_t param_len);
|
||||
|
||||
uint8_t read_param_len8(uint8_t *param_len_out = nullptr);
|
||||
uint16_t read_param_len16(uint16_t *param_len_out = nullptr);
|
||||
|
||||
void send_buffer(const uint8_t *param, uint16_t param_len, lastParam last_param = NO_LAST_PARAM);
|
||||
void send_param(uint16_t param, lastParam last_param = NO_LAST_PARAM);
|
||||
void send_byte_param(uint8_t param, lastParam last_param = NO_LAST_PARAM);
|
||||
void send_cmd(uint8_t cmd, uint8_t num_param);
|
||||
|
||||
void pad_to_multiple_of_4(int command_size);
|
||||
|
||||
private:
|
||||
void get_param(uint8_t *param_out);
|
||||
};
|
||||
}
|
|
@ -7,4 +7,5 @@ add_subdirectory(pico_explorer)
|
|||
add_subdirectory(pico_rgb_keypad)
|
||||
add_subdirectory(pico_rtc_display)
|
||||
add_subdirectory(pico_tof_display)
|
||||
add_subdirectory(pico_audio)
|
||||
add_subdirectory(pico_audio)
|
||||
add_subdirectory(pico_wireless)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
secrets.h
|
|
@ -0,0 +1,14 @@
|
|||
add_executable(
|
||||
wireless_demo
|
||||
demo.cpp
|
||||
)
|
||||
|
||||
# enable usb output, disable uart output
|
||||
pico_enable_stdio_usb(wireless_demo 1)
|
||||
pico_enable_stdio_uart(wireless_demo 0)
|
||||
|
||||
# Pull in pico libraries that we need
|
||||
target_link_libraries(wireless_demo pico_stdlib pico_wireless)
|
||||
|
||||
# create map/bin/hex file etc.
|
||||
pico_add_extra_outputs(wireless_demo)
|
|
@ -0,0 +1,125 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/uart.h"
|
||||
#include "hardware/gpio.h"
|
||||
#include "hardware/spi.h"
|
||||
#include "pico_wireless.hpp"
|
||||
#include "../../drivers/esp32wireless/spi_drv.hpp"
|
||||
#include <chrono>
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
#include "secrets.h"
|
||||
|
||||
using namespace pimoroni;
|
||||
|
||||
#define UART_ID uart1 //uart0
|
||||
#define BAUD_RATE 115200
|
||||
#define DATA_BITS 8
|
||||
#define STOP_BITS 1
|
||||
#define PARITY UART_PARITY_NONE
|
||||
|
||||
// We are using pins 0 and 1, but see the GPIO function select table in the
|
||||
// datasheet for information on which other pins can be used.
|
||||
#define UART_TX_PIN 8 //0
|
||||
#define UART_RX_PIN 9 //1
|
||||
|
||||
|
||||
|
||||
// HSV Conversion expects float inputs in the range of 0.00-1.00 for each channel
|
||||
// Outputs are rgb in the range 0-255 for each channel
|
||||
void from_hsv(float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) {
|
||||
float i = floor(h * 6.0f);
|
||||
float f = h * 6.0f - i;
|
||||
v *= 255.0f;
|
||||
uint8_t p = v * (1.0f - s);
|
||||
uint8_t q = v * (1.0f - f * s);
|
||||
uint8_t t = v * (1.0f - (1.0f - f) * s);
|
||||
|
||||
switch (int(i) % 6) {
|
||||
case 0: r = v; g = t; b = p; break;
|
||||
case 1: r = q; g = v; b = p; break;
|
||||
case 2: r = p; g = v; b = t; break;
|
||||
case 3: r = p; g = q; b = v; break;
|
||||
case 4: r = t; g = p; b = v; break;
|
||||
case 5: r = v; g = p; b = q; break;
|
||||
}
|
||||
}
|
||||
|
||||
PicoWireless wireless;
|
||||
|
||||
#define PICO_LED 25
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
//stdio_set_translate_crlf(&stdio_usb, false);
|
||||
|
||||
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
|
||||
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
|
||||
uart_init(UART_ID, BAUD_RATE);
|
||||
|
||||
gpio_put(PICO_LED, true);
|
||||
|
||||
printf("Initialised\n");
|
||||
|
||||
wireless.init();
|
||||
|
||||
sleep_ms(1000);
|
||||
|
||||
uint8_t r, g, b;
|
||||
uint8_t a = 0;
|
||||
while(!wireless.is_pressed(PicoWireless::A)) {
|
||||
from_hsv((float)a/256.0f, 1, 1, r, g, b);
|
||||
|
||||
wireless.set_led(0, 0, b);
|
||||
sleep_ms(10);
|
||||
a++;
|
||||
}
|
||||
wireless.set_led(16, 16, 0);
|
||||
|
||||
printf("firmware version Nina %s\n", wireless.get_fw_version());
|
||||
uint8_t* mac = wireless.get_mac_address();
|
||||
printf("mac address ", wireless.get_mac_address()[0]);
|
||||
for(uint i =0; i < WL_MAC_ADDR_LENGTH; i++) {
|
||||
printf("%d:", mac[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
printf("starting connection\n");
|
||||
|
||||
bool connected = wireless.wifi_set_passphrase(NETWORK, sizeof(NETWORK), PASSWORD, sizeof(PASSWORD));
|
||||
|
||||
printf("waiting to establish connection status\n");
|
||||
while(wireless.get_connection_status() != 3) {
|
||||
sleep_ms(1000);
|
||||
printf("still waiting\n");
|
||||
}
|
||||
|
||||
uint8_t ip[4];
|
||||
memset(ip, 0x00, sizeof(ip));
|
||||
wireless.get_ip_address(ip);
|
||||
printf("ip address: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
|
||||
|
||||
printf("SSID = %s\n", wireless.get_current_ssid());
|
||||
printf("RSSI = %d\n", wireless.get_current_rssi());
|
||||
|
||||
uint8_t t = 0;
|
||||
while (true) {
|
||||
from_hsv((float)t/256.0f, 1, 1, r, g, b);
|
||||
wireless.set_led(r, g, b);
|
||||
sleep_ms(10);
|
||||
t++;
|
||||
if(t == 0) {
|
||||
//printf("time: %d, temp: %f\n", wireless.get_time(), wireless.get_temperature());
|
||||
std::uint32_t time_date_stamp = wireless.get_time();
|
||||
std::time_t temp = time_date_stamp;
|
||||
std::tm* t = std::gmtime(&temp);
|
||||
std::stringstream ss; // or if you're going to print, just input directly into the output stream
|
||||
ss << std::put_time(t, "%Y-%m-%d %I:%M:%S %p\n");
|
||||
std::string output = ss.str();
|
||||
printf(output.c_str());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -4,4 +4,5 @@ add_subdirectory(pico_display)
|
|||
add_subdirectory(pico_unicorn)
|
||||
add_subdirectory(pico_scroll)
|
||||
add_subdirectory(pico_explorer)
|
||||
add_subdirectory(pico_rgb_keypad)
|
||||
add_subdirectory(pico_rgb_keypad)
|
||||
add_subdirectory(pico_wireless)
|
|
@ -0,0 +1,10 @@
|
|||
add_library(pico_wireless INTERFACE)
|
||||
|
||||
target_sources(pico_wireless INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/pico_wireless.cpp
|
||||
)
|
||||
|
||||
target_include_directories(pico_wireless INTERFACE ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
# Pull in pico libraries that we need
|
||||
target_link_libraries(pico_wireless INTERFACE pico_stdlib hardware_spi esp32wireless)
|
|
@ -0,0 +1,11 @@
|
|||
include(${CMAKE_CURRENT_LIST_DIR}/../../drivers/esp32wireless/esp32wireless.cmake)
|
||||
add_library(pico_wireless INTERFACE)
|
||||
|
||||
target_sources(pico_wireless INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/pico_wireless.cpp
|
||||
)
|
||||
|
||||
target_include_directories(pico_wireless INTERFACE ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
# Pull in pico libraries that we need
|
||||
target_link_libraries(pico_wireless INTERFACE pico_stdlib hardware_spi esp32wireless)
|
|
@ -0,0 +1,29 @@
|
|||
#include "pico_wireless.hpp"
|
||||
#include "../../drivers/esp32wireless/spi_drv.hpp"
|
||||
|
||||
namespace pimoroni {
|
||||
|
||||
bool PicoWireless::init() {
|
||||
bool success = parent::init();
|
||||
|
||||
// setup button input
|
||||
gpio_init(A);
|
||||
gpio_set_dir(A, GPIO_IN);
|
||||
gpio_pull_up(A);
|
||||
|
||||
set_led(0, 0, 0);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
void PicoWireless::set_led(uint8_t r, uint8_t g, uint8_t b) {
|
||||
analog_write(ESP_LED_R, 255 - r);
|
||||
analog_write(ESP_LED_G, 255 - g);
|
||||
analog_write(ESP_LED_B, 255 - b);
|
||||
}
|
||||
|
||||
bool PicoWireless::is_pressed(uint8_t button) {
|
||||
return !gpio_get(button);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
#pragma once
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "../../drivers/esp32wireless/esp32wireless.hpp"
|
||||
|
||||
namespace pimoroni {
|
||||
|
||||
class PicoWireless : public WifiNINA {
|
||||
typedef WifiNINA parent;
|
||||
|
||||
//--------------------------------------------------
|
||||
// Constants
|
||||
//--------------------------------------------------
|
||||
public:
|
||||
static const uint8_t A = 12;
|
||||
|
||||
static const uint8_t ESP_LED_R = 25;
|
||||
static const uint8_t ESP_LED_G = 26;
|
||||
static const uint8_t ESP_LED_B = 27;
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Variables
|
||||
//--------------------------------------------------
|
||||
private:
|
||||
|
||||
|
||||
public:
|
||||
//PicoWireless();
|
||||
|
||||
|
||||
//--------------------------------------------------
|
||||
// Methods
|
||||
//--------------------------------------------------
|
||||
public:
|
||||
virtual bool init();
|
||||
|
||||
void set_led(uint8_t r, uint8_t g, uint8_t b);
|
||||
bool is_pressed(uint8_t button);
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,124 @@
|
|||
#include "esp32spi.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// esp32spi Module
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/***** Module Functions *****/
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_init_obj, esp32spi_init);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_reset_obj, esp32spi_reset);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_status_obj, esp32spi_status);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_firmware_version_obj, esp32spi_firmware_version);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_MAC_address_obj, esp32spi_MAC_address);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_MAC_address_actual_obj, esp32spi_MAC_address_actual);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_start_scan_networks_obj, esp32spi_start_scan_networks);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_get_scan_networks_obj, esp32spi_get_scan_networks);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_scan_networks_obj, esp32spi_scan_networks);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_wifi_set_network_obj, esp32spi_wifi_set_network);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32spi_wifi_set_passphrase_obj, esp32spi_wifi_set_passphrase);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_wifi_set_entidentity_obj, esp32spi_wifi_set_entidentity);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_wifi_set_entusername_obj, esp32spi_wifi_set_entusername);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_wifi_set_entpassword_obj, esp32spi_wifi_set_entpassword);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_wifi_set_entenable_obj, esp32spi_wifi_set_entenable);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_ssid_obj, esp32spi_ssid);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_bssid_obj, esp32spi_bssid);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_rssi_obj, esp32spi_rssi);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_network_data_obj, esp32spi_network_data);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_ip_address_obj, esp32spi_ip_address);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_is_connected_obj, esp32spi_is_connected);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_ap_listening_obj, esp32spi_ap_listening);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_disconnect_obj, esp32spi_disconnect);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_connect_obj, esp32spi_connect);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp32spi_connect_AP_obj, esp32spi_connect_AP);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32spi_create_AP_obj, 4, 4, esp32spi_create_AP);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_pretty_ip_obj, esp32spi_pretty_ip);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_unpretty_ip_obj, esp32spi_unpretty_ip);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_get_host_by_name_obj, esp32spi_get_host_by_name);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32spi_ping_obj, esp32spi_ping);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_get_socket_obj, esp32spi_get_socket);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32spi_socket_open_obj, 4, 4, esp32spi_socket_open);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_socket_status_obj, esp32spi_socket_status);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_socket_connected_obj, esp32spi_socket_connected);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp32spi_socket_write_obj, esp32spi_socket_write);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_socket_available_obj, esp32spi_socket_available);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32spi_socket_read_obj, esp32spi_socket_read);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32spi_socket_connect_obj, 4, 4, esp32spi_socket_connect);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_socket_close_obj, esp32spi_socket_close);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp32spi_start_server_obj, 4, 4, esp32spi_start_server);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_server_state_obj, esp32spi_server_state);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_esp_debug_obj, esp32spi_esp_debug);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32spi_set_pin_mode_obj, esp32spi_set_pin_mode);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32spi_set_digital_write_obj, esp32spi_set_digital_write);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32spi_set_analog_write_obj, esp32spi_set_analog_write);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_set_digital_read_obj, esp32spi_set_digital_read);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32spi_set_analog_read_obj, esp32spi_set_analog_read);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32spi_get_time_obj, esp32spi_get_time);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_set_certificate_obj, esp32spi_set_certificate);
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32spi_set_private_key_obj, esp32spi_set_private_key);
|
||||
|
||||
/***** Globals Table *****/
|
||||
STATIC const mp_map_elem_t esp32spi_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp32spi) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&esp32spi_init_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&esp32spi_reset_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_status2), MP_ROM_PTR(&esp32spi_status_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_firmware_version), MP_ROM_PTR(&esp32spi_firmware_version_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_MAC_address), MP_ROM_PTR(&esp32spi_MAC_address_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_MAC_address_actual), MP_ROM_PTR(&esp32spi_MAC_address_actual_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_start_scan_networks), MP_ROM_PTR(&esp32spi_start_scan_networks_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_scan_networks), MP_ROM_PTR(&esp32spi_get_scan_networks_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_scan_networks), MP_ROM_PTR(&esp32spi_scan_networks_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_wifi_set_network), MP_ROM_PTR(&esp32spi_wifi_set_network_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_wifi_set_passphrase), MP_ROM_PTR(&esp32spi_wifi_set_passphrase_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_wifi_set_entidentity), MP_ROM_PTR(&esp32spi_wifi_set_entidentity_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_wifi_set_entusername), MP_ROM_PTR(&esp32spi_wifi_set_entusername_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_wifi_set_entpassword), MP_ROM_PTR(&esp32spi_wifi_set_entpassword_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_wifi_set_entenable), MP_ROM_PTR(&esp32spi_wifi_set_entenable_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ssid), MP_ROM_PTR(&esp32spi_ssid_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_bssid), MP_ROM_PTR(&esp32spi_bssid_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&esp32spi_rssi_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_network_data), MP_ROM_PTR(&esp32spi_network_data_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ip_address), MP_ROM_PTR(&esp32spi_ip_address_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_is_connected), MP_ROM_PTR(&esp32spi_is_connected_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ap_listening), MP_ROM_PTR(&esp32spi_ap_listening_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&esp32spi_disconnect_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_connect2), MP_ROM_PTR(&esp32spi_connect_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_connect_AP), MP_ROM_PTR(&esp32spi_connect_AP_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_create_AP), MP_ROM_PTR(&esp32spi_create_AP_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pretty_ip), MP_ROM_PTR(&esp32spi_pretty_ip_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_unpretty_ip), MP_ROM_PTR(&esp32spi_unpretty_ip_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_host_by_name), MP_ROM_PTR(&esp32spi_get_host_by_name_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_ping), MP_ROM_PTR(&esp32spi_ping_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_socket), MP_ROM_PTR(&esp32spi_get_socket_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_socket_open), MP_ROM_PTR(&esp32spi_socket_open_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_socket_status), MP_ROM_PTR(&esp32spi_socket_status_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_socket_connected), MP_ROM_PTR(&esp32spi_socket_connected_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_socket_write), MP_ROM_PTR(&esp32spi_socket_write_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_socket_available), MP_ROM_PTR(&esp32spi_socket_available_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_socket_read), MP_ROM_PTR(&esp32spi_socket_read_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_socket_connect), MP_ROM_PTR(&esp32spi_socket_connect_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_socket_close), MP_ROM_PTR(&esp32spi_socket_close_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_start_server2), MP_ROM_PTR(&esp32spi_start_server_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_server_state), MP_ROM_PTR(&esp32spi_server_state_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_esp_debug), MP_ROM_PTR(&esp32spi_esp_debug_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_pin_mode), MP_ROM_PTR(&esp32spi_set_pin_mode_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_digital_write), MP_ROM_PTR(&esp32spi_set_digital_write_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_analog_write), MP_ROM_PTR(&esp32spi_set_analog_write_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_digital_read), MP_ROM_PTR(&esp32spi_set_digital_read_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_analog_read), MP_ROM_PTR(&esp32spi_set_analog_read_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_get_time), MP_ROM_PTR(&esp32spi_get_time_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_certificate), MP_ROM_PTR(&esp32spi_set_certificate_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_private_key), MP_ROM_PTR(&esp32spi_set_private_key_obj) },
|
||||
};
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_esp32spi_globals, esp32spi_globals_table);
|
||||
|
||||
/***** Module Definition *****/
|
||||
const mp_obj_module_t esp32spi_user_cmodule = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_esp32spi_globals,
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
MP_REGISTER_MODULE(MP_QSTR_esp32spi, esp32spi_user_cmodule, MODULE_ESP32SPI_ENABLED);
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
@ -0,0 +1,837 @@
|
|||
#include "hardware/spi.h"
|
||||
#include "hardware/sync.h"
|
||||
#include "pico/binary_info.h"
|
||||
|
||||
//#include "../../../pimoroni-pico/libraries/pico_scroll/pico_scroll.hpp"
|
||||
|
||||
//using namespace pimoroni;
|
||||
|
||||
class ESP32SPI {
|
||||
|
||||
public:
|
||||
void init(void) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
ESP32SPI *esp = nullptr;
|
||||
|
||||
|
||||
extern "C" {
|
||||
#include "esp32spi.h"
|
||||
|
||||
#define NOT_INITIALISED_MSG "Cannot call this function, as esp32spi is not initialised. Call esp32spi.init() first."
|
||||
|
||||
mp_obj_t esp32spi_init() {
|
||||
if(esp == nullptr)
|
||||
esp = new ESP32SPI();
|
||||
esp->init();
|
||||
|
||||
// self._debug = debug
|
||||
// self.set_psk = False
|
||||
// self.set_crt = False
|
||||
// self._buffer = bytearray(10)
|
||||
// self._pbuf = bytearray(1) # buffer for param read
|
||||
// self._sendbuf = bytearray(256) # buffer for command sending
|
||||
// self._socknum_ll = [[0]] # pre-made list of list of socket #
|
||||
|
||||
// self._spi_device = SPIDevice(spi, cs_pin, baudrate=8000000)
|
||||
// self._cs = cs_pin
|
||||
// self._ready = ready_pin
|
||||
// self._reset = reset_pin
|
||||
// self._gpio0 = gpio0_pin
|
||||
// self._cs.direction = Direction.OUTPUT
|
||||
// self._ready.direction = Direction.INPUT
|
||||
// self._reset.direction = Direction.OUTPUT
|
||||
// # Only one TLS socket at a time is supported so track when we already have one.
|
||||
// self._tls_socket = None
|
||||
// if self._gpio0:
|
||||
// self._gpio0.direction = Direction.INPUT
|
||||
// self.reset()
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_reset() {
|
||||
// """Hard reset the ESP32 using the reset pin"""
|
||||
// if self._debug:
|
||||
// print("Reset ESP32")
|
||||
// if self._gpio0:
|
||||
// self._gpio0.direction = Direction.OUTPUT
|
||||
// self._gpio0.value = True # not bootload mode
|
||||
// self._cs.value = True
|
||||
// self._reset.value = False
|
||||
// time.sleep(0.01) # reset
|
||||
// self._reset.value = True
|
||||
// time.sleep(0.75) # wait for it to boot up
|
||||
// if self._gpio0:
|
||||
// self._gpio0.direction = Direction.INPUT
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_status() {
|
||||
// """The status of the ESP32 WiFi core. Can be WL_NO_SHIELD or WL_NO_MODULE
|
||||
// (not found), WL_IDLE_STATUS, WL_NO_SSID_AVAIL, WL_SCAN_COMPLETED,
|
||||
// WL_CONNECTED, WL_CONNECT_FAILED, WL_CONNECTION_LOST, WL_DISCONNECTED,
|
||||
// WL_AP_LISTENING, WL_AP_CONNECTED, WL_AP_FAILED"""
|
||||
// resp = self._send_command_get_response(_GET_CONN_STATUS_CMD)
|
||||
// if self._debug:
|
||||
// print("Connection status:", resp[0][0])
|
||||
// return resp[0][0] # one byte response
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_firmware_version() {
|
||||
// """A string of the firmware version on the ESP32"""
|
||||
// if self._debug:
|
||||
// print("Firmware version")
|
||||
// resp = self._send_command_get_response(_GET_FW_VERSION_CMD)
|
||||
// return resp[0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_MAC_address() {
|
||||
// """A bytearray containing the MAC address of the ESP32"""
|
||||
// if self._debug:
|
||||
// print("MAC address")
|
||||
// resp = self._send_command_get_response(_GET_MACADDR_CMD, [b"\xFF"])
|
||||
// return resp[0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_MAC_address_actual() {
|
||||
// """A bytearray containing the actual MAC address of the ESP32"""
|
||||
// if self._debug:
|
||||
// print("MAC address")
|
||||
// resp = self._send_command_get_response(_GET_MACADDR_CMD, [b"\xFF"])
|
||||
// new_resp = bytearray(resp[0])
|
||||
// new_resp = reversed(new_resp)
|
||||
// return new_resp
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_start_scan_networks() {
|
||||
// """Begin a scan of visible access points. Follow up with a call
|
||||
// to 'get_scan_networks' for response"""
|
||||
// if self._debug:
|
||||
// print("Start scan")
|
||||
// resp = self._send_command_get_response(_START_SCAN_NETWORKS)
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to start AP scan")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_get_scan_networks() {
|
||||
// """The results of the latest SSID scan. Returns a list of dictionaries with
|
||||
// 'ssid', 'rssi', 'encryption', bssid, and channel entries, one for each AP found"""
|
||||
// self._send_command(_SCAN_NETWORKS)
|
||||
// names = self._wait_response_cmd(_SCAN_NETWORKS)
|
||||
// # print("SSID names:", names)
|
||||
// APs = [] # pylint: disable=invalid-name
|
||||
// for i, name in enumerate(names):
|
||||
// a_p = {"ssid": name}
|
||||
// rssi = self._send_command_get_response(_GET_IDX_RSSI_CMD, ((i,),))[0]
|
||||
// a_p["rssi"] = struct.unpack("<i", rssi)[0]
|
||||
// encr = self._send_command_get_response(_GET_IDX_ENCT_CMD, ((i,),))[0]
|
||||
// a_p["encryption"] = encr[0]
|
||||
// bssid = self._send_command_get_response(_GET_IDX_BSSID_CMD, ((i,),))[0]
|
||||
// a_p["bssid"] = bssid
|
||||
// chan = self._send_command_get_response(_GET_IDX_CHAN_CMD, ((i,),))[0]
|
||||
// a_p["channel"] = chan[0]
|
||||
// APs.append(a_p)
|
||||
// return APs
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_scan_networks() {
|
||||
// """Scan for visible access points, returns a list of access point details.
|
||||
// Returns a list of dictionaries with 'ssid', 'rssi' and 'encryption' entries,
|
||||
// one for each AP found"""
|
||||
// self.start_scan_networks()
|
||||
// for _ in range(10): # attempts
|
||||
// time.sleep(2)
|
||||
// APs = self.get_scan_networks() # pylint: disable=invalid-name
|
||||
// if APs:
|
||||
// return APs
|
||||
// return None
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_wifi_set_network(mp_obj_t ssid) {
|
||||
// """Tells the ESP32 to set the access point to the given ssid"""
|
||||
// resp = self._send_command_get_response(_SET_NET_CMD, [ssid])
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to set network")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_wifi_set_passphrase(mp_obj_t ssid, mp_obj_t passphrase) {
|
||||
// """Sets the desired access point ssid and passphrase"""
|
||||
// resp = self._send_command_get_response(_SET_PASSPHRASE_CMD, [ssid, passphrase])
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to set passphrase")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_wifi_set_entidentity(mp_obj_t ident) {
|
||||
// """Sets the WPA2 Enterprise anonymous identity"""
|
||||
// resp = self._send_command_get_response(_SET_ENT_IDENT_CMD, [ident])
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to set enterprise anonymous identity")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_wifi_set_entusername(mp_obj_t username) {
|
||||
// """Sets the desired WPA2 Enterprise username"""
|
||||
// resp = self._send_command_get_response(_SET_ENT_UNAME_CMD, [username])
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to set enterprise username")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_wifi_set_entpassword(mp_obj_t password) {
|
||||
// """Sets the desired WPA2 Enterprise password"""
|
||||
// resp = self._send_command_get_response(_SET_ENT_PASSWD_CMD, [password])
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to set enterprise password")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_wifi_set_entenable() {
|
||||
// """Enables WPA2 Enterprise mode"""
|
||||
// resp = self._send_command_get_response(_SET_ENT_ENABLE_CMD)
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to enable enterprise mode")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_ssid() {
|
||||
// """The name of the access point we're connected to"""
|
||||
// resp = self._send_command_get_response(_GET_CURR_SSID_CMD, [b"\xFF"])
|
||||
// return resp[0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_bssid() {
|
||||
// """The MAC-formatted service set ID of the access point we're connected to"""
|
||||
// resp = self._send_command_get_response(_GET_CURR_BSSID_CMD, [b"\xFF"])
|
||||
// return resp[0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_rssi() {
|
||||
// """The receiving signal strength indicator for the access point we're
|
||||
// connected to"""
|
||||
// resp = self._send_command_get_response(_GET_CURR_RSSI_CMD, [b"\xFF"])
|
||||
// return struct.unpack("<i", resp[0])[0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_network_data() {
|
||||
// """A dictionary containing current connection details such as the 'ip_addr',
|
||||
// 'netmask' and 'gateway'"""
|
||||
// resp = self._send_command_get_response(
|
||||
// _GET_IPADDR_CMD, [b"\xFF"], reply_params=3
|
||||
// )
|
||||
// return {"ip_addr": resp[0], "netmask": resp[1], "gateway": resp[2]}
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_ip_address() {
|
||||
// """Our local IP address"""
|
||||
// return self.network_data["ip_addr"]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_is_connected() {
|
||||
// """Whether the ESP32 is connected to an access point"""
|
||||
// try:
|
||||
// return self.status == WL_CONNECTED
|
||||
// except RuntimeError:
|
||||
// self.reset()
|
||||
// return False
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_ap_listening() {
|
||||
// """Returns if the ESP32 is in access point mode and is listening for connections"""
|
||||
// try:
|
||||
// return self.status == WL_AP_LISTENING
|
||||
// except RuntimeError:
|
||||
// self.reset()
|
||||
// return False
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_disconnect() {
|
||||
// """Disconnect from the access point"""
|
||||
// resp = self._send_command_get_response(_DISCONNECT_CMD)
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to disconnect")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_connect(mp_obj_t secrets) {
|
||||
// """Connect to an access point using a secrets dictionary
|
||||
// that contains a 'ssid' and 'password' entry"""
|
||||
// self.connect_AP(secrets["ssid"], secrets["password"])
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_connect_AP(mp_obj_t ssid, mp_obj_t password, mp_obj_t timeout_s/* = 10*/) {
|
||||
// """
|
||||
// Connect to an access point with given name and password.
|
||||
// Will wait until specified timeout seconds and return on success
|
||||
// or raise an exception on failure.
|
||||
// :param ssid: the SSID to connect to
|
||||
// :param passphrase: the password of the access point
|
||||
// :param timeout_s: number of seconds until we time out and fail to create AP
|
||||
// """
|
||||
// if self._debug:
|
||||
// print("Connect to AP", ssid, password)
|
||||
// if isinstance(ssid, str):
|
||||
// ssid = bytes(ssid, "utf-8")
|
||||
// if password:
|
||||
// if isinstance(password, str):
|
||||
// password = bytes(password, "utf-8")
|
||||
// self.wifi_set_passphrase(ssid, password)
|
||||
// else:
|
||||
// self.wifi_set_network(ssid)
|
||||
// times = time.monotonic()
|
||||
// while (time.monotonic() - times) < timeout_s: # wait up until timeout
|
||||
// stat = self.status
|
||||
// if stat == WL_CONNECTED:
|
||||
// return stat
|
||||
// time.sleep(0.05)
|
||||
// if stat in (WL_CONNECT_FAILED, WL_CONNECTION_LOST, WL_DISCONNECTED):
|
||||
// raise RuntimeError("Failed to connect to ssid", ssid)
|
||||
// if stat == WL_NO_SSID_AVAIL:
|
||||
// raise RuntimeError("No such ssid", ssid)
|
||||
// raise RuntimeError("Unknown error 0x%02X" % stat)
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_create_AP(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
// """
|
||||
// Create an access point with the given name, password, and channel.
|
||||
// Will wait until specified timeout seconds and return on success
|
||||
// or raise an exception on failure.
|
||||
// :param str ssid: the SSID of the created Access Point. Must be less than 32 chars.
|
||||
// :param str password: the password of the created Access Point. Must be 8-63 chars.
|
||||
// :param int channel: channel of created Access Point (1 - 14).
|
||||
// :param int timeout: number of seconds until we time out and fail to create AP
|
||||
// """
|
||||
// if len(ssid) > 32:
|
||||
// raise RuntimeError("ssid must be no more than 32 characters")
|
||||
// if password and (len(password) < 8 or len(password) > 64):
|
||||
// raise RuntimeError("password must be 8 - 63 characters")
|
||||
// if channel < 1 or channel > 14:
|
||||
// raise RuntimeError("channel must be between 1 and 14")
|
||||
|
||||
// if isinstance(channel, int):
|
||||
// channel = bytes(channel)
|
||||
// if isinstance(ssid, str):
|
||||
// ssid = bytes(ssid, "utf-8")
|
||||
// if password:
|
||||
// if isinstance(password, str):
|
||||
// password = bytes(password, "utf-8")
|
||||
// self._wifi_set_ap_passphrase(ssid, password, channel)
|
||||
// else:
|
||||
// self._wifi_set_ap_network(ssid, channel)
|
||||
|
||||
// times = time.monotonic()
|
||||
// while (time.monotonic() - times) < timeout: # wait up to timeout
|
||||
// stat = self.status
|
||||
// if stat == WL_AP_LISTENING:
|
||||
// return stat
|
||||
// time.sleep(0.05)
|
||||
// if stat == WL_AP_FAILED:
|
||||
// raise RuntimeError("Failed to create AP", ssid)
|
||||
// raise RuntimeError("Unknown error 0x%02x" % stat)
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_pretty_ip(mp_obj_t ip) {
|
||||
// """Converts a bytearray IP address to a dotted-quad string for printing"""
|
||||
// return "%d.%d.%d.%d" % (ip[0], ip[1], ip[2], ip[3])
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_unpretty_ip(mp_obj_t ip) {
|
||||
// """Converts a dotted-quad string to a bytearray IP address"""
|
||||
// octets = [int(x) for x in ip.split(".")]
|
||||
// return bytes(octets)
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_get_host_by_name(mp_obj_t hostname) {
|
||||
// """Convert a hostname to a packed 4-byte IP address. Returns
|
||||
// a 4 bytearray"""
|
||||
// if self._debug:
|
||||
// print("*** Get host by name")
|
||||
// if isinstance(hostname, str):
|
||||
// hostname = bytes(hostname, "utf-8")
|
||||
// resp = self._send_command_get_response(_REQ_HOST_BY_NAME_CMD, (hostname,))
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to request hostname")
|
||||
// resp = self._send_command_get_response(_GET_HOST_BY_NAME_CMD)
|
||||
// return resp[0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_ping(mp_obj_t dest, mp_obj_t ttl/* = 250*/) {
|
||||
// """Ping a destination IP address or hostname, with a max time-to-live
|
||||
// (ttl). Returns a millisecond timing value"""
|
||||
// if isinstance(dest, str): # convert to IP address
|
||||
// dest = self.get_host_by_name(dest)
|
||||
// # ttl must be between 0 and 255
|
||||
// ttl = max(0, min(ttl, 255))
|
||||
// resp = self._send_command_get_response(_PING_CMD, (dest, (ttl,)))
|
||||
// return struct.unpack("<H", resp[0])[0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_get_socket() {
|
||||
// """Request a socket from the ESP32, will allocate and return a number that
|
||||
// can then be passed to the other socket commands"""
|
||||
// if self._debug:
|
||||
// print("*** Get socket")
|
||||
// resp = self._send_command_get_response(_GET_SOCKET_CMD)
|
||||
// resp = resp[0][0]
|
||||
// if resp == 255:
|
||||
// raise OSError(23) # ENFILE - File table overflow
|
||||
// if self._debug:
|
||||
// print("Allocated socket #%d" % resp)
|
||||
// return resp
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_socket_open(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
// """Open a socket to a destination IP address or hostname
|
||||
// using the ESP32's internal reference number. By default we use
|
||||
// 'conn_mode' TCP_MODE but can also use UDP_MODE or TLS_MODE
|
||||
// (dest must be hostname for TLS_MODE!)"""
|
||||
// self._socknum_ll[0][0] = socket_num
|
||||
// if self._debug:
|
||||
// print("*** Open socket to", dest, port, conn_mode)
|
||||
// if conn_mode == ESP_SPIcontrol.TLS_MODE and self._tls_socket is not None:
|
||||
// raise OSError(23) # ENFILE - File table overflow
|
||||
// port_param = struct.pack(">H", port)
|
||||
// if isinstance(dest, str): # use the 5 arg version
|
||||
// dest = bytes(dest, "utf-8")
|
||||
// resp = self._send_command_get_response(
|
||||
// _START_CLIENT_TCP_CMD,
|
||||
// (
|
||||
// dest,
|
||||
// b"\x00\x00\x00\x00",
|
||||
// port_param,
|
||||
// self._socknum_ll[0],
|
||||
// (conn_mode,),
|
||||
// ),
|
||||
// )
|
||||
// else: # ip address, use 4 arg vesion
|
||||
// resp = self._send_command_get_response(
|
||||
// _START_CLIENT_TCP_CMD,
|
||||
// (dest, port_param, self._socknum_ll[0], (conn_mode,)),
|
||||
// )
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Could not connect to remote server")
|
||||
// if conn_mode == ESP_SPIcontrol.TLS_MODE:
|
||||
// self._tls_socket = socket_num
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_socket_status(mp_obj_t socket_num) {
|
||||
// """Get the socket connection status, can be SOCKET_CLOSED, SOCKET_LISTEN,
|
||||
// SOCKET_SYN_SENT, SOCKET_SYN_RCVD, SOCKET_ESTABLISHED, SOCKET_FIN_WAIT_1,
|
||||
// SOCKET_FIN_WAIT_2, SOCKET_CLOSE_WAIT, SOCKET_CLOSING, SOCKET_LAST_ACK, or
|
||||
// SOCKET_TIME_WAIT"""
|
||||
// self._socknum_ll[0][0] = socket_num
|
||||
// resp = self._send_command_get_response(
|
||||
// _GET_CLIENT_STATE_TCP_CMD, self._socknum_ll
|
||||
// )
|
||||
// return resp[0][0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_socket_connected(mp_obj_t socket_num) {
|
||||
// """Test if a socket is connected to the destination, returns boolean true/false"""
|
||||
// return self.socket_status(socket_num) == SOCKET_ESTABLISHED
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_socket_write(mp_obj_t socket_num, mp_obj_t buffer, mp_obj_t conn_mode/* = TCP_MODE*/) {
|
||||
// """Write the bytearray buffer to a socket"""
|
||||
// if self._debug:
|
||||
// print("Writing:", buffer)
|
||||
// self._socknum_ll[0][0] = socket_num
|
||||
// sent = 0
|
||||
// total_chunks = (len(buffer) // 64) + 1
|
||||
// send_command = _SEND_DATA_TCP_CMD
|
||||
// if conn_mode == self.UDP_MODE: # UDP requires a different command to write
|
||||
// send_command = _INSERT_DATABUF_TCP_CMD
|
||||
// for chunk in range(total_chunks):
|
||||
// resp = self._send_command_get_response(
|
||||
// send_command,
|
||||
// (
|
||||
// self._socknum_ll[0],
|
||||
// memoryview(buffer)[(chunk * 64) : ((chunk + 1) * 64)],
|
||||
// ),
|
||||
// sent_param_len_16=True,
|
||||
// )
|
||||
// sent += resp[0][0]
|
||||
|
||||
// if conn_mode == self.UDP_MODE:
|
||||
// # UDP verifies chunks on write, not bytes
|
||||
// if sent != total_chunks:
|
||||
// raise RuntimeError(
|
||||
// "Failed to write %d chunks (sent %d)" % (total_chunks, sent)
|
||||
// )
|
||||
// # UDP needs to finalize with this command, does the actual sending
|
||||
// resp = self._send_command_get_response(_SEND_UDP_DATA_CMD, self._socknum_ll)
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to send UDP data")
|
||||
// return
|
||||
|
||||
// if sent != len(buffer):
|
||||
// self.socket_close(socket_num)
|
||||
// raise RuntimeError(
|
||||
// "Failed to send %d bytes (sent %d)" % (len(buffer), sent)
|
||||
// )
|
||||
|
||||
// resp = self._send_command_get_response(_DATA_SENT_TCP_CMD, self._socknum_ll)
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to verify data sent")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_socket_available(mp_obj_t socket_num) {
|
||||
// """Determine how many bytes are waiting to be read on the socket"""
|
||||
// self._socknum_ll[0][0] = socket_num
|
||||
// resp = self._send_command_get_response(_AVAIL_DATA_TCP_CMD, self._socknum_ll)
|
||||
// reply = struct.unpack("<H", resp[0])[0]
|
||||
// if self._debug:
|
||||
// print("ESPSocket: %d bytes available" % reply)
|
||||
// return reply
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_socket_read(mp_obj_t socket_num, mp_obj_t size) {
|
||||
// """Read up to 'size' bytes from the socket number. Returns a bytearray"""
|
||||
// if self._debug:
|
||||
// print(
|
||||
// "Reading %d bytes from ESP socket with status %d"
|
||||
// % (size, self.socket_status(socket_num))
|
||||
// )
|
||||
// self._socknum_ll[0][0] = socket_num
|
||||
// resp = self._send_command_get_response(
|
||||
// _GET_DATABUF_TCP_CMD,
|
||||
// (self._socknum_ll[0], (size & 0xFF, (size >> 8) & 0xFF)),
|
||||
// sent_param_len_16=True,
|
||||
// recv_param_len_16=True,
|
||||
// )
|
||||
// return bytes(resp[0])
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_socket_connect(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
// """Open and verify we connected a socket to a destination IP address or hostname
|
||||
// using the ESP32's internal reference number. By default we use
|
||||
// 'conn_mode' TCP_MODE but can also use UDP_MODE or TLS_MODE (dest must
|
||||
// be hostname for TLS_MODE!)"""
|
||||
// if self._debug:
|
||||
// print("*** Socket connect mode", conn_mode)
|
||||
|
||||
// self.socket_open(socket_num, dest, port, conn_mode=conn_mode)
|
||||
// if conn_mode == self.UDP_MODE:
|
||||
// # UDP doesn't actually establish a connection
|
||||
// # but the socket for writing is created via start_server
|
||||
// self.start_server(port, socket_num, conn_mode)
|
||||
// return True
|
||||
|
||||
// times = time.monotonic()
|
||||
// while (time.monotonic() - times) < 3: # wait 3 seconds
|
||||
// if self.socket_connected(socket_num):
|
||||
// return True
|
||||
// time.sleep(0.01)
|
||||
// raise RuntimeError("Failed to establish connection")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_socket_close(mp_obj_t socket_num) {
|
||||
// """Close a socket using the ESP32's internal reference number"""
|
||||
// if self._debug:
|
||||
// print("*** Closing socket #%d" % socket_num)
|
||||
// self._socknum_ll[0][0] = socket_num
|
||||
// try:
|
||||
// self._send_command_get_response(_STOP_CLIENT_TCP_CMD, self._socknum_ll)
|
||||
// except RuntimeError:
|
||||
// pass
|
||||
// if socket_num == self._tls_socket:
|
||||
// self._tls_socket = None
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_start_server(mp_uint_t n_args, const mp_obj_t *args) {
|
||||
// """Opens a server on the specified port, using the ESP32's internal reference number"""
|
||||
// if self._debug:
|
||||
// print("*** starting server")
|
||||
// self._socknum_ll[0][0] = socket_num
|
||||
// params = [struct.pack(">H", port), self._socknum_ll[0], (conn_mode,)]
|
||||
// if ip:
|
||||
// params.insert(0, ip)
|
||||
// resp = self._send_command_get_response(_START_SERVER_TCP_CMD, params)
|
||||
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Could not start server")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_server_state(mp_obj_t socket_num) {
|
||||
// """Get the state of the ESP32's internal reference server socket number"""
|
||||
// self._socknum_ll[0][0] = socket_num
|
||||
// resp = self._send_command_get_response(_GET_STATE_TCP_CMD, self._socknum_ll)
|
||||
// return resp[0][0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_esp_debug(mp_obj_t enabled) {
|
||||
// """Enable/disable debug mode on the ESP32. Debug messages will be
|
||||
// written to the ESP32's UART."""
|
||||
// resp = self._send_command_get_response(_SET_DEBUG_CMD, ((bool(enabled),),))
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to set debug mode")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_set_pin_mode(mp_obj_t pin, mp_obj_t mode) {
|
||||
// """
|
||||
// Set the io mode for a GPIO pin.
|
||||
// :param int pin: ESP32 GPIO pin to set.
|
||||
// :param value: direction for pin, digitalio.Direction or integer (0=input, 1=output).
|
||||
// """
|
||||
// if mode == Direction.OUTPUT:
|
||||
// pin_mode = 1
|
||||
// elif mode == Direction.INPUT:
|
||||
// pin_mode = 0
|
||||
// else:
|
||||
// pin_mode = mode
|
||||
// resp = self._send_command_get_response(_SET_PIN_MODE_CMD, ((pin,), (pin_mode,)))
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to set pin mode")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_set_digital_write(mp_obj_t pin, mp_obj_t value) {
|
||||
// """
|
||||
// Set the digital output value of pin.
|
||||
// :param int pin: ESP32 GPIO pin to write to.
|
||||
// :param bool value: Value for the pin.
|
||||
// """
|
||||
// resp = self._send_command_get_response(
|
||||
// _SET_DIGITAL_WRITE_CMD, ((pin,), (value,))
|
||||
// )
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to write to pin")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_set_analog_write(mp_obj_t pin, mp_obj_t analog_value) {
|
||||
// """
|
||||
// Set the analog output value of pin, using PWM.
|
||||
// :param int pin: ESP32 GPIO pin to write to.
|
||||
// :param float value: 0=off 1.0=full on
|
||||
// """
|
||||
// value = int(255 * analog_value)
|
||||
// resp = self._send_command_get_response(
|
||||
// _SET_ANALOG_WRITE_CMD, ((pin,), (value,))
|
||||
// )
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to write to pin")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_set_digital_read(mp_obj_t pin) {
|
||||
// """
|
||||
// Get the digital input value of pin. Returns the boolean value of the pin.
|
||||
// :param int pin: ESP32 GPIO pin to read from.
|
||||
// """
|
||||
// # Verify nina-fw => 1.5.0
|
||||
// fw_semver_maj = bytes(self.firmware_version).decode("utf-8")[2]
|
||||
// assert int(fw_semver_maj) >= 5, "Please update nina-fw to 1.5.0 or above."
|
||||
|
||||
// resp = self._send_command_get_response(_SET_DIGITAL_READ_CMD, ((pin,),))[0]
|
||||
// if resp[0] == 0:
|
||||
// return False
|
||||
// if resp[0] == 1:
|
||||
// return True
|
||||
// raise ValueError(
|
||||
// "_SET_DIGITAL_READ response error: response is not boolean", resp[0]
|
||||
// )
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_set_analog_read(mp_obj_t pin, mp_obj_t atten/* = ADC_ATTEN_DB_11*/) {
|
||||
// """
|
||||
// Get the analog input value of pin. Returns an int between 0 and 65536.
|
||||
// :param int pin: ESP32 GPIO pin to read from.
|
||||
// :param int atten: attenuation constant
|
||||
// """
|
||||
// # Verify nina-fw => 1.5.0
|
||||
// fw_semver_maj = bytes(self.firmware_version).decode("utf-8")[2]
|
||||
// assert int(fw_semver_maj) >= 5, "Please update nina-fw to 1.5.0 or above."
|
||||
|
||||
// resp = self._send_command_get_response(_SET_ANALOG_READ_CMD, ((pin,), (atten,)))
|
||||
// resp_analog = struct.unpack("<i", resp[0])
|
||||
// if resp_analog[0] < 0:
|
||||
// raise ValueError(
|
||||
// "_SET_ANALOG_READ parameter error: invalid pin", resp_analog[0]
|
||||
// )
|
||||
// if self._debug:
|
||||
// print(resp, resp_analog, resp_analog[0], 16 * resp_analog[0])
|
||||
// return 16 * resp_analog[0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_get_time() {
|
||||
// """The current unix timestamp"""
|
||||
// if self.status == WL_CONNECTED:
|
||||
// resp = self._send_command_get_response(_GET_TIME)
|
||||
// resp_time = struct.unpack("<i", resp[0])
|
||||
// if resp_time == (0,):
|
||||
// raise ValueError("_GET_TIME returned 0")
|
||||
// return resp_time
|
||||
// if self.status in (WL_AP_LISTENING, WL_AP_CONNECTED):
|
||||
// raise RuntimeError(
|
||||
// "Cannot obtain NTP while in AP mode, must be connected to internet"
|
||||
// )
|
||||
// raise RuntimeError("Must be connected to WiFi before obtaining NTP.")
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_set_certificate(mp_obj_t client_certificate) {
|
||||
// """Sets client certificate. Must be called
|
||||
// BEFORE a network connection is established.
|
||||
// :param str client_certificate: User-provided .PEM certificate up to 1300 bytes.
|
||||
// """
|
||||
// if self._debug:
|
||||
// print("** Setting client certificate")
|
||||
// if self.status == WL_CONNECTED:
|
||||
// raise RuntimeError(
|
||||
// "set_certificate must be called BEFORE a connection is established."
|
||||
// )
|
||||
// if isinstance(client_certificate, str):
|
||||
// client_certificate = bytes(client_certificate, "utf-8")
|
||||
// if "-----BEGIN CERTIFICATE" not in client_certificate:
|
||||
// raise TypeError(".PEM must start with -----BEGIN CERTIFICATE")
|
||||
// assert len(client_certificate) < 1300, ".PEM must be less than 1300 bytes."
|
||||
// resp = self._send_command_get_response(_SET_CLI_CERT, (client_certificate,))
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to set client certificate")
|
||||
// self.set_crt = True
|
||||
// return resp[0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
mp_obj_t esp32spi_set_private_key(mp_obj_t private_key) {
|
||||
// """Sets private key. Must be called
|
||||
// BEFORE a network connection is established.
|
||||
// :param str private_key: User-provided .PEM file up to 1700 bytes.
|
||||
// """
|
||||
// if self._debug:
|
||||
// print("** Setting client's private key.")
|
||||
// if self.status == WL_CONNECTED:
|
||||
// raise RuntimeError(
|
||||
// "set_private_key must be called BEFORE a connection is established."
|
||||
// )
|
||||
// if isinstance(private_key, str):
|
||||
// private_key = bytes(private_key, "utf-8")
|
||||
// if "-----BEGIN RSA" not in private_key:
|
||||
// raise TypeError(".PEM must start with -----BEGIN RSA")
|
||||
// assert len(private_key) < 1700, ".PEM must be less than 1700 bytes."
|
||||
// resp = self._send_command_get_response(_SET_PK, (private_key,))
|
||||
// if resp[0][0] != 1:
|
||||
// raise RuntimeError("Failed to set private key.")
|
||||
// self.set_psk = True
|
||||
// return resp[0]
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
|
||||
// mp_obj_t esp32spi_get_width() {
|
||||
// return mp_obj_new_int(ESP32SPI::WIDTH);
|
||||
// }
|
||||
|
||||
// mp_obj_t esp32spi_get_height() {
|
||||
// return mp_obj_new_int(ESP32SPI::HEIGHT);
|
||||
// }
|
||||
|
||||
// mp_obj_t esp32spi_update() {
|
||||
// if(scroll != nullptr)
|
||||
// scroll->update();
|
||||
// else
|
||||
// mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
|
||||
// return mp_const_none;
|
||||
// }
|
||||
|
||||
// mp_obj_t esp32spi_set_pixel(mp_obj_t x_obj, mp_obj_t y_obj, mp_obj_t v_obj) {
|
||||
// if(scroll != nullptr) {
|
||||
// int x = mp_obj_get_int(x_obj);
|
||||
// int y = mp_obj_get_int(y_obj);
|
||||
// int val = mp_obj_get_int(v_obj);
|
||||
|
||||
// if(x < 0 || x >= ESP32SPI::WIDTH || y < 0 || y >= ESP32SPI::HEIGHT)
|
||||
// mp_raise_ValueError("x or y out of range.");
|
||||
// else {
|
||||
// if(val < 0 || val > 255)
|
||||
// mp_raise_ValueError("val out of range. Expected 0 to 255");
|
||||
// else
|
||||
// scroll->set_pixel(x, y, val);
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
|
||||
|
||||
// return mp_const_none;
|
||||
// }
|
||||
|
||||
// mp_obj_t esp32spi_clear() {
|
||||
// if(scroll != nullptr)
|
||||
// scroll->clear();
|
||||
// else
|
||||
// mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
|
||||
// return mp_const_none;
|
||||
// }
|
||||
|
||||
// mp_obj_t esp32spi_is_pressed(mp_obj_t button_obj) {
|
||||
// bool buttonPressed = false;
|
||||
|
||||
// if(scroll != nullptr) {
|
||||
// int buttonID = mp_obj_get_int(button_obj);
|
||||
// switch(buttonID) {
|
||||
// case 0:
|
||||
// buttonPressed = scroll->is_pressed(ESP32SPI::A);
|
||||
// break;
|
||||
|
||||
// case 1:
|
||||
// buttonPressed = scroll->is_pressed(ESP32SPI::B);
|
||||
// break;
|
||||
|
||||
// case 2:
|
||||
// buttonPressed = scroll->is_pressed(ESP32SPI::X);
|
||||
// break;
|
||||
|
||||
// case 3:
|
||||
// buttonPressed = scroll->is_pressed(ESP32SPI::Y);
|
||||
// break;
|
||||
|
||||
// default:
|
||||
// mp_raise_ValueError("button not valid. Expected 0 to 3");
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
|
||||
|
||||
// return buttonPressed ? mp_const_true : mp_const_false;
|
||||
// }
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
// Include MicroPython API.
|
||||
#include "py/runtime.h"
|
||||
|
||||
// Declare the functions we'll make available in Python
|
||||
extern mp_obj_t esp32spi_init();
|
||||
extern mp_obj_t esp32spi_reset();
|
||||
extern mp_obj_t esp32spi_status();
|
||||
extern mp_obj_t esp32spi_firmware_version();
|
||||
extern mp_obj_t esp32spi_MAC_address();
|
||||
extern mp_obj_t esp32spi_MAC_address_actual();
|
||||
extern mp_obj_t esp32spi_start_scan_networks();
|
||||
extern mp_obj_t esp32spi_get_scan_networks();
|
||||
extern mp_obj_t esp32spi_scan_networks();
|
||||
extern mp_obj_t esp32spi_wifi_set_network(mp_obj_t ssid);
|
||||
extern mp_obj_t esp32spi_wifi_set_passphrase(mp_obj_t ssid, mp_obj_t passphrase);
|
||||
extern mp_obj_t esp32spi_wifi_set_entidentity(mp_obj_t ident);
|
||||
extern mp_obj_t esp32spi_wifi_set_entusername(mp_obj_t username);
|
||||
extern mp_obj_t esp32spi_wifi_set_entpassword(mp_obj_t password);
|
||||
extern mp_obj_t esp32spi_wifi_set_entenable();
|
||||
extern mp_obj_t esp32spi_ssid();
|
||||
extern mp_obj_t esp32spi_bssid();
|
||||
extern mp_obj_t esp32spi_rssi();
|
||||
extern mp_obj_t esp32spi_network_data();
|
||||
extern mp_obj_t esp32spi_ip_address();
|
||||
extern mp_obj_t esp32spi_is_connected();
|
||||
extern mp_obj_t esp32spi_ap_listening();
|
||||
extern mp_obj_t esp32spi_disconnect();
|
||||
extern mp_obj_t esp32spi_connect(mp_obj_t secrets);
|
||||
extern mp_obj_t esp32spi_connect_AP(mp_obj_t ssid, mp_obj_t password, mp_obj_t timeout_s/* = 10*/);
|
||||
extern mp_obj_t esp32spi_create_AP(mp_uint_t n_args, const mp_obj_t *args);//(mp_obj_t ssid, mp_obj_t password, mp_obj_t channel/* = 1*/, mp_obj_t timeout/* = 10*/);
|
||||
extern mp_obj_t esp32spi_pretty_ip(mp_obj_t ip);
|
||||
extern mp_obj_t esp32spi_unpretty_ip(mp_obj_t ip);
|
||||
extern mp_obj_t esp32spi_get_host_by_name(mp_obj_t hostname);
|
||||
extern mp_obj_t esp32spi_ping(mp_obj_t dest, mp_obj_t ttl/* = 250*/);
|
||||
extern mp_obj_t esp32spi_get_socket();
|
||||
extern mp_obj_t esp32spi_socket_open(mp_uint_t n_args, const mp_obj_t *args);//(mp_obj_t socket_num, mp_obj_t dest, mp_obj_t port, mp_obj_t conn_mode/* = TCP_MODE*/);
|
||||
extern mp_obj_t esp32spi_socket_status(mp_obj_t socket_num);
|
||||
extern mp_obj_t esp32spi_socket_connected(mp_obj_t socket_num);
|
||||
extern mp_obj_t esp32spi_socket_write(mp_obj_t socket_num, mp_obj_t buffer, mp_obj_t conn_mode/* = TCP_MODE*/);
|
||||
extern mp_obj_t esp32spi_socket_available(mp_obj_t socket_num);
|
||||
extern mp_obj_t esp32spi_socket_read(mp_obj_t socket_num, mp_obj_t size);
|
||||
extern mp_obj_t esp32spi_socket_connect(mp_uint_t n_args, const mp_obj_t *args);//(mp_uint_t n_args, const mp_obj_t *args);//(mp_obj_t socket_num, mp_obj_t dest, mp_obj_t port, mp_obj_t conn_mode/* = TCP_MODE*/);
|
||||
extern mp_obj_t esp32spi_socket_close(mp_obj_t socket_num);
|
||||
extern mp_obj_t esp32spi_start_server(mp_uint_t n_args, const mp_obj_t *args);//(mp_obj_t port, mp_obj_t socket_num, mp_obj_t conn_mode/* = TCP_MODE*/, mp_obj_t ip/* = None*/);
|
||||
extern mp_obj_t esp32spi_server_state(mp_obj_t socket_num);
|
||||
extern mp_obj_t esp32spi_esp_debug(mp_obj_t enabled);
|
||||
extern mp_obj_t esp32spi_set_pin_mode(mp_obj_t pin, mp_obj_t mode);
|
||||
extern mp_obj_t esp32spi_set_digital_write(mp_obj_t pin, mp_obj_t value);
|
||||
extern mp_obj_t esp32spi_set_analog_write(mp_obj_t pin, mp_obj_t analog_value);
|
||||
extern mp_obj_t esp32spi_set_digital_read(mp_obj_t pin);
|
||||
extern mp_obj_t esp32spi_set_analog_read(mp_obj_t pin, mp_obj_t atten/* = ADC_ATTEN_DB_11*/);
|
||||
extern mp_obj_t esp32spi_get_time();
|
||||
extern mp_obj_t esp32spi_set_certificate(mp_obj_t client_certificate);
|
||||
extern mp_obj_t esp32spi_set_private_key(mp_obj_t private_key);
|
|
@ -0,0 +1,26 @@
|
|||
set(MOD_NAME esp32spi)
|
||||
string(TOUPPER ${MOD_NAME} MOD_NAME_UPPER)
|
||||
add_library(usermod_${MOD_NAME} INTERFACE)
|
||||
|
||||
target_sources(usermod_${MOD_NAME} INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.cpp
|
||||
#${CMAKE_CURRENT_LIST_DIR}/../../../libraries/${MOD_NAME}/${MOD_NAME}.cpp
|
||||
#${CMAKE_CURRENT_LIST_DIR}/../../../drivers/msa301/msa301.cpp
|
||||
)
|
||||
|
||||
target_include_directories(usermod_${MOD_NAME} INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
target_compile_definitions(usermod_${MOD_NAME} INTERFACE
|
||||
-DMODULE_${MOD_NAME_UPPER}_ENABLED=1
|
||||
)
|
||||
|
||||
target_link_libraries(usermod INTERFACE usermod_${MOD_NAME})
|
||||
|
||||
set_source_files_properties(
|
||||
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c
|
||||
PROPERTIES COMPILE_FLAGS
|
||||
"-Wno-discarded-qualifiers -Wno-implicit-int"
|
||||
)
|
|
@ -0,0 +1,12 @@
|
|||
ESP32SPI_MOD_DIR := $(USERMOD_DIR)
|
||||
|
||||
# Add our source files to the respective variables.
|
||||
SRC_USERMOD += $(ESP32SPI_MOD_DIR)/esp32spi.c
|
||||
SRC_USERMOD_CXX += $(ESP32SPI_MOD_DIR)/esp32spi.cpp
|
||||
|
||||
# Add our module directory to the include path.
|
||||
CFLAGS_USERMOD += -I$(ESP32SPI_MOD_DIR)
|
||||
CXXFLAGS_USERMOD += -I$(ESP32SPI_MOD_DIR)
|
||||
|
||||
# We use C++ features so have to link against the standard library.
|
||||
LDFLAGS_USERMOD += -lstdc++
|
|
@ -4,4 +4,5 @@ include(${CMAKE_CURRENT_LIST_DIR}/pico_rgb_keypad/micropython.cmake)
|
|||
include(${CMAKE_CURRENT_LIST_DIR}/pico_unicorn/micropython.cmake)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/pico_display/micropython.cmake)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/pico_explorer/micropython.cmake)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/esp32spi/micropython.cmake)
|
||||
include(${CMAKE_CURRENT_LIST_DIR}/ulab/code/micropython.cmake)
|
||||
|
|
Ładowanie…
Reference in New Issue