package main

import (
	"fmt"
	"log"
	"math"

	"github.com/go-daq/canbus"
)

func main() {
	// Creates a new CAN bus device socket
	canBus, err := canbus.New()
	if err != nil {
		log.Fatalf("Failed to open CAN bus: %v", err)
	}
	defer canBus.Close()

	// Binds can1 to the socket
	err = canBus.Bind("can1")
	if err != nil {
		log.Fatalf("Failed to bind CAN socket to interface: %v", err)
	}

	if err != nil {
		log.Fatalln(err.Error())
	}

	for {
		frame, err := canBus.Recv()
		if err != nil {
			log.Fatalf("Can not recv frame: %+v", err)
		}

		var x uint64 = 0
		fmt.Print("Received CAN message: ")
		for i := 0; i < len(frame.Data); i++ {

			fmt.Print(frame.Data[i], " ")
			//right byte is most important
			x += uint64(frame.Data[i]) * uint64(math.Pow(256, float64(i)))

		}
		print("\n")
		fmt.Println("Scanned Barcode is:", x)
		print("\n")
	}
}
