first commit
This commit is contained in:
commit
a5a0434432
1126 changed files with 439481 additions and 0 deletions
|
|
@ -0,0 +1,135 @@
|
|||
#!/usr/bin/env python
|
||||
#
|
||||
# GrovePi Library for using the Grove Chainable RGB LED (http://www.seeedstudio.com/wiki/Grove_-_Chainable_RGB_LED)
|
||||
#
|
||||
# The GrovePi connects the Raspberry Pi and Grove sensors. You can learn more about GrovePi here: http://www.dexterindustries.com/GrovePi
|
||||
#
|
||||
# Have a question about this example? Ask on the forums here: http://forum.dexterindustries.com/c/grovepi
|
||||
#
|
||||
# Derived from the C library for the P9813 LED's by DaochenShi here: https://github.com/DC-Shi/PN532SPI-P9813GPIO
|
||||
'''
|
||||
## License
|
||||
The MIT License (MIT)
|
||||
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
|
||||
Copyright (C) 2015 Dexter Industries
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
'''
|
||||
# Note: Connect the chainable LED to port RPISER on the GrovePi
|
||||
import time,sys
|
||||
import RPi.GPIO as GPIO
|
||||
import smbus
|
||||
|
||||
class rgb_led:
|
||||
r_all=[]
|
||||
g_all=[]
|
||||
b_all=[]
|
||||
|
||||
def __init__(self,led=1):
|
||||
GPIO.setwarnings(False)
|
||||
self.num_led=led
|
||||
self.r_all=[0] * self.num_led
|
||||
self.g_all=[0] * self.num_led
|
||||
self.b_all=[0] * self.num_led
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
self.clk_pin= 15 #RX pin BCM
|
||||
self.data_pin= 14 # TX pin BCM
|
||||
self.tv_nsec= 100
|
||||
GPIO.setup(self.clk_pin, GPIO.OUT)
|
||||
GPIO.setup(self.data_pin, GPIO.OUT)
|
||||
|
||||
def sendByte(self,b):
|
||||
# print b
|
||||
for loop in range(8):
|
||||
# digitalWrite(CLKPIN, LOW);
|
||||
GPIO.output(self.clk_pin,0)
|
||||
time.sleep(self.tv_nsec/1000000.0)
|
||||
# nanosleep(&TIMCLOCKINTERVAL, NULL);
|
||||
|
||||
# The ic will latch a bit of data when the rising edge of the clock coming, And the data should changed after the falling edge of the clock;
|
||||
# Copyed from P9813 datasheet
|
||||
|
||||
if (b & 0x80) != 0:
|
||||
# digitalWrite(DATPIN, HIGH)
|
||||
GPIO.output(self.data_pin,1)
|
||||
else:
|
||||
# digitalWrite(DATPIN, LOW):
|
||||
GPIO.output(self.data_pin,0)
|
||||
|
||||
# digitalWrite(CLKPIN, HIGH);
|
||||
GPIO.output(self.clk_pin,1)
|
||||
# nanosleep(&TIMCLOCKINTERVAL, NULL);
|
||||
time.sleep(self.tv_nsec/1000000.0)
|
||||
# //usleep(CLOCKINTERVAL);
|
||||
|
||||
b <<= 1
|
||||
|
||||
def sendColor(self,r, g, b):
|
||||
prefix = 0b11000000;
|
||||
if (b & 0x80) == 0:
|
||||
prefix |= 0b00100000
|
||||
if (b & 0x40) == 0:
|
||||
prefix |= 0b00010000
|
||||
if (g & 0x80) == 0:
|
||||
prefix |= 0b00001000
|
||||
if (g & 0x40) == 0:
|
||||
prefix |= 0b00000100
|
||||
if (r & 0x80) == 0:
|
||||
prefix |= 0b00000010
|
||||
if (r & 0x40) == 0:
|
||||
prefix |= 0b00000001
|
||||
|
||||
self.sendByte(prefix)
|
||||
self.sendByte(b)
|
||||
self.sendByte(g)
|
||||
self.sendByte(r)
|
||||
|
||||
def setColorRGB(self,r,g,b):
|
||||
for i in range(4):
|
||||
self.sendByte(0)
|
||||
|
||||
self.sendColor(r, g, b);
|
||||
|
||||
for i in range(4):
|
||||
self.sendByte(0)
|
||||
|
||||
def setColorRGBs(self,r,g,b,count):
|
||||
for i in range(4):
|
||||
self.sendByte(0)
|
||||
for i in range(count):
|
||||
self.sendColor(r[i], g[i], b[i])
|
||||
for i in range(4):
|
||||
self.sendByte(0)
|
||||
|
||||
def setOneLED(self,r,g,b,led_num):
|
||||
self.r_all[led_num]=r
|
||||
self.g_all[led_num]=g
|
||||
self.b_all[led_num]=b
|
||||
|
||||
self.setColorRGBs(self.r_all,self.g_all,self.b_all,self.num_led)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
num_led=3
|
||||
|
||||
l= rgb_led(num_led)
|
||||
|
||||
l.setColorRGB(255,0,0)
|
||||
r=[0,0,255]
|
||||
g=[0,255,0]
|
||||
b=[255,0,0]
|
||||
l.setColorRGBs(r,g,b,num_led)
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
#!/usr/bin/env python
|
||||
#
|
||||
# GrovePi Example for using the Grove Chainable RGB LED (http://www.seeedstudio.com/wiki/Grove_-_Chainable_RGB_LED)
|
||||
#
|
||||
# The GrovePi connects the Raspberry Pi and Grove sensors. You can learn more about GrovePi here: http://www.dexterindustries.com/GrovePi
|
||||
#
|
||||
# Have a question about this example? Ask on the forums here: http://forum.dexterindustries.com/c/grovepi
|
||||
#
|
||||
# Derived from the C library for the P9813 LED's by DaochenShi here: https://github.com/DC-Shi/PN532SPI-P9813GPIO
|
||||
'''
|
||||
## License
|
||||
The MIT License (MIT)
|
||||
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
|
||||
Copyright (C) 2015 Dexter Industries
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
'''
|
||||
# Note: Connect the chainable LED to port RPISER on the GrovePi
|
||||
import chainable_rgb_direct
|
||||
num_led=3
|
||||
|
||||
l= chainable_rgb_direct.rgb_led(num_led)
|
||||
|
||||
###########################
|
||||
#Set Color on the first LED
|
||||
#l.setColorRGB(255,0,0)
|
||||
|
||||
###########################
|
||||
#Set color on LED chain
|
||||
#RGB array needs to be sent
|
||||
#r[0],g[0],b[0] specifies the color for led[0]
|
||||
#r[1],g[1],b[1] specifies the color for led[1]
|
||||
#r[2],g[2],b[1] specifies the color for led[2]
|
||||
#Make the array length as the number of LED's
|
||||
|
||||
# r=[0,0,255]
|
||||
# g=[0,255,0]
|
||||
# b=[255,0,0]
|
||||
# l.setColorRGBs(r,g,b,num_led)
|
||||
|
||||
###########################
|
||||
#Turn off all LED's
|
||||
# r=[0,0,0]
|
||||
# g=[0,0,0]
|
||||
# b=[0,0,0]
|
||||
# l.setColorRGBs(r,g,b,num_led)
|
||||
|
||||
###########################
|
||||
# #Show a pattern with 3 LED's
|
||||
# while 1:
|
||||
# for i in range(0,255,5):
|
||||
# r[0]=i
|
||||
# g[0]=255-i
|
||||
# b[0]=0
|
||||
|
||||
# r[1]=0
|
||||
# g[1]=i
|
||||
# b[1]=255-i
|
||||
|
||||
# r[2]=255-i
|
||||
# g[2]=0
|
||||
# b[2]=i
|
||||
|
||||
# l.setColorRGBs(r,g,b,num_led)
|
||||
|
||||
###########################
|
||||
# Control one LED at at time
|
||||
l.setOneLED(127,127,0,0) #Set LED 0
|
||||
l.setOneLED(0,127,127,1) #Set LED 1
|
||||
l.setOneLED(0,0,0,0) #Clear LED 0
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
# compiler:
|
||||
CC = gcc
|
||||
## args: especially libs
|
||||
CFLAGS = -O3 -Wunused
|
||||
LIBS = -lwiringPi
|
||||
OUTPUT = NFCLight
|
||||
|
||||
all: NFCLight
|
||||
|
||||
debug: CC += -DDEBUG
|
||||
debug: NFCLight
|
||||
|
||||
|
||||
NFCLightusable: main.o P9813GPIO.o PN532SPIusable.o
|
||||
$(CC) $(CFLAGS) -DDEBUG -DPN532DEBUG $(LIBS) -o $@ $^
|
||||
|
||||
NFCLight: main.o P9813GPIO.o PN532SPI.o
|
||||
$(CC) $(CFLAGS) $(LIBS) -o $@ $^
|
||||
|
||||
|
||||
main.o: main.c PN532SPI.h
|
||||
|
||||
|
||||
PN532SPI.o: PN532SPI.c PN532SPI.h
|
||||
|
||||
|
||||
P9813GPIO.0: P9813GPIO.c P9813GPIO.h
|
||||
|
||||
|
||||
clean:
|
||||
rm -rf *.o NFCLight
|
||||
|
||||
|
|
@ -0,0 +1,328 @@
|
|||
/*
|
||||
Bitbanged the GPIO to simulate SPI interface.
|
||||
Used CLKPIN and DATPIN.
|
||||
LED1,2,3 gives the indicator of different light levels.
|
||||
|
||||
Since P9813 and PN532 are using SPI, but P9813 is without ChipSelect wire, so any data on MOSI would be accepted by P9813, that leads to blinking light sometimes.
|
||||
|
||||
But this simulation is also unstable when raspberry pi under high load.
|
||||
|
||||
Reference here: http://www.seeedstudio.com/wiki/File:P9813_datasheet.pdf
|
||||
|
||||
Verified for 1 LED, and this should be compatible with multiple LEDs. It is said that 1024 LEDs is allowed.
|
||||
|
||||
This project is created by @DaochenShi (shidaochen@live.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h> // printf,etc.
|
||||
#include <stdlib.h> // exit
|
||||
#include <signal.h> // for Ctrl+C
|
||||
//#include <unistd.h> // usleep
|
||||
#include <time.h> // nanosleep
|
||||
#include <math.h> // abs
|
||||
#include <errno.h> // errno
|
||||
|
||||
#include "P9813GPIO.h"
|
||||
|
||||
|
||||
|
||||
|
||||
static struct timespec TIMCLOCKINTERVAL;
|
||||
|
||||
// Send a byte bit by bit using digitalWrite
|
||||
void sendByte(unsigned char b)
|
||||
{
|
||||
char loop = 8;
|
||||
#ifndef DRYRUN
|
||||
for(loop = 0; loop < 8; loop++)
|
||||
{
|
||||
digitalWrite(CLKPIN, LOW);
|
||||
nanosleep(&TIMCLOCKINTERVAL, NULL);
|
||||
//usleep(CLOCKINTERVAL);
|
||||
// The ic will latch a bit of data when the rising edge of the clock coming, And the data should changed after the falling edge of the clock;
|
||||
// Copyed from P9813 datasheet
|
||||
|
||||
if ((b & 0x80) != 0)
|
||||
digitalWrite(DATPIN, HIGH);
|
||||
else
|
||||
digitalWrite(DATPIN, LOW);
|
||||
|
||||
digitalWrite(CLKPIN, HIGH);
|
||||
nanosleep(&TIMCLOCKINTERVAL, NULL);
|
||||
//usleep(CLOCKINTERVAL);
|
||||
|
||||
b <<= 1;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Send a color(RGB) information to LED.
|
||||
void sendColor(unsigned char r, unsigned char g, unsigned char b)
|
||||
{
|
||||
unsigned char prefix = 0b11000000;
|
||||
if ((b & 0x80) == 0) prefix |= 0b00100000;
|
||||
if ((b & 0x40) == 0) prefix |= 0b00010000;
|
||||
if ((g & 0x80) == 0) prefix |= 0b00001000;
|
||||
if ((g & 0x40) == 0) prefix |= 0b00000100;
|
||||
if ((r & 0x80) == 0) prefix |= 0b00000010;
|
||||
if ((r & 0x40) == 0) prefix |= 0b00000001;
|
||||
|
||||
sendByte(prefix);
|
||||
sendByte(b);sendByte(g);sendByte(r);
|
||||
}
|
||||
|
||||
#ifdef GPIO_PURE_LED
|
||||
#ifdef GPIO_PURE_LED1
|
||||
static unsigned char LED1value = 0;
|
||||
#endif
|
||||
|
||||
#ifdef GPIO_PURE_LED2
|
||||
static unsigned char LED2value = 0;
|
||||
#endif
|
||||
|
||||
#ifdef GPIO_PURE_LED3
|
||||
static unsigned char LED3value = 0;
|
||||
#endif
|
||||
#endif
|
||||
static unsigned char previousR = 0;
|
||||
static unsigned char previousG = 0;
|
||||
static unsigned char previousB = 0;
|
||||
static unsigned char unchangedTimes = 0;
|
||||
|
||||
// Set the color, used stored previous RGB information to avoid writing the same thing repeatedly.
|
||||
// This is 'buffered'. If the color is the same as previous color, we might ignore it.
|
||||
// Also, this function handled GPIO pure LEDs, so remove GPIO_PURE_LED to get access only to P9813.
|
||||
// Advantage: Fewer GPIO write leads to less CPU costs.
|
||||
// Disadvantage: Your request may not be applied, apply once every 2^8 times.
|
||||
void setColorRGBbuffered(unsigned char r, unsigned char g, unsigned char b)
|
||||
{
|
||||
unsigned char max = 0;
|
||||
max = (max > r) ? max : r;
|
||||
max = (max > g) ? max : g;
|
||||
max = (max > b) ? max : b;
|
||||
|
||||
#ifdef GPIO_PURE_LED
|
||||
|
||||
#ifdef GPIO_PURE_LED1
|
||||
if (LED1value != (max > 0x40))
|
||||
{
|
||||
digitalWrite(GPIO_PURE_LED1, (max > 0x40));
|
||||
LED1value = (max > 0x40);
|
||||
}
|
||||
else
|
||||
{
|
||||
unchangedTimes++;
|
||||
#ifdef P9813DEBUG
|
||||
printf("Unchanged this time, %d\n", unchangedTimes);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef GPIO_PURE_LED1
|
||||
if (LED2value != (max > 0x80))
|
||||
{
|
||||
digitalWrite(GPIO_PURE_LED2, (max > 0x80));
|
||||
LED2value = (max > 0x80);
|
||||
}
|
||||
else
|
||||
{
|
||||
unchangedTimes++;
|
||||
#ifdef P9813DEBUG
|
||||
printf("Unchanged this time, %d\n", unchangedTimes);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef GPIO_PURE_LED3
|
||||
if (LED3value != (max > 0xC0))
|
||||
{
|
||||
digitalWrite(GPIO_PURE_LED3, (max > 0xC0));
|
||||
LED3value = (max > 0xC0);
|
||||
}
|
||||
else
|
||||
{
|
||||
unchangedTimes++;
|
||||
#ifdef P9813DEBUG
|
||||
printf("Unchanged this time, %d\n", unchangedTimes);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if ( (previousR != r) || (previousG != g) || (previousB != b) || (!unchangedTimes))
|
||||
{
|
||||
sendByte(0);sendByte(0);sendByte(0);sendByte(0);
|
||||
sendColor(r, g, b);
|
||||
sendByte(0);sendByte(0);sendByte(0);sendByte(0);
|
||||
previousR = r;
|
||||
previousG = g;
|
||||
previousB = b;
|
||||
}
|
||||
else
|
||||
{
|
||||
unchangedTimes++;
|
||||
#ifdef P9813DEBUG
|
||||
printf("Unchanged this time, %d\n", unchangedTimes);
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Set the color to LED, or the first LED.
|
||||
// This is not 'buffered', every time you invoke this method, it would send the signals directly to the bus.
|
||||
// Advantage: What you see is what you want.
|
||||
// Disadvantage: If you invoke this all the time, and write the same color, it costs a lot of CPU.
|
||||
void setColorRGB(unsigned char r, unsigned char g, unsigned char b)
|
||||
{
|
||||
sendByte(0);sendByte(0);sendByte(0);sendByte(0);
|
||||
sendColor(r, g, b);
|
||||
sendByte(0);sendByte(0);sendByte(0);sendByte(0);
|
||||
previousR = r;
|
||||
previousG = g;
|
||||
previousB = b;
|
||||
}
|
||||
|
||||
// Set the color with multiple LEDs.
|
||||
// Not tested yet.
|
||||
void setColorRGBs(unsigned char* r, unsigned char* g, unsigned char* b, int count)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
sendByte(0);sendByte(0);sendByte(0);sendByte(0);
|
||||
for ( i = 0; i < count; i++ )
|
||||
{
|
||||
printf("led %d color = %d,%d,%d\n",i,r[i], g[i], b[i]);
|
||||
sendColor(r[i], g[i], b[i]);
|
||||
}
|
||||
sendByte(0);sendByte(0);sendByte(0);sendByte(0);
|
||||
}
|
||||
|
||||
// Initializing
|
||||
// I noted this because occasionally the light was brought up, and cannot be set.
|
||||
// Because I rebooted the Pi, and forgot to set to OUTPUT direction.
|
||||
void initialP9813GPIO()
|
||||
{
|
||||
int result = wiringPiSetup();
|
||||
if (result < 0)
|
||||
{
|
||||
printf("wiringPi setup failed, are you root?\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
TIMCLOCKINTERVAL.tv_sec = 0;
|
||||
TIMCLOCKINTERVAL.tv_nsec = 20000L;
|
||||
|
||||
pinMode(CLKPIN, OUTPUT);
|
||||
pinMode(DATPIN, OUTPUT);
|
||||
setColorRGB(0, 0, 0);
|
||||
|
||||
#ifdef GPIO_PURE_LED
|
||||
// use wiringPi pinout
|
||||
#ifdef GPIO_PURE_LED1
|
||||
pinMode(GPIO_PURE_LED1, OUTPUT);
|
||||
digitalWrite(GPIO_PURE_LED1, 0);
|
||||
printf("Set up wiringPi GPIO%d\n", GPIO_PURE_LED1);
|
||||
#endif
|
||||
|
||||
#ifdef GPIO_PURE_LED2
|
||||
pinMode(GPIO_PURE_LED2, OUTPUT);
|
||||
digitalWrite(GPIO_PURE_LED2, 0);
|
||||
printf("Set up wiringPi GPIO%d\n", GPIO_PURE_LED2);
|
||||
#endif
|
||||
|
||||
#ifdef GPIO_PURE_LED3
|
||||
pinMode(GPIO_PURE_LED3, OUTPUT);
|
||||
digitalWrite(GPIO_PURE_LED3, 0);
|
||||
printf("Set up wiringPi GPIO%d\n", GPIO_PURE_LED3);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
////////////////////////////////////
|
||||
// These functions below are for testing alone. You can test this file only when changing 'LEDmain' to 'main'
|
||||
////////////////////////////////////
|
||||
/// Question: I do NOT want these functions to be called outside this file, how to do it?
|
||||
|
||||
#ifdef SINGLE_FILE_DEBUG
|
||||
static int loop = 1;
|
||||
void CtrlCBreak(int sig)
|
||||
{
|
||||
signal(sig, SIG_IGN);
|
||||
loop = 0;
|
||||
signal(sig, SIG_DFL);
|
||||
}
|
||||
|
||||
|
||||
int LEDmain(int argc, const char* argv[])
|
||||
{
|
||||
int result = 0;
|
||||
int nextColor;
|
||||
unsigned char nextR, nextG, nextB, colorR, colorG, colorB, maxDiff;
|
||||
result = wiringPiSetup();
|
||||
signal(SIGINT,CtrlCBreak);
|
||||
if (result < 0)
|
||||
{
|
||||
printf("wiringPi setup failed, are you root?\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
pinMode(CLKPIN, OUTPUT);
|
||||
pinMode(DATPIN, OUTPUT);
|
||||
|
||||
setColorRGBbuffered(0, 0, 0);
|
||||
sleep(1);
|
||||
//sendColor(255, 0, 0);
|
||||
setColorRGBbuffered(255, 0, 0);
|
||||
sleep(1);
|
||||
//sendColor(0, 255, 0);
|
||||
setColorRGBbuffered(0, 255, 0);
|
||||
sleep(1);
|
||||
//sendColor(0, 0, 255);
|
||||
setColorRGBbuffered(0, 0, 255);
|
||||
sleep(1);
|
||||
|
||||
while(loop)
|
||||
{
|
||||
if ((colorR == nextR) && (colorG == nextG) && (colorB == nextB))
|
||||
{
|
||||
nextColor = rand() & 0x00FFFFFF;
|
||||
nextR = (nextColor & 0x00FF0000) >> 16;
|
||||
nextG = (nextColor & 0x0000FF00) >> 8;
|
||||
nextB = (nextColor & 0x000000FF);
|
||||
//printf("nextColor is %dR, %dG, %dB\n", nextR, nextG, nextB);
|
||||
}
|
||||
else
|
||||
{
|
||||
// colorR = (color & 0x00FF0000) >> 16;
|
||||
// colorG = (color & 0x0000FF00) >> 8;
|
||||
// colorB = (color & 0x000000FF);
|
||||
|
||||
maxDiff = 0;
|
||||
maxDiff = (maxDiff > abs(colorR - nextR)) ? maxDiff : abs(colorR - nextR);
|
||||
maxDiff = (maxDiff > abs(colorG - nextG)) ? maxDiff : abs(colorG - nextG);
|
||||
maxDiff = (maxDiff > abs(colorB - nextB)) ? maxDiff : abs(colorB - nextB);
|
||||
|
||||
if (maxDiff == 0)
|
||||
{
|
||||
printf("Bug comes out,,,,,\n");
|
||||
break;
|
||||
}
|
||||
colorR = colorR - (colorR - nextR) / maxDiff;
|
||||
colorR = colorR & 0xFF;
|
||||
colorG = colorG - (colorG - nextG) / maxDiff;
|
||||
colorG = colorG & 0xFF;
|
||||
colorB = colorB - (colorB - nextB) / maxDiff;
|
||||
colorB = colorB & 0xFF;
|
||||
}
|
||||
|
||||
setColorRGBbuffered(colorR, colorG, colorB);
|
||||
usleep(15000);
|
||||
}
|
||||
|
||||
printf("End.\n");
|
||||
setColorRGBbuffered(0, 0, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
Bitbanged the GPIO to simulate SPI interface.
|
||||
Used CLKPIN and DATPIN.
|
||||
LED1,2,3 gives the indicator of different light levels.
|
||||
|
||||
Since P9813 and PN532 are using SPI, but P9813 is without ChipSelect wire, so any data on MOSI would be accepted by P9813, that leads to blinking light sometimes.
|
||||
|
||||
But this simulation is also unstable when raspberry pi under high load.
|
||||
|
||||
Reference here: http://www.seeedstudio.com/wiki/File:P9813_datasheet.pdf
|
||||
|
||||
Verified for 1 LED, and this should be compatible with multiple LEDs. It is said that 1024 LEDs is allowed.
|
||||
|
||||
This project is created by @DaochenShi (shidaochen@live.com)
|
||||
*/
|
||||
|
||||
|
||||
#include <wiringPi.h> // GPIO handling
|
||||
|
||||
// use wiringPi pinout
|
||||
#define CLKPIN 16
|
||||
#define DATPIN 15
|
||||
|
||||
// 20 microseconds to sleep
|
||||
#define CLOCKINTERVAL 20
|
||||
|
||||
|
||||
// This is to let other LEDs (pure light) controlled by GPIO(on/off)
|
||||
// Comment out the line would control P9813 only.
|
||||
//#define GPIO_PURE_LED
|
||||
|
||||
#ifdef GPIO_PURE_LED
|
||||
// use wiringPi pinout
|
||||
#define GPIO_PURE_LED1 0
|
||||
#define GPIO_PURE_LED2 2
|
||||
#define GPIO_PURE_LED3 3
|
||||
#endif
|
||||
|
||||
// Send a byte bit by bit using digitalWrite
|
||||
void sendByte(unsigned char b);
|
||||
|
||||
// Send a color(RGB) information to LED.
|
||||
void sendColor(unsigned char r, unsigned char g, unsigned char b);
|
||||
|
||||
// Set the color, used stored previous RGB information to avoid writing the same thing repeatedly.
|
||||
// This is 'buffered'. If the color is the same as previous color, we might ignore it.
|
||||
// Advantage: Fewer GPIO write leads to less CPU costs.
|
||||
// Disadvantage: Your request may not be applied, apply once every 2^8 times.
|
||||
void setColorRGBbuffered(unsigned char r, unsigned char g, unsigned char b);
|
||||
|
||||
// Set the color to LED, or the first LED.
|
||||
// This is not 'buffered', every time you invoke this method, it would send the signals directly to the bus.
|
||||
// Advantage: What you see is what you want.
|
||||
// Disadvantage: If you invoke this all the time, and write the same color, it costs a lot of CPU.
|
||||
void setColorRGB(unsigned char r, unsigned char g, unsigned char b);
|
||||
|
||||
// Set the color with multiple LEDs.
|
||||
// Not tested yet.
|
||||
void setColorRGBs(unsigned char* r, unsigned char* g, unsigned char* b, int count);
|
||||
|
||||
// Initializing
|
||||
// I noted this because occasionally the light was brought up, and cannot be set.
|
||||
// Because I rebooted the Pi, and forgot to set to OUTPUT direction.
|
||||
void initialP9813GPIO();
|
||||
Binary file not shown.
|
|
@ -0,0 +1,537 @@
|
|||
/*
|
||||
The original files can be found in http://blog.iteadstudio.com/to-drive-itead-pn532-nfc-module-with-raspberry-pi/
|
||||
All the contents are based on that article. I made some modification to the file, removed P2P communication, simplified some codes.
|
||||
|
||||
This files(include the header file and source file) are modified by @DaochenShi (shidaochen@live.com)
|
||||
*/
|
||||
|
||||
#include "PN532SPI.h"
|
||||
|
||||
//#define PN532DEBUG
|
||||
|
||||
byte pn532ack[] = {0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00};
|
||||
byte pn532response_firmwarevers[] = {0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03};
|
||||
|
||||
#define PN532_PACK_BUFF_SIZE 64
|
||||
#define SPISPEED 1000000
|
||||
|
||||
byte pn532_packetbuffer[PN532_PACK_BUFF_SIZE];
|
||||
|
||||
/*Construct PN532 object and construct PN532's own SPI interface object */
|
||||
int i,j;
|
||||
unsigned char a=0xaa;
|
||||
void initialPN532SPI()
|
||||
{
|
||||
j = wiringPiSetup();
|
||||
wiringPiSPISetup(channel, SPISPEED);
|
||||
#ifdef PN532DEBUG
|
||||
printf("wiringPiSetup is %d\n",j);
|
||||
#endif
|
||||
|
||||
#ifdef CHIPSELECT
|
||||
pinMode(_chipSelect, OUTPUT);
|
||||
|
||||
digitalWrite(_chipSelect, HIGH);
|
||||
digitalWrite(_chipSelect, LOW);
|
||||
#endif
|
||||
|
||||
sleep(1);
|
||||
|
||||
pn532_packetbuffer[0] = PN532_FIRMWAREVERSION;
|
||||
|
||||
sendCommandCheckAck(pn532_packetbuffer, 1, 1000);
|
||||
#ifdef CHIPSELECT
|
||||
//digitalWrite(_chipSelect, HIGH); // to prevent wrong select.
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
uint32_t getFirmwareVersion(void)
|
||||
{
|
||||
uint32_t response;
|
||||
|
||||
pn532_packetbuffer[0] = PN532_FIRMWAREVERSION;
|
||||
|
||||
if (!sendCommandCheckAck(pn532_packetbuffer, 1, 1000))
|
||||
return 0;
|
||||
|
||||
//nfcPN532Read data packet
|
||||
nfcPN532Read(pn532_packetbuffer, 12);
|
||||
//Check response headers.
|
||||
if (0 != strncmp((char *)pn532_packetbuffer, (char *)pn532response_firmwarevers, 6))
|
||||
{
|
||||
#ifdef PN532DEBUG
|
||||
for (response = 0; response < 12; response++)
|
||||
{
|
||||
printf("%X ", pn532response_firmwarevers[response]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
response = pn532_packetbuffer[6];
|
||||
response <<= 8;
|
||||
response |= pn532_packetbuffer[7];
|
||||
response <<= 8;
|
||||
response |= pn532_packetbuffer[8];
|
||||
response <<= 8;
|
||||
response |= pn532_packetbuffer[9];
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**********************************************************************/
|
||||
/*Function: Send command to PN532 with SPI and check the ACK. */
|
||||
/*Parameter:-uint8_t* cmd,The pointer that saves the command code to be sent;*/
|
||||
/* -uint8_t cmd_len,The number of bytes of the command; */
|
||||
/* -uint16_t timeout,default timeout is one second */
|
||||
/*Return: boolean,ture = send command successfully */
|
||||
boolean sendCommandCheckAck(uint8_t* cmd, uint8_t cmd_len, uint16_t timeout)
|
||||
{
|
||||
uint16_t timer = 0;
|
||||
|
||||
// nfcPN532Write the command
|
||||
writeCommand(cmd, cmd_len);
|
||||
|
||||
// Wait for chip to say it's ready!
|
||||
while (readSpiStatus() != PN532_SPI_READY)
|
||||
{
|
||||
if (timeout != 0)
|
||||
{
|
||||
timer+=20;
|
||||
if (timer > timeout)
|
||||
{
|
||||
#ifdef PN532DEBUG
|
||||
printf("timeout\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
}
|
||||
usleep(20 * 1000);
|
||||
}
|
||||
|
||||
// nfcPN532Read acknowledgement
|
||||
if (!checkSpiAck())
|
||||
{
|
||||
#ifdef PN532DEBUG
|
||||
printf("spi no answer\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
timer = 0;
|
||||
#ifdef PN532DEBUG
|
||||
printf("check spi finsh\n");
|
||||
#endif
|
||||
// Wait for chip to say its ready!
|
||||
while (readSpiStatus() != PN532_SPI_READY)
|
||||
{
|
||||
if (timeout != 0)
|
||||
{
|
||||
timer+=20;
|
||||
if (timer > timeout)
|
||||
#ifdef PN532DEBUG
|
||||
printf("nfcPN532Read spi timeout\n");
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
usleep(20 * 1000);
|
||||
}
|
||||
#ifdef PN532DEBUG
|
||||
printf("the spi return ture\n");
|
||||
#endif
|
||||
return true; // ack'd command
|
||||
}
|
||||
|
||||
boolean SAMConfig(void)
|
||||
{
|
||||
pn532_packetbuffer[0] = PN532_SAMCONFIGURATION;
|
||||
pn532_packetbuffer[1] = 0x01; // normal mode;
|
||||
pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second
|
||||
pn532_packetbuffer[3] = 0x01; // use IRQ pin! What's that?
|
||||
|
||||
if (! sendCommandCheckAck(pn532_packetbuffer, 4,1000))
|
||||
return false;
|
||||
|
||||
// nfcPN532Read data packet
|
||||
nfcPN532Read(pn532_packetbuffer, 8);
|
||||
|
||||
return (pn532_packetbuffer[5] == 0x15);
|
||||
}
|
||||
|
||||
|
||||
uint32_t authenticateBlock(uint8_t cardnumber /*1 or 2*/,uint32_t cid /*Card NUID*/, uint8_t blockaddress /*0 to 63*/,uint8_t authtype/*Either KEY_A or KEY_B */, uint8_t * keys)
|
||||
{
|
||||
|
||||
pn532_packetbuffer[0] = PN532_INDATAEXCHANGE;
|
||||
pn532_packetbuffer[1] = cardnumber; // either card 1 or 2 (tested for card 1)
|
||||
|
||||
if(authtype == KEY_A)
|
||||
{
|
||||
pn532_packetbuffer[2] = PN532_AUTH_WITH_KEYA;
|
||||
}
|
||||
else
|
||||
{
|
||||
pn532_packetbuffer[2] = PN532_AUTH_WITH_KEYB;
|
||||
}
|
||||
pn532_packetbuffer[3] = blockaddress; //This address can be 0-63 for MIFARE 1K card
|
||||
|
||||
pn532_packetbuffer[4] = keys[0];
|
||||
pn532_packetbuffer[5] = keys[1];
|
||||
pn532_packetbuffer[6] = keys[2];
|
||||
pn532_packetbuffer[7] = keys[3];
|
||||
pn532_packetbuffer[8] = keys[4];
|
||||
pn532_packetbuffer[9] = keys[5];
|
||||
|
||||
pn532_packetbuffer[10] = ((cid >> 24) & 0xFF);
|
||||
pn532_packetbuffer[11] = ((cid >> 16) & 0xFF);
|
||||
pn532_packetbuffer[12] = ((cid >> 8) & 0xFF);
|
||||
pn532_packetbuffer[13] = ((cid >> 0) & 0xFF);
|
||||
|
||||
if (! sendCommandCheckAck(pn532_packetbuffer, 14,1000))
|
||||
return false;
|
||||
|
||||
// nfcPN532Read data packet
|
||||
nfcPN532Read(pn532_packetbuffer, 2+6);
|
||||
|
||||
#ifdef PN532DEBUG
|
||||
int iter = 0;
|
||||
for(iter=0;iter<14;iter++)
|
||||
{
|
||||
printf("%X ",(pn532_packetbuffer[iter]));
|
||||
}
|
||||
printf("\n");
|
||||
// check some basic stuff
|
||||
|
||||
printf("AUTH");
|
||||
for(i=0;i<2+6;i++)
|
||||
{
|
||||
printf("%d\n",(pn532_packetbuffer[i]));
|
||||
printf(" ");
|
||||
}
|
||||
#endif
|
||||
|
||||
if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
/****************************************************************************/
|
||||
/*Function: nfcPN532Read a block(16 bytes) from the tag and stores in the parameter.*/
|
||||
/*Parameter:-uint8_t cardnumber,can be 1 or 2; */
|
||||
/* -blockaddress,range from 0 to 63; */
|
||||
/* -uint8_t* block,will save 16bytes that nfcPN532Read from tag. */
|
||||
/*Return: boolean */
|
||||
boolean readMemoryBlock(uint8_t cardnumber,uint8_t blockaddress,uint8_t * block)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
pn532_packetbuffer[0] = PN532_INDATAEXCHANGE;
|
||||
pn532_packetbuffer[1] = cardnumber; // either card 1 or 2 (tested for card 1)
|
||||
pn532_packetbuffer[2] = PN532_MIFARE_READ;
|
||||
pn532_packetbuffer[3] = blockaddress; //This address can be 0-63 for MIFARE 1K card
|
||||
|
||||
if (! sendCommandCheckAck(pn532_packetbuffer, 4,1000))
|
||||
return false;
|
||||
|
||||
nfcPN532Read(pn532_packetbuffer, 64);
|
||||
#ifdef PN532DEBUG
|
||||
for( i = 0; i < 64; i++)
|
||||
{
|
||||
printf("%x ", pn532_packetbuffer[i]);
|
||||
if(i%8==7)
|
||||
printf("\r\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
// nfcPN532Read data packet
|
||||
nfcPN532Read(pn532_packetbuffer, 18+6);
|
||||
// check some basic stuff
|
||||
#ifdef PN532DEBUG
|
||||
printf("nfcPN532Read");
|
||||
#endif
|
||||
for(i=8;i<18+6;i++)
|
||||
{
|
||||
block[i-8] = pn532_packetbuffer[i];
|
||||
#ifdef PN532DEBUG
|
||||
printf("%d\n",(pn532_packetbuffer[i]));
|
||||
#endif
|
||||
}
|
||||
|
||||
if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
|
||||
{
|
||||
return true; //nfcPN532Read successful
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/*Function: nfcPN532Write a block(16 bytes) to the tag. */
|
||||
/*Parameter:-uint8_t cardnumber,can be 1 or 2; */
|
||||
/* -blockaddress,range from 0 to 63; */
|
||||
/* -uint8_t* block,saves 16bytes that will nfcPN532Write to the tag. */
|
||||
/*Return: boolean */
|
||||
/*Note:Donot nfcPN532Write to Sector Trailer Block unless you know what you are doing.*/
|
||||
boolean writeMemoryBlock(uint8_t cardnumber,uint8_t blockaddress,uint8_t * block)
|
||||
{
|
||||
uint8_t bytes;
|
||||
|
||||
pn532_packetbuffer[0] = PN532_INDATAEXCHANGE;
|
||||
pn532_packetbuffer[1] = cardnumber; // either card 1 or 2 (tested for card 1)
|
||||
pn532_packetbuffer[2] = PN532_MIFARE_WRITE;
|
||||
pn532_packetbuffer[3] = blockaddress;
|
||||
|
||||
for(bytes = 0; bytes < 16; bytes++)
|
||||
{
|
||||
pn532_packetbuffer[4+bytes] = block[bytes];
|
||||
}
|
||||
|
||||
if (! sendCommandCheckAck(pn532_packetbuffer, 20,1000))
|
||||
return false;
|
||||
// nfcPN532Read data packet
|
||||
nfcPN532Read(pn532_packetbuffer, 2+6);
|
||||
|
||||
#ifdef PN532DEBUG
|
||||
// check some basic stuff
|
||||
printf("nfcPN532Write");
|
||||
|
||||
for(i=0;i<2+6;i++)
|
||||
{
|
||||
printf("%d\n",(pn532_packetbuffer[i]));
|
||||
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
if((pn532_packetbuffer[6] == 0x41) && (pn532_packetbuffer[7] == 0x00))
|
||||
{
|
||||
return true; //nfcPN532Write successful
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t readPassiveTargetID(uint8_t cardbaudrate)
|
||||
{
|
||||
uint32_t cid;
|
||||
uint16_t sens_res;
|
||||
uint8_t i;
|
||||
|
||||
pn532_packetbuffer[0] = PN532_INLISTPASSIVETARGET;
|
||||
pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later)
|
||||
pn532_packetbuffer[2] = cardbaudrate;
|
||||
|
||||
if (! sendCommandCheckAck(pn532_packetbuffer, 3,1000))
|
||||
return 0x0; // no cards nfcPN532Read
|
||||
|
||||
// nfcPN532Read data packet
|
||||
nfcPN532Read(pn532_packetbuffer, 20);// why 20?! buffer total is 64!
|
||||
// check some basic stuff
|
||||
|
||||
#ifdef PN532DEBUG
|
||||
printf("Found ");
|
||||
printf("%d",(pn532_packetbuffer[7]));
|
||||
printf(" tags\n");
|
||||
#endif
|
||||
|
||||
if (pn532_packetbuffer[7] != 1)
|
||||
return 0;
|
||||
|
||||
sens_res = pn532_packetbuffer[9];
|
||||
sens_res <<= 8;
|
||||
sens_res |= pn532_packetbuffer[10];
|
||||
#ifdef PN532DEBUG
|
||||
printf("Sens Response: ");
|
||||
printf("%d\n",(sens_res));
|
||||
printf("Sel Response: ");
|
||||
printf("%d",(pn532_packetbuffer[11]));
|
||||
printf("\n");
|
||||
#endif
|
||||
cid = 0;
|
||||
|
||||
for (i=0; i< pn532_packetbuffer[12]; i++)
|
||||
{
|
||||
cid <<= 8;
|
||||
cid |= pn532_packetbuffer[13+i];
|
||||
#ifdef PN532DEBUG
|
||||
printf(" 0x");
|
||||
printf("%X\n",(pn532_packetbuffer[13+i]));
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef PN532DEBUG
|
||||
printf("TargetID");
|
||||
for(i=0;i<20;i++)
|
||||
{
|
||||
printf("%d\n",(pn532_packetbuffer[i]));
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
return cid;
|
||||
}
|
||||
|
||||
/**********************************************************************/
|
||||
/*Function: nfcPN532Read n bytes data and it stores in the parameter . */
|
||||
/*Parameter:-uint8_t* buff,saves the data nfcPN532Read from PN532; */
|
||||
/* -uint8_t n,tells it wll nfcPN532Read n bytes. */
|
||||
/*Return: void */
|
||||
void nfcPN532Read(uint8_t* buff, uint8_t n)
|
||||
{
|
||||
uint8_t i;
|
||||
#ifdef CHIPSELECT
|
||||
digitalWrite(_chipSelect, LOW);
|
||||
#endif
|
||||
usleep(2000);
|
||||
nfcPN532Write(PN532_SPI_DATAREAD);
|
||||
|
||||
#ifdef PN532DEBUG
|
||||
printf("Reading:\n");
|
||||
#endif
|
||||
|
||||
for (i=0; i < n; i ++)
|
||||
{
|
||||
usleep(1000);
|
||||
buff[i] = readF();
|
||||
#ifdef PN532DEBUG
|
||||
printf("debug readf is %d\n",buff[i]);
|
||||
#endif
|
||||
}
|
||||
#ifdef CHIPSELECT
|
||||
digitalWrite(_chipSelect, HIGH);
|
||||
#endif
|
||||
}
|
||||
|
||||
void writeCommand(uint8_t* cmd, uint8_t cmd_len)
|
||||
{
|
||||
uint8_t checksum;
|
||||
uint8_t cmdlen_1;
|
||||
uint8_t i;
|
||||
uint8_t checksum_1;
|
||||
|
||||
cmd_len++;
|
||||
|
||||
#ifdef PN532DEBUG
|
||||
printf("Sending: \n");
|
||||
#endif
|
||||
|
||||
#ifdef CHIPSELECT
|
||||
digitalWrite(_chipSelect, LOW);
|
||||
#endif
|
||||
usleep(2000); // or whatever the delay is for waking up the board
|
||||
|
||||
nfcPN532Write(PN532_SPI_DATAWRITE); //0x01
|
||||
|
||||
checksum = PN532_PREAMBLE + PN532_PREAMBLE + PN532_STARTCODE2;
|
||||
nfcPN532Write(PN532_PREAMBLE); //0x00
|
||||
nfcPN532Write(PN532_PREAMBLE); //0x00
|
||||
nfcPN532Write(PN532_STARTCODE2); //0xff
|
||||
|
||||
nfcPN532Write(cmd_len); //0x02
|
||||
cmdlen_1 = ~cmd_len + 1;
|
||||
nfcPN532Write(cmdlen_1); //0x01
|
||||
|
||||
nfcPN532Write(PN532_HOSTTOPN532); //0xd4
|
||||
checksum += PN532_HOSTTOPN532;
|
||||
|
||||
#ifdef PN532DEBUG
|
||||
printf("preamble is %X\n",(PN532_PREAMBLE));
|
||||
printf("startcode2 is %X\n",(PN532_STARTCODE2));
|
||||
printf("cmd_len is %X\n",(cmd_len));
|
||||
printf("cmdlen_1 is %X\n",(cmdlen_1));
|
||||
printf("hosttopn532 is %X\n",(PN532_HOSTTOPN532));
|
||||
#endif
|
||||
|
||||
for (i=0; i<cmd_len-1; i++)
|
||||
{
|
||||
nfcPN532Write(cmd[i]);
|
||||
checksum += cmd[i];
|
||||
#ifdef PN532DEBUG
|
||||
printf("cmd[i] is %X\n",(cmd[i]));
|
||||
#endif
|
||||
}
|
||||
|
||||
checksum_1 = ~checksum;
|
||||
nfcPN532Write(checksum_1);
|
||||
nfcPN532Write(PN532_POSTAMBLE);
|
||||
#ifdef CHIPSELECT
|
||||
digitalWrite(_chipSelect, HIGH);
|
||||
#endif
|
||||
|
||||
#ifdef PN532DEBUG
|
||||
printf("checksum is %d\n", (checksum_1));
|
||||
printf("postamble is %d\n", (PN532_POSTAMBLE));
|
||||
printf("postamble is %d\n", (PN532_POSTAMBLE));
|
||||
#endif
|
||||
}
|
||||
/************** high level SPI */
|
||||
boolean checkSpiAck()
|
||||
{
|
||||
uint8_t ackbuff[6];
|
||||
nfcPN532Read(ackbuff, 6);
|
||||
return (0 == strncmp((char *)ackbuff, (char *)pn532ack, 6));
|
||||
}
|
||||
|
||||
/************** mid level SPI */
|
||||
uint8_t readSpiStatus(void)
|
||||
{
|
||||
uint8_t status;
|
||||
|
||||
#ifdef CHIPSELECT
|
||||
digitalWrite(_chipSelect, LOW);
|
||||
#endif
|
||||
usleep(2000);
|
||||
nfcPN532Write(PN532_SPI_STATREAD);
|
||||
status = readF();
|
||||
#ifdef CHIPSELECT
|
||||
digitalWrite(_chipSelect, HIGH);
|
||||
#endif
|
||||
return status;
|
||||
}
|
||||
|
||||
/************** low level SPI ********/
|
||||
/*Function:Transmit a byte to PN532 through the SPI interface. */
|
||||
void nfcPN532Write(uint8_t _data)
|
||||
{
|
||||
unsigned char writeData = 0, p;
|
||||
|
||||
for(p=0;p<8;p++)
|
||||
{
|
||||
if(_data & 0x01)
|
||||
writeData |= 1<<(7-p);
|
||||
|
||||
_data = _data>>1;
|
||||
}
|
||||
wiringPiSPIDataRW(channel, &writeData, 1);
|
||||
|
||||
}
|
||||
|
||||
/*Function:Receive a byte from PN532 through the SPI interface */
|
||||
uint8_t readF(void)
|
||||
{
|
||||
unsigned char readData,redata = 0,p;
|
||||
wiringPiSPIDataRW(channel, &readData, 1);
|
||||
|
||||
for(p=0;p<8;p++)
|
||||
{
|
||||
if(readData & 0x01)
|
||||
{
|
||||
redata |= 1<<(7-p);
|
||||
}
|
||||
readData = readData >> 1;
|
||||
}
|
||||
return redata;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
The original files can be found in http://blog.iteadstudio.com/to-drive-itead-pn532-nfc-module-with-raspberry-pi/
|
||||
All the contents are based on that article. I made some modification to the file, removed P2P communication, simplified some codes.
|
||||
|
||||
This files(include the header file and source file) are modified by @DaochenShi (shidaochen@live.com)
|
||||
*/
|
||||
|
||||
#ifndef __PN532_H__
|
||||
#define __PN532_H__
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <wiringPiSPI.h>
|
||||
|
||||
#define PN532_PREAMBLE 0x00
|
||||
#define PN532_STARTCODE1 0x00
|
||||
#define PN532_STARTCODE2 0xFF
|
||||
#define PN532_POSTAMBLE 0x00
|
||||
|
||||
#define PN532_HOSTTOPN532 0xD4
|
||||
|
||||
#define PN532_FIRMWAREVERSION 0x02
|
||||
#define PN532_GETGENERALSTATUS 0x04
|
||||
#define PN532_SAMCONFIGURATION 0x14
|
||||
#define PN532_INLISTPASSIVETARGET 0x4A
|
||||
#define PN532_INDATAEXCHANGE 0x40
|
||||
#define PN532_INJUMPFORDEP 0x56
|
||||
#define PN532_TGINITASTARGET 0x8C
|
||||
#define PN532_TGGETDATA 0x86
|
||||
#define PN532_TGSETDATA 0x8E
|
||||
|
||||
#define PN532_MIFARE_READ 0x30
|
||||
#define PN532_MIFARE_WRITE 0xA0
|
||||
|
||||
#define PN532_AUTH_WITH_KEYA 0x60
|
||||
#define PN532_AUTH_WITH_KEYB 0x61
|
||||
|
||||
#define PN532_WAKEUP 0x55
|
||||
|
||||
#define PN532_SPI_STATREAD 0x02
|
||||
#define PN532_SPI_DATAWRITE 0x01
|
||||
#define PN532_SPI_DATAREAD 0x03
|
||||
|
||||
#define PN532_SPI_READY 0x01
|
||||
|
||||
#define PN532_MIFARE_ISO14443A 0x0
|
||||
|
||||
#define KEY_A 1
|
||||
#define KEY_B 2
|
||||
|
||||
#define PN532_BAUDRATE_201K 1
|
||||
#define PN532_BAUDRATE_424K 2
|
||||
|
||||
#define boolean int
|
||||
#define channel 1
|
||||
#define byte unsigned char
|
||||
#define false 0
|
||||
#define true 1
|
||||
|
||||
#define CHIPSELECT
|
||||
// This is which pin you used for ChipSelect. SPI slave accept the instrctions when its CS pulled to ground.
|
||||
#ifdef CHIPSELECT
|
||||
#define _chipSelect 11
|
||||
#endif
|
||||
|
||||
void initialPN532SPI(void);
|
||||
boolean SAMConfig(void);
|
||||
uint32_t getFirmwareVersion(void);
|
||||
uint32_t readPassiveTargetID(uint8_t cardbaudrate);
|
||||
uint32_t authenticateBlock(
|
||||
uint8_t cardnumber /*1 or 2*/,
|
||||
uint32_t cid /*Card NUID*/,
|
||||
uint8_t blockaddress /*0 to 63*/,
|
||||
uint8_t authtype /*Either KEY_A or KEY_B */,
|
||||
uint8_t* keys);
|
||||
boolean readMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block);
|
||||
//boolean writeMemoryBlock(uint8_t cardnumber /*1 or 2*/,uint8_t blockaddress /*0 to 63*/, uint8_t * block);
|
||||
//uint32_t configurePeerAsInitiator(uint8_t baudrate /* Any number from 0-2. 0 for 106kbps or 1 for 201kbps or 2 for 424kbps */); //106kps is not supported
|
||||
//uint32_t configurePeerAsTarget();
|
||||
//boolean initiatorTxRx(char* dataOut,char* dataIn);
|
||||
//uint32_t targetTxRx(char* dataOut,char* dataIn);
|
||||
|
||||
boolean sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout);
|
||||
|
||||
|
||||
void nfcPN532Write(uint8_t _data);
|
||||
uint8_t readF(void);
|
||||
uint8_t readSpiStatus(void);
|
||||
boolean checkSpiAck();
|
||||
void nfcPN532Read(uint8_t* buff, uint8_t n);
|
||||
void writeCommand(uint8_t* cmd, uint8_t cmdlen);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
This one is to read RFID card by PN532 through SPI, respond to LED lights(P9813) and a beeper
|
||||
|
||||
Input: PN532(SPI)
|
||||
Output: P9813, GPIO 1 port
|
||||
|
||||
NFC module: http://www.seeedstudio.com/wiki/NFC_Shield_V2.0
|
||||
|
||||
LED light: http://www.seeedstudio.com/wiki/Grove_-_Chainable_RGB_LED
|
||||
|
||||
beeper: http://item.taobao.com/item.htm?spm=0.0.0.0.A0Zkth&id=6859691900
|
||||
|
||||
|
||||
Outline:
|
||||
Repeatedly read from NFC, use cardID to identify, // currently I don't know how to read the blocks.
|
||||
if it matches some fixed number, it shows green light and beepGPIO;
|
||||
otherwise, it shows red and beepGPIO another sound.
|
||||
|
||||
You need the wiringPi lib.
|
||||
|
||||
Compile:
|
||||
make
|
||||
|
||||
Run:
|
||||
sudo ./NFClight
|
||||
|
||||
|
||||
Thanks the following persons developed the libs which this project used.
|
||||
wiringPi lib from: Gordons Projects @ https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
nfc lib from: Katherine @ http://blog.iteadstudio.com/to-drive-itead-pn532-nfc-module-with-raspberry-pi/
|
||||
|
||||
This project is created by @DaochenShi (shidaochen@live.com)
|
||||
|
||||
|
|
@ -0,0 +1,303 @@
|
|||
/*
|
||||
This one is to read RFID card by PN532 through SPI, respond to LED lights(P9813) and a beeper
|
||||
|
||||
Input: PN532(SPI)
|
||||
Output: P9813, GPIO 1 port
|
||||
|
||||
NFC module: http://www.seeedstudio.com/wiki/NFC_Shield_V2.0
|
||||
LED light: http://www.seeedstudio.com/wiki/Grove_-_Chainable_RGB_LED
|
||||
beeper: http://item.taobao.com/item.htm?spm=0.0.0.0.A0Zkth&id=6859691900
|
||||
|
||||
|
||||
Outline:
|
||||
Repeatedly read from NFC, use cardID to identify, // currently I don't know how to read the blocks.
|
||||
if it matches some fixed number, it shows white light and beepGPIO;
|
||||
if it matches another number, the program exits.
|
||||
otherwise, it gradually dim the light.
|
||||
|
||||
You need the wiringPi lib.
|
||||
|
||||
Compile:
|
||||
make
|
||||
|
||||
Run:
|
||||
sudo ./NFClight
|
||||
|
||||
|
||||
Thanks the following persons developed the libs which this project used.
|
||||
wiringPi lib from: Gordons Projects @ https://projects.drogon.net/raspberry-pi/wiringpi/
|
||||
nfc lib from: Katherine @ http://blog.iteadstudio.com/to-drive-itead-pn532-nfc-module-with-raspberry-pi/
|
||||
|
||||
This project is created by @DaochenShi (shidaochen@live.com)
|
||||
|
||||
20140322 maintaince:
|
||||
LED init and control should be done by P9813GPIO, so any LED init related are removed from this file. Go check P9813GPIO.c
|
||||
*/
|
||||
|
||||
|
||||
#include "PN532SPI.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h> // for usleep
|
||||
#include <signal.h> // for SIG_IGN etc.
|
||||
#include <fcntl.h> // for O_WRONLY
|
||||
#include <errno.h> // for errno
|
||||
#include <time.h> // for clock()
|
||||
#include <pthread.h> // for multithreading
|
||||
|
||||
//#define DEBUG_COLOR
|
||||
|
||||
//#define BEEPER_GPIO_PIN 6
|
||||
|
||||
int initialWiringPi();
|
||||
|
||||
void break_program(int sig);
|
||||
void* adjust_color();
|
||||
//#define MAXWAITINGTIME 10
|
||||
static int loopingStatus = 0;
|
||||
static unsigned char colorBase[3];
|
||||
static unsigned char colorTarget[3];
|
||||
|
||||
//gcc main.c -lwiringPi P9813GPIO.h P9813GPIO.c
|
||||
|
||||
void rgbcycle(void)
|
||||
{
|
||||
int i=0;
|
||||
while (1)
|
||||
{
|
||||
i++;
|
||||
if (i>255)
|
||||
i=0;
|
||||
setColorRGBbuffered(0,0,1);
|
||||
printf("color = %d\n",i);
|
||||
usleep(20 * 1000);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
{
|
||||
|
||||
unsigned char r[]={255,0,0};
|
||||
unsigned char g[]={0,255,0};
|
||||
unsigned char b[]={0,0,255};
|
||||
int i;
|
||||
// NFC related, the read card ID. Currently I can only read this. I don't know how to get other infos.
|
||||
// uint32_t cardID;
|
||||
// int allowFailureTimes = 2;
|
||||
|
||||
// // run this program in background when not in Debug mode
|
||||
// #ifndef DEBUG
|
||||
// {
|
||||
// pid_t pid, sid;
|
||||
// pid = fork();
|
||||
// if (pid < 0)
|
||||
// {
|
||||
// exit(EXIT_FAILURE);
|
||||
// }
|
||||
// if (pid > 0)
|
||||
// {
|
||||
// exit(EXIT_SUCCESS);
|
||||
// }
|
||||
|
||||
// // change file mode mask
|
||||
// umask(0);
|
||||
// // open logs,,, but I did not record any logs
|
||||
|
||||
// // create SID for child process
|
||||
// sid = setsid();
|
||||
// if (sid < 0)
|
||||
// {
|
||||
// exit(EXIT_FAILURE);
|
||||
// }
|
||||
|
||||
// close(STDIN_FILENO);
|
||||
// close(STDOUT_FILENO);
|
||||
// //close(STDERR_FILENO);
|
||||
// }
|
||||
// #endif
|
||||
|
||||
initialWiringPi();
|
||||
// pthread_t _colorThread;
|
||||
// loopingStatus = 1;
|
||||
// pthread_create(&_colorThread, NULL, adjust_color, NULL);
|
||||
// adjust_color();
|
||||
printf("All initialized...\n");
|
||||
// adjust_color();
|
||||
// setColorRGB(0, 0, 0);
|
||||
// rgbcycle();
|
||||
setColorRGBs(&r[0],&g[0],&b[0],3);
|
||||
for ( i = 0; i < 3; i++ )
|
||||
{
|
||||
printf("led %d color = %d,%d,%d\n",i,r[i], g[i], b[i]);
|
||||
}
|
||||
// while(loopingStatus)
|
||||
// {
|
||||
// #ifdef DEBUG
|
||||
// printf("waiting for card read...\n");
|
||||
// #endif
|
||||
|
||||
// cardID = readPassiveTargetID(PN532_MIFARE_ISO14443A);
|
||||
|
||||
// if ( cardID == 0 && allowFailureTimes > 0)
|
||||
// {
|
||||
// allowFailureTimes--;
|
||||
// continue;
|
||||
// }
|
||||
// if ( cardID != 0 )
|
||||
// {
|
||||
// allowFailureTimes = 2;
|
||||
// if ((cardID % 256) == authCID)
|
||||
// {
|
||||
// colorBase[0] = 0xFF; colorBase[1] = 0xFF; colorBase[2] = 0xFF;
|
||||
// colorTarget[0] = 0xFF; colorTarget[1] = 0xFF; colorTarget[2] = 0xFF;
|
||||
// setColorRGBbuffered(colorBase[0], colorBase[1], colorBase[2]);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if ((cardID % 256) == exitCID)
|
||||
// {
|
||||
// loopingStatus = 0;
|
||||
// break;
|
||||
// }
|
||||
// colorTarget[0] = (cardID >> 16) % 256;
|
||||
// colorTarget[1] = (cardID >> 8) % 256;
|
||||
// colorTarget[2] = cardID % 256;
|
||||
// #ifdef DEBUG
|
||||
// printf("cardID = %X\n", cardID);
|
||||
// #endif
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// // allowFailureTimes < 0 and cardID == 0, which means no card.
|
||||
// #ifdef DEBUG
|
||||
// printf("no card\n");
|
||||
// #endif
|
||||
// colorTarget[0] = 0; colorTarget[1] = 0; colorTarget[2] = 0;
|
||||
// }
|
||||
// sleep(1);
|
||||
|
||||
// }
|
||||
// colorTarget[0] = 0; colorTarget[1] = 0; colorTarget[2] = 0;
|
||||
// colorBase[0] = 2; colorBase[1] = 3; colorBase[2] = 6;
|
||||
// setColorRGB(10, 7, 6);
|
||||
// #ifdef BEEPER_GPIO_PIN
|
||||
// digitalWrite(BEEPER_GPIO_PIN, 1);
|
||||
// usleep(100 * 1000);
|
||||
// digitalWrite(BEEPER_GPIO_PIN, 0);
|
||||
// #endif
|
||||
// sleep(1);
|
||||
// setColorRGB(0, 0, 0);
|
||||
|
||||
// pthread_join(_colorThread, NULL);
|
||||
// return 0;
|
||||
}
|
||||
|
||||
// Set-up some necessary wiringPi and devices.
|
||||
int initialWiringPi()
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf("Initializing.\n");
|
||||
#endif
|
||||
|
||||
// uint32_t nfcVersion;
|
||||
|
||||
// First to setup wiringPi
|
||||
if (wiringPiSetup() < 0)
|
||||
{
|
||||
fprintf(stderr, "Unable to setup wiringPi: %s \n", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
#ifdef DEBUG
|
||||
printf("Set wiringPi.\n");
|
||||
#endif
|
||||
|
||||
|
||||
// Initializing LEDs
|
||||
initialP9813GPIO();
|
||||
|
||||
// Hook crtl+c event
|
||||
signal(SIGINT, break_program);
|
||||
#ifdef DEBUG
|
||||
printf("Hooked Ctrl+C.\n");
|
||||
#endif
|
||||
|
||||
// #ifdef BEEPER_GPIO_PIN
|
||||
// pinMode(BEEPER_GPIO_PIN, OUTPUT);
|
||||
// digitalWrite(BEEPER_GPIO_PIN, 1);
|
||||
// usleep(100 * 1000);
|
||||
// digitalWrite(BEEPER_GPIO_PIN, 0);
|
||||
// #endif
|
||||
// // Use init function from nfc.c
|
||||
// // why you use such a simple function name?!
|
||||
// initialPN532SPI();
|
||||
// #ifdef DEBUG
|
||||
// printf("NFC initialized begin().\n");
|
||||
// #endif
|
||||
// nfcVersion = getFirmwareVersion();
|
||||
// if (!nfcVersion)
|
||||
// {
|
||||
// fprintf(stderr, "Cannot find PN53x board after getFirmwareVersion.\n");
|
||||
// exit(1);
|
||||
// }
|
||||
|
||||
// SAMConfig();
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("Initialized.\n");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Accept Ctrl+C command, this seems not work when main process is forked.
|
||||
void break_program(int sig)
|
||||
{
|
||||
signal(sig, SIG_IGN);
|
||||
loopingStatus = 0;
|
||||
printf("Program end.\n");
|
||||
signal(sig, SIG_DFL);
|
||||
}
|
||||
|
||||
// static clock_t previousTimeClock;
|
||||
// Adjust the P9813 color by another thread.
|
||||
void* adjust_color(void)
|
||||
{
|
||||
// LED related
|
||||
int colorMaxDiff;
|
||||
while (1){
|
||||
/// Parse current color, and gradually fade-in/fade-out
|
||||
|
||||
printf("Changing color...loop = %d\n", loopingStatus);
|
||||
|
||||
colorMaxDiff = 0;
|
||||
colorMaxDiff = (colorMaxDiff > abs(colorBase[0] - colorTarget[0])) ? colorMaxDiff : abs(colorBase[0] - colorTarget[0]);
|
||||
colorMaxDiff = (colorMaxDiff > abs(colorBase[1] - colorTarget[1])) ? colorMaxDiff : abs(colorBase[1] - colorTarget[1]);
|
||||
colorMaxDiff = (colorMaxDiff > abs(colorBase[2] - colorTarget[2])) ? colorMaxDiff : abs(colorBase[2] - colorTarget[2]);
|
||||
|
||||
|
||||
colorMaxDiff = (colorMaxDiff > 15) ? colorMaxDiff/16 : colorMaxDiff;
|
||||
|
||||
if (colorMaxDiff)
|
||||
{
|
||||
{
|
||||
colorBase[0] = colorBase[0] - (colorBase[0] - colorTarget[0]) / colorMaxDiff;
|
||||
colorBase[1] = colorBase[1] - (colorBase[1] - colorTarget[1]) / colorMaxDiff;
|
||||
colorBase[2] = colorBase[2] - (colorBase[2] - colorTarget[2]) / colorMaxDiff;
|
||||
setColorRGBbuffered(colorBase[0], colorBase[1], colorBase[2]);
|
||||
}
|
||||
|
||||
// printf("interval of previous %d\n", (clock() - previousTimeClock));
|
||||
// previousTimeClock = clock();
|
||||
printf("colorMaxDiff = %d, %dR->%dR, %dG->%dG, %dB->%dB\n",
|
||||
colorMaxDiff,
|
||||
colorBase[0], colorTarget[0],
|
||||
colorBase[1], colorTarget[1],
|
||||
colorBase[2], colorTarget[2]);
|
||||
|
||||
}
|
||||
usleep(20 * 1000);
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue