<commit_before>package main
/*
 * Temper.go by <NAME> (c) 2015 (<EMAIL>)
 * based on pcsensor.c by <NAME> (c) 2011 (<EMAIL>)
 * based on Temper.c by <NAME> (c) 2009 (relavak.com)
*/

import (
	"github.com/kylelemons/gousb/usb"
	"log"
)

func main() {
	ctx := usb.NewContext()
	defer ctx.Close()

	devs, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
		return desc.Vendor == 0x0c45 && desc.Product == 0x7401
	})

	defer func() {
		for _, d := range devs {
			d.Close()
		}
	}()

	if err != nil {
		log.Fatalf("list: %s", err)
	}

	if len(devs) == 0 {
		log.Fatalf("No thermometers found.")
	}

	dev := devs[0]
	if err = dev.SetConfig(1); err != nil {
		log.Fatalf("SetConfig: %s", err)
	}

	ep, err := dev.OpenEndpoint(1, 1, 0, 0x82)
	if err != nil {
		log.Fatalf("open: %s", err)
	}
	if _, err = dev.Control(0x21, 0x09, 0x0200, 0x01, []byte{0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00}); err != nil {
		log.Fatalf("Control: %s", err)
	}
	buf := make([]byte, 8)
	if _, err = ep.Read(buf); err != nil {
		log.Fatalf("Read: %s", err)
	}
	c := float64(buf[4]) + float64(buf[5])/256
	log.Printf("Temperature: %.2fF %.2fC\n", 9.0/5.0*c+32, c)
}<commit_msg>refactor temp getter into a function.<commit_after>package main
/*
 * Temper.go by <NAME> (c) 2015 (<EMAIL>)
 * based on pcsensor.c by <NAME> (c) 2011 (<EMAIL>)
 * based on Temper.c by <NAME> (c) 2009 (relavak.com)
*/

import (
	"github.com/kylelemons/gousb/usb"
	"log"
	"fmt"
)

func temperature() (float64, error) {
	ctx := usb.NewContext()
	defer ctx.Close()

	devs, err := ctx.ListDevices(func(desc *usb.Descriptor) bool {
		return desc.Vendor == 0x0c45 && desc.Product == 0x7401
	})

	defer func() {
		for _, d := range devs {
			d.Close()
		}
	}()

	if err != nil {
		return 0.0, err
	}

	if len(devs) == 0 {
		return 0.0, fmt.Errorf("No thermometers found.")
	}

	dev := devs[0]
	if err = dev.SetConfig(1); err != nil {
		return 0.0, err
	}

	ep, err := dev.OpenEndpoint(1, 1, 0, 0x82)
	if err != nil {
		return 0.0, err
	}
	if _, err = dev.Control(0x21, 0x09, 0x0200, 0x01, []byte{0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00}); err != nil {
		return 0.0, err
	}
	buf := make([]byte, 8)
	if _, err = ep.Read(buf); err != nil {
		return 0.0, err
	}
	return float64(buf[4]) + float64(buf[5])/256, nil
}

func main() {
	c, err := temperature()
	if err == nil {
		log.Printf("Temperature: %.2fF %.2fC\n", 9.0/5.0*c+32, c)
	} else {
		log.Fatalf("Failed: %s", err)
	}
}
{'fraction_non_alphanumeric': 0.2018875666803447, 'fraction_numerical': 0.07632334837915469, 'mean_word_length': 3.2697022767075308, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '37808', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1086, 'n_words': 267}