Sunday, December 31, 2017

Raspberry Pi & Groovy : Morse Code Translator

I got a Raspberry Pi and the first thing I wanted to get working is Groovy programming of the GPIO. Took some fiddling but the eventual code was easy.

I ended up building a simple Mores code generator that outputs text to a led light.

First the wiring for the Pi and Breadboard
First gotcha is that to access the GPIO you need to run the Groovy script as ROOT. So changed to root

sudo su


Install SDKMan to install Groovy.

curl -s "https://get.sdkman.io" | bash


Then I just installed Groovy with

sdk install groovy


The next was to use Pi4j and groovy to do the code. The code is pretty rough, but it works.


@Grapes(
        @Grab(group='com.pi4j', module='pi4j-core', version='1.1')
)

import com.pi4j.io.gpio.*

    println('getting gpio controller')

    GpioController gpio = GpioFactory.getInstance()
    GpioPinDigitalOutput outputPin = gpio.provisionDigitalOutputPin(RaspiPin.getPinByAddress(1))

    def alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
    "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
    "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8",
    "9", "0", " " ]

    def morse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.",
    "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
    "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
    "-.--", "--..", ".----", "..---", "...--", "....-", ".....",
    "-....", "--...", "---..", "----.", "-----", "|"]

    def say = "SOS SOS SOS SOS"
    
    def dotTime = 250
    def dashTime = 750
    def between = 500
    def space = 1000

    def outputLed = {time->
        println("dot")
        outputPin.setState(true)
        sleep(time)
        outputPin.setState(false)
        sleep(between)
    }

    def doLetter = {letter-> 
        def index = alpha.findIndexOf {it.equalsIgnoreCase(letter)}
        def morseCode = morse.get(index)
        println "letter: ${letter} = ${morseCode}"
        
        morseCode.each {bit->
            println("bit ${bit}")
            if (bit.equals('.')) {
                outputLed(dotTime)
            } else if (bit.equals('-')) {
                outputLed(dashTime)
            } else if (bit.equals('|')) {
                sleep(space)
            }
        }
    }

println "Saying: ${say}"
outputPin.setState(false)
say.each {letter->
    doLetter(letter)
}
outputPin.setState(false)
println 'done'

gpio.shutdown()


Then just run the above script with
groovy [file].groovy


But wait, you probably will get this error.
Unable to determine hardware version. I see: Hardware : BCM2835


This I had to fix by reverting my kernel, information was in this thread
But this was the script that reverted the Kernel
sudo rpi-update 52241088c1da59a359110d39c1875cda56496764 

Here is a video of it doing SOS SOS SOS.




Monday, May 29, 2017

Parrot Compiler

I posed the question of what the importance of the Parrot Compiler groovy is (ELI5) on the groovy slack channel http://groovy-community.slack.com.
And got an answer direct from Guillaume Laforge

the syntax of Groovy hasn’t evolved in a long time
the current / old parser is a bit complicated to evolve
and is using a very old version of the parsing library
so any change we’d want to make to the language (a new operator, for example) becomes very complicated
So we’ve been wanting to upgrade the underlying parser library for a while, but since the library evolved a lot, that also required a rewrite of the grammar of the language
But there’s another thing to consider
Groovy’s always been adopted by Java developers easily because of how close to the Java syntax it’s always been
so most Java programs are also valid Groovy programs
it’s been important to Groovy’s success to have this source compatibility
Java 8's been out for a while already
and we’ve been asked countless times if we’d support this or that particular syntax enhancement from Java 8
for “copy’n paste compatibility”, if you will
We decided to upgrade to a newer version of our parsing library (from v2 to v4 of Antlr)
to allow Groovy’s syntax to continue to evolve
to also support new operators and things like that
but to also support Java 8 constructs, for continued compatibility
And that’s about it :slightly_smiling_face:
Would a 5yo understand my explanation?