Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Last active August 17, 2022 04:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WebReflection/79d7eb6f0589acdfd7b8aef8c0dde704 to your computer and use it in GitHub Desktop.
Save WebReflection/79d7eb6f0589acdfd7b8aef8c0dde704 to your computer and use it in GitHub Desktop.
Electroff Oled Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Oled Update</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<script type="module">
import CommonJS from '/electroff?module';
// works within an async closure, ofering some utils
CommonJS(async ({require, until}) => {
// require any module installed in the Raspberry
const five = require('johnny-five');
const {RaspiIO} = require('raspi-io');
const font = require('oled-font-5x7');
const Oled = require('oled-js');
const {ceil, pow} = Math;
const options = {
width: 128,
height: 32,
address: 0x3c
};
const board = new five.Board({io: new RaspiIO});
// wait until the board is ready
await until(board).is('ready');
// wait for the main instance to be usable
const oled = await new Oled(board, five, options);
// regular DOM operations, as in any Web page
const input = document.querySelector('input');
const button = document.querySelector('button');
button.addEventListener('click', async (event) => {
event.preventDefault();
const scale = 2;
const h = 7;
button.disabled = true;
// commit each method one after the other
await oled.clearDisplay();
await oled.setCursor(1, ceil((options.height - h) / pow(2, scale)));
await oled.writeString(font, scale, input.value.trim(), 1, true, 2);
await oled.update();
// be sure we can update again later on
button.disabled = false;
});
button.disabled = false;
});
</script>
</head>
<body>
<input placeholder="write something">
<button disabled>update</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment