Dead Simple


While playing around with neoPixels the other day, someone commented that the code for driving the display was "dead simple".  I kind of agree.  Simplicity is one of the core principles of the microMighty platform, and has always been a driving factor for the implementation of the interfaces.

I know that I've always asked myself if there is something that I thought I "should" be able to do, and then asked myself if it could be done better.  I'd like to belive that all that hard work has ultimately led to improving the functionality while maintaining an elegant simplicity that prevents people from having to read the 2000 page API reference manual. Okay, it's only 1967 pages long, but it's still a little more than I'd like to read just to get something done!

Since I think the code is pretty simple, here's how I drove a neoPixel ring from the microMighty in just about 57 lines of code...

function ledFun(pixPin)
    local pixBuf = {}
    local cmap = {0x800000, 0x008000, 0x000080, 0x808000, 0x008080, 0x800080}
    local color = {0,0,0}
    local c1 = 1
    local c2 = 2
    local p1 = 1
    local p2 = 24
    -- define the pixel buffer elements and zero the buffer
    for p=1,24 do pixBuf[p] = 0 end,pin.mode.OUTPUT)
    -- loop forever updating the display, or until a key is pressed
    -- in the VCP terminal connection.
        for p=1,24 do
            -- split the pixel into RGB color values
            color[1] = (pixBuf[p]>>8)&0xFF
            color[2] = (pixBuf[p]>>16)&0xFF
            color[3] = (pixBuf[p])&0xFF
            -- Fade the pixel
            for c=1,3 do
                color[c] = math.floor(color[c] * 0.8)
            -- rebuild the pixel color from the {RGB} value
            pixBuf[p] = pin.neopixelFormat(color[1],color[2],color[3])
            -- When the present pixel is the pixel to be updated...
            if p1 == p then
                pixBuf[p] = pixBuf[p] | cmap[c1]
            if p2 == p then
                pixBuf[p] = pixBuf[p] | cmap[c2]
        -- move the pixel to be drawm
        p1 = p1 + 1
        if p1 > #pixBuf then
            p1 = 1
            c1 = cpu.rand(1,#cmap)
        p2 = p2 - 1
        if p2 <= 0 then
            p2 = 24
            c2 = cpu.rand(1,#cmap)
        -- draw the pixel buffer
        -- wait for some time to make the display fluid
    until serial.available(0) > 0
    -- clean the character from the buffer so it doesn't appear in the REPL

... And someday when I figure out how to use syntax hilighting, there'll be syntax higlights too   !(o_O)!