Blinkt with wiringPiΒΆ

Sample code driving the Pimoroni blinkt with wiringPi.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// C+- code: C++ constructs but more of a C style of coding
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
// build with: gcc wpblinkt.cc -o wpblinkt -lwiringPi
////////////////////////////////////////////////////////////////////////////////////////////////////
// Boilerplate - things I'm used to...
typedef unsigned char      u8;
typedef unsigned short     u16;
typedef unsigned long      u32;
typedef unsigned long long u64;
typedef float              f32;
typedef double             f64;
static_assert(sizeof(u8)  == 1);
static_assert(sizeof(u16) == 2);
static_assert(sizeof(u32) == 4);
static_assert(sizeof(u64) == 8);
static_assert(sizeof(f32) == 4);
static_assert(sizeof(f64) == 8);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Pimoroni Blinkt uses 2 pins to control LEDs
const int data_pin = 23;
const int clock_pin = 24;
////////////////////////////////////////////////////////////////////////////////////////////////////
u64 nanoseconds()
{
  struct timespec ts;
  clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  return u64(ts.tv_sec) * 1000000000ULL + u64(ts.tv_nsec);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void yieldNano(u64 delta)
{
  u64 now = nanoseconds();
  u64 end = now + delta;
  do
  {
    struct timespec ts, dummy;
    ts.tv_sec = delta / 1000000000ULL;
    ts.tv_nsec = delta % 1000000000ULL;
    nanosleep(&ts,&dummy);
    
    now = nanoseconds();
    delta = end - now;
  } while(end > now);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void xmitbit(int n)
{
  digitalWrite(data_pin, n ? HIGH : LOW);
  yieldNano(100);
  digitalWrite(clock_pin,  HIGH) ;
  yieldNano(100);
  digitalWrite(clock_pin, LOW);
  yieldNano(100);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void write_byte(u8 byte)
{
  int i;
  for (int i = 0; i < 8; ++ i)
    xmitbit((byte >> (7 - i)) & 1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int clamp01_scale_int(f32 x, int i)
{
  if(x < 0.0)
    return 0;
  else if(x > 1.0)
    return 0;
  else
    return (int)(x * i + 0.5);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
  if(argc < 5)
  {
    fprintf(stderr,"wpblinkt needs 4 f32s! Exiting..\n");
    exit(1);
  }
  if(wiringPiSetupGpio() < 0)
  {
    fprintf(stderr,"wiringPi failed to init! Exiting...\n");
    exit(1);
  }

  pinMode (data_pin, OUTPUT) ;
  pinMode (clock_pin, OUTPUT) ;

  // Convert floating point values to range for LEDs
  u8 r = clamp01_scale_int(atof(argv[1]), 255);
  u8 g = clamp01_scale_int(atof(argv[2]), 255);
  u8 b = clamp01_scale_int(atof(argv[3]), 255);
  u8 a = clamp01_scale_int(atof(argv[4]), 31);

  // Sync to start sending; count from Pimoroni
  for(int i = 0; i < 32; ++ i)
    xmitbit(0);

  // Send the same value to all 8 LEDs
  for(int i = 0; i < 8; ++ i)
  {
    write_byte(0b11100000 | a);
    write_byte(b);
    write_byte(g);
    write_byte(r);
  }

  // Sync to finish sending; count from Pimoroni
  for(int i = 0; i < 36; ++i)
    xmitbit(0);

  return 0 ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
  • Blinkt with links to the Blinkt library on github.
  • Me on RPi forums.