Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
48 lines (40 loc) · 1.21 KB

File metadata and controls

48 lines (40 loc) · 1.21 KB
Copy raw file
Download raw file
Outline
Edit and raw actions
sidebar_position 9
description Learn how to use buffers for dynamic allocation, memory conservation, and creating arrays with fixed upper limits.
keywords
buffers
markdown
dynamic allocation
memory conservation
arrays

Buffers

Buffers can be dynamically allocated, read and written. This can be used to conserve memory (regular variables always take 8 bytes) and create arrays (with fixed upper limit).

const mybuf = Buffer.alloc(12) // 12 byte buffer
mybuf.setAt(10, "u16", 123)
mybuf.setAt(3, "u22.10", 173.282)
const z = mybuf.getAt(3, "u22.10")

hex

hex is a string literal template that converts data in hexadecimal form into a readonly Buffer in flash.

// Buffer in flash!
const data = hex`010203040506070809`
console.log(data)

packet

There is a special buffer called ds.packet which represents a buffer to be passed to next command or register write. It supports ds.packet.setLength() function (unlike regular buffers), and can be passed to any command or register write. For example lamp.intensity.write(0.7) is equivalent to:

const lamp = new ds.Led()
ds.packet.setLength(2)
ds.packet.setAt(0, "u0.16", 0.7)
lamp.intensity.write(ds.packet)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.