Fork me on GitHub

Bitbang

This library provides Bit Banging API.

C API Reference mgos_bitbang.h

#include "mgos_bitbang.h"

View this file on GitHub: mgos_bitbang.h


enum mgos_delay_unit {
  MGOS_DELAY_MSEC = 0,
  MGOS_DELAY_USEC = 1,
  MGOS_DELAY_100NSEC = 2,
};

void mgos_bitbang_write_bits(int gpio, enum mgos_delay_unit delay_unit,
                             uint32_t t0h, uint32_t t0l, uint32_t t1h,
                             uint32_t t1l, const uint8_t *data, size_t len);

Bit bang GPIO pin gpio.

t0h and t0l specify time pattern for zero bit. t0h is the number of delay_unit to hold gpio high. t0l is the number of delay_unit to hold gpio low.

Subsequently, t1h and t1l specify time pattern for bit 1.

Finally, data and len contain bit sequence.

void mgos_bitbang_write_bits_js(int gpio, enum mgos_delay_unit delay_unit,
                                uint32_t t, const uint8_t *data, size_t len);

This function is a wrapper for mgos_bitbang_write_bits(). It has smaller number of arguments (less than 6) and therefore could be FFI-ed to JavaScript. Essentially, it just packs all time patterns into a single value t.

extern uint32_t mgos_bitbang_n100_cal;

JAVASCRIPT API Reference api_bitbang.js

load("api_bitbang.js");


BitBang.write(pin, delay_unit, t0h, t0l, t1h, t1l, ptr, len)

Write bits to a given pin. delay_unit is one of the: BitBang.DELAY_MSEC, BitBang.DELAY_USEC, BitBang.DELAY_100NSEC. ptr, len is a bit pattern to write. t0h, t0l is the time pattern for zero bit, t1h, t1l is the time pattern for 1. The time pattern specifies the number of time units to hold the pin high, and the number of units to hold the pin low. Return value: none.