Skip to content

Instantly share code, notes, and snippets.

@cagataycali
Created July 19, 2024 22:02
Show Gist options
  • Save cagataycali/08358b88ca4eda09c181df7e5368d783 to your computer and use it in GitHub Desktop.
Save cagataycali/08358b88ca4eda09c181df7e5368d783 to your computer and use it in GitHub Desktop.
A comprehensive cheat sheet for developing JavaScript code on the Flipper Zero, including examples and explanations for various functionalities like GPIO usage, BadUSB, BLE Beacon, UART, USB Disk Emulation, Sub-GHz communication, Textbox, Submenu, Notifications, Storage, and additional menus. This cheat sheet is designed to streamline the coding…

Flipper Zero JavaScript Cheat Sheet

Overview

This repository contains a comprehensive cheat sheet for developing JavaScript code on the Flipper Zero. It includes examples and explanations for various functionalities like GPIO usage, BadUSB, BLE Beacon, UART, USB Disk Emulation, Sub-GHz communication, Textbox, Submenu, Notifications, Storage, and additional menus. This cheat sheet is designed to streamline the coding process and provide quick reference for commonly used features.

Contents

  • General Structure
  • GPIO Usage
  • BadUSB Usage
  • BLE Beacon
  • Serial Communication (UART)
  • USB Disk Emulation
  • Sub-GHz Communication
  • Textbox
  • Submenu
  • Notifications
  • Storage
  • Additional Menus and Their Controls
    • File Manager
    • Settings Menu
    • Infrared Menu
    • NFC Menu

How to Use

Clone or download the repository and open the Flipper_Zero_JS_Cheat_Sheet.md file for a quick reference guide on how to implement various functionalities on your Flipper Zero using JavaScript.

License

This repository is licensed under the MIT License. Feel free to use, modify, and distribute this cheat sheet as needed.

Flipper Zero JavaScript Cheat Sheet

General Structure

  • Path and File Information:
    print("Script path:", __filepath);
    print("Script directory:", __dirpath);

GPIO Usage

  • Initialize Pins:

    let gpio = require("gpio");
    gpio.init("PA7", "analog", "no");
    gpio.init("PA6", "analog", "no");
    gpio.init("PA4", "analog", "no");
    gpio.startAnalog(2048);
  • Read Analog Pin Values:

    let pa7_value = gpio.readAnalog("PA7");
    let pa6_value = gpio.readAnalog("PA6");
    let pa4_value = gpio.readAnalog("PA4");
  • Write and Read GPIO Pins:

    gpio.write("PC3", true);
    let value = gpio.read("PC1");

BadUSB Usage

  • BadUSB Setup and Usage:
    let badusb = require("badusb");
    badusb.setup({ vid: 0xAAAA, pid: 0xBBBB, mfr_name: "Flipper", prod_name: "Zero", layout_path: "/ext/badusb/assets/layouts/en-US.kl" });
    badusb.println("Hello, world!");
    badusb.press("CTRL", "a");

BLE Beacon

  • BLE Beacon Broadcast:
    let blebeacon = require("blebeacon");
    blebeacon.setConfig(generateRandomMac(), 0x1F, 50, 150);
    blebeacon.setData(Uint8Array([14, 0xFF, 0x75, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x01, 0xFF, 0x00, 0x00, 0x43, model]));
    blebeacon.start();

Serial Communication (UART)

  • UART Echo:
    let serial = require("serial");
    serial.setup("usart", 230400);
    while (1) {
      let rx_data = serial.readBytes(1, 0);
      if (rx_data !== undefined) {
        serial.write(rx_data);
      }
    }

USB Disk Emulation

  • Start and Stop USB Disk:
    let usbdisk = require("usbdisk");
    usbdisk.start("/ext/apps_data/mass_storage/128MB.img");
    while (!usbdisk.wasEjected()) { delay(1000); }
    usbdisk.stop();

Sub-GHz Communication

  • Change Frequency and Transmit Data:
    let subghz = require("subghz");
    subghz.setup();
    subghz.setFrequency(433920000);
    subghz.transmitFile("/ext/subghz/0.sub");

Textbox

  • Dynamic Textbox:
    let textbox = require("textbox");
    textbox.setConfig("end", "text");
    textbox.clearText();
    textbox.addText("Example dynamic updating textbox\n");
    textbox.show();
    while (textbox.isOpen() && i < 20) {
      textbox.addText("textbox " + to_string(i++) + "\n");
      delay(500);
    }
    if (textbox.isOpen()) { textbox.close(); }

Submenu

  • Submenu Usage:
    let submenu = require("submenu");
    submenu.addItem("Item 1", 0);
    submenu.addItem("Item 2", 1);
    submenu.addItem("Item 3", 2);
    submenu.setHeader("Select an option:");
    let result = submenu.show();
    print("Result:", result);

Notifications

  • Show Notifications:
    let notify = require("notification");
    notify.error();
    delay(1000);
    notify.success();
    delay(1000);

Storage

  • Write, Read, and Delete Files:
    let storage = require("storage");
    storage.write("/ext/storage.test", "Hello ");
    storage.append("/ext/storage.test", "World!");
    let data = storage.read("/ext/storage.test");
    storage.remove("/ext/storage.test");

Additional Menus and Their Controls

File Manager

The File Manager in Flipper Zero allows you to navigate and manage files stored on the device. Here's how you can interact with the File Manager using JavaScript:

  • List Files:

    let storage = require("storage");
    let files = storage.listFiles("/ext/");
    files.forEach(file => {
      print("File:", file);
    });
  • Read a File:

    let data = storage.read("/ext/example.txt");
    print("File content:", arraybuf_to_string(data));
  • Write to a File:

    storage.write("/ext/example.txt", "Hello, Flipper!");

Settings Menu

The Settings Menu allows you to configure various aspects of the Flipper Zero. You can create custom settings or adjust existing ones:

  • Set Configuration:

    let settings = require("settings");
    settings.set("display.brightness", 80);
  • Get Configuration:

    let brightness = settings.get("display.brightness");
    print("Brightness level:", brightness);

Infrared Menu

The Infrared Menu allows you to send and receive infrared signals. Here's how you can control it:

  • Send Infrared Signal:

    let infrared = require("infrared");
    infrared.transmit("/ext/infrared_codes/power_on.ir");
  • Receive Infrared Signal:

    let signal = infrared.receive();
    print("Received signal:", signal);

NFC Menu

The NFC Menu lets you interact with NFC tags and devices:

  • Read NFC Tag:

    let nfc = require("nfc");
    let tagData = nfc.read();
    print("NFC Tag Data:", tagData);
  • Write NFC Tag:

    nfc.write("http://example.com");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment