• Stars
    star
    122
  • Rank 292,031 (Top 6 %)
  • Language
    Nim
  • Created almost 3 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ratel

Next-generation, zero-cost abstraction microconroller programming in Nim

Getting started

Getting started with Ratel is fairly simply. First you need to have Nim installed along with the toolchain for the controller you want to compile for. This guide is written with the Arduino Uno in mind, so for that board this would be the AVR toolchain. If you have written code for Arduino before chances are good you already have these tools installed. Once that is done you need to install Ratel itself:

nimble install ratel

If your board is not included in the official distribution you need to grab support for that as well. You should be able to search for all Ratel based packages in the package directory. In order to compile and build for your board you will also need the toolchain to compile C code and upload that to your board as well. The details of this should be found with the board support library.

Once you have Ratel and your board support installed you need to set up your project. In this tutorial we'll be building for an Arduino Uno, but this process is pretty much the same no matter the board. Simply create a config.nims file in your folder with the content:

import boardConf
board "unor3"

--avr.any.gcc.path: "/usr/bin"

This does a couple of things, starting with including the boardConf module from Ratel. Then it calls board which tells Ratel that the board we want to build for is the Arduino Uno rev. 3. This will automatically include a set of sane defaults for compiling for this micro-controller, along with some procedures that we'll get back to later.

The last three lines are simply telling Nim where to find the specific compiler we need to use for the CPU/OS combination we're using. These could also be placed in your global configuration as they will only be applied when compiling for these platforms.

Writing our code

Now that our project is all set up we need to write some code, the sample from the front page is a good start. Simply save the following code in a file with the .nim extension named the same as the folder it's in.

import board
import board / [times, serial, progmem]

Serial.init(9600.Hz)
Serial.send p"Hello world\n"
Led.output()

while true:
  Led.high()
  Serial.send p"Led is on\n"
  delayMs(1000)
  Led.low()
  Serial.send p"Led is off\n"
  delayMs(1000)

To write your own code have a browse through the documentation and check out any Ratel modules in Nimble.

Compiling and uploading

With the project set up and the code written it is time to compile. When we imported the board configuration earlier we got some tasks for doing this loaded into our configuration. To run these we use the ratel binary. So to build simply run:

ratel build

If you missed the sentence earlier about putting your file in a folder of the same name this will fail, but fret not, simply pass the file to build with the -f flag. This command should have created a binary file in the same folder with the same name as your file but without the extension. In order to check how big the resulting binary is we can simply run:

ratel size

Or if you're of the curious kind:

ratel sizeDetails

The size breakdown you get from this should be familiar to you if you have done any kind of programming with Arduino, it's the same one which is written out in the terminal before uploading. It should look something like this:

AVR Memory Usage
----------------
Device: atmega328p

Program:     298 bytes (0.9% Full)
(.text + .data + .bootloader)

Data:          0 bytes (0.0% Full)
(.data + .bss + .noinit)

Now the final step of the process is to upload our code to the controller. You can of course do this manually with avrdude but Ratel comes with a task for this as well:

ratel upload --device=/dev/ttyACM0

For me the board is connected to the USB port at /dev/ttyACM0 but this might be different for you.

And that should be it! Your board should now be flashing an LED and printing to the serial terminal. To view the output you can either use the serial terminal that comes with Arduino if you have that installed, or you can use a number of terminal applications. The easiest might even be to run tail -f /dev/ttyACM0.

More Repositories

1

nimlsp

Language Server Protocol implementation for Nim
Nim
410
star
2

futhark

Automatic wrapping of C headers in Nim
Nim
341
star
3

protobuf-nim

Protobuf implementation in pure Nim that leverages the power of the macro system to not depend on any external tools
Nim
166
star
4

nimcr

Nim
81
star
5

badger

Keyboard firmware written from scratch using Nim
Nim
73
star
6

binaryparse

Binary parser for Nim
Nim
69
star
7

macroutils

A package that makes creating macros easier
Nim
59
star
8

nancy

Nancy - Nim fancy ANSI tables
Nim
53
star
9

nim-playground-frontend

The front-end for https://play.nim-lang.org
Nim
44
star
10

jsonschema

Schema validation of JSON for Nim
Nim
42
star
11

nim-optionsutils

Utility macros for easier handling of options in Nim
Nim
35
star
12

superlog

Nim
28
star
13

i3ColourChanger

Python
28
star
14

termstyle

Easy to use styles for terminal output
Nim
28
star
15

