Skip to content

Commit 4b35a54

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
2 parents 552f7c4 + 5fb775a commit 4b35a54

4 files changed

Lines changed: 97 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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 "py/pfenv.h"
36+
#include MICROPY_HAL_H
37+
#include "queue.h"
38+
#include "user_interface.h"
39+
40+
// Singleton instance of scan callback, meaning that there can be only
41+
// one concurrent AP scan.
42+
STATIC mp_obj_t scan_cb_obj;
43+
44+
mp_obj_t call_function_1_protected(mp_obj_t fun, mp_obj_t arg) {
45+
nlr_buf_t nlr;
46+
if (nlr_push(&nlr) == 0) {
47+
return mp_call_function_1(fun, arg);
48+
} else {
49+
mp_obj_print_exception(printf_wrapper, NULL, (mp_obj_t)nlr.ret_val);
50+
return (mp_obj_t)nlr.ret_val;
51+
}
52+
}
53+
54+
STATIC void esp_scan_cb(scaninfo *si, STATUS status) {
55+
//printf("in pyb_scan_cb: %d, si=%p, si->pbss=%p\n", status, si, si->pbss);
56+
struct bss_info *bs;
57+
if (si->pbss) {
58+
STAILQ_FOREACH(bs, si->pbss, next) {
59+
mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
60+
t->items[0] = mp_obj_new_bytes(bs->ssid, strlen((char*)bs->ssid));
61+
t->items[1] = mp_obj_new_bytes(bs->bssid, sizeof(bs->bssid));
62+
t->items[2] = MP_OBJ_NEW_SMALL_INT(bs->channel);
63+
t->items[3] = MP_OBJ_NEW_SMALL_INT(bs->rssi);
64+
t->items[4] = MP_OBJ_NEW_SMALL_INT(bs->authmode);
65+
t->items[5] = MP_OBJ_NEW_SMALL_INT(bs->is_hidden);
66+
call_function_1_protected(scan_cb_obj, t);
67+
}
68+
}
69+
}
70+
71+
STATIC mp_obj_t esp_scan(mp_obj_t cb_in) {
72+
scan_cb_obj = cb_in;
73+
wifi_set_opmode(STATION_MODE);
74+
wifi_station_scan(NULL, (scan_done_cb_t)esp_scan_cb);
75+
return mp_const_none;
76+
}
77+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_scan_obj, esp_scan);
78+
79+
STATIC const mp_map_elem_t esp_module_globals_table[] = {
80+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_esp) },
81+
82+
{ MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&esp_scan_obj },
83+
};
84+
85+
STATIC MP_DEFINE_CONST_DICT(esp_module_globals, esp_module_globals_table);
86+
87+
const mp_obj_module_t esp_module = {
88+
.base = { &mp_type_module },
89+
.name = MP_QSTR_esp,
90+
.globals = (mp_obj_dict_t*)&esp_module_globals,
91+
};

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)