Portable Raspberry PI Zero | I2C on an Attiny85
2017-01-01 22:47:50
So I thought I would just add the Attiny85 family to the boards by adding the following link to my Additional Boards Manager URLs in my Arduino 1.6.9 IDE and get away with it and use the wire library to hack a little LED PWM backlight controller together. But since that didn't work for the tone() lib on the #Nyan Board , why would it here?
https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json
So I then look around and find this library on GitHub https://github.com/rambo/TinyWire and with it's help I was able to whip together the LED controller! The idea here is that the pin 3 is connected to the PI and when toggled will write the PWM to pin 4. This PWM value can be set via I2C address 0x04.
#define I2C_SLAVE_ADDRESS 0x4
#include <TinyWireS.h>
int pwm_value = 127;
#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif
void setup() {
TinyWireS.begin(I2C_SLAVE_ADDRESS);
TinyWireS.onReceive(receiveEvent);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
}
void loop() {
TinyWireS_stop_check();
writeLed();
}
void writeLed()
{
if (digitalRead(3) == HIGH)
{
analogWrite(4, pwm_value);
}
else
{
digitalWrite(4, LOW);
}
}
void receiveEvent(uint8_t howMany)
{
if (howMany < 1)
{
return;
}
if (howMany > TWI_RX_BUFFER_SIZE)
{
return;
}
TinyWireS.receive();
howMany--;
if (!howMany)
{
return;
}
while(howMany--)
{
pwm_value = TinyWireS.receive();
}
}
Portable Raspberry PI Zero | I2C on an Attiny85
2017-01-01 22:47:50
So I thought I would just add the Attiny85 family to the boards by adding the following link to my Additional Boards Manager URLs in my Arduino 1.6.9 IDE and get away with it and use the wire library to hack a little LED PWM backlight controller together. But since that didn't work for the tone() lib on the #Nyan Board , why would it here?
https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json
So I then look around and find this library on GitHub https://github.com/rambo/TinyWire and with it's help I was able to whip together the LED controller! The idea here is that the pin 3 is connected to the PI and when toggled will write the PWM to pin 4. This PWM value can be set via I2C address 0x04.
#define I2C_SLAVE_ADDRESS 0x4
#include <TinyWireS.h>
int pwm_value = 127;
#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif
void setup() {
TinyWireS.begin(I2C_SLAVE_ADDRESS);
TinyWireS.onReceive(receiveEvent);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
}
void loop() {
TinyWireS_stop_check();
writeLed();
}
void writeLed()
{
if (digitalRead(3) == HIGH)
{
analogWrite(4, pwm_value);
}
else
{
digitalWrite(4, LOW);
}
}
void receiveEvent(uint8_t howMany)
{
if (howMany < 1)
{
return;
}
if (howMany > TWI_RX_BUFFER_SIZE)
{
return;
}
TinyWireS.receive();
howMany--;
if (!howMany)
{
return;
}
while(howMany--)
{
pwm_value = TinyWireS.receive();
}
}
Portable Raspberry PI Zero | changing pins works!
2016-11-27 22:06:35
I've mapped pin12 and pin13 to play audio! woot.
The following code is not mine and taken from:
https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=39138
I just want to have everything in one place and have a copy of it somewhere.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
int mem_fd;
void *gpio_map;
volatile unsigned *gpio;
void setup_io();
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
#define GPIO_SET *(gpio+7)
#define GPIO_CLR *(gpio+10)
int main (int argc, char **argv) {
int opt, flag, n_pin, n_alt;
flag=0;
while ((opt = getopt (argc, argv, "hp:f:")) != -1) {
switch (opt) {
case 'h':
break;
case 'p':
n_pin = atoi(optarg); flag |= 0b0001; break;
case 'f':
n_alt = atoi(optarg); flag |= 0b0010; break;
case '?':
return 1;
default:
abort ();
}
}
if (flag != 0b0011) {
fprintf (stderr, "Usage:\n$ gpio_alt -p PIN_NUM -f FUNC_NUM\n");
return 1;
}
setup_io();
INP_GPIO(n_pin);
SET_GPIO_ALT(n_pin, n_alt);
printf("Set pin %i to alternative-function %i\n", n_pin, n_alt);
return 0;
}
void setup_io() {
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit(-1);
}
gpio_map = mmap(
NULL,
BLOCK_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED,
mem_fd,
GPIO_BASE
);
close(mem_fd);
if (gpio_map == MAP_FAILED) {
printf("mmap error %d\n", (int)gpio_map);
exit(-1);
}
gpio = (volatile unsigned *)gpio_map;
}
One time setup:
gcc -o gpio_alt gpio_alt.c
sudo chown root:root gpio_alt
sudo chmod u+s gpio_alt
sudo mv gpio_alt /usr/local/bin/
Cron that:
gpio_alt -p 13 -f 0
gpio_alt -p 12 -f 0
Portable Raspberry PI Zero | changing pins works!
2016-11-27 22:06:35
I've mapped pin12 and pin13 to play audio! woot.
The following code is not mine and taken from:
https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=39138
I just want to have everything in one place and have a copy of it somewhere.
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
int mem_fd;
void *gpio_map;
volatile unsigned *gpio;
void setup_io();
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
#define GPIO_SET *(gpio+7)
#define GPIO_CLR *(gpio+10)
int main (int argc, char **argv) {
int opt, flag, n_pin, n_alt;
flag=0;
while ((opt = getopt (argc, argv, "hp:f:")) != -1) {
switch (opt) {
case 'h':
break;
case 'p':
n_pin = atoi(optarg); flag |= 0b0001; break;
case 'f':
n_alt = atoi(optarg); flag |= 0b0010; break;
case '?':
return 1;
default:
abort ();
}
}
if (flag != 0b0011) {
fprintf (stderr, "Usage:\n$ gpio_alt -p PIN_NUM -f FUNC_NUM\n");
return 1;
}
setup_io();
INP_GPIO(n_pin);
SET_GPIO_ALT(n_pin, n_alt);
printf("Set pin %i to alternative-function %i\n", n_pin, n_alt);
return 0;
}
void setup_io() {
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit(-1);
}
gpio_map = mmap(
NULL,
BLOCK_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED,
mem_fd,
GPIO_BASE
);
close(mem_fd);
if (gpio_map == MAP_FAILED) {
printf("mmap error %d\n", (int)gpio_map);
exit(-1);
}
gpio = (volatile unsigned *)gpio_map;
}
One time setup:
gcc -o gpio_alt gpio_alt.c
sudo chown root:root gpio_alt
sudo chmod u+s gpio_alt
sudo mv gpio_alt /usr/local/bin/
Cron that:
gpio_alt -p 13 -f 0
gpio_alt -p 12 -f 0
Portable Raspberry PI Zero | boards ordered
2016-11-24 17:04:32
There is currently no sound on this board, but I've ordered this anyway with some testpoints for the possible sound pins and an extra board you can check out below (not designed by me but someone who builds raspberry pi portables). The attiny85 is connected to the PI via I2C and the TFT LED output of the driver - think basic on/off via driver and pwm via i2c. It's a decent prototyping board to get along, I think.
Gameboy Zero PWM Audio SMD - by Helder
https://oshpark.com/shared_projects/qUEOjE0n
https://cdn.hackaday.io/images/2776481480003209837.png
https://cdn.hackaday.io/images/1744551480003136117.png
Portable Raspberry PI Zero | boards ordered
2016-11-24 17:04:32
There is currently no sound on this board, but I've ordered this anyway with some testpoints for the possible sound pins and an extra board you can check out below (not designed by me but someone who builds raspberry pi portables). The attiny85 is connected to the PI via I2C and the TFT LED output of the driver - think basic on/off via driver and pwm via i2c. It's a decent prototyping board to get along, I think.
Gameboy Zero PWM Audio SMD - by Helder
https://oshpark.com/shared_projects/qUEOjE0n
https://cdn.hackaday.io/images/2776481480003209837.png
https://cdn.hackaday.io/images/1744551480003136117.png
Portable Raspberry PI Zero | party on!
2016-11-19 21:53:02
soo, still not validated, but I'm happy to give it a try as soon as possible - a breakout board to connect the Raspberry PI with the TFT display, wi-fi and I2C and stuff. I'm tired.
https://cdn.hackaday.io/images/2105701479588343377.png
Portable Raspberry PI Zero | party on!
2016-11-19 21:53:02
soo, still not validated, but I'm happy to give it a try as soon as possible - a breakout board to connect the Raspberry PI with the TFT display, wi-fi and I2C and stuff. I'm tired.
https://cdn.hackaday.io/images/2105701479588343377.png
Portable Raspberry PI Zero | eyes for dem pis!
2016-11-18 09:52:52
Yay, I won something! When I saw the blog post about the Enlightened Raspberry Pi Contest I must have over looked that I won something, because I was surprised when Adam Fabio wrote me to get my address to send me a sweet camera module! Not sure if it will end up in this project or the cluster thing, but I will find a good project for it! With all those PIs I have it shouldn't be a problem.
https://hackaday.io/contest/15532/log/49182-and-the-winners-are
Portable Raspberry PI Zero | eyes for dem pis!
2016-11-18 09:52:52
Yay, I won something! When I saw the blog post about the Enlightened Raspberry Pi Contest I must have over looked that I won something, because I was surprised when Adam Fabio wrote me to get my address to send me a sweet camera module! Not sure if it will end up in this project or the cluster thing, but I will find a good project for it! With all those PIs I have it shouldn't be a problem.
https://hackaday.io/contest/15532/log/49182-and-the-winners-are
Portable Raspberry PI Zero | more wiring adventures
Portable Raspberry PI Zero | more wiring adventures
Portable Raspberry PI Zero | Odyssey continues
2016-11-09 21:55:22
Python script is about the same where I was before. Python is weird when you're coming from JS and PHP, let me tell you.
AND the uinput part works!! woot! Thanks to adafruit, mainly. And my copy and paste skills.
I still need to map the keys and find the driver for the display (MacGyver was drawn by an adafruit python library).
import subprocess
import sys
import time
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.I2C as I2C
import Adafruit_GPIO.PCF8574 as PCF
import uinput
row = 1
rowI = 1
col = 1024
addressH = 0x20
addressL = 0x21
gpioH = PCF.PCF8574(addressH, busnum=1)
gpioL = PCF.PCF8574(addressL, busnum=1)
KEY_MAPPING = {
0: uinput.KEY_UP,
1: uinput.KEY_DOWN,
2: uinput.KEY_LEFT,
3: uinput.KEY_RIGHT,
4: uinput.KEY_B,
5: uinput.KEY_A,
6: uinput.KEY_ENTER,
7: uinput.KEY_SPACE,
}
subprocess.check_call(['modprobe', 'uinput'])
device = uinput.Device(KEY_MAPPING.values())
def shiftRange (start, end):
while start > end:
yield start
start = start>>1
while True:
rowS = 0x03FF ^ row
rowL = (rowS<<5) & 0x00FF
rowH = (rowS>>3) & 0x00FF
addL = rowL | 0x1F
addH = rowH
gpioH.iodir = addH
gpioL.iodir = addL
gpioH._write_pins()
gpioL._write_pins()
keys = gpioL._read_pins()
keys = keys & 0x1F;
keysI = 1
for k in shiftRange(16,0):
if k & keys == 0:
device.emit_click(KEY_MAPPING[rowI-1])
time.sleep(0.5)
keysI += 1
rowI += 1
row = row << 1
if row == col:
row = 1
rowI = 1
Portable Raspberry PI Zero | Odyssey continues
2016-11-09 21:55:22
Python script is about the same where I was before. Python is weird when you're coming from JS and PHP, let me tell you.
AND the uinput part works!! woot! Thanks to adafruit, mainly. And my copy and paste skills.
I still need to map the keys and find the driver for the display (MacGyver was drawn by an adafruit python library).
import subprocess
import sys
import time
import Adafruit_GPIO as GPIO
import Adafruit_GPIO.I2C as I2C
import Adafruit_GPIO.PCF8574 as PCF
import uinput
row = 1
rowI = 1
col = 1024
addressH = 0x20
addressL = 0x21
gpioH = PCF.PCF8574(addressH, busnum=1)
gpioL = PCF.PCF8574(addressL, busnum=1)
KEY_MAPPING = {
0: uinput.KEY_UP,
1: uinput.KEY_DOWN,
2: uinput.KEY_LEFT,
3: uinput.KEY_RIGHT,
4: uinput.KEY_B,
5: uinput.KEY_A,
6: uinput.KEY_ENTER,
7: uinput.KEY_SPACE,
}
subprocess.check_call(['modprobe', 'uinput'])
device = uinput.Device(KEY_MAPPING.values())
def shiftRange (start, end):
while start > end:
yield start
start = start>>1
while True:
rowS = 0x03FF ^ row
rowL = (rowS<<5) & 0x00FF
rowH = (rowS>>3) & 0x00FF
addL = rowL | 0x1F
addH = rowH
gpioH.iodir = addH
gpioL.iodir = addL
gpioH._write_pins()
gpioL._write_pins()
keys = gpioL._read_pins()
keys = keys & 0x1F;
keysI = 1
for k in shiftRange(16,0):
if k & keys == 0:
device.emit_click(KEY_MAPPING[rowI-1])
time.sleep(0.5)
keysI += 1
rowI += 1
row = row << 1
if row == col:
row = 1
rowI = 1
Portable Raspberry PI Zero | Keyboard adventures - Episode 2
2016-11-06 01:53:28
That's my current state for the actual keyboard. I will probably drop this and move over to python, since I just don't get the uinput stuff in C and the linux input system. Hey future Dave, research before developing, you know this stuff!
https://learn.adafruit.com/mpr121-capacitive-touch-sensor-on-raspberry-pi-and-beaglebone-black/software
https://github.com/adafruit/Adafruit_Python_MPR121
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
int fdH;
int fdL;
int fd;
int rpl;
int ret;
int row = 1;
int rowI = 1;
int col = 1024;
int i = 0;
int j = 0;
int k = 0;
void printByte(int a)
{
printf("\n0x");
for (j=0x8000; j>0; j=j>>1)
{
if ((a&j) > 0) printf("1");
else printf("0");
}
}
int main (void)
{
wiringPiSetup ();
fdH = wiringPiI2CSetup(0x20);
fdL = wiringPiI2CSetup(0x21);
for (;;)
{
int rowS = 0x03FF ^ row;
int rowL = (rowS<<5) & 0x00FF;
int rowH = (rowS>>3) & 0x00FF;
int addL = rowL | 0x1F;
int addH = rowH;
wiringPiI2CWrite(fdH, addH);
wiringPiI2CWrite(fdL, addL);
int keys = wiringPiI2CRead(fdL);
keys = keys & 0x1F;
int keyI = 1;
for (k=0x10; k>0; k=k>>1)
{
if (!(k & keys))
{
delay(50);
printf("%i\t%i\n", keyI, rowI);
}
keyI++;
}
rowI++;
row = row << 1;
if (row == col)
{
row = 1;
rowI = 1;
}
}
return 0 ;
}
Portable Raspberry PI Zero | Keyboard adventures - Episode 2
2016-11-06 01:53:28
That's my current state for the actual keyboard. I will probably drop this and move over to python, since I just don't get the uinput stuff in C and the linux input system. Hey future Dave, research before developing, you know this stuff!
https://learn.adafruit.com/mpr121-capacitive-touch-sensor-on-raspberry-pi-and-beaglebone-black/software
https://github.com/adafruit/Adafruit_Python_MPR121
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
int fdH;
int fdL;
int fd;
int rpl;
int ret;
int row = 1;
int rowI = 1;
int col = 1024;
int i = 0;
int j = 0;
int k = 0;
void printByte(int a)
{
printf("\n0x");
for (j=0x8000; j>0; j=j>>1)
{
if ((a&j) > 0) printf("1");
else printf("0");
}
}
int main (void)
{
wiringPiSetup ();
fdH = wiringPiI2CSetup(0x20);
fdL = wiringPiI2CSetup(0x21);
for (;;)
{
int rowS = 0x03FF ^ row;
int rowL = (rowS<<5) & 0x00FF;
int rowH = (rowS>>3) & 0x00FF;
int addL = rowL | 0x1F;
int addH = rowH;
wiringPiI2CWrite(fdH, addH);
wiringPiI2CWrite(fdL, addL);
int keys = wiringPiI2CRead(fdL);
keys = keys & 0x1F;
int keyI = 1;
for (k=0x10; k>0; k=k>>1)
{
if (!(k & keys))
{
delay(50);
printf("%i\t%i\n", keyI, rowI);
}
keyI++;
}
rowI++;
row = row << 1;
if (row == col)
{
row = 1;
rowI = 1;
}
}
return 0 ;
}
Portable Raspberry PI Zero | I2C wiringPI for the PCF8574
2016-11-01 23:29:46
It took me a while to get this far and I'm even further away from being done. I couldn't find a good example how to use the I2C library for it. http://wiringpi.com/reference/i2c-library/
As I understand now, the function wiringPiI2CSetup gives you a file handle (everything in linux is a file, right?) and you need to store it! That's what I'm doing with the fd variable. Ignore the broken math, it's what I got from 4 hours raging on the keyboard after hours of work. From there you can use the fileHandler thing to read and write from and to. This way I got some data from a 4x4 keyboard.
Little console helpers:
i2cget -y 1 0x20
i2cset -y 1 0x20 0x87
gpio i2cd
I also messed up the PCF8574 addressing pins :D of course I did. Here's my messy script.
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
int fd;
int rpl;
int row = 1;
int col = 16;
int main (void)
{
fd = wiringPiI2CSetup(0x20);
wiringPiSetup ();
for (;;)
{
if (row>8)
{
row = 1;
col = col << 1;
if (col>128) col = 16;
}
rpl = wiringPiI2CWrite(fd, 240-col+row);
int b = wiringPiI2CRead(fd);
if (b != 240-col+row)
{
printf("\t%i", b);
printf("\t%i\n", 240-col+row);
delay(250);
}
row*=2;
}
return 0 ;
}
https://cdn.hackaday.io/images/6503061478041123518.jpg
Portable Raspberry PI Zero | I2C wiringPI for the PCF8574
2016-11-01 23:29:46
It took me a while to get this far and I'm even further away from being done. I couldn't find a good example how to use the I2C library for it. http://wiringpi.com/reference/i2c-library/
As I understand now, the function wiringPiI2CSetup gives you a file handle (everything in linux is a file, right?) and you need to store it! That's what I'm doing with the fd variable. Ignore the broken math, it's what I got from 4 hours raging on the keyboard after hours of work. From there you can use the fileHandler thing to read and write from and to. This way I got some data from a 4x4 keyboard.
Little console helpers:
i2cget -y 1 0x20
i2cset -y 1 0x20 0x87
gpio i2cd
I also messed up the PCF8574 addressing pins :D of course I did. Here's my messy script.
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
int fd;
int rpl;
int row = 1;
int col = 16;
int main (void)
{
fd = wiringPiI2CSetup(0x20);
wiringPiSetup ();
for (;;)
{
if (row>8)
{
row = 1;
col = col << 1;
if (col>128) col = 16;
}
rpl = wiringPiI2CWrite(fd, 240-col+row);
int b = wiringPiI2CRead(fd);
if (b != 240-col+row)
{
printf("\t%i", b);
printf("\t%i\n", 240-col+row);
delay(250);
}
row*=2;
}
return 0 ;
}
https://cdn.hackaday.io/images/6503061478041123518.jpg
Portable Raspberry PI Zero | Updated 3D printing files
2016-10-31 20:16:54
You can now download an print along :D only thing missing is the cover for the raspberry PI bottom. Notes came in handy.
https://cdn.hackaday.io/images/7028211477941318740.png
Portable Raspberry PI Zero | Updated 3D printing files
2016-10-31 20:16:54
You can now download an print along :D only thing missing is the cover for the raspberry PI bottom. Notes came in handy.
https://cdn.hackaday.io/images/7028211477941318740.png