{"id":20660970,"url":"https://github.com/liatemplates/avr8js","last_synced_at":"2025-10-10T12:34:18.596Z","repository":{"id":107256828,"uuid":"310693158","full_name":"LiaTemplates/AVR8js","owner":"LiaTemplates","description":"A port of AVR8js simulator to LiaScript","archived":false,"fork":false,"pushed_at":"2023-07-10T17:57:58.000Z","size":761,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-07T02:01:53.328Z","etag":null,"topics":["avr","avr8js-simulator","liascript","liascript-template","simulator"],"latest_commit_sha":null,"homepage":"https://liascript.github.io/course/?https://raw.githubusercontent.com/LiaTemplates/AVR8js/main/README.md","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LiaTemplates.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-11-06T19:55:23.000Z","updated_at":"2024-07-04T17:59:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"62b731e3-03c4-48a9-b2a0-fde956e3f47e","html_url":"https://github.com/LiaTemplates/AVR8js","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/LiaTemplates/AVR8js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiaTemplates%2FAVR8js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiaTemplates%2FAVR8js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiaTemplates%2FAVR8js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiaTemplates%2FAVR8js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LiaTemplates","download_url":"https://codeload.github.com/LiaTemplates/AVR8js/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiaTemplates%2FAVR8js/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279003897,"owners_count":26083641,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["avr","avr8js-simulator","liascript","liascript-template","simulator"],"created_at":"2024-11-16T19:06:39.797Z","updated_at":"2025-10-10T12:34:18.589Z","avatar_url":"https://github.com/LiaTemplates.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!--\nauthor:   André Dietrich\n\nemail:    LiaScript@web.de\n\nversion:  0.0.10\n\nlanguage: en\n\nnarrator: US English Male\n\ncomment:  LiaScript template for the AVR8js simulator.\n\nscript:   https://cdn.jsdelivr.net/gh/liatemplates/avr8js@0.0.11/dist/index.js\n\n@AVR8js.sketch: @AVR8js.project(@0,sketch.ino)\n\n@AVR8js.project\n\u003cscript\u003e\nlet id = \"@0\"\n\nlet name = [\n  \"@1\", \"@2\", \"@3\", \"@4\", \"@5\", \"@6\", \"@7\", \"@8\", \"@9\"\n  ]\n  .map((e) =\u003e e.trim())\n  .filter((e) =\u003e { return (e[0] !== '@' \u0026\u0026 e !== \"\") })\n\nlet content = [\n  `@input(0)`,\n  `@input(1)`,\n  `@input(2)`,\n  `@input(3)`,\n  `@input(4)`,\n  `@input(5)`,\n  `@input(6)`,\n  `@input(7)`,\n  `@input(8)`,\n  `@input(9)`\n  ]\n\nlet sketch;\nlet files = []\n\nfor(let i=0; i\u003cname.length; i++) {\n  if (name[i] == \"sketch.ino\") {\n    sketch = content[i]\n  } else {\n    files.push({name: name[i], content: content[i]})\n  }\n\n  if (content[i].match(/#include\\s+\u003cAdafruit_SSD1306\\.h\u003e/g)) {\n    files.push({ name: 'libraries.txt', content: 'Adafruit SSD1306' })\n  }\n}\n\nAVR8js.build(sketch, files)\n   .then((e) =\u003e {\n     if (e.hex === \"\") {\n       let msgs = []\n\n       for(let i = 0; i\u003cname.length; i++) {\n         msgs.push([])\n       }\n\n       let iter = e.stderr.matchAll(/(\\w+\\.\\w+):(\\d+):(\\d+): ([^:]+):(.+)/g)\n\n       for(let err=iter.next(); !err.done; err=iter.next()) {\n         msgs[name.findIndex((e) =\u003e e==err.value[1])].push({\n           row :    parseInt(err.value[2]) - 1,\n           column : parseInt(err.value[3]),\n           text :   err.value[5],\n           type :   err.value[4]\n         })\n       }\n       send.lia(e.stderr, msgs, false)\n       send.lia(\"LIA: stop\")\n     }\n     else {\n       console.debug(e.stdout)\n\n       if (e.hex) {\n         let runner = AVR8js.execute(e.hex, console.stream, id)\n\n         send.handle(\"input\", (input) =\u003e {\n            runner.serial(input.slice(0, -1))\n         })\n\n         send.lia(\"LIA: terminal\")\n\n         send.handle(\"stop\", e =\u003e {\n           if(runner) {\n             runner.stop()\n             runner = null\n             console.debug(\"execution stopped\")\n           }\n         })\n       } else {\n         send.lia(\"LIA: stop\")\n       }\n     }\n   })\n\"LIA: wait\"\n\u003c/script\u003e\n\n@end\n\n\n\n@AVR8js.asm\n\u003cscript\u003e\nlet id = \"@0\"\n\nAVR8js.buildASM(`@input`)\n   .then((e) =\u003e {\n     if (e.hex === \"\") {\n       let msgs = []\n\n       let iter = e.stderr.matchAll(/main\\.s:(\\d+):(\\d+): ([^:]+):(.+)/g)\n\n       for(let err=iter.next(); !err.done; err=iter.next()) {\n         msgs.push({\n           row :    parseInt(err.value[1]) - 1,\n           column : parseInt(err.value[2]),\n           text :   err.value[4],\n           type :   err.value[3].toLower()\n         })\n       }\n       send.lia(e.stderr, [msgs], false)\n       send.lia(\"LIA: stop\")\n     }\n     else {\n       console.debug(e.stdout)\n\n       if (e.hex) {\n         let runner = AVR8js.execute(e.hex, console.stream, id)\n\n         send.handle(\"input\", (input) =\u003e {\n            runner.serial(input.slice(0, -1))\n         })\n\n         send.lia(\"LIA: terminal\")\n\n         send.handle(\"stop\", e =\u003e {\n           if(runner) {\n             runner.stop()\n             runner = null\n             console.debug(\"execution stopped\")\n           }\n         })\n       } else {\n         send.lia(\"LIA: stop\")\n       }\n     }\n   })\n\"LIA: wait\"\n\u003c/script\u003e\n\n@end\n\n--\u003e\n\n[![LiaScript](https://raw.githubusercontent.com/LiaScript/LiaScript/master/badges/course.svg)](https://LiaScript.github.io/course/?https://raw.githubusercontent.com/LiaTemplates/AVR8js/main/README.md)\n\n# AVR8js - Template\n\n          --{{0}}--\nThis document defines some basic macros for integrating the Arduino Simulator\n[AVR8js](https://github.com/wokwi/avr8js) into\n[LiaScript](https://LiaScript.github.io) and to make Markdown code-blocks\nexecutable.\n\n__Try it on LiaScript:__\n\nhttps://liascript.github.io/course/?https://raw.githubusercontent.com/liaTemplates/AVR8js/main/README.md\n\n__See the project on Github:__\n\nhttps://github.com/liaTemplates/AVR8js\n\n          --{{1}}--\nThere are three ways to use this template. The easiest way is to use the\n`import` statement and the url of the raw text-file of the master branch or any\nother branch or version. But you can also copy the required functionionality\ndirectly into the header of your Markdown document, see therefor the [last\nslide](#implementation). And of course, you could also clone this project and\nchange it, as you wish.\n\n           {{1}}\n1. Load the macros via\n\n   `import: https://raw.githubusercontent.com/liaTemplates/AVR8js/main/README.md`\n\n2. Copy the definitions into your Project\n\n3. Clone this repository on GitHub\n\n\n## `@AVR8js.sketch`\n\nIf you only have a simple sketch-file that you want to execute, then simply\nadd `@AVR8js.sketch` to the end of your code-block, to make it executable and\neditable. All errors within your code will be displayed in the terminal as well\nas in the editor. Serial.IO is already connected.\n\n```` markdown\n``` cpp\nvoid setup() {\n  Serial.begin(9600);\n}\n\nvoid loop() {\n   while (Serial.available() \u003e 0 ) {\n\n     String str = Serial.readString();\n\n     if (str.equals(\"send\")) {\n        Serial.println(\"identified\");\n     } else {\n        Serial.println(\"unknown\");\n     }\n   }\n}\n```\n@AVR8js.sketch\n````\n\nThe project tries to attach all pins of your wokwi-webcomponents automatically\nto the simulation, based on the defined pins. If you want to run multiple\nsimulations on one side, you can hide the relevant wokwi-elements within a div\nor span and pass the id of that element to `@AVR8js.sketch`. If you do not pass\nan id, all visible elements within a section will be attached to the simulation.\nIf an element with the id `simulation-time` is present, this element will be\nupdated with the current simulation time.\n\n\n```` markdown\n\u003cdiv id=\"example\"\u003e\n\u003cwokwi-led color=\"red\"   pin=\"13\" label=\"13\"\u003e\u003c/wokwi-led\u003e\n\u003cwokwi-led color=\"green\" pin=\"12\" label=\"12\"\u003e\u003c/wokwi-led\u003e\n\u003cwokwi-led color=\"blue\"  pin=\"11\" label=\"11\"\u003e\u003c/wokwi-led\u003e\n\u003cwokwi-led color=\"blue\"  pin=\"10\" label=\"10\"\u003e\u003c/wokwi-led\u003e\n\u003cspan id=\"simulation-time\"\u003e\u003c/span\u003e\n\u003c/div\u003e\n\n``` cpp\nbyte leds[] = {13, 12, 11, 10};\nvoid setup() {\n  Serial.begin(115200);\n  for (byte i = 0; i \u003c sizeof(leds); i++) {\n    pinMode(leds[i], OUTPUT);\n  }\n}\n\nint i = 0;\nvoid loop() {\n  Serial.print(\"LED: \");\n  Serial.println(i);\n  digitalWrite(leds[i], HIGH);\n  delay(250);\n  digitalWrite(leds[i], LOW);\n  i = (i + 1) % sizeof(leds);\n}\n```\n@AVR8js.sketch(example)\n````\n\n## `@AVR8js.project`\n\n\nIf you have a more complex example, you can also create a LiaScript project by\ndefining multiple code blocks, the names in the head are optional, but the the\nnaming in the `@AVR8js.project` has to match your code blocks and one\n`sketch.ino` file must exist. Checkout the last section for a more complex\nexample.\n\n\n```` markdown\n\u003cdiv id=\"example\"\u003e\n\u003cwokwi-led color=\"red\"   pin=\"13\" label=\"13\"\u003e\u003c/wokwi-led\u003e\n...\n\u003c/div\u003e\n\n``` cpp      params.h\nbyte leds[] = {13, 12, 11, 10};\n```\n``` cpp      sketch.ino\n#import params.h\nvoid setup() {\n  Serial.begin(115200);\n  for (byte i = 0; i \u003c sizeof(leds); i++) {\n    pinMode(leds[i], OUTPUT);\n  }\n}\n\nint i = 0;\nvoid loop() {\n  Serial.print(\"LED: \");\n  Serial.println(i);\n  digitalWrite(leds[i], HIGH);\n  delay(250);\n  digitalWrite(leds[i], LOW);\n  i = (i + 1) % sizeof(leds);\n}\n```\n@AVR8js.project( ,params.h,sketch.ino)\n````\n\n## `@AVR8js.asm`\n\ntodo\n\n\n## Examples\n\n### Serial.read\n\n``` cpp\nvoid setup() {\n  Serial.begin(9600);\n}\n\nvoid loop() {\n   while (Serial.available() \u003e 0 ) {\n\n     String str = Serial.readString();\n\n     if (str.equals(\"send\")) {\n        Serial.println(\"identified\");\n     } else {\n        Serial.println(\"unknown\");\n     }\n   }\n}\n```\n@AVR8js.sketch\n\n### LED\n\n\u003clia-keep\u003e\n\u003cdiv id=\"example1\"\u003e\n\u003cwokwi-led color=\"red\"   pin=\"13\" label=\"13\"\u003e\u003c/wokwi-led\u003e\n\u003cwokwi-led color=\"green\" pin=\"12\" label=\"12\"\u003e\u003c/wokwi-led\u003e\n\u003cwokwi-led color=\"blue\"  pin=\"11\" label=\"11\"\u003e\u003c/wokwi-led\u003e\n\u003cwokwi-led color=\"blue\"  pin=\"10\" label=\"10\"\u003e\u003c/wokwi-led\u003e\n\u003cspan id=\"simulation-time\"\u003e\u003c/span\u003e\n\u003c/div\u003e\n\u003c/lia-keep\u003e\n\n``` cpp\nbyte leds[] = {13, 12, 11, 10};\nvoid setup() {\n  Serial.begin(115200);\n  for (byte i = 0; i \u003c sizeof(leds); i++) {\n    pinMode(leds[i], OUTPUT);\n  }\n}\n\nint i = 0;\nvoid loop() {\n  Serial.print(\"LED: \");\n  Serial.println(i);\n  digitalWrite(leds[i], HIGH);\n  delay(250);\n  digitalWrite(leds[i], LOW);\n  i = (i + 1) % sizeof(leds);\n}\n```\n@AVR8js.sketch(example1)\n\n### Buttons\n\n\u003cdiv id=\"buttons-experiment\"\u003e\n  \u003cwokwi-pushbutton color=\"green\" pin=\"2\" \u003e\u003c/wokwi-pushbutton\u003e\n  \u003cwokwi-led        color=\"green\" pin=\"11\"\u003e\u003c/wokwi-led\u003e\n  \u003cwokwi-led        color=\"blue\"  pin=\"12\"\u003e\u003c/wokwi-led\u003e\n  \u003cwokwi-led        color=\"red\"   pin=\"13\"\u003e\u003c/wokwi-led\u003e\n  \u003cwokwi-pushbutton color=\"red\"   pin=\"3\" \u003e\u003c/wokwi-pushbutton\u003e\n\u003c/div\u003e\n\n``` cpp\nvoid setup() {\n  Serial.begin(115200);\n  pinMode(2, INPUT_PULLUP);\n  pinMode(3, INPUT_PULLUP);\n  pinMode(11, OUTPUT);\n  pinMode(12, OUTPUT);\n  pinMode(13, OUTPUT);\n}\n\nint i = 0;\nvoid loop() {\n  bool green = digitalRead(2);\n  bool red   = digitalRead(3);\n  Serial.print(\"LED: \");\n  Serial.println(i);\n\n  digitalWrite(11, green);\n  digitalWrite(13, red);\n  delay(250);\n\n  i += 1;\n\n  digitalWrite(12, i % 2);\n}\n```\n@AVR8js.sketch(buttons-experiment)\n\n### Assembly\n\n``` asm\n; Created: 25/11/2019 9:45:37 AM\n; Author : Annon\n\n; defining some labels to make the code easier to read\nLEDs = 20\nSwitches = 21\nouter = 22\ninner = 23\ninner1 = 24\n\n; GPIO registers (according to datasheet)\nDDRA = 0x1\nPORTA = 0x2\nPINC = 0x6\nDDRC = 0x7\nPORTC = 0x8\n\n.text\n.section\t.rodata\n.string \"Look ma, I'm on a chip!!\" ; :P\n\n.text\n.org 0000 ; start the code at 0x0000 - we arent using interrupts so don't waste space on them\n.global Start\nStart:\n  clr r19\n  OUT DDRC, r19\n  ser r19\n  OUT DDRA, r19\n  OUT PORTC, r19\n\nLoop:\n  in Switches,PINC ; read switches\n  com Switches ; invert\n  cbr Switches, 0x7F ; mask out the lower 7 bits\n  cpi r21, 0x80 ; compare r21 with 0x80\n  breq setLeds ; turn on leds\n  brne clrLeds ; make sure the leds turn off if not set\n\nsetLeds:\n  ser LEDs ; set all bits high\n  out PORTA, LEDs ; send them to the leds\n  rcall wait ; wait ~1s\n  clr LEDs ; set all bits low\n  out PORTA, LEDs ; send them to the leds\n  rcall wait ; wait ~1s\n  rjmp Loop ; jump back to main loop\n\nclrLeds:\n  clr LEDs ; set all bits low\n  out PORTA, LEDs ; send them to the leds\n  rjmp Loop ; jump back to main loop\n\nwait: ; 255 * 252 * 83 * 3 = 16000740/16mhz = 1.0004625s\n  ldi outer,0\nwaitouter:\n  rcall waitinner\n  dec outer\n  brne waitouter\n  ret\nwaitinner:\n  ldi inner,253\nwaitinner1:\n  rcall waitininner\n  dec inner\n  brne waitinner1\n  ret\nwaitininner:\n  ldi inner1,84\nwaitininner1:\n  dec inner1\n  brne waitininner1\n  ret\n```\n@AVR8js.asm\n\n### 7-Segment\n\n\u003cdiv id=\"7-experiment\"\u003e\n\u003cwokwi-7segment digits=\"1\" pin=\"13\"\u003e\u003c/wokwi-7segment\u003e\n\u003cspan id=\"simulation-time\"\u003e\u003c/span\u003e\n\u003c/div\u003e\n\n``` cpp\nbyte leds[] = {13, 12, 11, 10};\nvoid setup() {\n  Serial.begin(115200);\n  for (byte i = 0; i \u003c sizeof(leds); i++) {\n    pinMode(leds[i], OUTPUT);\n  }\n}\n\nint i = 0;\nvoid loop() {\n  Serial.print(\"LED: \");\n  Serial.println(i);\n  digitalWrite(leds[i], HIGH);\n  delay(250);\n  digitalWrite(leds[i], LOW);\n  i = (i + 1) % sizeof(leds);\n}\n```\n@AVR8js.sketch(7-experiment)\n\n\n### Buzzer\n\n\u003cdiv id=\"example1\"\u003e\n  \u003cwokwi-buzzer color=\"red\"   pin=\"13\" label=\"13\"\u003e\u003c/wokwi-buzzer\u003e\n  \u003cwokwi-buzzer color=\"green\" pin=\"12\" label=\"12\"\u003e\u003c/wokwi-buzzer\u003e\n  \u003cwokwi-buzzer color=\"blue\"  pin=\"11\" label=\"11\"\u003e\u003c/wokwi-buzzer\u003e\n  \u003cwokwi-buzzer color=\"blue\"  pin=\"10\" label=\"10\"\u003e\u003c/wokwi-buzzer\u003e\n\u003c/div\u003e\n\n``` cpp\nbyte leds[] = {13, 12, 11, 10};\nvoid setup() {\n  Serial.begin(115200);\n  for (byte i = 0; i \u003c sizeof(leds); i++) {\n    pinMode(leds[i], OUTPUT);\n  }\n}\n\nint i = 0;\nvoid loop() {\n  Serial.print(\"BUZZER: \");\n  Serial.println(i);\n  digitalWrite(leds[i], HIGH);\n  delay(250);\n  digitalWrite(leds[i], LOW);\n  i = (i + 1) % sizeof(leds);\n}\n```\n@AVR8js.sketch(example1)\n\n### Arduino-Mega\n\n\u003cwokwi-arduino-mega\u003e\u003c/wokwi-arduino-mega\u003e\n\n### Arduino-Uno\n\n\u003cwokwi-arduino-uno\u003e\u003c/wokwi-arduino-uno\u003e\n\n### Arduino-Nano\n\n\u003cwokwi-arduino-nano\u003e\u003c/wokwi-arduino-nano\u003e\n\n\n### DHT22\n\n\u003cwokwi-dht22\u003e\u003c/wokwi-dht22\u003e\n\n### LCD1602\n\n\u003cwokwi-lcd1602\u003e\u003c/wokwi-lcd1602\u003e\n\n``` cpp\n#include \u003cLiquidCrystal_I2C.h\u003e\n\n#define I2C_ADDR    0x27\n#define LCD_COLUMNS 20\n#define LCD_LINES   4\n\nLiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);\n\nvoid setup() {\n  // Init\n  lcd.init();\n  lcd.backlight();\n\n  // Print something\n  lcd.setCursor(3, 0);\n  lcd.print(\"Hello, world!\");\n  lcd.setCursor(2, 1);\n  lcd.print(\"Wokwi Online IoT\");\n  lcd.setCursor(5, 2);\n  lcd.print(\"Simulator\");\n  lcd.setCursor(7, 3);\n  lcd.print(\"Enjoy!\");\n}\n\nvoid loop() {\n}\n```\n@AVR8js.sketch\n\n### Membrane-Keypad\n\n\u003cwokwi-membrane-keypad\u003e\u003c/wokwi-membrane-keypad\u003e\n\n### Neopixel\n\n\u003cwokwi-neopixel\u003e\u003c/wokwi-neopixel\u003e\n\n### Neopixel-Matrix\n\n\u003cdiv id=\"matrix-experiment\"\u003e\n\u003cwokwi-neopixel-matrix pin=\"3\" cols=\"9\" rows=\"9\"\u003e\u003c/wokwi-neopixel-matrix\u003e\n\u003cspan id=\"simulation-time\"\u003e\u003c/span\u003e\n\u003c/div\u003e\n\n``` cpp\n#include \"FastLED.h\"\n\n// Matrix size\n#define NUM_ROWS 9\n#define NUM_COLS 9\n\n// LEDs pin\n#define DATA_PIN 3\n\n// LED brightness\n#define BRIGHTNESS 180\n\n#define NUM_LEDS NUM_ROWS * NUM_COLS\n\n// Define the array of leds\nCRGB leds[NUM_LEDS];\n\nvoid setup() {\n  FastLED.addLeds\u003cNEOPIXEL, DATA_PIN\u003e(leds, NUM_LEDS);\n  FastLED.setBrightness(BRIGHTNESS);\n}\n\nint counter = 0;\nvoid loop() {\n  for (byte row = 0; row \u003c NUM_ROWS; row++) {\n    for (byte col = 0; col \u003c NUM_COLS; col++) {\n      int delta = abs(NUM_ROWS - row * 2) + abs(NUM_COLS - col * 2);\n      leds[row * NUM_COLS + col] = CHSV(delta * 4 + counter, 255, 255);\n    }\n  }\n  FastLED.show();\n  delay(5);\n  counter++;\n}\n```\n@AVR8js.sketch(matrix-experiment)\n\n### Potentiometer\n\n\u003cwokwi-potentiometer\u003e\u003c/wokwi-potentiometer\u003e\n\n### Resistor\n\n\u003cwokwi-resistor\u003e\u003c/wokwi-resistor\u003e\n\n### Rotary-Dialer\n\n\u003cwokwi-rotary-dialer\u003e\u003c/wokwi-rotary-dialer\u003e\n\n### Servo\n\n\u003cwokwi-servo\u003e\u003c/wokwi-servo\u003e\n\n### SSD1306\n\n\n\n\u003cdiv id=\"ssd1306-experiment\"\u003e\n\u003cwokwi-ssd1306\u003e\u003c/wokwi-ssd1306\u003e\n\u003cspan id=\"simulation-time\"\u003e\u003c/span\u003e\n\u003c/div\u003e\n\n``` cpp\n#include \u003cWire.h\u003e\n#include \u003cAdafruit_GFX.h\u003e\n#include \u003cAdafruit_SSD1306.h\u003e\n\n#define SCREEN_WIDTH 128\n#define SCREEN_HEIGHT 64\n\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, \u0026Wire, -1);\n\nvoid setup() {\n  display.begin(SSD1306_SWITCHCAPVCC, 0x3D);\n  display.display();\n  delay(1000);\n}\n\nint counter = 0;\nvoid loop() {\n  display.clearDisplay();\n  display.setTextSize(1);\n  display.setTextColor(SSD1306_WHITE);\n  display.setCursor(4, 4);\n  display.println(F(\"Hello, Wokwi!\"));\n  display.setTextSize(2);\n  display.setCursor(54, 24);\n  display.println(counter);\n  display.display();\n  counter++;\n  delay(1000);\n}\n```\n@AVR8js.sketch(ssd1306-experiment)\n\n### Projects\n\n\u003e Example not working yet ...\n\n\u003cdiv id=\"game-container\"\u003e\n  \u003cwokwi-pushbutton color=\"red\" pin=\"2\"\u003e\u003c/wokwi-pushbutton\u003e\n  \u003cwokwi-led color=\"red\" label=\"9\" pin=\"9\"\u003e\u003c/wokwi-led\u003e\n  \u003cwokwi-led color=\"green\" label=\"10\" pin=\"10\"\u003e\u003c/wokwi-led\u003e\n  \u003cwokwi-pushbutton color=\"green\" pin=\"3\"\u003e\u003c/wokwi-pushbutton\u003e\n  \u003cwokwi-pushbutton color=\"blue\" pin=\"4\"\u003e\u003c/wokwi-pushbutton\u003e\n  \u003cwokwi-led color=\"blue\" label=\"11\" pin=\"11\"\u003e\u003c/wokwi-led\u003e\n  \u003cwokwi-led color=\"yellow\" label=\"12\" pin=\"12\"\u003e\u003c/wokwi-led\u003e\n  \u003cwokwi-pushbutton color=\"yellow\" pin=\"5\"\u003e\u003c/wokwi-pushbutton\u003e\n\u003c/div\u003e\n\n``` cpp           sketch.ino\n/**\n   Simon Game for Arduino\n\n   Copyright (C) 2016, Uri Shaked\n\n   Licensed under the MIT License.\n*/\n\n#include \"pitches.h\"\n\n/* Constants - define pin numbers for leds, buttons and speaker, and also the game tones */\nchar ledPins[] = {9, 10, 11, 12};\nchar buttonPins[] = {2, 3, 4, 5};\n#define SPEAKER_PIN 8\n\n// If you run this code directly on the WeMos D1 Mini Board, use the following pin mapping:\n//char ledPins[] = {D1, D3, D0, D8};\n//char buttonPins[] = {D4, D5, D6, D7};\n//#define SPEAKER_PIN D2\n\n#define MAX_GAME_LENGTH 100\n\nint gameTones[] = { NOTE_G3, NOTE_C4, NOTE_E4, NOTE_G5};\n\n/* Global variales - store the game state */\nbyte gameSequence[MAX_GAME_LENGTH] = {0};\nbyte gameIndex = 0;\n\n/**\n   Set up the Arduino board and initialize Serial communication\n*/\nvoid setup() {\n  Serial.begin(9600);\n  for (int i = 0; i \u003c 4; i++) {\n    pinMode(ledPins[i], OUTPUT);\n    pinMode(buttonPins[i], INPUT_PULLUP);\n  }\n  pinMode(SPEAKER_PIN, OUTPUT);\n  // The following line primes the random number generator. It assumes pin A0 is floating (disconnected)\n  randomSeed(analogRead(A0));\n}\n\n/**\n   Lights the given led and plays the suitable tone\n*/\nvoid lightLedAndPlaySound(byte ledIndex) {\n  digitalWrite(ledPins[ledIndex], HIGH);\n  tone(SPEAKER_PIN, gameTones[ledIndex]);\n  delay(300);\n  digitalWrite(ledPins[ledIndex], LOW);\n  noTone(SPEAKER_PIN);\n}\n\n/**\n   Plays the current sequence of notes that the user has to repeat\n*/\nvoid playSequence() {\n  for (int i = 0; i \u003c gameIndex; i++) {\n    char currentLed = gameSequence[i];\n    lightLedAndPlaySound(currentLed);\n    delay(50);\n  }\n}\n\n/**\n    Waits until the user pressed one of the buttons, and returns the index of that button\n*/\nbyte readButton() {\n  for (;;) {\n    for (int i = 0; i \u003c 4; i++) {\n      byte buttonPin = buttonPins[i];\n      if (digitalRead(buttonPin) == LOW) {\n        return i;\n      }\n    }\n    delay(1);\n  }\n}\n\n/**\n  Play the game over sequence, and report the game score\n*/\nvoid gameOver() {\n  Serial.print(\"Game over! your score: \");\n  Serial.println(gameIndex - 1);\n  gameIndex = 0;\n  delay(200);\n  // Play a Wah-Wah-Wah-Wah sound\n  tone(SPEAKER_PIN, NOTE_DS5);\n  delay(300);\n  tone(SPEAKER_PIN, NOTE_D5);\n  delay(300);\n  tone(SPEAKER_PIN, NOTE_CS5);\n  delay(300);\n  for (int i = 0; i \u003c 200; i++) {\n    tone(SPEAKER_PIN, NOTE_C5 + (i % 20 - 10));\n    delay(5);\n  }\n  noTone(SPEAKER_PIN);\n  delay(500);\n}\n\n/**\n   Get the user input and compare it with the expected sequence. If the user fails, play the game over sequence and restart the game.\n*/\nvoid checkUserSequence() {\n  for (int i = 0; i \u003c gameIndex; i++) {\n    char expectedButton = gameSequence[i];\n    char actualButton = readButton();\n    lightLedAndPlaySound(actualButton);\n    if (expectedButton == actualButton) {\n      /* good */\n    } else {\n      gameOver();\n      return;\n    }\n  }\n}\n\n/**\n   Plays an hooray sound whenever the user finishes a level\n*/\nvoid levelUp() {\n  tone(SPEAKER_PIN, NOTE_E4);\n  delay(150);\n  tone(SPEAKER_PIN, NOTE_G4);\n  delay(150);\n  tone(SPEAKER_PIN, NOTE_E5);\n  delay(150);\n  tone(SPEAKER_PIN, NOTE_C5);\n  delay(150);\n  tone(SPEAKER_PIN, NOTE_D5);\n  delay(150);\n  tone(SPEAKER_PIN, NOTE_G5);\n  delay(150);\n  noTone(SPEAKER_PIN);\n}\n\n/**\n   The main game loop\n*/\nvoid loop() {\n  // Add a random color to the end of the sequence\n  gameSequence[gameIndex] = random(0, 4);\n  gameIndex++;\n\n  playSequence();\n  checkUserSequence();\n  delay(300);\n\n  if (gameIndex \u003e 0) {\n    levelUp();\n    delay(300);\n  }\n}\n```\n``` cpp             -piches.h\n/*************************************************\n * Public Constants\n *************************************************/\n\n#define NOTE_B0  31\n#define NOTE_C1  33\n#define NOTE_CS1 35\n#define NOTE_D1  37\n#define NOTE_DS1 39\n#define NOTE_E1  41\n#define NOTE_F1  44\n#define NOTE_FS1 46\n#define NOTE_G1  49\n#define NOTE_GS1 52\n#define NOTE_A1  55\n#define NOTE_AS1 58\n#define NOTE_B1  62\n#define NOTE_C2  65\n#define NOTE_CS2 69\n#define NOTE_D2  73\n#define NOTE_DS2 78\n#define NOTE_E2  82\n#define NOTE_F2  87\n#define NOTE_FS2 93\n#define NOTE_G2  98\n#define NOTE_GS2 104\n#define NOTE_A2  110\n#define NOTE_AS2 117\n#define NOTE_B2  123\n#define NOTE_C3  131\n#define NOTE_CS3 139\n#define NOTE_D3  147\n#define NOTE_DS3 156\n#define NOTE_E3  165\n#define NOTE_F3  175\n#define NOTE_FS3 185\n#define NOTE_G3  196\n#define NOTE_GS3 208\n#define NOTE_A3  220\n#define NOTE_AS3 233\n#define NOTE_B3  247\n#define NOTE_C4  262\n#define NOTE_CS4 277\n#define NOTE_D4  294\n#define NOTE_DS4 311\n#define NOTE_E4  330\n#define NOTE_F4  349\n#define NOTE_FS4 370\n#define NOTE_G4  392\n#define NOTE_GS4 415\n#define NOTE_A4  440\n#define NOTE_AS4 466\n#define NOTE_B4  494\n#define NOTE_C5  523\n#define NOTE_CS5 554\n#define NOTE_D5  587\n#define NOTE_DS5 622\n#define NOTE_E5  659\n#define NOTE_F5  698\n#define NOTE_FS5 740\n#define NOTE_G5  784\n#define NOTE_GS5 831\n#define NOTE_A5  880\n#define NOTE_AS5 932\n#define NOTE_B5  988\n#define NOTE_C6  1047\n#define NOTE_CS6 1109\n#define NOTE_D6  1175\n#define NOTE_DS6 1245\n#define NOTE_E6  1319\n#define NOTE_F6  1397\n#define NOTE_FS6 1480\n#define NOTE_G6  1568\n#define NOTE_GS6 1661\n#define NOTE_A6  1760\n#define NOTE_AS6 1865\n#define NOTE_B6  1976\n#define NOTE_C7  2093\n#define NOTE_CS7 2217\n#define NOTE_D7  2349\n#define NOTE_DS7 2489\n#define NOTE_E7  2637\n#define NOTE_F7  2794\n#define NOTE_FS7 2960\n#define NOTE_G7  3136\n#define NOTE_GS7 3322\n#define NOTE_A7  3520\n#define NOTE_AS7 3729\n#define NOTE_B7  3951\n#define NOTE_C8  4186\n#define NOTE_CS8 4435\n#define NOTE_D8  4699\n#define NOTE_DS8 4978\n```\n@AVR8js.project( ,sketch.ino,pitches.h)\n\n\n## Implementation\n\nThe code below shows the implementation of the two macros. The `@AVR8js.project`\ndefines the main functionality, while `@AVR8js.sketch` only sets the default\nfile name and passes the id of the HTML-elmenent that contains all connected\nwokwi-webcomponents.\n\nThis template utilizes a global AVR8js object that is currently defined in\n`src/index.ts`. Code is compiled on https://hexi.wokwi.com/build and send back\nfor execution.\n\n``` html\n@@script:   https://cdn.jsdelivr.net/gh/liatemplates/avr8js@0.0.7/dist/index.js\n\nscript:   dist/index.js\n\n@AVR8js.sketch: @AVR8js.project(@0,sketch.ino)\n\n@AVR8js.project\n\u003cscript\u003e\nlet id = \"@0\"\n\nlet name = [\n  \"@1\", \"@2\", \"@3\", \"@4\", \"@5\", \"@6\", \"@7\", \"@8\", \"@9\"\n  ]\n  .map((e) =\u003e e.trim())\n  .filter((e) =\u003e { return (e[0] !== '@' \u0026\u0026 e !== \"\") })\n\nlet content = [\n  `@input(0)`,\n  `@input(1)`,\n  `@input(2)`,\n  `@input(3)`,\n  `@input(4)`,\n  `@input(5)`,\n  `@input(6)`,\n  `@input(7)`,\n  `@input(8)`,\n  `@input(9)`\n  ]\n\nlet sketch;\nlet files = []\n\nfor(let i=0; i\u003cname.length; i++) {\n  if (name[i] == \"sketch.ino\") {\n    sketch = content[i]\n  } else {\n    files.push({name: name[i], content: content[i]})\n  }\n}\n\nAVR8js.build(sketch, files)\n   .then((e) =\u003e {\n     if (e.hex === \"\") {\n       let msgs = []\n\n       for(let i = 0; i\u003cname.length; i++) {\n         msgs.push([])\n       }\n\n       let iter = e.stderr.matchAll(/(\\w+\\.\\w+):(\\d+):(\\d+): ([^:]+):(.+)/g)\n\n       for(let err=iter.next(); !err.done; err=iter.next()) {\n         msgs[name.findIndex((e) =\u003e e==err.value[1])].push({\n           row :    parseInt(err.value[2]) - 1,\n           column : parseInt(err.value[3]),\n           text :   err.value[5],\n           type :   err.value[4]\n         })\n       }\n       send.lia(e.stderr, msgs, false)\n       send.lia(\"LIA: stop\")\n     }\n     else {\n       console.debug(e.stdout)\n\n       if (e.hex) {\n         let runner = AVR8js.execute(e.hex, console.stream, id)\n\n         send.handle(\"input\", (input) =\u003e {\n            runner.serial(input.slice(0, -1))\n         })\n\n         send.lia(\"LIA: terminal\")\n\n         send.handle(\"stop\", e =\u003e {\n           if(runner) {\n             runner.stop()\n             runner = null\n             console.debug(\"execution stopped\")\n           }\n         })\n       } else {\n         send.lia(\"LIA: stop\")\n       }\n     }\n   })\n\"LIA: wait\"\n\u003c/script\u003e\n\n@end\n\n@AVR8js.asm\n\u003cscript\u003e\nlet id = \"@0\"\n\nAVR8js.buildASM(`@input`)\n   .then((e) =\u003e {\n     if (e.hex === \"\") {\n       let msgs = []\n\n       let iter = e.stderr.matchAll(/main\\.s:(\\d+):(\\d+): ([^:]+):(.+)/g)\n\n       for(let err=iter.next(); !err.done; err=iter.next()) {\n         msgs.push({\n           row :    parseInt(err.value[1]) - 1,\n           column : parseInt(err.value[2]),\n           text :   err.value[4],\n           type :   err.value[3].toLower()\n         })\n       }\n       send.lia(e.stderr, [msgs], false)\n       send.lia(\"LIA: stop\")\n     }\n     else {\n       console.debug(e.stdout)\n\n       if (e.hex) {\n         let runner = AVR8js.execute(e.hex, console.stream, id)\n\n         send.handle(\"input\", (input) =\u003e {\n            runner.serial(input.slice(0, -1))\n         })\n\n         send.lia(\"LIA: terminal\")\n\n         send.handle(\"stop\", e =\u003e {\n           if(runner) {\n             runner.stop()\n             runner = null\n             console.debug(\"execution stopped\")\n           }\n         })\n       } else {\n         send.lia(\"LIA: stop\")\n       }\n     }\n   })\n\"LIA: wait\"\n\u003c/script\u003e\n\n@end\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliatemplates%2Favr8js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliatemplates%2Favr8js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliatemplates%2Favr8js/lists"}