Monday, January 1, 2018

Groovy Vert.x in Intellij : Getting started

Vert.x :very interesting, language agnostic, verticles, async, fast.

 So as part of my ongoing project to run Vert.x on my Raspberry Pi (and in general learn about it). I decided to get the tutorial working with Groovy.

I read the first section and started coding once we moved to the Verticles which is what I am most interested in.

Easiest way to get started was to download the Gradle Groovy Verticle example from github. You have to actually download the entire samples project though.

Import into Intellij as new project, using Gradle.

Run the Gradle task runShadow

 http://localhost:8080

And there is is, a working verticle. No apache installation needed, everything is included in the shadow Jar.

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?



Monday, October 31, 2016

Spam email

Sometime spam email is so bad it is pure comedy genius. The amount of things wrong with this opening statement.

It is my pleasure to inform you that Diplomatic Agent Terry has arrive in yournearest Airport today with your package worthy $5.5million you have to contacthim at this email address (diplomaticagentfredjames33@gmail.com

Wednesday, October 26, 2016

Side effects with not having decent UNIT tests

This is not Grails/Groovy specific but in general.

Grails/Groovy is certainly more reliant on tests, as it is dynamically typed, so code needs to be executed to pickup errors. The Java world seems to rely on static typing while Groovy on tests. Tests are always better than typing, period.

Besides the obvious, if it is not tested how do you know it works? There are some interesting side effects I see with poorly unit tested code.

Makes your Code Better

  • Having to write unit tests for a class/method makes your code structurally better.
  • It leads to better separation of concerns. 
  • Starts enforcing the law of Demeter. 
  • Easily tested code is generally easier to understand. 


Documents Code

  • Documentation falls out of date and quickly becomes irrelevant. Properly written tests (Spock helps) is a great way to document code. 


Makes changing existing code possible

  • I cannot change existing if I have no idea if it works, or how it works. 
  • In larger systems knowing if existing code works without tests becomes increasingly difficult.
  • It can be very difficult to execute see what code is doing if you need to stand up the whole system just to test a method.


Quicker to Change and Safer to Change

  • If your code has a unit test you can execute it immediately.
  • You can see changes, and what they do to existing tests immediately
  • 100's of times an hour.

Tuesday, October 25, 2016

Intellij Color Schemes

Found a neat little site with colour schemes for Intellij

http://color-themes.com/?view=index

Once you have downloaded the jar, just go to File->import settings and load that jar.

Friday, October 14, 2016

Grails3 GORM running MongoDB

Had some issues getting Mongo to work with Grails3 based on this doc, ended up creating an example project of it all working

Feel free to download, have a look