How to create programs for Windows in Arduino

How to create programs for Windows in Arduino

One day I had the crazy idea to bring 500 laser pointers in one place. I spent a lot of time and did it. It turned out spectacular and useless, but I liked it. Six months ago I had another crazy idea. This time, not at all spectacular, but much more useful. I also spent a lot of time on it. And in this article, I present a beta version of my second crazy idea.

I called the project Nanonyam (Nanonyam) and even came up with a logo for it (I drew for 5 minutes).

How to create programs for Windows in Arduino

For those who think in terms of Arduino, we can say that Nanonyam is a virtual Arduino shield for controlling Windows.

In other words, Nanonyam is a virtual machine that uses firmware for the AVR microcontroller (ATMEGA2560 is recommended) as the bytecode. Inside this virtual machine is an AVR core simulator, but instead of peripherals, which are located at SRAM addresses from 0x0060 to 0x01FF, there is a special interface to virtual functions (including Windows API functions). And here it is very important to understand right away: the code for Nanonyam should not have any access to the specified memory range, so as not to accidentally call, for example, the function of deleting files or formatting a disk. The rest of the SRAM memory range from 0x0200 to 0xFFFF (this is more than in a real microcontroller) is available to the user for any purpose. I note right away that there is a special protection against accidental launch of the firmware of a real microcontroller (or firmware from another architecture): before activating "dangerous" functions, you need to call a special tricky virtual function. There are some other security features as well.

To create programs for Nanonyam, you need to use special libraries that implement all currently available virtual functions. Download the Nanonyam virtual machine and libraries for it You can then. But the virtual function description page. And yes, my site is very primitive and not adapted for mobile devices.

Nanonyam is free for home and commercial use. The Nanonyam program is provided on an "as is" basis. Source code is not provided.

The program is currently in the testing phase. Implemented about 200 virtual functions that allow you to create simple programs for Windows.
Obviously, creating something complicated in such a virtual machine will not work, since the memory for the code is only 256 kB. Data can be stored in separate files, the buffer for the graphic part is implemented externally. All functions are simplified and adapted for 8-bit architecture.

What can you do in Nanonyam? I came up with a few problems.

Development of program blocks

I once needed to design a complex menu for a 128x64 dot graphic display. I really didn't want to constantly load the firmware into a real microcontroller to see how the pixels look. And so the idea of ​​Nanonyam was born. The figure below shows an image from a real OLED display of one of the items on the same menu. Now I can work through it without a real device.

How to create programs for Windows in Arduino

Nanonyam (in its final idea) is a good tool for working out program blocks for microcontrollers, as there are functions for working with graphics (you can simulate displays and indicators), with files (you can make logs, read test data), with a keyboard (you can read up to 10 buttons at the same time), with COM ports (here is a separate item).

Creating Quick Programs

For example, you need to quickly process 100500 text files. Each one needs to be opened, slightly modified according to some simple algorithm, saved and closed. If you are a Python master, then I congratulate you, you have everything. But if you are an hardened arduino (and there are many of them), then Nanonyam will help you in solving this problem. This is my second goal in Nanonyam: to add many useful functions such as text processing, taking screenshots or simulating keystrokes in the system (all of which, by the way, are already there), as well as many other functions for solving routine tasks.

Testing hardware via COM port

Nanonyam can act as a terminal that works according to your algorithm. You can draw a small menu to control the device and display the data received from the port. You can save and read data from files for analysis. A handy tool for simple debugging and calibration of hardware, as well as for creating simple virtual instrument control panels. For students and young scientists, this project can be very useful.

Programming training

However, as with the entire Arduino project, the main usefulness of Nanonyam lies in the simplification of functions, interface and bootloader. Therefore, this project should be of interest to novice programmers and those who are satisfied with the level of arduino. By the way, I myself still have not studied arduino in detail, because I always used WinAVR or AVR Studio, but started with assembler. Therefore, the example program below will be a little wrong, but quite working.

Hello Habr!

It's time to get familiar with some Nanonyam features and write a simple program. We will write in Arduino, but not in the usual way, but in the way I can now (I already said that I have not figured out this environment very well yet). First, create a new sketch and select the Mega2560 board.

How to create programs for Windows in Arduino

Save the sketch to a file and copy next Nanonyam library. It would be correct to include the headers of the libraries, but I don’t know how to write compilation of individual files in Arduino, so for now we’ll just include the libraries directly (and all at once):

