-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathespruino.fsx
192 lines (143 loc) · 5.74 KB
/
espruino.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#r "./node_modules/fable-core/Fable.Core.dll"
#r "./node_modules/fable-compiler/bin/Fable.Compiler.dll"
open Fable.Core
open Fable.AST
open Fable.Plugins
open System.Collections.Generic
type Pin = Pin of int
type OutPutStatus = High=1 | Low=0
[<Emit("getSerial()")>]
let getSerial () : string = jsNative
[<Emit("get_system_time()")>]
let getSystemTime () : int = jsNative
type IntervalId = IntervalId of int
[<Emit("setInterval($0, $1)")>]
let setInterval (func:(unit->unit), (time:int)) : IntervalId = jsNative
[<Emit("clearInterval($0)")>]
let clearInterval (id:IntervalId) : unit = jsNative
[<Emit("setTimeout($0, $1)")>]
let SetTimeout (func:(unit->unit), (time:int)) : int = jsNative
[<Emit("digitalWrite($0, $1)")>]
let digitalWrite (pin:Pin, state:OutPutStatus) : unit = jsNative
// let intConst (x:int) =
// Fable.NumberConst (float x, Int32) |> Fable.Value
// let emitExpr =
// Fable.Emit("digitalWrite($0, $1)")
// |> Fable.Value
// let (Pin p) = pin
// Fable.Apply(emitExpr, [intConst <| p; intConst <| int state], Fable.ApplyMeth, Fable.Type.Unit, None)
//[<Erase>]
[<Emit("digitalPulse($0, $1, $2)")>]
let digitalPulse (pin:Pin, state:OutPutStatus, time:int) : unit = jsNative
[<Emit("save()")>]
let save () : unit = jsNative
[<Emit("require($0)")>]
let require (s:string) : obj = jsNative
type IParseResult =
[<Emit("$0.query")>]
member x.Query : obj = jsNative
type url =
[<Emit("url.parse($0, true)")>]
static member Parse (url:string) : IParseResult = jsNative
type INodeMCU =
abstract A1 : Pin
abstract D1 : Pin
abstract D2 : Pin
abstract D3 : Pin
abstract D4 : Pin
abstract D5 : Pin
abstract D6 : Pin
abstract D7 : Pin
abstract D8 : Pin
[<Emit("NodeMCU")>]
let NodeMCU : INodeMCU = jsNative
type ISpiPort =
[<Emit("$0.send($1, $2)")>]
member x.Send (data:byte[], nssPin:Pin) = jsNative
type ISpi =
[<Emit("$0.find($1)")>]
member x.Find (pin:Pin) : ISpiPort = jsNative
let SPI : ISpi = jsNative
type II2CPort =
[<Emit("$0.readFrom($1, $2")>]
member x.ReadFrom (address:uint8, quantity:int) = jsNative
[<Emit("$0.writeTo($1, $2")>]
member x.WriteTo (address:uint8, data:byte[]) = jsNative
type II2C =
[<Emit("$0.find($1)")>]
member x.Find (pin:Pin) : II2CPort = jsNative
let I2C : II2C = jsNative
type IIpAddress =
[<Emit("$0.ip")>]
abstract member Ip : string
[<Emit("$0.netmask")>]
abstract member Netmask : string
[<Emit("$0.gw")>]
abstract member Gw : string
[<Emit("$0.mac")>]
abstract member Mac : string
type IWifi =
[<Emit("$0.scan($1)")>]
member x.Scan (f: (string -> unit)) :unit = jsNative
[<Emit("$0.connect($1, {password: $2}, $3)")>]
member x.Connect ((accessPointName:string), (password:string), (onSuccess: string -> unit)) : unit = jsNative
[<Emit("$0.getIP()")>]
member x.GetIP () : IIpAddress = jsNative
[<Emit("$0.disconnect()")>]
member x.Disconnect () : unit = jsNative
type Options = {host:string; method:string}
type IHttpCRq =
[<Emit("$0.on($1, $2)")>]
member x.On (evtName:string, f:string->unit) : unit = jsNative
[<Emit("$0.end()")>]
member x.End () : unit = jsNative
[<Emit("$0.write($1)")>]
member c.Write (content:string) : unit = jsNative
type IHttpSrv =
[<Emit("$0.close()")>]
member x.Close () : unit = jsNative
[<Emit("$0.listen($1)")>]
member x.Listen (port:int) : unit = jsNative
type IResponse =
[<Emit("$0.writeHead($1)")>]
member x.WriteHead (status:int) = jsNative
[<Emit("$0.write($1)")>]
member s.Write string : unit = jsNative
[<Emit("$0.end($1)")>]
member s.End string : unit = jsNative
type IRequest =
[<Emit("$0.url")>]
abstract member Url : string
type IHttp =
[<Emit("$0.request({host:$1, method:$2, path:$3}, $4)")>]
member x.Request ((host:string), method:string, path:string, (cb: IHttpCRq -> unit)) : IHttpCRq = jsNative
[<Emit("$0.get($1, $2)")>]
member x.Get ((host:string), (cb: IHttpCRq -> unit)) : IHttpCRq = jsNative
[<Emit("$0.createServer($1)")>]
member x.CreateServer (callback:(System.Func<IRequest,IResponse,unit>)) : IHttpSrv = jsNative
type IEsp8266 =
[<Emit("$0.ping($1, $2)")>]
member x.Ping (host:string, callBack:string -> unit) : unit = jsNative
type IMqttPublishMessage =
[<Emit("$0.topic")>]
abstract member Topic : string
[<Emit("$0.message")>]
abstract member Message : string
[<StringEnum>]
type IEvent = Connected | Publish | Error | Subscribed
type IMqttConnection =
[<Emit("$0.on($1, $2)")>]
member x.On (evtName:IEvent, f:obj->unit) : unit = jsNative
member x.OnPublish (f:IMqttPublishMessage->unit) : unit = x.On (Publish, (fun o -> o :?> IMqttPublishMessage |> f))
[<Emit("$0.disconnect()")>]
member x.Disconnect () : unit = jsNative
[<Emit("$0.subscribe($1)")>]
member x.Subscribe (s:string) : unit = jsNative
[<Emit("$0.publish($1, $2)")>]
member x.Publish (topic:string, message:string) : unit = jsNative
[<Emit("$0.connect()")>]
member x.Connect () : unit = jsNative
type IMqtt =
[<Emit("$0.create($1)")>]
member x.Create (ip:string) : IMqttConnection = jsNative
let Mqtt : IMqtt = require "mqtt" :?> IMqtt