Adafruitの8×8のLED Matrixを購入。I2Cでコントロールするデバイスです。
商品はこちら。
この部品のI2Cのコマンドを叩くためのマニュアルを探したのですが、Arduino用のサンプルコードしか見つかりません。
そこで、そのコードを見ながら、自分のRaspberry Pi環境にかなり強引に移植してみました。内容は全然保証しません。もし変なところを見つけたら教えて下さい。
このコードで、LEDを使ってひらがなの「ま」という文字を出してみたのが以下の写真。
ということで、サンプルコードを以下に示します。
mainのプログラムから、initAda88() を呼んだ後、writeMark() を呼べばいろいろなパターンを書けます。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <math.h>
#define I2CSLAVE_ I2C_SLAVE
//-------------------------------------------------------------------------
// Variables
//-------------------------------------------------------------------------
static int i2cDscript; // file discripter
//-------------------------------------------------------------------------
// Constants
//-------------------------------------------------------------------------
static unsigned char LED_ADA88_ADDRESS = 0x70;
//-------------------------------------------------------------------------
// I2c Device Access Functions
//-------------------------------------------------------------------------
void initI2c( void )
{
const char *fileName = "/dev/i2c-1"; // I2C Drive File name
// Pressure Sensor
printf("***** start i2c *****\n");
// Open I2C port with Read/Write Attribute
if ((i2cDscript = open(fileName, O_RDWR)) < 0){
printf("Faild to open i2c port\n");
exit(1);
}
}
//-------------------------------------------------------------------------
// Adafruit LED 8*8 matrix (I2c Device)
//-------------------------------------------------------------------------
#define HT16K33_BLINK_CMD 0x80
#define HT16K33_BLINK_DISPLAYON 0x01
#define HT16K33_BLINK_OFF 0
#define HT16K33_BLINK_2HZ 1
#define HT16K33_BLINK_1HZ 2
#define HT16K33_BLINK_HALFHZ 3
#define HT16K33_CMD_BRIGHTNESS 0xE0
#define MATRIX_MAX 8
//-------------------------------------------------------------------------
void accessAda88( void )
{
int address = LED_ADA88_ADDRESS; // I2C
// Set Address
if (ioctl(i2cDscript, I2CSLAVE_, address) < 0){
printf("Unable to get bus access to talk to slave(LED Matrix)\n");
exit(1);
}
}
//-------------------------------------------------------------------------
void writeAda88( unsigned char* bitPtn )
{
unsigned char buf[MATRIX_MAX*2+1];
int i;
buf[0] = 0; // Commands for performing a ranging
for ( i=0; i<MATRIX_MAX; i++ ){
buf[i*2+1] = *(bitPtn+i);
buf[i*2+2] = 0;
}
if ((write(i2cDscript, buf, MATRIX_MAX*2+1)) != MATRIX_MAX*2+1) { // Write commands to the i2c port
printf("Error writing to i2c slave(LED)\n");
exit(1);
}
}
//-------------------------------------------------------------------------
void initAda88( void )
{
unsigned char bitPtnClr[MATRIX_MAX] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char cmd;
accessAda88();
cmd = 0x21;
if ((write(i2cDscript, &cmd, 1)) != 1) { // Write commands to the i2c port
printf("Error writing to i2c slave(LED)\n");
exit(1);
}
cmd = HT16K33_BLINK_CMD|HT16K33_BLINK_DISPLAYON|(HT16K33_BLINK_OFF<<1);
if ((write(i2cDscript, &cmd, 1)) != 1) { // Write commands to the i2c port
printf("Error writing to i2c slave(LED)\n");
exit(1);
}
cmd = HT16K33_CMD_BRIGHTNESS | 0x0f;
if ((write(i2cDscript, &cmd, 1)) != 1) { // Write commands to the i2c port
printf("Error writing to i2c slave(LED)\n");
exit(1);
}
writeAda88(bitPtnClr); // Clear All LEDs
}
//-------------------------------------------------------------------------
void writePattern( unsigned char* bitPtn )
{
accessAda88();
writeAda88(bitPtn);
}
//-------------------------------------------------------------------------
void writeMark( int type )
{
unsigned char bitPtn[2][MATRIX_MAX] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // nothing
{0x04,0x1f,0x04,0x1f,0x04,0x0f,0x15,0x22} // MA
};
writePattern(bitPtn[type]);
}

0 件のコメント:
コメントを投稿