Ruby for Microcontrollers?

Hi,

is there a ruby port for a microcontroller like AVR/8051/Nios? It would
be nice to use ruby on the mcu like the java/basic stamp as on the
desktop. Or is there something like the tinyos, only with ruby flavour?

Thanks

Markus

I don’t know about running Ruby on microcontrollers (I think you’d run
into a lot of trouble trying to do that, although it would be cool),
but I regularly use Ruby to write code for microcontrollers. Here is
an example for AVR (which I use at home a lot for hobby kinds of
things–I use this same methodology at work with all sorts of
microcontrollers and microprocessors (8051, ARM7, x86, M68K, etc) in
embedded systems).

Anyway, the nice thing about this kind of methodology is you have
various levels of control:

o Lowest-level: the very low level stuff is written in assembly to use
instructions that can’t be autogenerated (easily) by the uC compiler.
This includes stuff like atomically setting or clearing a bit, reading
ports without resorting to the memory map, etc. Personally, I prefer
using inline assembly in gcc. For example:

namespace avr {
namespace port {
template <Port port, unsigned char bit>
inline void setbit() {
asm (
“sbi %0, %1;”
:
: “I” (port), “I” (bit)
);
}
}
}

o Mid-level: all of the actual code must be compiled by the uC
compiler (in this case, gcc – of course!) to get an exectuable image.
All basic features of the microcontroller are wrapped up into a native
(in this case, C++) function or template library. Up to this point is
how most people proceed with uC development. For example:

namespace avr {
namespace port {
template <Port port, unsigned char bit>
inline void togglebit() {
if (testbit(bit)) clearbit<port,bit>();
else setbit<port,bit>();
}
}
}

However, they usually stop here, and then write all their main
high-level code in C (or C++), calling these library functions. I
rarely ever do that. See below.

o High-level: all of the the tasks and program flow, etc, I write in
Ruby. Ruby doesn’t run on the microcontroller; ruby generates C++
code that is compiled into a native executable. For example, if I
needed to write an embedded app that did something very complex like,
say, toggle an LED every second (ooohhh :wink: on an AVR micro, I might
write something like this:

#!/usr/bin/ruby
require ‘uC/AVR’

AVR.main {
AVR.togglebit(PORTA, 0)
AVR.sleep(1)
}

Which yields, in essense (there is really some startup code, etc), the
following, which can be fed into the native compiler (in this case,
gcc):

#include <avr/avr.hpp>
int main() {
while (true) {
avr::port::togglebit<PORTA,0>();
avr::timer::sleep(1);
}
};

Now, the disclaimer here is of course that almost everything I’ve done
has been pretty ad-hoc (as I am the only person I know that writes uC
code like this) so I don’t have a nice fleshed-out library to hand over
to anybody.

However, if anyone is interested in this kind of methodology for
programming microcontrollers, feel free to either:

  1. Do it yourself ad-hoc; it’s really really easy, and what I’ve
    explained above is pretty much all you need to know. :wink: OR

  2. Let me know if you’re interested in seeing this developped as a
    generic solution any further. I just might do that if I thought it
    would ever be used. =)

···

On Sunday 23 February 2003 10:34 am, El Doed wrote:

Hi,

is there a ruby port for a microcontroller like AVR/8051/Nios? It
would be nice to use ruby on the mcu like the java/basic stamp as on
the desktop. Or is there something like the tinyos, only with ruby
flavour?


Wesley J. Landaker - wjl@icecavern.net
OpenPGP FP: C99E DF40 54F6 B625 FD48 B509 A3DE 8D79 541F F830

Hi,

Wesley J Landaker wrote:

I don’t know about running Ruby on microcontrollers (I think you’d run
Schnip
interesting stuff
Schnap
However, if anyone is interested in this kind of methodology for
programming microcontrollers, feel free to either:

  1. Do it yourself ad-hoc; it’s really really easy, and what I’ve
    explained above is pretty much all you need to know. :wink: O

I try it.

If it is a too big hassle for me I try to get a smal ruby running on
uClinux.

Thanks

Markus