#include <stdio.h>
#include "NanonyamnN_System_lib.c"
#include "NanonyamnN_Keyboard_lib.c"
#include "NanonyamnN_File_lib.c"
#include "NanonyamnN_Math_lib.c"
#include "NanonyamnN_Text_lib.c"
#include "NanonyamnN_Graphics_lib.c"
#include "NanonyamnN_RS232_lib.c"

It would be even more correct to make a special module β€œNanonyam for Arduino”, which can be installed directly from Arduino. As soon as I figure it out, I'll do it, but for now I'm just showing the essence of working with a virtual machine. We write the following code:

//Π‘Ρ€Π°Π·Ρƒ послС запуска рисуСм тСкст Π² ΠΎΠΊΠ½Π΅
void setup() {
  sys_Nanonyam();//ΠŸΠΎΠ΄Ρ‚Π²Π΅Ρ€ΠΆΠ΄Π°Π΅ΠΌ ΠΊΠΎΠ΄ Π²ΠΈΡ€Ρ‚ΡƒΠ°Π»ΡŒΠ½ΠΎΠΉ ΠΌΠ°ΡˆΠΈΠ½Ρ‹
  g_SetScreenSize(400,200);//Π—Π°Π΄Π°Ρ‘ΠΌ Ρ€Π°Π·ΠΌΠ΅Ρ€ дисплСя 400Ρ…200 Ρ‚ΠΎΡ‡Π΅ΠΊ
  sys_WindowSetText("Example");//Π—Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ ΠΎΠΊΠ½Π°
  g_ConfigExternalFont(0,60,1,0,0,0,"Arial");//Π—Π°Π΄Π°Ρ‘ΠΌ ΡˆΡ€ΠΈΡ„Ρ‚ Windows Π² ячСйкС ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠ² 0
  g_SetExternalFont(0);//Π’Ρ‹Π±ΠΈΡ€Π°Π΅ΠΌ ячСйку ΡˆΡ€ΠΈΡ„Ρ‚ΠΎΠ² 0 для рисования тСкста
  g_SetBackRGB(0,0,255);//Π¦Π²Π΅Ρ‚ Ρ„ΠΎΠ½Π° синий
  g_SetTextRGB(255,255,0);//Π¦Π²Π΅Ρ‚ тСкста ΠΆΡ‘Π»Ρ‚Ρ‹ΠΉ
  g_ClearAll();//ΠžΡ‡ΠΈΡ‰Π°Π΅ΠΌ экран (Π·Π°Π»ΠΈΠ²ΠΊΠ° Ρ†Π²Π΅Ρ‚ΠΎΠΌ Ρ„ΠΎΠ½Π°)
  g_DrawTextCenterX(0,400,70,"Hello, Habr!");//РисуСм надпись
  g_Update();//Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ графичСский Π±ΡƒΡ„Π΅Ρ€ Π½Π° экран
}

//ΠŸΡ€ΠΎΡΡ‚ΠΎ ΠΆΠ΄Ρ‘ΠΌ закрытия ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹
void loop() {
  sys_Delay(100);//Π—Π°Π΄Π΅Ρ€ΠΆΠΊΠ° ΠΈ Ρ€Π°Π·Π³Ρ€ΡƒΠ·ΠΊΠ° процСссора
}

Sketch with this program can be downloaded here. Detailed description of functions search on the site. I hope the comments in this code are enough to get the gist of it. Here function sys_Nanonyam() plays the role of a "password" for the virtual machine, which removes restrictions on virtual functions. Without this function, the program will close after 3 seconds of operation.

We press the "Check" button and there should not be any errors.

How to create programs for Windows in Arduino

Now you need to get a binary file (firmware). Select the menu "Sketch>>Export binary file (CTRL+ALT+S)Β«.

How to create programs for Windows in Arduino

This will copy two HEX files to the sketch folder. We take only the file without the prefix "with_bootloader.mega".

There are several ways to specify a HEX file to the Nanonyam virtual machine, all of them are described on this page. I suggest to create next to the file Nanonyam.exe file path, in which to register the full path to our HEX file. After which you can run Nanonyam.exe. We get a window with our inscription.

How to create programs for Windows in Arduino

Similarly, you can create programs in other environments, such as AVR Studio or WinAVR.

This is where we finish our acquaintance with Nanonyam. The main idea should be clear. More examples are on the website.. If there are enough people willing to use this project, then I will make more examples and continue to "fill" the virtual function libraries. Concrete ideas for the development of the project and reports of malfunctions, bugs and bugs are accepted. It is advisable to direct them to contacts, indicated on the site. And discussion is welcome in the comments.

Thank you all for your attention and good programming!

Source: habr.com

Add a comment