<commit_before>// Copyright (c) 2016 The mccdaq developers. All rights reserved.
// Project site: https://github.com/gotmc/mccdaq
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.

package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path"
	"time"

	"github.com/gotmc/libusb"
	"github.com/gotmc/mccdaq/usb1608fsplus"
	"github.com/mitchellh/go-homedir"
	rpi "github.com/nathan-osman/go-rpigpio"
)

const (
	millisecondDelay = 100
	termWidth        = 70
)

func main() {

	/****************************
	* Configure the application *
	****************************/

	// Parse the config flags to determine the config JSON filename
	var (
		configFlag = flag.String("config", "./remote_config.json", "JSON config filename.")
	)
	flag.Parse()
	configFilename, err := homedir.Expand(*configFlag)
	if err != nil {
		log.Fatalln(err)
	}

	// Setup the application config
	appConfigData, err := ioutil.ReadFile(configFilename)
	if err != nil {
		log.Fatalf("Error reading the USB-1608FS-Plus JSON config file")
	}
	dec := json.NewDecoder(bytes.NewReader(appConfigData))
	type RPi struct {
		GPIO   int    `json:"gpio"`
		Output string `json:"output"`
	}
	type AppConfig struct {
		SN           string `json:"daq_sn"`
		DisableGPIO3 bool   `json:"disable_gpio3"`
		OutputFile   string `json:"output_file"`
		RPi          []RPi  `json:"rpi"`
	}
	var appConfig AppConfig
	if err := dec.Decode(&appConfig); err != nil {
		log.Fatalf("Couldn't parse app config: %v", err)
	}
	outputDir := appConfig.OutputFile

	// Set RPi, GPIO outputs as desired.
	var pins = make([]*rpi.Pin, len(appConfig.RPi))
	for i, rpiGPIO := range appConfig.RPi {
		pins[i], err = rpi.OpenPin(rpiGPIO.GPIO, rpi.OUT)
		if err != nil {
			panic(err)
		}
		defer pins[i].Close()
		if rpiGPIO.Output == "low" {
			pins[i].Write(rpi.LOW)
		} else if rpiGPIO.Output == "high" {
			pins[i].Write(rpi.HIGH)
		}
	}

	/***********************************
	* Start talking to the MCC via USB *
	***********************************/

	// Initialize the USB Context
	ctx, err := libusb.Init()
	if err != nil {
		log.Fatal("Couldn't create USB context. Ending now.")
	}
	defer ctx.Exit()

	// Create the USB-1608FS-Plus DAQ device using the given S/N
	daq, err := usb1608fsplus.NewViaSN(ctx, appConfig.SN)
	if err != nil {
		log.Fatalf("Couldn't get S/N %s: %s", appConfig.SN, err)
	}
	defer daq.Close()

	// Create new analog input and ensure the scan is stopped and buffer cleared
	ai, err := daq.NewAnalogInput()
	if err != nil {
		log.Fatalf("Error creating new analog input: %s", err)
	}<commit_msg>Grab first device instead of using S/N<commit_after>// Copyright (c) 2016 The mccdaq developers. All rights reserved.
// Project site: https://github.com/gotmc/mccdaq
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.

package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path"
	"time"

	"github.com/gotmc/libusb"
	"github.com/gotmc/mccdaq/usb1608fsplus"
	"github.com/mitchellh/go-homedir"
	rpi "github.com/nathan-osman/go-rpigpio"
)

const (
	millisecondDelay = 100
	termWidth        = 70
)

func main() {

	/****************************
	* Configure the application *
	****************************/

	// Parse the config flags to determine the config JSON filename
	var (
		configFlag = flag.String("config", "./remote_config.json", "JSON config filename.")
	)
	flag.Parse()
	configFilename, err := homedir.Expand(*configFlag)
	if err != nil {
		log.Fatalln(err)
	}

	// Setup the application config
	appConfigData, err := ioutil.ReadFile(configFilename)
	if err != nil {
		log.Fatalf("Error reading the USB-1608FS-Plus JSON config file")
	}
	dec := json.NewDecoder(bytes.NewReader(appConfigData))
	type RPi struct {
		GPIO   int    `json:"gpio"`
		Output string `json:"output"`
	}
	type AppConfig struct {
		SN           string `json:"daq_sn"`
		DisableGPIO3 bool   `json:"disable_gpio3"`
		OutputFile   string `json:"output_file"`
		RPi          []RPi  `json:"rpi"`
	}
	var appConfig AppConfig
	if err := dec.Decode(&appConfig); err != nil {
		log.Fatalf("Couldn't parse app config: %v", err)
	}
	outputDir := appConfig.OutputFile

	// Set RPi, GPIO outputs as desired.
	var pins = make([]*rpi.Pin, len(appConfig.RPi))
	for i, rpiGPIO := range appConfig.RPi {
		pins[i], err = rpi.OpenPin(rpiGPIO.GPIO, rpi.OUT)
		if err != nil {
			panic(err)
		}
		defer pins[i].Close()
		if rpiGPIO.Output == "low" {
			pins[i].Write(rpi.LOW)
		} else if rpiGPIO.Output == "high" {
			pins[i].Write(rpi.HIGH)
		}
	}

	/***********************************
	* Start talking to the MCC via USB *
	***********************************/

	// Initialize the USB Context
	ctx, err := libusb.Init()
	if err != nil {
		log.Fatal("Couldn't create USB context. Ending now.")
	}
	defer ctx.Exit()

	// Create the USB-1608FS-Plus DAQ device using the given S/N
	daq, err := usb1608fsplus.GetFirstDevice(ctx)
	if err != nil {
		log.Fatalf("Couldn't get first device: %s", appConfig.SN, err)
	}
	defer daq.Close()

	// Create new analog input and ensure the scan is stopped and buffer cleared
	ai, err := daq.NewAnalogInput()
	if err != nil {
		log.Fatalf("Error creating new analog input: %s", err)
	}
{'fraction_non_alphanumeric': 0.1784046329161218, 'fraction_numerical': 0.010087801232953483, 'mean_word_length': 3.579982891360137, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '4288142', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1889, 'n_words': 517}