瀏覽代碼

Nicer directory layout for digital watch example.

Joeri Exelmans 4 年之前
父節點
當前提交
99569186bb
共有 26 個文件被更改,包括 610 次插入20 次删除
  1. 27 2
      examples/digitalwatch/README.txt
  2. 0 0
      examples/digitalwatch/common/digitalwatch.svg
  3. 0 0
      examples/digitalwatch/common/digitalwatch.xml
  4. 0 0
      examples/digitalwatch/common/digitalwatch_priorities.svg
  5. 0 0
      examples/digitalwatch/common/font.ttf
  6. 0 0
      examples/digitalwatch/common/noteSmall.gif
  7. 0 0
      examples/digitalwatch/common/watch.gif
  8. 0 0
      examples/digitalwatch/common/watch.jpg
  9. 2 2
      examples/digitalwatch/DigitalWatchGUI.py
  10. 1 1
      examples/digitalwatch/run.py
  11. 0 2
      examples/digitalwatch/wasm/.gitignore
  12. 0 0
      examples/digitalwatch/webassembly/codegen/.gitignore
  13. 0 0
      examples/digitalwatch/webassembly/codegen/Cargo.toml
  14. 0 0
      examples/digitalwatch/webassembly/codegen/digitalwatch.rs
  15. 10 13
      examples/digitalwatch/index.html
  16. 1 0
      examples/digitalwatch/webassembly/wasm/.gitignore
  17. 155 0
      examples/digitalwatch/webassembly/wasm/Cargo.lock
  18. 0 0
      examples/digitalwatch/webassembly/wasm/Cargo.toml
  19. 0 0
      examples/digitalwatch/webassembly/wasm/lib.rs
  20. 0 0
      examples/digitalwatch/webassembly/wasm/outputhandler.js
  21. 113 0
      examples/digitalwatch/webassembly/wasm/pkg/dwatch.d.ts
  22. 221 0
      examples/digitalwatch/webassembly/wasm/pkg/dwatch.js
  23. 54 0
      examples/digitalwatch/webassembly/wasm/pkg/dwatch_bg.js
  24. 二進制
      examples/digitalwatch/webassembly/wasm/pkg/dwatch_bg.wasm
  25. 14 0
      examples/digitalwatch/webassembly/wasm/pkg/dwatch_bg.wasm.d.ts
  26. 12 0
      examples/digitalwatch/webassembly/wasm/pkg/package.json

+ 27 - 2
examples/digitalwatch/README.txt

@@ -1,3 +1,28 @@
-The python script run.py runs the example.
+Directory layout
+----------------
 
-There's also run.html, which will be the browser version of the example. This will involve generating Rust code from the statechart model, and compiling Rust to WebAssembly.
+common/
+    digitalwatch.xml
+        The statechart model
+
+python/
+    run.py
+        Python script that runs the demo with the statechart interpreter. Run it from the 'python' directory.
+
+webassembly/
+    index.html
+        Web page that runs the demo with WebAssembly code, generated from Rust code, in turn generated from the statechart model.
+
+        In order to run this demo, start a static HTTP server in the 'digitalwatch' directory:
+          python -m http.server
+
+        Then navigate your browser to http://localhost:8000/webassembly/
+
+    codegen/
+        Generated code: This is a Rust crate produced by SCCD compiler (module "sccd.test.cmd.to_rust") from 'common/digitalwatch.xml'. Building it with 'cargo' produces a library.
+
+    wasm/
+        Rust crate containing Rust glue code to interact with the generated code in 'codegen' from JavaScript. It should be built with the 'wasm-pack' command, producing JavaScript glue code, and a .wasm-file.
+
+        pkg/
+            Generated code: This directory contains the JavaScript glue code and .wasm-file built from this crate.

examples/digitalwatch/digitalwatch.svg → examples/digitalwatch/common/digitalwatch.svg


examples/digitalwatch/digitalwatch.xml → examples/digitalwatch/common/digitalwatch.xml


examples/digitalwatch/digitalwatch_priorities.svg → examples/digitalwatch/common/digitalwatch_priorities.svg


examples/digitalwatch/font.ttf → examples/digitalwatch/common/font.ttf


examples/digitalwatch/noteSmall.gif → examples/digitalwatch/common/noteSmall.gif


examples/digitalwatch/watch.gif → examples/digitalwatch/common/watch.gif


examples/digitalwatch/watch.jpg → examples/digitalwatch/common/watch.jpg


+ 2 - 2
examples/digitalwatch/DigitalWatchGUI.py

