Skip to content

Commit 6ec650b

Browse files
committed
esp8266: Add "esp" module with esp8266-specific "cooperative" networking.
So far implements .scan(lambda x: print(x)) function to scan for WiFi access points.
1 parent ad33e24 commit 6ec650b

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

esp8266/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ SRC_C = \
4848
pybstdio.c \
4949
uart.c \
5050
modpyb.c \
51+
modesp.c \
5152

5253
STM_SRC_C = $(addprefix stmhal/,\
5354
printf.c \

esp8266/modesp.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Paul Sokolovsky
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <string.h>
29+
#include <stdbool.h>
30+
31+
#include "py/nlr.h"
32+
#include "py/obj.h"
33+
#include "py/gc.h"
34+
#include "py/runtime.h"
35+
#include MICROPY_HAL_H
36+
#include "queue.h"
37+
#include "user_interface.h"
38+
39+
STATIC mp_obj_t scan_cb_obj;
40+
41+
STATIC void esp_scan_cb(scaninfo *si, STATUS status) {
42+
//printf("in pyb_scan_cb: %d, si=%p, si->pbss=%p\n", status, si, si->pbss);
43+
struct bss_info *bs;
44+
if (si->pbss) {
45+
STAILQ_FOREACH(bs, si->pbss, next) {
46+
mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
47+
t->items[0] = mp_obj_new_bytes(bs->ssid, strlen((char*)bs->ssid));
48+
t->items[1] = mp_obj_new_bytes(bs->bssid, sizeof(bs->bssid));
49+
t->items[2] = MP_OBJ_NEW_SMALL_INT(bs->channel);
50+
t->items[3] = MP_OBJ_NEW_SMALL_INT(bs->rssi);
51+
t->items[4] = MP_OBJ_NEW_SMALL_INT(bs->authmode);
52+
t->items[5] = MP_OBJ_NEW_SMALL_INT(bs->is_hidden);
53+
mp_call_function_1(scan_cb_obj, t);
54+
}
55+
}
56+
}
57+
58+
STATIC mp_obj_t esp_scan(mp_obj_t cb_in) {
59+
scan_cb_obj = cb_in;
60+
wifi_set_opmode(STATION_MODE);
61+
wifi_station_scan(NULL, (scan_done_cb_t)esp_scan_cb);
62+
return mp_const_none;
63+
}
64+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_scan_obj, esp_scan);
65+
66+
STATIC const mp_map_elem_t esp_module_globals_table[] = {
67+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_esp) },
68+
69+
{ MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&esp_scan_obj },
70+
};
71+
72+
STATIC MP_DEFINE_CONST_DICT(esp_module_globals, esp_module_globals_table);
73+
74+
const mp_obj_module_t esp_module = {
75+
.base = { &mp_type_module },
76+
.name = MP_QSTR_esp,
77+
.globals = (mp_obj_dict_t*)&esp_module_globals,
78+
};

esp8266/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
5959

6060
// extra built in modules to add to the list of known ones
6161
extern const struct _mp_obj_module_t pyb_module;
62+
extern const struct _mp_obj_module_t esp_module;
6263

6364
#define MICROPY_PORT_BUILTIN_MODULES \
6465
{ MP_OBJ_NEW_QSTR(MP_QSTR_pyb), (mp_obj_t)&pyb_module }, \
66+
{ MP_OBJ_NEW_QSTR(MP_QSTR_esp), (mp_obj_t)&esp_module }, \
6567

6668
#define MP_STATE_PORT MP_STATE_VM
6769

esp8266/qstrdefsport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ Q(delay)
4040
Q(udelay)
4141
Q(sync)
4242
Q(hard_reset)
43+
44+
Q(esp)
45+
Q(scan)

0 commit comments

Comments
 (0)