SkillAgentSearch skills...

AVR8js

A port of AVR8js simulator to LiaScript

Install / Use

/learn @LiaTemplates/AVR8js
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<!-- author: André Dietrich email: LiaScript@web.de version: 0.0.10 language: en narrator: US English Male comment: LiaScript template for the AVR8js simulator. script: https://cdn.jsdelivr.net/gh/liatemplates/avr8js@0.0.11/dist/index.js @AVR8js.sketch: @AVR8js.project(@0,sketch.ino) @AVR8js.project <script> let id = "@0" let name = [ "@1", "@2", "@3", "@4", "@5", "@6", "@7", "@8", "@9" ] .map((e) => e.trim()) .filter((e) => { return (e[0] !== '@' && e !== "") }) let content = [ `@input(0)`, `@input(1)`, `@input(2)`, `@input(3)`, `@input(4)`, `@input(5)`, `@input(6)`, `@input(7)`, `@input(8)`, `@input(9)` ] let sketch; let files = [] for(let i=0; i<name.length; i++) { if (name[i] == "sketch.ino") { sketch = content[i] } else { files.push({name: name[i], content: content[i]}) } if (content[i].match(/#include\s+<Adafruit_SSD1306\.h>/g)) { files.push({ name: 'libraries.txt', content: 'Adafruit SSD1306' }) } } AVR8js.build(sketch, files) .then((e) => { if (e.hex === "") { let msgs = [] for(let i = 0; i<name.length; i++) { msgs.push([]) } let iter = e.stderr.matchAll(/(\w+\.\w+):(\d+):(\d+): ([^:]+):(.+)/g) for(let err=iter.next(); !err.done; err=iter.next()) { msgs[name.findIndex((e) => e==err.value[1])].push({ row : parseInt(err.value[2]) - 1, column : parseInt(err.value[3]), text : err.value[5], type : err.value[4] }) } send.lia(e.stderr, msgs, false) send.lia("LIA: stop") } else { console.debug(e.stdout) if (e.hex) { let runner = AVR8js.execute(e.hex, console.stream, id) send.handle("input", (input) => { runner.serial(input.slice(0, -1)) }) send.lia("LIA: terminal") send.handle("stop", e => { if(runner) { runner.stop() runner = null console.debug("execution stopped") } }) } else { send.lia("LIA: stop") } } }) "LIA: wait" </script> @end @AVR8js.asm <script> let id = "@0" AVR8js.buildASM(`@input`) .then((e) => { if (e.hex === "") { let msgs = [] let iter = e.stderr.matchAll(/main\.s:(\d+):(\d+): ([^:]+):(.+)/g) for(let err=iter.next(); !err.done; err=iter.next()) { msgs.push({ row : parseInt(err.value[1]) - 1, column : parseInt(err.value[2]), text : err.value[4], type : err.value[3].toLower() }) } send.lia(e.stderr, [msgs], false) send.lia("LIA: stop") } else { console.debug(e.stdout) if (e.hex) { let runner = AVR8js.execute(e.hex, console.stream, id) send.handle("input", (input) => { runner.serial(input.slice(0, -1)) }) send.lia("LIA: terminal") send.handle("stop", e => { if(runner) { runner.stop() runner = null console.debug("execution stopped") } }) } else { send.lia("LIA: stop") } } }) "LIA: wait" </script> @end -->

LiaScript

AVR8js - Template

      --{{0}}--

This document defines some basic macros for integrating the Arduino Simulator AVR8js into LiaScript and to make Markdown code-blocks executable.

Try it on LiaScript:

https://liascript.github.io/course/?https://raw.githubusercontent.com/liaTemplates/AVR8js/main/README.md

See the project on Github:

https://github.com/liaTemplates/AVR8js

      --{{1}}--

There are three ways to use this template. The easiest way is to use the import statement and the url of the raw text-file of the master branch or any other branch or version. But you can also copy the required functionionality directly into the header of your Markdown document, see therefor the last slide. And of course, you could also clone this project and change it, as you wish.

       {{1}}
  1. Load the macros via

    import: https://raw.githubusercontent.com/liaTemplates/AVR8js/main/README.md

  2. Copy the definitions into your Project

  3. Clone this repository on GitHub

@AVR8js.sketch

If you only have a simple sketch-file that you want to execute, then simply add @AVR8js.sketch to the end of your code-block, to make it executable and editable. All errors within your code will be displayed in the terminal as well as in the editor. Serial.IO is already connected.

``` cpp
void setup() {
  Serial.begin(9600);
}

void loop() {
   while (Serial.available() > 0 ) {

     String str = Serial.readString();

     if (str.equals("send")) {
        Serial.println("identified");
     } else {
        Serial.println("unknown");
     }
   }
}
```
@AVR8js.sketch

The project tries to attach all pins of your wokwi-webcomponents automatically to the simulation, based on the defined pins. If you want to run multiple simulations on one side, you can hide the relevant wokwi-elements within a div or span and pass the id of that element to @AVR8js.sketch. If you do not pass an id, all visible elements within a section will be attached to the simulation. If an element with the id simulation-time is present, this element will be updated with the current simulation time.

