<commit_before>package agent

import (
	"log"
	"math"

	"github.com/bitly/go-simplejson"
	"github.com/joshlf13/gopack"
	"github.com/ninjasphere/go-ninja"
	"github.com/wolfeidau/epoller"
)

const (
	DSPIfoFlag uint16 = 1 << iota
	GestureInfoFlag
	TouchInfoFlag
	AirWheelInfoFlag
	CoordinateInfoFlag
)

// Gestic
// http://ww1.microchip.com/downloads/en/DeviceDoc/40001718B.pdf
// Page 36

// Gestic device path
const GesticDevicePath = "/dev/gestic"

// Flag which indicates if the payload contains data
const SensorDataPresentFlag = 0x91

const (
	IdSensorDataOutput = 0x91
)

type Reader struct {
	conn           *ninja.NinjaConnection
	log            *ninja.Logger
	currentGesture *gestureData
}

func NewReader(conn *ninja.NinjaConnection, log *ninja.Logger) *Reader {
	return &Reader{conn: conn, log: log}
}

type gestureData struct {
	Event       *EventHeader
	DataHeader  *DataHeader
	Gesture     *GestureInfo
	Touch       *TouchInfo
	AirWheel    *AirWheelInfo
	Coordinates *CoordinateInfo
}

func NewGestureData() *gestureData {
	return &gestureData{
		Event:       &EventHeader{},
		DataHeader:  &DataHeader{},
		Gesture:     &GestureInfo{},
		Touch:       &TouchInfo{},
		AirWheel:    &AirWheelInfo{},
		Coordinates: &CoordinateInfo{},
	}
}

type EventHeader struct {
	Length, Flags, Seq, Id uint8
}

type DataHeader struct {
	DataMask              uint16
	TimeStamp, SystemInfo uint8
}

type GestureInfo struct {
	GestureVal uint32
}

func (gi *GestureInfo) Name() string {
	return Gestures[gi.GestureVal]
}

type TouchInfo struct {
	TouchVal uint32
}

func (ti *TouchInfo) Name() string {
	if ti.TouchVal > 0 {
		i := math.Log(float64(ti.TouchVal)) / math.Log(2)
		return TouchList[int(i)]
	}
	return "None"
}

type AirWheelInfo struct {
	AirWheelVal uint8
	Crap        uint8
}

type CoordinateInfo struct {
	X uint8
	Y uint8
	Z uint8
}

var Gestures = []string{
	"None",
	"Garbage",
	"WestToEast",
	"EastToWest",
	"SouthToNorth",
	"NorthToSouth",
	"CircleClockwise",
	"CircleCounterClockwise",
}

var TouchList = []string{
	"TouchSouth",
	"TouchWest",
	"TouchNorth",
	"TouchEast",
	"TouchCenter",
	"TapSouth",
	"TapWest",
	"TapNorth",
	"TapEast",
	"TapCenter",
	"DoubleTapSouth",
	"DoubleTapWest",
	"DoubleTapNorth",
	"DoubleTapEast",
	"DoubleTapCenter",
}

func (r *Reader) Start() {
	r.log.Infof("Opening %s", GesticDevicePath)

	r.currentGesture = NewGestureData()

	if err := epoller.OpenAndDispatchEvents(GesticDevicePath, r.buildGestureEvent); err != nil {
		log.Fatalf("Error opening device reader %v", err)
	}
}

