AVR8js
A port of AVR8js simulator to LiaScript
Install / Use
/learn @LiaTemplates/AVR8jsREADME
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}}
-
Load the macros via
import: https://raw.githubusercontent.com/liaTemplates/AVR8js/main/README.md -
Copy the definitions into your Project
-
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
