firmware icon indicating copy to clipboard operation
firmware copied to clipboard

Refactor JavaScript interpreter

Open Tawank opened this issue 11 months ago β€’ 6 comments

| This is a project issue to track progress | Add functions from the flipper zero JavaScript https://github.com/jamisonderek/flipper-zero-tutorials/wiki/JavaScript

Update

Progress so far: I've created docs for new API: https://github.com/Tawank/bruce-js-tooling/blob/master/packages/bruce-sdk/DOCS.md Added new functions and fixed existing functions.

I created dino game, http browser for bruce and tried to run NES emulator (failed). https://github.com/Tawank/bruce-js-tooling/tree/master/examples

To do:

  • [ ] create test script to test all "new" API and "old" API. We need to be sure that old and new is working, and we will have testing so we see regress.
  • [ ] give more examples in the docs
  • [ ] add permissions, show dialog if script uses http or storage API, so user can agree, store permissions in the config file.

Legend

  • 😴 Function does nothing on ESP32, e.g., startAnalog and stopAnalog. There's no need to initialize or deinitialize analog functions. These functions are implemented as placeholders - you can use them in code, but they won't do anything.
  • 🦈 Function does not exist in FZ JS but is implemented in Bruce JS.

Functions

  • Globals
    • [x] console
    • [x] delay
    • [x] parse_int
    • [x] print
    • [x] require
    • [x] to_string
    • [x] to_hex_string
    • [x] to_lower_case
    • [x] to_upper_case
    • [x] random 🦈(does not exist in fz js)
    • [x] filepath
    • [x] dirpath
  • badusb
    • [x] setup
    • [ ] quit
    • [ ] isConnected
    • [x] press
    • [x] hold
    • [x] release
    • [x] releaseAll 🦈(does not exist in fz js)
    • [x] print
    • [x] println
    • [x] pressRaw 🦈(does not exist in fz js)
    • [x] runFile 🦈(does not exist in fz js)
    • [ ] altPrint
    • [ ] altPrintln
  • blebeacon
    • [ ] isActive
    • [ ] setConfig
    • [ ] setData
    • [ ] start
    • [ ] stop
    • [ ] keepAlive
  • dialog
    • [x] message
    • [x] error 🦈(does not exist in fz js)
    • [x] choice 🦈(does not exist in fz js)
    • [ ] custom
    • [x] pickFile
    • [x] viewFile 🦈(does not exist in fz js)
  • flipper/device
    • [x] getModel - alias to getBoard
    • [x] getBoard 🦈(does not exist in fz js) shows m5stack model
    • [x] getName (shows bruceConfig.wifiAp.ssid)
    • [x] getBatteryCharge
  • gpio
    • [x] init - arduino and fz syntax possible: gpio.init(25, INPUT); gpio.init("G26", "outputPushPull", "no");
    • [x] read
    • [x] write
    • [x] startAnalog(reference?: double)😴(reference voltage not possible on esp32)
    • [x] stopAnalog 😴 (on esp32 there is no need to init analog)
    • [x] readAnalog
    • [x] writeAnalog 🦈(does not exist in fz js)
    • [x] dacWrite 🦈(does not exist in fz js)
  • keyboard/input
    • [x] setHeader
    • [x] text
      • selected: boolean is not implemented, but it can be passed - it will just be ignored
      • bruce and fz js syntax works (title: string, maxlen: number, initval: string): string and (allocSize: number, defaultText: string, selected: boolean)
    • [ ] byte
    • [x] getKeysPressed 🦈(does not exist in fz js)
    • [x] getPrevPress 🦈(does not exist in fz js)
    • [x] getSelPress 🦈(does not exist in fz js)
    • [x] getNextPress 🦈(does not exist in fz js)
    • [x] getAnyPress 🦈(does not exist in fz js)
  • math
    • [x] abs
    • [x] acos
    • [x] acosh
    • [x] asin
    • [x] asinh
    • [x] atan
    • [x] atan2
    • [x] atanh
    • [x] cbrt
    • [x] ceil
    • [x] clz32
    • [x] cos
    • [x] exp
    • [x] floor
    • [x] is_equal
    • [x] log
    • [x] max
    • [x] min
    • [x] pow
    • [x] random
    • [x] sign
    • [x] sin
    • [x] sqrt
    • [x] trunc
    • [x] PI
    • [x] E
    • [ ] EPSILON
  • notification
    • [ ] success
    • [ ] error
    • [x] blink - ignores first parameter, as m5 stick has only red diode
  • serial
    • [ ] setup
    • [ ] end
    • [x] write alias to serial.print
    • [x] print 🦈(does not exist in fz js)
    • [x] println 🦈(does not exist in fz js)
    • [ ] read
    • [x] readln
    • [ ] readBytes
    • [ ] readAny
    • [ ] expect
  • storage
    • [x] read
    • [x] write
    • [x] rename
    • [x] remove
    • [x] readdir
    • [x] mkdir
    • [x] rmdir
    • [ ] append
    • [ ] copy
    • [ ] move
    • [ ] exists
    • maybe api for FS on PSRAM
  • subghz
    • [ ] end
    • [ ] getFrequency
    • [ ] getRssi
    • [ ] getState
    • [ ] isExternal
    • [x] setFrequency
    • [x] setIdle 😴
    • [ ] setRx
    • [x] setup 😴
    • [x] transmitFile
  • usbdisk
    • [ ] createImage
    • [ ] start
    • [ ] stop
    • [ ] wasEjected
  • sensors
    • [ ] readAccelData (needs new name)
    • [ ] readGyroData (needs new name)
    • [ ] readMagData (needs new name)

