(Hacked) mruby/c can work on Arduino Uno

2019-04-20Fanzine, TechArduino, c, English, mruby, mruby/c

As you know, Arduino Uno has only 2KB RAM and 32KB ROM. It is fine for simple project written by C or C++. However it is generally impossible to run major script languages.
I thought it may be possible to run mruby/c on Arduino.
I checked my mruby/c library for ESP32, then found despair…

ROM and RAM requirement
ROM and RAM requirement

I decided to hack mruby/c to run itself on Arduino Uno.

Basic strategy

  • Footprint first
    Performance doesn’t matter.
  • Limit functionality
    Some basic functionality will not be implemented in order to reduce ROM and RAM. (I found a new question, “What makes Ruby?”)
  • Reduce redundancy
    Of course, redundancy shall be removed.
  • Utilize ROM as much as possible
    Any data on RAM shall be examined for the possibility to move to ROM using PROGMEM.

Result

Finally I did it.

ROM and RAM usage
ROM and RAM usage

I can execute following Ruby code. I implemented the embedded Arduino class for testing.


puts "LED Test"
pin = 13
Arduino.pin_mode(pin,:OUTPUT)
st = :LOW
while(true) do
    if(st==:LOW)
        st=:HIGH
    else
        st=:LOW
    end
    Arduino.digital_write(pin,st)
    Arduino.delay(500)
end
micro mruby on Arduino Uno
micro mruby on Arduino Uno

(mruby/c provides subset functions of mruby. My micro mruby provides subset functions of mruby/c. I have no idea if I can say “This is Ruby!” or not…)

Source code is here.
https://github.com/kishima/micro_mruby_for_arduino_uno

My first fanzine(Doujinshi)

I wrote a fanzine – I don’t know a good word for Doujinshi in English – for the TechBookFest5(技術書典5), to describe detail of my idea with some sample code sets. I also introduced and explained mruby and mruby/c for micro processor boards.
I offered a cover picture to my friend. He provided great one! I really appreciate to him.

It was brilliant experience to write a book and sell it by myself. I think this is the best idea to learn something tech by writing a tech book.

If you want to know detail explanation and you can read Japanese, you can buy my fanzine pdf on BOOTH. Now it is available on Amazon!