diff options
author | Mitch Bradley <wmb@firmworks.com> | 2016-10-13 11:41:16 -1000 |
---|---|---|
committer | Mitch Bradley <wmb@firmworks.com> | 2016-10-13 11:41:16 -1000 |
commit | ff81d5719f88190a4036fc737a607bda0c669bb4 (patch) | |
tree | 445e9f9cea3edfd194aa42624106e41aae7e3b29 | |
parent | 58909e63a5ef8f9ad2a5f13a192fd228d7e1f12c (diff) | |
download | cforth-ff81d5719f88190a4036fc737a607bda0c669bb4.tar.gz |
esp8266: Added some wifi and tcp connection words
-rw-r--r-- | src/app/esp8266/app.fth | 1 | ||||
-rw-r--r-- | src/app/esp8266/sdspi.fth | 2 | ||||
-rw-r--r-- | src/app/esp8266/tcpnew.fth | 19 | ||||
-rw-r--r-- | src/app/esp8266/wifi.fth | 60 | ||||
-rw-r--r-- | src/lib/struct.fth | 5 | ||||
-rw-r--r-- | src/ofw/ofw-support.fth | 3 |
6 files changed, 87 insertions, 3 deletions
diff --git a/src/app/esp8266/app.fth b/src/app/esp8266/app.fth index de1650d..dde660a 100644 --- a/src/app/esp8266/app.fth +++ b/src/app/esp8266/app.fth @@ -91,6 +91,7 @@ fl bme280.fth fl pca9685.fth fl hcsr04.fth +fl wifi.fth fl redirect.fth fl tcpnew.fth diff --git a/src/app/esp8266/sdspi.fth b/src/app/esp8266/sdspi.fth index 5ff4538..fc5f9bd 100644 --- a/src/app/esp8266/sdspi.fth +++ b/src/app/esp8266/sdspi.fth @@ -1,4 +1,4 @@ -\ +\ Driver for SD card connected via SPI defer spi-bits-in ( #bits -- n ) defer spi-out-in ( outbuf inbuf #bytes -- ) diff --git a/src/app/esp8266/tcpnew.fth b/src/app/esp8266/tcpnew.fth index d2312c3..801f907 100644 --- a/src/app/esp8266/tcpnew.fth +++ b/src/app/esp8266/tcpnew.fth @@ -201,3 +201,22 @@ defer respond ( pcb -- close? ) fl sendfile.fth + +: simple-connected ( err pcb arg -- stat ) + drop nip + ." Connected, pcb is " . cr + ERR_OK +; + +defer connected +' simple-connected to connected + +: connect ( port# host -- ) + \ XXX handle error callbacks + ['] connected -rot ( cb port# host ) + tcp-new ( cb port# host pcb ) + tcp-connect 0<> abort" tcp-connect failed" +; + +\ This is the default host IP for ESP8266's in softap mode +create esp-ip #192 c, #168 c, #4 c, #1 c, diff --git a/src/app/esp8266/wifi.fth b/src/app/esp8266/wifi.fth new file mode 100644 index 0000000..452a5ae --- /dev/null +++ b/src/app/esp8266/wifi.fth @@ -0,0 +1,60 @@ +\needs struct fl ../../lib/struct.fth +struct + #32 field >ssid + #64 field >password + /c field >ssid-len + /c field >channel + 2 + \ Padding to align + /l field >authmode + /c field >ssid-hidden + /c field >max-connection + /w field >beacon-interval +constant /ap-config + +: ?wifi-error ( status -- ) 1 <> abort" Wifi failed" ; + +: set-ap ( ssid$ password$ channel# authmode -- ) + pad /ap-config erase ( ssid$ password$ channel# authmode ) + pad >authmode l! ( ssid$ password$ channel# ) + pad >channel c! ( ssid$ password$ ) + pad >password swap move ( ssid$ ) + dup pad >ssid-len c! ( ssid$ ) + pad >ssid swap move ( ) + 0 pad >ssid-hidden c! ( ) + 4 pad >max-connection c! ( ) + #100 pad >beacon-interval w! ( ) + pad wifi-ap-config! ?wifi-error +; +: set-ap-psk ( ssid$ password$ channel# -- ) 4 set-ap ; +: set-ap-open ( ssid$ channel# -- ) " " rot 0 set-ap ; + +: ap-mode ( -- ) 2 wifi-opmode! ; +: station-mode ( -- ) 1 wifi-opmode! ; +: set-station ( ssid$ password$ -- ) + pad #104 erase ( ssid$ password$ ) + pad >password swap move ( ssid$ ) + pad >ssid swap move ( ) + pad wifi-sta-config! ?wifi-error +; +: station-connect ( ssid$ password$ -- ) + station-mode + set-station wifi-sta-connect ?wifi-error + wifi-sta-dhcpc-start ?wifi-error + begin ( ) + wifi-sta-connect@ ( status ) + dup 1 = ( status connecting? ) + while ( status ) + drop #10 ms ( ) + repeat ( status ) + wifi-sta-dhcpc-stop ?wifi-error ( status ) + case + 0 of ." Idle!" endof + \ 1 is handled in the loop above + 2 of ." Wrong password" cr endof + 3 of ." No AP found" cr endof + 4 of ." Connect failed" cr endof + 5 of exit endof \ Got IP; the okay case + ( default ) ." Bad status: " dup .d cr swap + endcase + abort +; diff --git a/src/lib/struct.fth b/src/lib/struct.fth new file mode 100644 index 0000000..80a47e4 --- /dev/null +++ b/src/lib/struct.fth @@ -0,0 +1,5 @@ +0 constant struct +: field ( offset size -- offset' ) + create over , + ( offset' ) + does> ( adr -- adr' ) @ + +; diff --git a/src/ofw/ofw-support.fth b/src/ofw/ofw-support.fth index fdcf6fd..5ca1863 100644 --- a/src/ofw/ofw-support.fth +++ b/src/ofw/ofw-support.fth @@ -198,8 +198,7 @@ alias link> ta1+ alias transient noop alias resident noop 0 value my-self -0 constant struct -: field ( offset size ) create over , + does> ( adr -- adr' ) @ + ; +\needs struct fl ../lib/struct.fth \needs $= : $= ( $1 $2 -- ) compare 0= ; alias headerless? false alias ascii [char] |