nim-persistent-vector

Implementation of Clojures persistent vector in Nim for easy immutable lists.
Nim
25
star
16

SDLGamelib

A library of functions to make creating games using Nim and SDL2 easier. This does not intend to be a full blown engine and tries to keep all the components loosely coupled so that individual parts can be used separately.
Nim
25
star
17

notifishower

Nim
21
star
18

nim-electron

Nim
18
star
19

notificatcher

Simple program to read freedesktop notifications and format them as strings
Nim
18
star
20

Configuration

Shell
18
star
21

combparser

A parser combinator library for easy generation of complex parsers
Nim
18
star
22

strslice

Nim
17
star
23

nim-pcap

Tiny pure Nim library to read PCAP files
Nim
16
star
24

labeltry

A new approach to dealing with exceptions
Nim
16
star
25

drawille-nim

Drawing in terminal with Unicode Braille characters. This is the Nim version of the Python original.
Nim
16
star
26

plotter

Simple tool to plot input piped to it
Nim
14
star
27

ikeahomesmart

IKEA Home Smart library for Nim
Nim
13
star
28

libkeepass

Library for reading KeePass files and decrypt the passwords within it
Nim
12
star
29

autotemplate

Nim
12
star
30

nim-cache

Simple cache module for Nim, supports LRU and max-count pruning
Nim
12
star
31

genui

This is what might become a really kick-ass cross-platform native UI toolkit
Nim
10
star
32

getmac

Package to get MAC addresses from an IP address in Nim
Nim
9
star
33

ansiparse

Nim library to parse ANSI escape codes
Nim
9
star
34

xevloop

Library to more easily create X11 event loops
Nim
9
star
35

aoc2021

Advent of Code 2021 solutions in Nim
Nim
9
star
36

stacklang

A stack based calculator/minimal language
Nim
8
star
37

gtkgenui

The genui DSL for creating GUIs implemented for the gtk2 bindings in nimble.
Nim
7
star
38

nimscriptexamples

Examples for my article on embedding NimScript
Nim
7
star
39

nimbleutils

A Nimble package to inspect Nimble packages
Nim
7
star
40

mapm-nim

Matt's Arbitrary Precision Math library - Nim wrapper
Nim
6
star
41

deriveables

Deriveable types in Nim
Nim
6
star
42

ratel-bme280

BME280 implementation for Ratel
Nim
6
star
43

libcoap

Nim wrapper for libcoap
Nim
6
star
44

skeletal

HTML
6
star
45

gtk3genui

The genui DSL for creating GUIs implemented for the gtk3 bindings by StefanSalewski/ngtk3.
Nim
6
star
46

libfuse-nim

Nim
5
star
47

nimtours

A tour of Nim, in multiple parts. Available online at https://play.nim-lang.org/tours/index.html
HTML
5
star
48

MannBarSchwein-arduboy

Arduboy game jam game
C++
4
star
49

aoc2020

Nim
4
star
50

Imlib2

Nim
4
star
51

termfm

Nim
4
star
52

aoc2022

Nim
4
star
53

webexperiment

Nim
3
star
54

TromsoGameJam2017

Nim
3
star
55

fibers

Fibers in Nim, experiment
Nim
3
star
56

zhangshasha

This module is a port of the Java implementation of the Zhang-Shasha algorithm for tree edit distance
Nim
3
star
57

statusbar

Libfuse based statusbar for Nimdow and other xsetroot -name WMs
Nim
3
star
58

ansitohtml

Converts ANSI colour codes to HTML span tags with style tags
Nim
2
star
59

conf.nim-lang.org

Simple conference information page for NimConf2020
CSS
2
star
60

nim-coroutines

A simple coroutines library for Nim based in iterators, untested and mostly for experimentation
Nim
2
star
61

ArcticGameJam2014

JavaScript
2
star
62

libbuilder

Tool to create condensed Nim standard libraries for NimScript integration
Nim
2
star
63

nim-homeassistant

Nim
1
star
64

aoc2023

Nim
1
star
65

echooverride

Simple test to override the Nim standard output in the entire project
Nim
1
star
66

colourfinder

Create nice spectrum images of colours
Nim
1
star
67

femtozip

Nim
1
star
68

pangoterm-altfonts

A copy of the original pangoterm found here: https://www.leonerd.org.uk/code/pangoterm/ but with improved support for alternate fonts
C
1
star
69

data.vm

Experiment into a data based VM
Nim
1
star