Skip to content

Commit 803a871

Browse files
committed
Fixed microbit demo
1 parent 31b3062 commit 803a871

File tree

7 files changed

+511
-31
lines changed

7 files changed

+511
-31
lines changed

examples/microbit/Build.fs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
open System.IO
12
open Fake.Core
23
open Fake.IO
34

@@ -6,27 +7,39 @@ open Helpers
67
initializeContext()
78

89
let srcPath = Path.getFullName "src"
10+
let appName = "app.py"
911

1012
Target.create "Clean" (fun _ ->
1113
run dotnet "fable-py clean --yes" srcPath // Delete *.py files created by Fable
1214
)
1315

1416
Target.create "Build" (fun _ ->
1517
run dotnet $"fable-py -c Release" srcPath
18+
19+
// Rewrite imports to flat file system.
20+
let python = File.ReadAllText($"{srcPath}/{appName}")
21+
let python = python.Replace("fable_modules.fable_library.", "")
22+
File.WriteAllText($"{srcPath}/{appName}", python)
1623
)
1724

1825
Target.create "Flash" (fun _ ->
19-
run flash "" $"{srcPath}/app.py"
26+
run flash appName srcPath
27+
)
28+
29+
Target.create "FableLibrary" (fun _ ->
30+
run ufs $"put util.py" srcPath
2031
)
2132

2233
open Fake.Core.TargetOperators
2334

2435
let dependencies = [
2536
"Clean"
2637
==> "Build"
27-
2838
"Clean"
39+
==> "Build"
2940
==> "Flash"
41+
"Clean"
42+
==> "FableLibrary"
3043
]
3144

3245
[<EntryPoint>]

examples/microbit/Helpers.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ let createProcess exe arg dir =
7373

7474
let dotnet = createProcess "dotnet"
7575
let flash = createProcess "uflash"
76+
let ufs = createProcess "ufs"
7677

7778
let npm =
7879
let npmPath =

examples/microbit/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
Write your F# program in `src/App.fs`.
44

5+
Note that the Fable Library is not supported so it's very limited what
6+
you can do. The needed parts of fable library need to be ported to work
7+
with MicroPython see `util.fs` as an example.
8+
9+
The micro:bit have a flat file-system so all files needs to be in the
10+
same top-level directory.
11+
512
## Install
613

14+
These tools are needed by the Build script:
15+
716
```sh
817
pip install uflash
18+
pip install microfs
919
```
1020

1121
## Build

examples/microbit/src/App.fs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,31 @@ module App
22

33
open Fable.Python.MicroBit
44

5-
display.scroll ("Fable Python!") |> ignore
5+
//display.scroll ("Fable Python!") |> ignore
6+
7+
let mutable time = 0
8+
let mutable start = 0
9+
let mutable running = false
10+
11+
while true do
12+
if running then
13+
display.show (Image.HEART)
14+
sleep (300)
15+
display.show (Image.HEART_SMALL)
16+
sleep (300)
17+
else
18+
display.show (Image.ASLEEP)
19+
20+
if button_a.was_pressed () then
21+
running <- true
22+
start <- running_time ()
23+
24+
if button_b.was_pressed () then
25+
if running then
26+
time <- time + running_time () - start
27+
28+
running <- false
29+
30+
if pin_logo.is_touched () then
31+
if not running then
32+
display.scroll (int (time / 1000))

0 commit comments

Comments
 (0)