<div id="example">
<wokwi-led color="red"   pin="13" label="13"></wokwi-led>
<wokwi-led color="green" pin="12" label="12"></wokwi-led>
<wokwi-led color="blue"  pin="11" label="11"></wokwi-led>
<wokwi-led color="blue"  pin="10" label="10"></wokwi-led>
<span id="simulation-time"></span>
</div>

``` cpp
byte leds[] = {13, 12, 11, 10};
void setup() {
  Serial.begin(115200);
  for (byte i = 0; i < sizeof(leds); i++) {
    pinMode(leds[i], OUTPUT);
  }
}

int i = 0;
void loop() {
  Serial.print("LED: ");
  Serial.println(i);
  digitalWrite(leds[i], HIGH);
  delay(250);
  digitalWrite(leds[i], LOW);
  i = (i + 1) % sizeof(leds);
}
```
@AVR8js.sketch(example)

@AVR8js.project

If you have a more complex example, you can also create a LiaScript project by defining multiple code blocks, the names in the head are optional, but the the naming in the @AVR8js.project has to match your code blocks and one sketch.ino file must exist. Checkout the last section for a more complex example.

<div id="example">
<wokwi-led color="red"   pin="13" label="13"></wokwi-led>
...
</div>

``` cpp      params.h
byte leds[] = {13, 12, 11, 10};
```
``` cpp      sketch.ino
#import params.h
void setup() {
  Serial.begin(115200);
  for (byte i = 0; i < sizeof(leds); i++) {
    pinMode(leds[i], OUTPUT);
  }
}

int i = 0;
void loop() {
  Serial.print("LED: ");
  Serial.println(i);
  digitalWrite(leds[i], HIGH);
  delay(250);
  digitalWrite(leds[i], LOW);
  i = (i + 1) % sizeof(leds);
}
```
@AVR8js.project( ,params.h,sketch.ino)

@AVR8js.asm

todo

Examples

Serial.read

void setup() {
  Serial.begin(9600);
}

void loop() {
   while (Serial.available() > 0 ) {

     String str = Serial.readString();

     if (str.equals("send")) {
        Serial.println("identified");
     } else {
        Serial.println("unknown");
     }
   }
}

@AVR8js.sketch

LED

<lia-keep> <div id="example1"> <wokwi-led color="red" pin="13" label="13"></wokwi-led> <wokwi-led color="green" pin="12" label="12"></wokwi-led> <wokwi-led color="blue" pin="11" label="11"></wokwi-led> <wokwi-led color="blue" pin="10" label="10"></wokwi-led> <span id="simulation-time"></span> </div> </lia-keep>
byte leds[] = {13, 12, 11, 10};
void setup() {
  Serial.begin(115200);
  for (byte i = 0; i < sizeof(leds); i++) {
    pinMode(leds[i], OUTPUT);
  }
}

int i = 0;
void loop() {
  Serial.print("LED: ");
  Serial.println(i);
  digitalWrite(leds[i], HIGH);
  delay(250);
  digitalWrite(leds[i], LOW);
  i = (i + 1) % sizeof(leds);
}

@AVR8js.sketch(example1)

Buttons

<div id="buttons-experiment"> <wokwi-pushbutton color="green" pin="2" ></wokwi-pushbutton> <wokwi-led color="green" pin="11"></wokwi-led> <wokwi-led color="blue" pin="12"></wokwi-led> <wokwi-led color="red" pin="13"></wokwi-led> <wokwi-pushbutton color="red" pin="3" ></wokwi-pushbutton> </div>
void setup() {
  Serial.begin(115200);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

int i = 0;
void loop() {
  bool green = digitalRead(2);
  bool red   = digitalRead(3);
  Serial.print("LED: ");
  Serial.println(i);

  digitalWrite(11, green);
  digitalWrite(13, red);
  delay(250);

  i += 1;

  digitalWrite(12, i % 2);
}

@AVR8js.sketch(buttons-experiment)

Assembly

; Created: 25/11/2019 9:45:37 AM
; Author : Annon

; defining some labels to make the code easier to read
LEDs = 20
Switches = 21
outer = 22
inner = 23
inner1 = 24

; GPIO registers (according to datasheet)
DDRA = 0x1
PORTA = 0x2
PINC = 0x6
DDRC = 0x7
PORTC = 0x8

.text
.section	.rodata
.string "Look ma, I'm on a chip!!" ; :P

.text
.org 0000 ; start the code at 0x0000 - we arent using interrupts so don't waste space on them
.global Start
Start:
  clr r19
  OUT DDRC, r19
  ser r19
  OUT DDRA, r19
  OUT PORTC, r19

Loop:
  in Switches,PINC ; read switches
  com Switches ; invert
  cbr Switches, 0x7F ; mask out the lower 7 bits
  cpi r21, 0x80 ; compare r21 with 0x80
  breq setLeds ; turn on leds
  brne clrLeds ; make sure the leds turn off if not set

setLeds:
  ser LEDs ; set all bits high
  out PORTA, LEDs ; send them to the leds
  rcall wait ; wait 
View on GitHub
GitHub Stars7
CategoryDevelopment
Updated5mo ago
Forks2

Languages

TypeScript

Security Score

87/100

Audited on Nov 1, 2025

No findings