/
main.go
65 lines (57 loc) · 1.33 KB
/
main.go
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
package main
import (
"fmt"
"os"
. "github.com/conclave/pcduino/core"
"github.com/conclave/pcduino/module/pn532"
)
const (
SCK = 13
MOSI = 11
SS = 10
MISO = 12
)
var nfc *pn532.PN532
var written bool = false
func init() {
Init()
setup()
}
func main() {
for {
loop()
}
}
func setup() {
nfc = pn532.New(SCK, MISO, MOSI, SS)
version := nfc.GetFirmwareVersion()
if version == 0 {
fmt.Fprintln(os.Stderr, "PN53x board not found")
os.Exit(-1)
}
nfc.SAMConfig() // configure board to read RFID tags and cards
fmt.Printf("Found chip PN5: %x\n", byte((version>>24)&0xFF))
fmt.Printf("Firmware ver. %d.%d\n", byte((version>>16)&0xFF), byte((version>>8)&0xFF))
fmt.Printf("Supports: %d\n", byte(version&0xFF))
}
func loop() {
id := nfc.ReadPassiveTargetID(pn532.PN532_MIFARE_ISO14443A)
if id != 0 {
fmt.Printf("Read card #%d\n", id)
keys := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
writeBuffer := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
if nfc.AuthenticateBlock(1, byte(id), 0x08, pn532.KEY_A, keys) { //authenticate block 0x08
if !written {
written = nfc.WriteMemoryBlock(1, 0x08, writeBuffer)
if written {
fmt.Println("Write done")
}
}
block := make([]byte, 16)
if nfc.ReadMemoryBlock(1, 0x08, block) {
fmt.Printf("Read block_0x08: %x\n", block)
}
}
}
Delay(500)
}