func (r *Reader) buildGestureEvent(buf []byte, n int) {

	g := r.currentGesture

	gopack.Unpack(buf[:4], g.Event)
	gopack.Unpack(buf[4:8], g.DataHeader)

	// var for offset
	offset := 8

	// grab the DSPIfo
	if g.DataHeader.DataMask&DSPIfoFlag == DSPIfoFlag {
		offset += 2
	}

	// grab the GestureInfo
	if g.DataHeader.DataMask&GestureInfoFlag == GestureInfoFlag {

		gopack.Unpack(buf[offset:offset+4], g.Gesture)
		g.Gesture.GestureVal = g.Gesture.GestureVal & uint32(0xff)
		offset += 4
	}

	// grab the TouchInfo
	if g.DataHeader.DataMask&TouchInfoFlag == TouchInfoFlag {
		gopack.Unpack(buf[offset:offset+4], g.Touch)
		offset += 4
	}

	// grab the AirWheelInfo
	if g.DataHeader.DataMask&AirWheelInfoFlag == AirWheelInfoFlag {
		gopack.Unpack(buf[offset:offset+2], g.AirWheel)
		offset += 2
	}

	// grab the CoordinateInfo
	if g.DataHeader.DataMask&CoordinateInfoFlag == CoordinateInfoFlag {
		gopack.Unpack(buf[offset:offset+6], g.Coordinates)
		offset += 6
	}

	r.log.Infof("Gesture: %s, Airwheel: %d, Touch: %s", g.Gesture.Name(), g.AirWheel.AirWheelVal, g.Touch.Name())
<commit_msg>Tweak to log level to shut this thing up.<commit_after>package agent

import (
	"log"
	"math"

	"github.com/bitly/go-simplejson"
	"github.com/joshlf13/gopack"
	"github.com/ninjasphere/go-ninja"
	"github.com/wolfeidau/epoller"
)

const (
	DSPIfoFlag uint16 = 1 << iota
	GestureInfoFlag
	TouchInfoFlag
	AirWheelInfoFlag
	CoordinateInfoFlag
)

// Gestic
// http://ww1.microchip.com/downloads/en/DeviceDoc/40001718B.pdf
// Page 36

// Gestic device path
const GesticDevicePath = "/dev/gestic"

// Flag which indicates if the payload contains data
const SensorDataPresentFlag = 0x91

const (
	IdSensorDataOutput = 0x91
)

type Reader struct {
	conn           *ninja.NinjaConnection
	log            *ninja.Logger
	currentGesture *gestureData
}

func NewReader(conn *ninja.NinjaConnection, log *ninja.Logger) *Reader {
	return &Reader{conn: conn, log: log}
}

type gestureData struct {
	Event       *EventHeader
	DataHeader  *DataHeader
	Gesture     *GestureInfo
	Touch       *TouchInfo
	AirWheel    *AirWheelInfo
	Coordinates *CoordinateInfo
}

func NewGestureData() *gestureData {
	return &gestureData{
		Event:       &EventHeader{},
		DataHeader:  &DataHeader{},
		Gesture:     &GestureInfo{},
		Touch:       &TouchInfo{},
		AirWheel:    &AirWheelInfo{},
		Coordinates: &CoordinateInfo{},
	}
}

type EventHeader struct {
	Length, Flags, Seq, Id uint8
}

type DataHeader struct {
	DataMask              uint16
	TimeStamp, SystemInfo uint8
}

type GestureInfo struct {
	GestureVal uint32
}

func (gi *GestureInfo) Name() string {
	return Gestures[gi.GestureVal]
}

type TouchInfo struct {
	TouchVal uint32
}

func (ti *TouchInfo) Name() string {
	if ti.TouchVal > 0 {
		i := math.Log(float64(ti.TouchVal)) / math.Log(2)
		return TouchList[int(i)]
	}
	return "None"
}

type AirWheelInfo struct {
	AirWheelVal uint8
	Crap        uint8
}

type CoordinateInfo struct {
	X uint8
	Y uint8
	Z uint8
}

var Gestures = []string{
	"None",
	"Garbage",
	"WestToEast",
	"EastToWest",
	"SouthToNorth",
	"NorthToSouth",
	"CircleClockwise",
	"CircleCounterClockwise",
}

var TouchList = []string{
	"TouchSouth",
	"TouchWest",
	"TouchNorth",
	"TouchEast",
	"TouchCenter",
	"TapSouth",
	"TapWest",
	"TapNorth",
	"TapEast",
	"TapCenter",
	"DoubleTapSouth",
	"DoubleTapWest",
	"DoubleTapNorth",
	"DoubleTapEast",
	"DoubleTapCenter",
}

func (r *Reader) Start() {
	r.log.Infof("Opening %s", GesticDevicePath)

	r.currentGesture = NewGestureData()

	if err := epoller.OpenAndDispatchEvents(GesticDevicePath, r.buildGestureEvent); err != nil {
		log.Fatalf("Error opening device reader %v", err)
	}
}

func (r *Reader) buildGestureEvent(buf []byte, n int) {

	g := r.currentGesture

	gopack.Unpack(buf[:4], g.Event)
	gopack.Unpack(buf[4:8], g.DataHeader)

	// var for offset
	offset := 8

	// grab the DSPIfo
	if g.DataHeader.DataMask&DSPIfoFlag == DSPIfoFlag {
		offset += 2
	}

	// grab the GestureInfo
	if g.DataHeader.DataMask&GestureInfoFlag == GestureInfoFlag {

		gopack.Unpack(buf[offset:offset+4], g.Gesture)
		g.Gesture.GestureVal = g.Gesture.GestureVal & uint32(0xff)
		offset += 4
	}

	// grab the TouchInfo
	if g.DataHeader.DataMask&TouchInfoFlag == TouchInfoFlag {
		gopack.Unpack(buf[offset:offset+4], g.Touch)
		offset += 4
	}

	// grab the AirWheelInfo
	if g.DataHeader.DataMask&AirWheelInfoFlag == AirWheelInfoFlag {
		gopack.Unpack(buf[offset:offset+2], g.AirWheel)
		offset += 2
	}

	// grab the CoordinateInfo
	if g.DataHeader.DataMask&CoordinateInfoFlag == CoordinateInfoFlag {
		gopack.Unpack(buf[offset:offset+6], g.Coordinates)
		offset += 6
	}

	r.log.Debugf("Gesture: %s, Airwheel: %d, Touch: %s", g.Gesture.Name(), g.AirWheel.AirWheelVal, g.Touch.Name())

{'fraction_non_alphanumeric': 0.12762742134908642, 'fraction_numerical': 0.015111965929385905, 'mean_word_length': 4.076708507670851, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '885411', 'n_tokens_mistral': 2978, 'n_tokens_neox': 2785, 'n_words': 580}