Added digital_read and analog_read functions

pull/126/head
ZodiusInfuser 2021-04-19 16:44:11 +01:00
rodzic 6167589812
commit 15ccd8f001
7 zmienionych plików z 155 dodań i 25 usunięć

Wyświetl plik

@ -73,8 +73,8 @@ namespace pimoroni {
SET_PIN_MODE = 0x50, SET_PIN_MODE = 0x50,
SET_DIGITAL_WRITE = 0x51, SET_DIGITAL_WRITE = 0x51,
SET_ANALOG_WRITE = 0x52, SET_ANALOG_WRITE = 0x52,
SET_DIGITAL_READ = 0x53, //TODO No Matching Function //NOTE Exposed in CPy SET_DIGITAL_READ = 0x53,
SET_ANALOG_READ = 0x54, //TODO No Matching Function //NOTE Exposed in CPy SET_ANALOG_READ = 0x54,
}; };
bool Esp32Spi::init() { bool Esp32Spi::init() {
@ -803,7 +803,7 @@ namespace pimoroni {
float data = 0; float data = 0;
uint8_t data_len = 0; uint8_t data_len = 0;
if(!driver.wait_response_cmd(GET_TEMPERATURE, SpiDrv::PARAM_NUMS_1, (uint8_t*)&data, &data_len)) { if(!driver.wait_response_cmd(GET_TEMPERATURE, SpiDrv::PARAM_NUMS_1, (uint8_t*)&data, &data_len)) {
printf("error get_temperature\n"); WARN("Response Err: GET_TEMPERATURE\n");
} }
driver.esp_deselect(); driver.esp_deselect();
@ -827,7 +827,7 @@ namespace pimoroni {
// Wait for reply // Wait for reply
uint8_t data = 0, data_len = 0; uint8_t data = 0, data_len = 0;
if(!driver.wait_response_cmd(SET_PIN_MODE, SpiDrv::PARAM_NUMS_1, &data, &data_len)) { if(!driver.wait_response_cmd(SET_PIN_MODE, SpiDrv::PARAM_NUMS_1, &data, &data_len)) {
WARN("Response Err: WL_FAILURE\n"); WARN("Response Err: SET_PIN_MODE\n");
data = WL_FAILURE; data = WL_FAILURE;
} }
driver.esp_deselect(); driver.esp_deselect();
@ -850,7 +850,7 @@ namespace pimoroni {
// Wait for reply // Wait for reply
uint8_t data = 0, data_len = 0; uint8_t data = 0, data_len = 0;
if(!driver.wait_response_cmd(SET_DIGITAL_WRITE, SpiDrv::PARAM_NUMS_1, &data, &data_len)) { if(!driver.wait_response_cmd(SET_DIGITAL_WRITE, SpiDrv::PARAM_NUMS_1, &data, &data_len)) {
printf("error digital_write\n"); WARN("Response Err: SET_DIGITAL_WRITE\n");
data = WL_FAILURE; data = WL_FAILURE;
} }
driver.esp_deselect(); driver.esp_deselect();
@ -873,12 +873,61 @@ namespace pimoroni {
// Wait for reply // Wait for reply
uint8_t data = 0, data_len = 0; uint8_t data = 0, data_len = 0;
if(!driver.wait_response_cmd(SET_ANALOG_WRITE, SpiDrv::PARAM_NUMS_1, &data, &data_len)) { if(!driver.wait_response_cmd(SET_ANALOG_WRITE, SpiDrv::PARAM_NUMS_1, &data, &data_len)) {
printf("error analog_write\n"); WARN("Response Err: SET_ANALOG_WRITE\n");
data = WL_FAILURE; data = WL_FAILURE;
} }
driver.esp_deselect(); driver.esp_deselect();
} }
bool Esp32Spi::digital_read(uint8_t pin) {
driver.wait_for_esp_select();
// Send Command
driver.send_cmd(SET_DIGITAL_READ, SpiDrv::PARAM_NUMS_1);
driver.send_param(&pin, 1, SpiDrv::LAST_PARAM);
// Pad to multiple of 4
driver.read_byte();
driver.read_byte();
driver.esp_deselect();
driver.wait_for_esp_select();
// Wait for reply
uint8_t data = 0, data_len = 0;
if(!driver.wait_response_cmd(SET_DIGITAL_READ, SpiDrv::PARAM_NUMS_1, &data, &data_len)) {
WARN("Response Err: SET_DIGITAL_READ\n");
}
driver.esp_deselect();
return (data == 1);
}
uint16_t Esp32Spi::analog_read(uint8_t pin, uint8_t atten) {
driver.wait_for_esp_select();
// Send Command
driver.send_cmd(SET_ANALOG_READ, SpiDrv::PARAM_NUMS_2);
driver.send_param(&pin, 1, SpiDrv::NO_LAST_PARAM);
driver.send_param(&atten, 1, SpiDrv::LAST_PARAM);
// Pad to multiple of 4
driver.read_byte();
driver.esp_deselect();
driver.wait_for_esp_select();
// Wait for reply
uint32_t data = 0;
uint8_t data_len = 0;
if(!driver.wait_response_cmd(SET_ANALOG_READ, SpiDrv::PARAM_NUMS_1, (uint8_t*)&data, &data_len)) {
WARN("Response Err: SET_ANALOG_READ\n");
}
driver.esp_deselect();
return (uint16_t)data; //ESP only has a 12-bit ADC
}
void Esp32Spi::start_server(uint16_t port, uint8_t sock, uint8_t protocol_mode) { void Esp32Spi::start_server(uint16_t port, uint8_t sock, uint8_t protocol_mode) {
driver.wait_for_esp_select(); driver.wait_for_esp_select();
@ -897,7 +946,7 @@ namespace pimoroni {
// Wait for reply // Wait for reply
uint8_t data = 0, data_len = 0; uint8_t data = 0, data_len = 0;
if(!driver.wait_response_cmd(START_SERVER_TCP, SpiDrv::PARAM_NUMS_1, &data, &data_len)) { if(!driver.wait_response_cmd(START_SERVER_TCP, SpiDrv::PARAM_NUMS_1, &data, &data_len)) {
printf("No response to server start/n"); WARN("Response Err: START_SERVER_TCP\n");
} }
driver.esp_deselect(); driver.esp_deselect();
} }
@ -1255,7 +1304,7 @@ namespace pimoroni {
sleep_ms(100); sleep_ms(100);
} }
} while((data==0) && (timeout < TIMEOUT_DATA_SENT)); } while((data == 0) && (timeout < TIMEOUT_DATA_SENT));
return (timeout == TIMEOUT_DATA_SENT) ? 0 : 1; return (timeout == TIMEOUT_DATA_SENT) ? 0 : 1;
} }

