// Tries to write an entire 4-byte page at the specified block address // data should be exactly 4 bytes long // TAG Type PAGES USER START USER STOP // -------- ----- ---------- --------- // NTAG 203 42 4 39 // NTAG 213 45 4 39 // NTAG 215 135 4 129 // NTAG 216 231 4 225 func NtagWritePage(device Device, page uint8, data []byte) error { if page < 4 || page > 225 { return ErrPageOutOfRange } if len(data) != 4 { return ErrInvalidDataLen } p := []byte{ COMMAND_INDATAEXCHANGE, 1, MIFARE_ULTRALIGHT_CMD_WRITE, page, data[0], data[1], data[2], data[3], } if !SendCommandCheckAck(device, p, defaultTimeoutMs) { return ErrNoAckReceived } core.Delay(10) p = make([]byte, 26) device.ReadData(p) return nil }
func clockRoutine() { if clock != nil { var err error for { if now, err = clock.Get(); err != nil { printToLcd(fmt.Sprintf("err in read clock: %v", err)) } core.Delay(step) } } else { for { now = time.Now() core.Delay(step) } } }
// Return true if the PN532 is ready with a response func (id *deviceSPI) Ready() bool { id.ss.DigitalWrite(core.LOW) core.Delay(2) id.write([]byte{SPI_STATREAD}) p := make([]byte, 1) id.ReadData(p) return p[0] == SPI_READY }
// Reads data into p from the PN532 via SPI func (id *deviceSPI) ReadData(p []byte) { id.ss.DigitalWrite(core.LOW) core.Delay(2) id.write([]byte{SPI_DATAREAD}) id.read(p) id.ss.DigitalWrite(core.HIGH) return }
func printToLcd(s string) { for len(s) > lcd_size { printToLcd2(s[:lcd_size]) s = s[lcd_size:] core.Delay(1000) } if len(s) > 0 { printToLcd2(s) } }
func openDeviceI2c(irq, rst uint8) (d *deviceI2c, err error) { dev, err := bus.NewI2C(0, 0x01) if err != nil { return } d = &deviceI2c{ irq: core.Pin(irq), reset: core.Pin(rst), dev: dev, } d.irq.Input() d.reset.Output() d.reset.DigitalWrite(core.HIGH) d.reset.DigitalWrite(core.LOW) core.Delay(400) d.reset.DigitalWrite(core.HIGH) core.Delay(10) return }
func (id *Device) Reset() (err error) { if err = id.WriteByte(CommandReg, PCD_RESETPHASE); err != nil { return } core.Delay(10) if err = id.ClearMask(TxControlReg, 0x03); err != nil { return } core.Delay(10) if err = id.SetMask(TxControlReg, 0x03); err != nil { return } if err = id.WriteByte(TModeReg, 0x8D); err != nil { return } if err = id.WriteByte(TPrescalerReg, 0x3E); err != nil { return } if err = id.WriteByte(TReloadRegL, 30); err != nil { return } if err = id.WriteByte(TReloadRegH, 0); err != nil { return } if err = id.WriteByte(TxASKReg, 0x40); err != nil { return } if err = id.WriteByte(ModeReg, 0x3D); err != nil { return } if err = id.WriteByte(RxThresholdReg, 0x84); err != nil { return } if err = id.WriteByte(RFCfgReg, 0x68); err != nil { return } if err = id.WriteByte(GsNReg, 0xff); err != nil { return } err = id.WriteByte(CWGsCfgReg, 0x2f) return }
// Waits until the PN532 is ready or timeout func (id *deviceSPI) WaitReady(timeout int64) bool { var timer int64 = 0 for !id.Ready() { if timeout != 0 { timer += 10 if timer > timeout { return false } } core.Delay(10) } return true }
func loop() { now := time.Now() if sysinfo, err := sys.Info(); err == nil { lcd.Clear() lcd.DrawString(0, 0, hostname) lcd.DrawLine(0, 9, 83, 9, pcd8544.BLACK) lcd.DrawString(0, 12, "UP "+sysinfo.Uptime.String()) lcd.DrawString(0, 20, fmt.Sprintf("LD %2.1f %2.1f %2.1f", sysinfo.Loads[0], sysinfo.Loads[1], sysinfo.Loads[2])) lcd.DrawString(0, 28, fmt.Sprintf("%v %.2d:%.2d:%.2d", now.Weekday().String()[:3], now.Hour(), now.Minute(), now.Second())) lcd.DrawString(0, 36, addr) lcd.DrawLine(0, 45, 83, 45, pcd8544.BLACK) lcd.Display() } core.Delay(step) }
func main() { flag.Parse() fmt.Println("[Constantine]") initLcd() initService() if err := initClock(); err != nil { printToLcd(fmt.Sprintf("RTC Error: %v", err)) } else { printToLcd("RTC ready.") } core.Delay(500) go clockRoutine() go lcdRoutine() <-quit }
func main() { flag.Int64Var(&step, "step", 5000, "clock step") flag.Parse() lcd = pcd8544.OpenLCD(19, 26, 13, 5, 6, 60) core.Delay(500) ch := make(chan os.Signal, 1) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) go func() { for s := range ch { fmt.Printf("%v caught, exit\n", s) lcd.Reset() os.Exit(0) } }() for { loop() } }
// Writes a command to the PN532, automatically inserting the preamble and required frame details (checksum, len, etc.) func (id *deviceSPI) WriteCommand(p []byte) { length := len(p) + 1 var checksum byte = PREAMBLE + PREAMBLE + STARTCODE2 id.ss.DigitalWrite(core.LOW) core.Delay(2) id.write([]byte{ PREAMBLE, PREAMBLE, STARTCODE2, byte(length), byte((^length) + 1), HOSTTOPN532}) checksum += HOSTTOPN532 id.write(p) for i := 0; i < len(p); i++ { checksum += p[i] } id.write([]byte{^checksum, POSTAMBLE}) id.ss.DigitalWrite(core.HIGH) }