@@ -184,8 +184,8 @@ class DigitalWatchGUI_Static(Frame):
         self.curChrono = [0, 0, 0]
         self.chronoTag = None
         
-        self.noteImage = PhotoImage(file="./noteSmall.gif")        
-        self.watchImage = PhotoImage(file="./watch.gif")
+        self.noteImage = PhotoImage(file="../common/noteSmall.gif")        
+        self.watchImage = PhotoImage(file="../common/watch.gif")
         
         self.alarmNoteTag = None
         

+ 1 - 1
examples/digitalwatch/run.py

@@ -26,7 +26,7 @@ def main():
             method = getattr(gui.controller, event.name)
             method()
 
-    cd = load_cd("digitalwatch.xml")
+    cd = load_cd("../common/digitalwatch.xml")
 
     # from sccd.statechart.static import tree
     # tree.concurrency_arena_orthogonal( cd.statechart.tree )

+ 0 - 2
examples/digitalwatch/wasm/.gitignore

@@ -1,2 +0,0 @@
-/pkg/
-/target/

examples/digitalwatch/codegen/.gitignore → examples/digitalwatch/webassembly/codegen/.gitignore


examples/digitalwatch/codegen/Cargo.toml → examples/digitalwatch/webassembly/codegen/Cargo.toml


examples/digitalwatch/codegen/digitalwatch.rs → examples/digitalwatch/webassembly/codegen/digitalwatch.rs


+ 10 - 13
examples/digitalwatch/index.html

@@ -4,8 +4,9 @@
   <title>DWatch</title>
   <style type="text/css">
     body {
-      background-color: #000;
+      background-color: #222;
       color: #fff;
+      font-weight: bold;
     }
     #canvas{
       /* center on page */
@@ -15,13 +16,11 @@
     }
     @font-face{
       font-family: 'digital-font';
-      src: url('font.ttf');
+      src: url('../common/font.ttf');
     }
-    svg text{
-      font: 28px digital-font;
-    }
-    /* disable text selection */
     svg text {
+      font: 28px digital-font;
+      /* disable text selection */
       cursor: default;
         -webkit-user-select: none;
            -moz-user-select: none;
@@ -37,7 +36,7 @@
      xmlns="http://www.w3.org/2000/svg"
      id="canvas">
 
-    <image width="222" height="236" xlink:href="watch.gif"/>
+    <image width="222" height="236" xlink:href="../common/watch.gif"/>
 
       <rect id="display" x="51" y="95" width="120" height="55" rx="2" fill="#DCDCDC">
       </rect>
@@ -50,10 +49,9 @@
     <rect id="bottomLeft" x="0" y="158", width="16", height="16" fill="#fff" fill-opacity="0.2" />
     <rect id="bottomRight" x="208" y="158", width="16", height="16" fill="#fff" fill-opacity="0.2" />
 
-    <image id="note" x="54" y="96" xlink:href="noteSmall.gif" style="display:none"/>
+    <image id="note" x="54" y="96" xlink:href="../common/noteSmall.gif" style="display:none"/>
   </svg>
-  <p id="log">
-  </p>
+  <p>Note: On page reload, time is set to wall-clock time, and alarm is set to wall-clock time + 10s.</p>
 
   <script type="module">
     Number.prototype.pad = function(size) {
@@ -145,7 +143,6 @@
         import("./wasm/outputhandler.js"),
       ]);
       const memory = await rust.default();
-      let purposefully_behind = 0;
 
       const elementNote = document.getElementById("note");
       const elementDisplay = document.getElementById("display");
@@ -156,9 +153,8 @@
       // Chrono is zero
       const chrono = new DisplayText([0, 0, 0], [100, 60, 100], document.getElementById("chronoText"));
 
-      const alarm = new DisplayText([startedAt.getSeconds(), startedAt.getMinutes(), startedAt.getHours()], [60, 60, 24], document.getElementById("alarmText"));
-
       // Alarm is always equal to time + 10 seconds (makes it easier to test the alarm)
+      const alarm = new DisplayText([... time.values], [... time.max], document.getElementById("alarmText"));
       for (let i=0; i<10; i++) {
         alarm.increment();
       }
@@ -253,6 +249,7 @@
       const handle = rust.setup(outHandler);
       let timeout;
       let simtime;
