changeset 1490:455b1b80516e

introduce capnp, more build, some demos Ignore-this: 29801e9e9f8a85fd0d822207c6eacce1 darcs-hash:b2da18876d27b0fb9192811375752b5c4e7322e0
author drewp <drewp@bigasterisk.com>
date Thu, 23 Jan 2020 21:00:47 -0800
parents a4fb4cc60ae1
children 6cd9341f0a28
files service/iot2/iot2_esp32.nim service/iot2/iot2_esp32.nim.cfg service/iot2/iot2_linux.nim service/iot2/iot2_linux.nim.cfg service/iot2/messages.capnp service/iot2/nim.cfg service/iot2/panicoverride.nim service/iot2/py_read_messages_demo.py service/iot2/requirements.txt service/iot2/tasks.py
diffstat 8 files changed, 121 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/iot2/iot2_esp32.nim	Thu Jan 23 21:00:47 2020 -0800
@@ -0,0 +1,3 @@
+echo "hello on esp"
+proc add(x: int, y: int): int =
+  x + y
--- a/service/iot2/iot2_linux.nim	Tue Jan 21 23:59:45 2020 -0800
+++ b/service/iot2/iot2_linux.nim	Thu Jan 23 21:00:47 2020 -0800
@@ -4,13 +4,23 @@
   let sys = pyImport("sys")
   discard sys.path.extend([
     ".", # for devices.py itself
-    "env/lib/python3.7/site-packages" # for deeper imports
+    "build/py/lib/python3.7/site-packages" # for deeper imports
   ])
 
   pyImport("devices")
 
+import json
+import osproc
+proc getSomeTemp(): float =
+  let sensorsJson = execProcess("sensors", args=["-j"], options={poUsePath})
+  let res = parseJson(sensorsJson)
+  res["coretemp-isa-0000"]["Core 0"]["temp2_input"].getFloat
+
+
+
+
 
 echo "iot2_linux starting"
-
+echo getSomeTemp()
 let devices = setup_python()
 discard devices.hello()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/iot2/messages.capnp	Thu Jan 23 21:00:47 2020 -0800