Wyświetl plik

@ -96,6 +96,9 @@ namespace pimoroni {
// Constants // Constants
//-------------------------------------------------- //--------------------------------------------------
public: public:
static const uint8_t INPUT = 0;
static const uint8_t OUTPUT = 1;
static const uint8_t INPUT_PULLUP = 2;
//-------------------------------------------------- //--------------------------------------------------
@ -116,8 +119,6 @@ namespace pimoroni {
uint8_t local_ip[WL_IPV4_LENGTH]; uint8_t local_ip[WL_IPV4_LENGTH];
uint8_t subnet_mask[WL_IPV4_LENGTH]; uint8_t subnet_mask[WL_IPV4_LENGTH];
uint8_t gateway_ip[WL_IPV4_LENGTH]; uint8_t gateway_ip[WL_IPV4_LENGTH];
public:
//Esp32Spi();
//-------------------------------------------------- //--------------------------------------------------
@ -183,16 +184,17 @@ namespace pimoroni {
void digital_write(uint8_t pin, uint8_t value); void digital_write(uint8_t pin, uint8_t value);
void analog_write(uint8_t pin, uint8_t value); void analog_write(uint8_t pin, uint8_t value);
bool digital_read(uint8_t pin);
uint16_t analog_read(uint8_t pin, uint8_t atten = 3);
//-------------------------------------------------- //--------------------------------------------------
//From https://github.com/adafruit/WiFiNINA/blob/master/src/utility/server_drv.cpp //From https://github.com/adafruit/WiFiNINA/blob/master/src/utility/server_drv.cpp
//-------------------------------------------------- //--------------------------------------------------
void start_server(uint16_t port, uint8_t sock, uint8_t protocol_mode=TCP_MODE); void start_server(uint16_t port, uint8_t sock, uint8_t protocol_mode = TCP_MODE);
void start_server(uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t protocol_mode=TCP_MODE); void start_server(uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t protocol_mode = TCP_MODE);
void start_client(uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t protocol_mode=TCP_MODE); void start_client(uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t protocol_mode = TCP_MODE);
void start_client(const std::string host, uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t protocol_mode=TCP_MODE); void start_client(const std::string host, uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t protocol_mode = TCP_MODE);
void stop_client(uint8_t sock); void stop_client(uint8_t sock);
uint8_t get_server_state(uint8_t sock); uint8_t get_server_state(uint8_t sock);
@ -213,11 +215,6 @@ namespace pimoroni {
// TODO Sort below ones out, particularly wpa2s from CPy // 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_entidentity(); //SET_ENT_IDENT
// void wifi_set_entusername(); //SET_ENT_UNAME // void wifi_set_entusername(); //SET_ENT_UNAME
// void wifi_set_entpassword(); //SET_ENT_PASSWD // void wifi_set_entpassword(); //SET_ENT_PASSWD

Wyświetl plik

@ -13,6 +13,8 @@ namespace pimoroni {
set_led(0, 0, 0); set_led(0, 0, 0);
pin_mode(ESP_SD_DETECT, Esp32Spi::INPUT_PULLUP);
return success; return success;
} }
@ -26,4 +28,8 @@ namespace pimoroni {
return !gpio_get(button); return !gpio_get(button);
} }
bool PicoWireless::is_sdcard_detected() {
return digital_read(ESP_SD_DETECT);
}
} }