+      let purposefully_behind = 0;
       const wakeup = () => {
         clearTimeout(timeout);
         const status = rust.run_until(handle, realtime() + purposefully_behind, outHandler);

+ 1 - 0
examples/digitalwatch/webassembly/wasm/.gitignore

@@ -0,0 +1 @@
+/target/

+ 155 - 0
examples/digitalwatch/webassembly/wasm/Cargo.lock

@@ -0,0 +1,155 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "bumpalo"
+version = "3.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820"
+
+[[package]]
+name = "cfg-if"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "console_error_panic_hook"
+version = "0.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8d976903543e0c48546a91908f21588a680a8c8f984df9a5d69feccb2b2a211"
+dependencies = [
+ "cfg-if 0.1.10",
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "digitalwatch"
+version = "0.1.0"
+dependencies = [
+ "sccd",
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "log"
+version = "0.4.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
+dependencies = [
+ "cfg-if 0.1.10",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "sccd"
+version = "0.1.0"
+
+[[package]]
+name = "syn"
+version = "1.0.55"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a571a711dddd09019ccc628e1b17fe87c59b09d513c06c026877aa708334f37a"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-xid",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
+
+[[package]]
+name = "wasm"
+version = "0.1.0"
+dependencies = [
+ "console_error_panic_hook",
+ "digitalwatch",
+ "sccd",
+ "wasm-bindgen",
+]
+
+[[package]]
+name = "wasm-bindgen"
+version = "0.2.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3cd364751395ca0f68cafb17666eee36b63077fb5ecd972bbcd74c90c4bf736e"
+dependencies = [
+ "cfg-if 1.0.0",
+ "wasm-bindgen-macro",
+]
+
+[[package]]
+name = "wasm-bindgen-backend"
+version = "0.2.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1114f89ab1f4106e5b55e688b828c0ab0ea593a1ea7c094b141b14cbaaec2d62"
+dependencies = [
+ "bumpalo",
+ "lazy_static",
+ "log",
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-macro"
+version = "0.2.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7a6ac8995ead1f084a8dea1e65f194d0973800c7f571f6edd70adf06ecf77084"
+dependencies = [
+ "quote",
+ "wasm-bindgen-macro-support",
+]
+
+[[package]]
+name = "wasm-bindgen-macro-support"
+version = "0.2.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5a48c72f299d80557c7c62e37e7225369ecc0c963964059509fbafe917c7549"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+ "wasm-bindgen-backend",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-shared"
+version = "0.2.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7e7811dd7f9398f14cc76efd356f98f03aa30419dea46aa810d71e819fc97158"

examples/digitalwatch/wasm/Cargo.toml → examples/digitalwatch/webassembly/wasm/Cargo.toml


examples/digitalwatch/wasm/lib.rs → examples/digitalwatch/webassembly/wasm/lib.rs


examples/digitalwatch/wasm/outputhandler.js → examples/digitalwatch/webassembly/wasm/outputhandler.js


+ 113 - 0
examples/digitalwatch/webassembly/wasm/pkg/dwatch.d.ts

@@ -0,0 +1,113 @@
+/* tslint:disable */
+/* eslint-disable */
+/**
+* @param {any} out
+* @returns {Handle}
+*/
+export function setup(out: any): Handle;
+/**
+* @param {Handle} h
+* @param {number} delay
+* @param {number} i
+* @returns {RunUntilResult}
+*/
+export function add_event(h: Handle, delay: number, i: number): RunUntilResult;
+/**
+* @param {Handle} h
+* @param {number} t
+* @param {any} out
+* @returns {RunUntilResult}
+*/
+export function run_until(h: Handle, t: number, out: any): RunUntilResult;
+/**
+*/
+export enum InEvent {
+  E_bottomLeftPressed,
+  E_bottomLeftReleased,
+  E_bottomRightPressed,
+  E_bottomRightReleased,
+  E_topLeftPressed,
+  E_topLeftReleased,
+  E_topRightPressed,
+  E_topRightReleased,
+  E_alarmStart,
+  A0,
+  A1,
+  A2,
+  A3,
+  A4,
+  A5,
+  A6,
+  A7,
+  A8,
+  A9,
+  A10,
+}
+/**
+*/
+export enum OutEvent {
+  E_setAlarm,
+  E_setIndiglo,
+  E_unsetIndiglo,
+  E_increaseChronoByOne,
+  E_resetChrono,
+  E_refreshTimeDisplay,
+  E_refreshAlarmDisplay,
+  E_startSelection,
+  E_stopSelection,
+  E_selectNext,
+  E_increaseSelection,
+  E_refreshChronoDisplay,
+  E_increaseTimeByOne,
+  E_checkTime,
+}
+/**
+*/
+export class Handle {
+  free(): void;
+}
+/**
+*/
+export class RunUntilResult {
+  free(): void;
+/**
+* @returns {number}
+*/
+  at: number;
+/**
+* @returns {boolean}
+*/
+  reschedule: boolean;
+/**
+* @returns {number}
+*/
+  simtime: number;
+}
+
+export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
+
+export interface InitOutput {
+  readonly memory: WebAssembly.Memory;
+  readonly __wbg_handle_free: (a: number) => void;
+  readonly setup: (a: number) => number;
+  readonly add_event: (a: number, b: number, c: number) => number;
+  readonly __wbg_rununtilresult_free: (a: number) => void;
+  readonly __wbg_get_rununtilresult_simtime: (a: number) => number;
+  readonly __wbg_set_rununtilresult_simtime: (a: number, b: number) => void;
+  readonly __wbg_get_rununtilresult_reschedule: (a: number) => number;
+  readonly __wbg_set_rununtilresult_reschedule: (a: number, b: number) => void;
+  readonly __wbg_get_rununtilresult_at: (a: number) => number;
+  readonly __wbg_set_rununtilresult_at: (a: number, b: number) => void;
+  readonly run_until: (a: number, b: number, c: number) => number;
+}
+
+/**
+* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
+* for everything else, calls `WebAssembly.instantiate` directly.
+*
+* @param {InitInput | Promise<InitInput>} module_or_path
+*
+* @returns {Promise<InitOutput>}
+*/
+export default function init (module_or_path?: InitInput | Promise<InitInput>): Promise<InitOutput>;
+        

文件差異過大導致無法顯示
+ 221 - 0
examples/digitalwatch/webassembly/wasm/pkg/dwatch.js


+ 54 - 0
examples/digitalwatch/webassembly/wasm/pkg/dwatch_bg.js

@@ -0,0 +1,54 @@
+import * as wasm from './dwatch_bg.wasm';
+
+const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
+
+let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
+
+cachedTextDecoder.decode();
+
+let cachegetUint8Memory0 = null;
+function getUint8Memory0() {
+    if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
+        cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
+    }
+    return cachegetUint8Memory0;
+}
+
+function getStringFromWasm0(ptr, len) {
+    return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
+}
+/**
+* @returns {Handle}
+*/
+export function init() {
+    var ret = wasm.init();
+    return Handle.__wrap(ret);
+}
+
+/**
+*/
+export class Handle {
+
+    static __wrap(ptr) {
+        const obj = Object.create(Handle.prototype);
+        obj.ptr = ptr;
+
+        return obj;
+    }
+
+    free() {
+        const ptr = this.ptr;
+        this.ptr = 0;
+
+        wasm.__wbg_handle_free(ptr);
+    }
+}
+
+export const __wbg_alert_f5393de24ed74e50 = function(arg0, arg1) {
+    alert(getStringFromWasm0(arg0, arg1));
+};
+
+export const __wbindgen_throw = function(arg0, arg1) {
+    throw new Error(getStringFromWasm0(arg0, arg1));
+};
+

二進制
examples/digitalwatch/webassembly/wasm/pkg/dwatch_bg.wasm


+ 14 - 0
examples/digitalwatch/webassembly/wasm/pkg/dwatch_bg.wasm.d.ts

@@ -0,0 +1,14 @@
+/* tslint:disable */
+/* eslint-disable */
+export const memory: WebAssembly.Memory;
+export function __wbg_handle_free(a: number): void;
+export function setup(a: number): number;
+export function add_event(a: number, b: number, c: number): number;
+export function __wbg_rununtilresult_free(a: number): void;
+export function __wbg_get_rununtilresult_simtime(a: number): number;
+export function __wbg_set_rununtilresult_simtime(a: number, b: number): void;
+export function __wbg_get_rununtilresult_reschedule(a: number): number;
+export function __wbg_set_rununtilresult_reschedule(a: number, b: number): void;
+export function __wbg_get_rununtilresult_at(a: number): number;
+export function __wbg_set_rununtilresult_at(a: number, b: number): void;
+export function run_until(a: number, b: number, c: number): number;

+ 12 - 0
examples/digitalwatch/webassembly/wasm/pkg/package.json

@@ -0,0 +1,12 @@
+{
+  "name": "wasm",
+  "version": "0.1.0",
+  "files": [
+    "dwatch_bg.wasm",
+    "dwatch.js",
+    "dwatch.d.ts"
+  ],
+  "module": "dwatch.js",
+  "types": "dwatch.d.ts",
+  "sideEffects": false
+}