@@ -0,0 +1,8 @@
+@0x884608a79526d38a;
+
+# Can't use Text type; nim doesn't support it. The Data type becomes nim 'string'.
+
+struct Report {
+  sensor @0 :Data;
+  value @1 :Float32;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/iot2/nim.cfg	Thu Jan 23 21:00:47 2020 -0800
@@ -0,0 +1,3 @@
+nimcache = "build/nimcache"
+hint[Processing] = off
+-d:nimOldCaseObjects
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/iot2/panicoverride.nim	Thu Jan 23 21:00:47 2020 -0800
@@ -0,0 +1,13 @@
+proc printf(frmt: cstring) {.varargs, importc, header: "<stdio.h>", cdecl.}
+proc exit(code: int) {.importc, header: "<stdlib.h>", cdecl.}
+
+{.push stack_trace: off, profiler:off.}
+
+proc rawoutput(s: string) =
+  printf("%s\n", s)
+
+proc panic(s: string) =
+  rawoutput(s)
+  exit(1)
+
+{.pop.}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/iot2/py_read_messages_demo.py	Thu Jan 23 21:00:47 2020 -0800
@@ -0,0 +1,10 @@
+import sys
+import capnp
+# (No compile step)
+messages_capnp = capnp.load('messages.capnp')
+print(dir(messages_capnp))
+
+report = messages_capnp.Report.new_message()
+report.sensor = b'hello'
+report.value = 1.234
+print(repr(report.to_bytes()))
--- a/service/iot2/requirements.txt	Tue Jan 21 23:59:45 2020 -0800
+++ b/service/iot2/requirements.txt	Thu Jan 23 21:00:47 2020 -0800
@@ -5,3 +5,5 @@
 w1thermsensor
 
 standardservice==0.6.0
+pycapnp==0.6.4
+
--- a/service/iot2/tasks.py	Tue Jan 21 23:59:45 2020 -0800
+++ b/service/iot2/tasks.py	Thu Jan 23 21:00:47 2020 -0800
@@ -1,51 +1,104 @@
 from invoke import task
 from pathlib import Path
 
+PY_ENV = f'build/py'
+NIM_ENV = f'build/nim'
+NIM_BIN = f'{NIM_ENV}/bin'
+PY_SITE_PACKAGES = f'{PY_ENV}/lib/python3.7/site-packages'
+SOME_PY_DEP = Path(f'{PY_SITE_PACKAGES}/standardservice')
+
 @task
 def nim_install(ctx):
-    if Path('nim-1.0.4/bin/nim').exists():
+    if Path(f'{NIM_BIN}/nim').exists():
         return
-    ctx.run(f'curl https://nim-lang.org/download/nim-1.0.4-linux_x64.tar.xz | xz -dc | tar x')
+    ctx.run(f'curl https://nim-lang.org/download/nim-1.0.4-linux_x64.tar.xz | '
+            f'xz -dc | '
+            f'tar --extract --strip-components=1 --one-top-level={NIM_ENV}')
 
 @task
 def py_install(ctx):
-    if Path('env/bin/python').exists():
+    if Path(f'{PY_ENV}/bin/python').exists():
         return
-    ctx.run(f'mkdir -p env')
-    ctx.run(f'virtualenv -p /usr/bin/python3.7 env')
+    ctx.run(f'mkdir -p {PY_ENV}')
+    ctx.run(f'virtualenv -p /usr/bin/python3.7 {PY_ENV}')
+    # now .../wheel is newer than requirements.txt
 
 @task(pre=[py_install])
 def py_deps(ctx):
-    pip_install_ran = Path('env/lib/python3.7/site-packages/wheel').stat().st_mtime
+    pip_install_ever_ran = SOME_PY_DEP.exists()
+    pip_install_last_ran = Path(f'{PY_SITE_PACKAGES}/wheel').stat().st_mtime
     requirements = Path('requirements.txt').stat().st_mtime
-    if pip_install_ran > requirements:
+    if pip_install_ever_ran and pip_install_last_ran > requirements:
         return
-    ctx.run(f'env/bin/pip install --quiet --index-url https://projects.bigasterisk.com/ --extra-index-url https://pypi.org/simple -r requirements.txt')
+    ctx.run(f'{PY_ENV}/bin/pip install '
+            #f'--quiet '
+            f'--index-url https://projects.bigasterisk.com/ '
+            f'--extra-index-url https://pypi.org/simple '
+            f'-r requirements.txt')
 
 @task(pre=[nim_install])
 def nim_deps(ctx):
-    pkgs = ['nimpy-0.1.0']
-    if all(Path(f'~/.nimble/pkgs/{pkg}').expanduser().exists() for pkg in pkgs):
+    pkgs = [('nimpy', 'nimpy-0.1.0'),
+            ('https://github.com/avsej/capnp.nim.git', 'capnp-0.0.3'),
+            ]
+    if all(Path(f'~/.nimble/pkgs/{pkg[1]}').expanduser().exists() for pkg in pkgs):
         return
-    plain_names = ' '.join(p.split('-')[0] for p in pkgs)
-    ctx.run(f'nim-1.0.4/bin/nimble install {plain_names}', pty=True)
+    plain_names = ' '.join(p[0] for p in pkgs)
+    ctx.run(f'{NIM_BIN}/nimble install -y {plain_names}', pty=True)
+    ctx.run(f'ln -s ~/.nimble/bin/capnpc ~/.nimble/bin/capnpc-nim')
 
 
-@task(pre=[nim_install])
+@task(pre=[nim_deps])
 def nim_build_x86(ctx):
-    ctx.run(f'nim-1.0.4/bin/nim c --out:iot2_linux_x86 iot2_linux.nim', pty=True)
+    ctx.run(f'{NIM_BIN}/nim compile '
+            f'--out:build/iot2_linux_x86 '
+            f'iot2_linux.nim',
+            pty=True)
 
 @task
 def arm_cross_compiler_install(ctx):
+    if Path('/usr/share/crossbuild-essential-armhf/list').exists():
+        return
     ctx.run(f'sudo apt install -y crossbuild-essential-armhf', pty=True)
 
-@task(pre=[nim_install])
+@task(pre=[arm_cross_compiler_install, nim_install])
 def nim_build_arm(ctx):
-    ctx.run(f'nim-1.0.4/bin/nim c --cpu:arm --out:iot2_linux_arm iot2_linux.nim', pty=True)
+    ctx.run(f'{NIM_BIN}/nim compile '
+            f'--cpu:arm '
+            f'--out:build/iot2_linux_arm '
+            f'iot2_linux.nim',
+            pty=True)
 
 @task(pre=[py_deps, nim_build_x86])
 def local_run(ctx):
-    ctx.run(f'./iot2_linux_x86')
+    ctx.run(f'build/iot2_linux_x86')
+
+@task
+def nim_build_esp32(ctx):
+    ctx.run(f'{NIM_BIN}/nim compile '
+            f'--cpu:arm '
+            f'--os:standalone '
+            #f'--deadCodeElim:on '
+            # --gc:refc|v2|markAndSweep|boehm|go|none|regions
+            f'--gc:stack '
+            f'--compileOnly:on '
+            f'--noMain '
+            f'--nimcache:build/nimcache '
+            f'--embedsrc:on '
+            f'--verbosity:3 '
+            #f'-d:release '
+            f'iot2_esp32.nim')
+    ctx.run(f'')
+
+@task
+def install_nim_capnp(ctx):
+    ctx.run(f'git clone git@github.com:drewp/capnp.nim.git build/capnp.nim')
+    ctx.run(f'cd build/capnp.nim; ./build.sh')
+    ctx.run(f'cd build/capnp.nim; bin/nim c capnp/capnpc.nim')
+
+@task
+def messages_build_nim(ctx):
+    ctx.run(f'capnp compile -o ./build/capnp.nim/capnp/capnpc messages.capnp > build/messages.nim')
 
 
 # pack this into docker for pushing to Pi