Wyświetl plik

@ -18,6 +18,8 @@ namespace pimoroni {
static const uint8_t ESP_LED_G = 26; static const uint8_t ESP_LED_G = 26;
static const uint8_t ESP_LED_B = 27; static const uint8_t ESP_LED_B = 27;
static const uint8_t ESP_SD_DETECT = 15;
//-------------------------------------------------- //--------------------------------------------------
// Variables // Variables
@ -37,6 +39,7 @@ namespace pimoroni {
void set_led(uint8_t r, uint8_t g, uint8_t b); void set_led(uint8_t r, uint8_t g, uint8_t b);
bool is_pressed(uint8_t button); bool is_pressed(uint8_t button);
bool is_sdcard_detected();
}; };
} }

Wyświetl plik

@ -58,6 +58,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_pin_mode_obj, 2, picowireless_pin
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_digital_write_obj, 2, picowireless_digital_write); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_digital_write_obj, 2, picowireless_digital_write);
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_analog_write_obj, 2, picowireless_analog_write); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_analog_write_obj, 2, picowireless_analog_write);
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_digital_read_obj, 1, picowireless_digital_read);
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_analog_read_obj, 1, picowireless_analog_read);
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_server_start_obj, 3, picowireless_server_start); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_server_start_obj, 3, picowireless_server_start);
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_client_start_obj, 4, picowireless_client_start); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_client_start_obj, 4, picowireless_client_start);
@ -79,6 +81,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(picowireless_get_socket_obj, picowireless_get_s
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_set_led_obj, 3, picowireless_set_led); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_set_led_obj, 3, picowireless_set_led);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picowireless_is_pressed_obj, picowireless_is_pressed); STATIC MP_DEFINE_CONST_FUN_OBJ_0(picowireless_is_pressed_obj, picowireless_is_pressed);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(picowireless_is_sdcard_detected_obj, picowireless_is_sdcard_detected);
/***** Globals Table *****/ /***** Globals Table *****/
@ -131,6 +134,8 @@ STATIC const mp_map_elem_t picowireless_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_digital_write), MP_ROM_PTR(&picowireless_digital_write_obj) }, { MP_ROM_QSTR(MP_QSTR_digital_write), MP_ROM_PTR(&picowireless_digital_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_analog_write), MP_ROM_PTR(&picowireless_analog_write_obj) }, { MP_ROM_QSTR(MP_QSTR_analog_write), MP_ROM_PTR(&picowireless_analog_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_digital_read), MP_ROM_PTR(&picowireless_digital_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_analog_read), MP_ROM_PTR(&picowireless_analog_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_server_start), MP_ROM_PTR(&picowireless_server_start_obj) }, { MP_ROM_QSTR(MP_QSTR_server_start), MP_ROM_PTR(&picowireless_server_start_obj) },
{ MP_ROM_QSTR(MP_QSTR_client_start), MP_ROM_PTR(&picowireless_client_start_obj) }, { MP_ROM_QSTR(MP_QSTR_client_start), MP_ROM_PTR(&picowireless_client_start_obj) },
@ -151,7 +156,7 @@ STATIC const mp_map_elem_t picowireless_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_set_led), MP_ROM_PTR(&picowireless_set_led_obj) }, { MP_ROM_QSTR(MP_QSTR_set_led), MP_ROM_PTR(&picowireless_set_led_obj) },
{ MP_ROM_QSTR(MP_QSTR_is_pressed), MP_ROM_PTR(&picowireless_is_pressed_obj) }, { MP_ROM_QSTR(MP_QSTR_is_pressed), MP_ROM_PTR(&picowireless_is_pressed_obj) },
//TODO Add function for SD card detect { MP_ROM_QSTR(MP_QSTR_is_sdcard_detected), MP_ROM_PTR(&picowireless_is_sdcard_detected_obj) },
}; };
STATIC MP_DEFINE_CONST_DICT(mp_module_picowireless_globals, picowireless_globals_table); STATIC MP_DEFINE_CONST_DICT(mp_module_picowireless_globals, picowireless_globals_table);

