<commit_before>// Enumerates USB devices, finds and identifies CrazyRadio USB dongle.
package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/kylelemons/gousb/usb"
)

const (
	Vendor  = 0x1915
	Product = 0x7777
)

type Request uint8

const (
	SET_RADIO_CHANNEL Request = 0x01
	SET_RADIO_ADDRESS Request = 0x02
	SET_DATA_RATE     Request = 0x03
	SET_RADIO_POWER   Request = 0x04
	SET_RADIO_ARD     Request = 0x05
	SET_RADIO_ARC     Request = 0x06
	ACK_ENABLE        Request = 0x10
	SET_CONT_CARRIER  Request = 0x20
	CHANNEL_SCANN     Request = 0x21
	LAUNCH_BOOTLOADER Request = 0xFF
)

type DataRate uint16

const (
	DATA_RATE_250K DataRate = 0
	DATA_RATE_1M   DataRate = 1
	DATA_RATE_2M   DataRate = 2

	RADIO_POWER_M18dBm = 0
	RADIO_POWER_M12dBm = 1
	RADIO_POWER_M6dBm  = 2
	RADIO_POWER_0dBm   = 3
)

func control(d *usb.Device, req Request, val uint16, data []byte) error {
	_, err := d.Control(usb.REQUEST_TYPE_VENDOR, uint8(req), val, 0, data)
	return err
}

func initDongle(d *usb.Device, ch uint16, rate DataRate) (err error) {
	if err = control(d, SET_DATA_RATE, uint16(DATA_RATE_250K), nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_CHANNEL, 2, nil); err != nil {
		return
	}
	if err = control(d, SET_CONT_CARRIER, 0, nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_ADDRESS, 0, []byte{0xE7, 0xE7, 0xE7, 0xE7, 0xE7}); err != nil {
		return
	}
	if err = control(d, SET_RADIO_POWER, RADIO_POWER_0dBm, nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_ARC, 3, nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_ARD, 0x80|32, nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_ARC, 10, nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_CHANNEL, ch, nil); err != nil {
		return
	}
	if err = control(d, SET_DATA_RATE, uint16(rate), nil); err != nil {
		return
	}
	return
}

func reader(in usb.Endpoint, ch chan<- []byte) {
	buf := make([]byte, 64)
	for {
		n, err := in.Read(buf)
		if err != nil {
			log.Printf("Error: reader: %v", err)
			continue
		}
		p := make([]byte, n)
		copy(p, buf)
		ch <- p
		log.Printf("Reader, len: %d, package: %v", n, buf[:n])
	}
}

func consume(cnt int, readCh <-chan []byte) {
	for {
		log.Printf("Consuming at least %d package", cnt)
		for i := 0; i < cnt; i++ {
			p := <-readCh
			log.Printf("Writer, incoming package: %v", p)
		}

		select {
		case p := <-readCh:
			log.Printf("Writer, incoming package: %v", p)
		default:
			return
		}
	}<commit_msg>Cut off the ACK byte. This inneficiency comes from a misuse of a nrf protocol (and misuse comes from the fact that nrf is mostly one-way)<commit_after>// Enumerates USB devices, finds and identifies CrazyRadio USB dongle.
package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/kylelemons/gousb/usb"
)

const (
	Vendor  = 0x1915
	Product = 0x7777
)

type Request uint8

const (
	SET_RADIO_CHANNEL Request = 0x01
	SET_RADIO_ADDRESS Request = 0x02
	SET_DATA_RATE     Request = 0x03
	SET_RADIO_POWER   Request = 0x04
	SET_RADIO_ARD     Request = 0x05
	SET_RADIO_ARC     Request = 0x06
	ACK_ENABLE        Request = 0x10
	SET_CONT_CARRIER  Request = 0x20
	CHANNEL_SCANN     Request = 0x21
	LAUNCH_BOOTLOADER Request = 0xFF
)

type DataRate uint16

const (
	DATA_RATE_250K DataRate = 0
	DATA_RATE_1M   DataRate = 1
	DATA_RATE_2M   DataRate = 2

	RADIO_POWER_M18dBm = 0
	RADIO_POWER_M12dBm = 1
	RADIO_POWER_M6dBm  = 2
	RADIO_POWER_0dBm   = 3
)

func control(d *usb.Device, req Request, val uint16, data []byte) error {
	_, err := d.Control(usb.REQUEST_TYPE_VENDOR, uint8(req), val, 0, data)
	return err
}

func initDongle(d *usb.Device, ch uint16, rate DataRate) (err error) {
	if err = control(d, SET_DATA_RATE, uint16(DATA_RATE_250K), nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_CHANNEL, 2, nil); err != nil {
		return
	}
	if err = control(d, SET_CONT_CARRIER, 0, nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_ADDRESS, 0, []byte{0xE7, 0xE7, 0xE7, 0xE7, 0xE7}); err != nil {
		return
	}
	if err = control(d, SET_RADIO_POWER, RADIO_POWER_0dBm, nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_ARC, 3, nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_ARD, 0x80|32, nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_ARC, 10, nil); err != nil {
		return
	}
	if err = control(d, SET_RADIO_CHANNEL, ch, nil); err != nil {
		return
	}
	if err = control(d, SET_DATA_RATE, uint16(rate), nil); err != nil {
		return
	}
	return
}

func reader(in usb.Endpoint, ch chan<- []byte) {
	buf := make([]byte, 64)
	for {
		n, err := in.Read(buf)
		if err != nil {
			log.Printf("Error: reader: %v", err)
			continue
		}
		p := make([]byte, n)
		copy(p, buf)
		// Cut off the ACK byte
		if len(p) >= 1 {
			p = p[1:]
		}
		ch <- p
		log.Printf("Reader, len: %d, package: %v", n, buf[:n])
	}
}

func consume(cnt int, readCh <-chan []byte) {
	for {
		log.Printf("Consuming at least %d package", cnt)
		for i := 0; i < cnt; i++ {
			p := <-readCh
			log.Printf("Writer, incoming package: %v", p)
		}

		select {
		case p := <-readCh:
			log.Printf("Writer, incoming package: %v", p)
		default:
			return
		}
	}
{'fraction_non_alphanumeric': 0.13045171339563863, 'fraction_numerical': 0.0381619937694704, 'mean_word_length': 3.4208261617900173, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 4, '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': '488521', 'n_tokens_mistral': 2380, 'n_tokens_neox': 2134, 'n_words': 579}