<commit_before>package main

import (
	"fmt"
	"log"
	"time"

	"github.com/kidoman/embd"
	_ "github.com/kidoman/embd/host/all"
)

type LidarSensor struct {
	bus     embd.I2CBus
	address byte
}

func NewLidarSensor(i2cbus, addr byte) *LidarSensor {

	lSensor := LidarSensor{bus: embd.NewI2CBus(i2cbus), address: byte(addr)}
	if e := lSensor.bus.WriteByteToReg(lSensor.address, 0x00, 0x00); e != nil {
		log.Panic("Write ", e)
	}
	time.Sleep(300 * time.Millisecond)
	return &lSensor

}

func (ls *LidarSensor) Distance() {

	if v, e := ls.bus.ReadByteFromReg(ls.address, 0x01); e != nil {
		log.Panic("ReadByteFromReg ", e)
	} else {
		fmt.Printf("status: %.8b\n", v)
	}

	for {
		if e := ls.bus.WriteByteToReg(ls.address, 0x00, 0x04); e != nil {
			log.Panic("Write ", e)
		}

		time.Sleep(100 * time.Millisecond)

		if v, e := ls.bus.ReadByteFromReg(ls.address, 0x01); e != nil {
			log.Panic("ReadByteFromReg ", e)
		} else {
			fmt.Printf("status: %.8b\n", v)
		}

		// TODO: error check
		v1, _ := ls.bus.ReadByteFromReg(ls.address, 0x10)
		v2, _ := ls.bus.ReadByteFromReg(ls.address, 0x0f)

		fmt.Println((int(v2) << 8) + int(v1))

		time.Sleep(1 * time.Second)
	}
}

func main() {
	log.SetFlags(log.Lshortfile)
	lidarSensor := NewLidarSensor(1, 0x62)
	lidarSensor.Distance()
	defer lidarSensor.bus.Close()
}
<commit_msg>Add a flag in Distance func for taking val with stabilization or without<commit_after>package main

import (
	"fmt"
	"log"
	"time"

	"github.com/kidoman/embd"
	_ "github.com/kidoman/embd/host/all"
)

type Lidar struct {
	bus     embd.I2CBus
	address byte
}

//Ready - Ready status. 0 - ready for a new command, 1 - busy
//          with acquisition
//RefOverflow - Overflow detected in correlation process assotieted with
//          a reference acquisition
//SignalOverflow - Overflow detected in correlation process assotieted with a signal
//          acquisition
//SignalNotValid - Indicates that the signal correlation peak is equal to or below
//          correlation record threshold
//SecondaryReturn - Secondary return detected above correlation noise floor threshold
//Health - 1 if is good, 0 if is bad
//ErrorDetection - Process error detected/measurement invalid
//EyeSafe - This bit will go high if eye-safety protection has been activated
const (
	Ready           = 1 << iota
	RefOverflow     = 1 << iota
	SigOverflow     = 1 << iota
	SignalNotValid  = 1 << iota
	SecondatyReturn = 1 << iota
	Health          = 1 << iota
	ErrorDetection  = 1 << iota
	EyeSafe         = 1 << iota
)

func NewLidar(i2cbus, addr byte) *Lidar {

	lSensor := Lidar{bus: embd.NewI2CBus(i2cbus), address: byte(addr)}
	if e := lSensor.bus.WriteByteToReg(lSensor.address, 0x00, 0x00); e != nil {
		log.Panic("Write ", e)
	}
	time.Sleep(300 * time.Millisecond)
	return &lSensor

}

func (ls *Lidar) GetStatus() (byte, error) {

	if val, err := ls.bus.ReadByteFromReg(ls.address, 0x01); err == nil {
		fmt.Printf("status: %.8b\n", val)
		return val, nil
	} else {
		log.Panic("GetStatus ", err)
		return 0, err
	}

}

//stablizePreampFlag - true - take aquisition with DC stabilisation/correction.
//false - it will read faster, but you will need to stabilize DC every once in
//awhile(ex. 1 out of every 100 readings is typically good)
func (ls *Lidar) Distance(stablizePreampFlag bool) {

	ls.GetStatus()

	for {
		if stablizePreampFlag {
			if e := ls.bus.WriteByteToReg(ls.address, 0x00, 0x04); e != nil {
				log.Panic("Write ", e)
			}
		} else if e := ls.bus.WriteByteToReg(ls.address, 0x00, 0x03); e != nil {
			log.Panic("Write ", e)
		}

		time.Sleep(100 * time.Millisecond)

		ls.GetStatus()
		// TODO: error check
		v1, _ := ls.bus.ReadByteFromReg(ls.address, 0x10)
		v2, _ := ls.bus.ReadByteFromReg(ls.address, 0x0f)

		fmt.Println((int(v2) << 8) + int(v1))

		time.Sleep(1 * time.Second)
	}
}

func main() {
	log.SetFlags(log.Lshortfile)
	lidar := NewLidar(1, 0x62)
	lidar.Distance(true)
	defer lidar.bus.Close()
}

{'fraction_non_alphanumeric': 0.13705583756345177, 'fraction_numerical': 0.02766497461928934, 'mean_word_length': 3.3450937155457554, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1575174', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1510, 'n_words': 374}