Wyświetl plik

@ -713,6 +713,61 @@ mp_obj_t picowireless_analog_write(size_t n_args, const mp_obj_t *pos_args, mp_m
return mp_const_none; return mp_const_none;
} }
mp_obj_t picowireless_digital_read(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
if(wireless != nullptr) {
enum { ARG_pin, ARG_value };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_esp_pin, MP_ARG_REQUIRED | MP_ARG_INT },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
uint8_t pin = args[ARG_pin].u_int;
return mp_obj_new_bool(wireless->digital_read(pin));
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none;
}
mp_obj_t picowireless_analog_read(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
if(wireless != nullptr) {
if(n_args == 1) {
enum { ARG_pin };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_esp_pin, MP_ARG_REQUIRED | MP_ARG_INT },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
uint8_t pin = args[ARG_pin].u_int;
return mp_obj_new_int(wireless->analog_read(pin) * 16); //Returns a 16 bit-ish* number as per CircuitPython
}
else {
enum { ARG_pin, ARG_atten };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_esp_pin, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_atten, MP_ARG_REQUIRED | MP_ARG_INT },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
uint8_t pin = args[ARG_pin].u_int;
uint8_t atten = args[ARG_atten].u_int;
return mp_obj_new_int(wireless->analog_read(pin, atten) * 16); //Returns a 16 bit-ish* number as per CircuitPython
}
//NOTE *better way would be to mult by 65535 then div by 4095
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return mp_const_none;
}
mp_obj_t picowireless_server_start(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { mp_obj_t picowireless_server_start(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
if(wireless != nullptr) { if(wireless != nullptr) {
if(n_args == 3) { if(n_args == 3) {
@ -1069,14 +1124,26 @@ mp_obj_t picowireless_set_led(size_t n_args, const mp_obj_t *pos_args, mp_map_t
} }
mp_obj_t picowireless_is_pressed() { mp_obj_t picowireless_is_pressed() {
bool buttonPressed = false; bool pressed = false;
if(wireless != nullptr) { if(wireless != nullptr) {
buttonPressed = wireless->is_pressed(PicoWireless::A); pressed = wireless->is_pressed(PicoWireless::A);
} }
else else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG); mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return buttonPressed ? mp_const_true : mp_const_false; return pressed ? mp_const_true : mp_const_false;
}
mp_obj_t picowireless_is_sdcard_detected() {
bool detected = false;
if(wireless != nullptr) {
detected = wireless->is_sdcard_detected();
}
else
mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG);
return detected ? mp_const_true : mp_const_false;
} }
} }

Wyświetl plik

@ -56,6 +56,8 @@ extern mp_obj_t picowireless_pin_mode(size_t n_args, const mp_obj_t *pos_args, m
extern mp_obj_t picowireless_digital_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t picowireless_digital_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t picowireless_analog_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t picowireless_analog_write(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t picowireless_digital_read(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t picowireless_analog_read(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t picowireless_server_start(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t picowireless_server_start(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t picowireless_client_start(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t picowireless_client_start(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
@ -76,4 +78,5 @@ extern mp_obj_t picowireless_check_data_sent(size_t n_args, const mp_obj_t *pos_
extern mp_obj_t picowireless_get_socket(); extern mp_obj_t picowireless_get_socket();
extern mp_obj_t picowireless_set_led(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t picowireless_set_led(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t picowireless_is_pressed(); extern mp_obj_t picowireless_is_pressed();
extern mp_obj_t picowireless_is_sdcard_detected();