# Non-python API for Blinkt device

**URL:** <https://forums.pimoroni.com/t/non-python-api-for-blinkt-device/3453>\
**Category:** Raspberry Pi\
**Created:** [November 30, 2016, 8:38pm UTC](https://forums.pimoroni.com/t/non-python-api-for-blinkt-device/3453 "2016-11-30T20:38:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdonth](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jdonth](https://forums.pimoroni.com/u/jdonth)\
**Post date:** [November 30, 2016, 8:38pm UTC](https://forums.pimoroni.com/t/non-python-api-for-blinkt-device/3453/1 "2016-11-30T20:38:51Z")

</div>

I’m not a python user but want to use the Blinkt module. Is there a command line program, or library with defined API that I can call from other languages like PHP or Pascal? Free Pascal is my go-to language and would be my first choice.

If nothing is available, any documentation the would enable me to create a FPC module? I’d be glad to share the results.

Thanks,  
Joe

---

<div class="post-metadata">

**Author:** ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)\
**Post date:** [December 1, 2016, 7:51am UTC](https://forums.pimoroni.com/t/non-python-api-for-blinkt-device/3453/2 "2016-12-01T07:51:29Z")

</div>

There’s no hardware documentation as such, but the Blinkt! is just a chain of 8 APA102 LED pixels, albeit ones with a particularly quirky implementation of the protocol, which can be driven by just toggling a couple of GPIO pins (data and clock).

It would be easy to, for example, create a C library you could bind into. I started with C for Blinkt!, intending to bind against it with Python, until I realized that it was simply overkill for most uses.

Anyway. The Blinkt! protocol is relatively straight forward. It’s just like driving a shift register, toggling the clock line and setting data bits in turn.

It comprises 8 APA102 pixels, the protocol of which is explored here: [https://cpldcpu.com/2014/08/27/apa102/](https://cpldcpu.com/2014/08/27/apa102/)

The data line is connected to BCM `23` and the clock line to BCM `24`.

The pixels are 24bits, 8bits per colour and include an 8bit “start frame” that has a 5bit brightness value for a total of 32bits per pixel:

```nohighlight
   start blue green red
   111 00000 00000000 00000000 00000000
           ^
           5-bit brightness value

```

All of the important stuff happens here: [https://github.com/pimoroni/blinkt/blob/master/library/blinkt.py#L62-L79](https://github.com/pimoroni/blinkt/blob/master/library/blinkt.py#L62-L79)

Here’s that chunk with just the interesting bits of code:

```python
def show():
    """Output the buffer to Blinkt!"""
    _sof()

    for pixel in pixels:
        r, g, b, brightness = pixel
        _write_byte(0b11100000 | brightness)
        _write_byte(b)
        _write_byte(g)
        _write_byte(r)

    _eof()

```

For each pixel, you need to clock out the pixel start frame which is `0b11100000` plus a brightness value from 0 to 31.

Then the b, g and r values are clocked out in the same way.

`_write_byte()` does simply that, it outputs each bit of a byte to the data line and pulses the clock: [https://github.com/pimoroni/blinkt/blob/master/library/blinkt.py#L41-L46](https://github.com/pimoroni/blinkt/blob/master/library/blinkt.py#L41-L46)

It does this by clocking out the most significant bit of the byte and shifting that byte left once for each bit.

```python
def _write_byte(byte):
    for x in range(8):
        GPIO.output(DAT, byte & 0b10000000)
        GPIO.output(CLK, 1)
        byte <<= 1
        GPIO.output(CLK, 0)

```

`_sof()` and `_eof()` stand for Start Of Frame and End Of Frame, these are the data bits that reset and latch the APA102 pixels: [https://github.com/pimoroni/blinkt/blob/master/library/blinkt.py#L50-L60](https://github.com/pimoroni/blinkt/blob/master/library/blinkt.py#L50-L60)

```auto
def _eof():
    GPIO.output(DAT, 0)
    for x in range(36):
        GPIO.output(CLK, 1)
        GPIO.output(CLK, 0)

def _sof():
    GPIO.output(DAT,0)
    for x in range(32):
        GPIO.output(CLK, 1)
        GPIO.output(CLK, 0)

```

In this case, they’re just setting the data line and pulsing the clock. `_eof()` pulses the clock 36 times, clocking out 36 zeros. `_sof()` pulses the clock 32 times, clocking out 32 zeros.

So to summarize, in order to drive Blintk! you should:

- Clock out 36 zeros
- Clock out 8 pixels, for each pixel:
  - Clock out start frame with brightness from 0 to 31: `0b11100000 | brightness`
    - Bytes are clocked out most-significant-bit (the left-most bit) first

  - Clock out blue byte
  - Clock out red byte
  - Clock out green byte

- Clock out 32 zeros

---

<div class="post-metadata">

**Author:** ![jdonth](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jdonth](https://forums.pimoroni.com/u/jdonth)\
**Post date:** [December 1, 2016, 2:27pm UTC](https://forums.pimoroni.com/t/non-python-api-for-blinkt-device/3453/3 "2016-12-01T14:27:53Z")

</div>

Phil,

Thank you for your detailed response. I’m going to give it a try.

---

<div class="post-metadata">

**Author:** ![richrarobi](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richrarobi](https://forums.pimoroni.com/u/richrarobi)\
**Post date:** [October 12, 2018, 3:47pm UTC](https://forums.pimoroni.com/t/non-python-api-for-blinkt-device/3453/5 "2018-10-12T15:47:32Z")

</div>

This is an old thread, and I don’t know if this is still needed, but … (and I am still working with this) …

> **[richrarobi/mangolink](https://github.com/richrarobi/mangolink)**
>
> request / reply with nanomsg / mangos. Contribute to richrarobi/mangolink development by creating an account on GitHub.

The link above has info on my go interfaces to the Blinkt,and my method for remotely running them across a LAN. All in go.

The latest example has mreply\_a.go and mreq.go showing remote access of Blinkt AND control of the pantilt hat via across the local network.
