コード例 #1
0
ファイル: gpio_linux.go プロジェクト: julianfez/hkuvr1611
func NewConnection(file string, callback uvr1611.PacketCallback) *gpio {
	pin, err := InitGPIO(file)
	if err != nil {
		log.Fatal(err)
	}

	packetReceiver := uvr1611.NewPacketReceiver()
	packetDecoder := uvr1611.NewPacketDecoder(packetReceiver)
	byteDecoder := uvr.NewByteDecoder(packetDecoder, uvr.NewTimeout(488.0, 0.4))
	syncDecoder := uvr1611.NewSyncDecoder(byteDecoder, byteDecoder, uvr.NewTimeout(488.0*2, 0.4))
	signal := uvr.NewSignal(syncDecoder)

	pin_callback := func(pin embd.DigitalPin) {
		value, read_err := pin.Read()
		if read_err != nil {
			fmt.Println(read_err)
		} else {
			signal.Consume(big.Word(value))
		}
	}

	packetReceiver.RegisterCallback(func(packet uvr1611.Packet) {
		if callback != nil {
			callback(packet)
		}

		// Stop watching the pin and let other threads do their job
		pin.StopWatching()
		syncDecoder.Reset()
		byteDecoder.Reset()
		packetDecoder.Reset()

		// Rewatch after 10 seconds again
		time.AfterFunc(30*time.Second, func() {
			pin.Watch(embd.EdgeBoth, pin_callback)
			if err != nil {
				log.Fatal("Could not watch pin.", err)
			}
		})
	})

	err = pin.Watch(embd.EdgeBoth, pin_callback)
	if err != nil {
		log.Fatal("Could not watch pin.", err)
	}

	return &gpio{
		pin: pin,
	}
}
コード例 #2
0
ファイル: replay.go プロジェクト: julianfez/hkuvr1611
func (r *replay) sendPacket() {
	packetReceiver := uvr1611.NewPacketReceiver()
	packetDecoder := uvr1611.NewPacketDecoder(packetReceiver)
	byteDecoder := uvr.NewByteDecoder(packetDecoder, uvr.NewTimeout(488.0, 0.2))
	syncDecoder := uvr1611.NewSyncDecoder(byteDecoder, byteDecoder, uvr.NewTimeout(488.0*2, 0.3))
	interrupt := uvr.NewEdgeInterrupt(syncDecoder)
	replayer := uvr.NewReplayer(interrupt)

	packetReceiver.RegisterCallback(func(packet uvr1611.Packet) {
		r.callback(packet)
		syncDecoder.Reset()
		byteDecoder.Reset()
		packetDecoder.Reset()
	})
	err := replayer.Replay(r.filePath)
	if err != nil {
		log.Fatal("Could not replay file.", err)
	}
}