Tawank avatar Jan 29 '25 15:01 Tawank

Copy existing global functions to adequate require modules (🦈these functions does not exist in fz js):

  • ir
    • [x] read
    • [x] readRaw
    • [x] transmitFile
  • display
    • [x] color
    • [x] fillScreen
    • [x] setTextColor
    • [x] setTextSize
    • [x] drawString
    • [x] setCursor
    • [x] print
    • [x] println
    • [x] drawPixel
    • [x] drawLine
    • [x] drawRect
    • [x] drawFillRect
    • [x] drawJpg
    • [x] drawGif
    • [x] gifOpen
    • [x] width
    • [x] height
  • fetch
    • [x] implement node-fetch.
      • I changed http from fetch, because nodejs have http module which is creating a WebServer.
      • If we want to use node scripts or libraries from npm or other sources we need to comply to nodejs API.
  • wifi
    • [x] connect
    • [x] connectDialog
    • [x] disconnect
    • [x] scan
  • audio
    • [x] playAudioFile
    • [x] tone

Tawank avatar Jan 29 '25 15:01 Tawank

Instead of a full refactoring , you could simply add a compatibility layer while keeping the old functions.

eadmaster avatar Jan 30 '25 01:01 eadmaster

I didn't removed existing functions, old scripts are working normally.

Tawank avatar Jan 30 '25 01:01 Tawank

I said refactor because in some cases I refratored some older functions to accept flipper zero syntax, but old syntax is working normally.

Tawank avatar Jan 30 '25 01:01 Tawank

You know the mantra: "never break userspace" :-)

eadmaster avatar Jan 30 '25 01:01 eadmaster

Progress so far: I've created docs for new API: https://github.com/Tawank/bruce-js-tooling/blob/master/packages/bruce-sdk/DOCS.md Added new functions and fixed existing functions.

I created dino game, http browser for bruce and tried to run NES emulator (failed). https://github.com/Tawank/bruce-js-tooling/tree/master/examples

To do:

  • [ ] create test script to test all "new" API and "old" API. We need to be sure that old and new is working, and we will have testing so we see regress.
  • [ ] give more examples in the docs
  • [ ] add permissions, show dialog if script uses http or storage API, so user can agree, store permissions in the config file.

Tawank avatar Mar 01 '25 20:03 Tawank