<commit_before>package input

import (
	"errors"
	"fmt"
	"log"
	"time"
	"bitbucket.org/cswank/gogadgets/models"
	"bitbucket.org/cswank/gogadgets/output"
	"bitbucket.org/cswank/gogadgets/pins"
)

//Switch is an input device that waits for a GPIO pin
//to change value (1 to 0 or 0 to 1).  When that change
//happens it sends an update to the rest of the system.
type Switch struct {
	GPIO      Poller
	Value     interface{}
	TrueValue interface{}
	Units     string
	out       chan<- models.Value
}

func NewSwitch(pin *models.Pin) (InputDevice, error) {
	pin.Direction = "in"
	var err error
	var s *Switch
	gpio, err := output.NewGPIO(pin)
	poller, ok := gpio.(Poller)
	if !ok {
		return nil, errors.New(fmt.Sprintf("couldn't create a poller: %s", pin))
	}
	if err == nil {
		s = &Switch{
			GPIO:      poller,
			TrueValue: pin.Value,
			Units:     pin.Units,
		}
		switch s.TrueValue.(type) {
		case bool:
			s.Value = false
		default:
			s.Value = float64(0.0)
		}
	}
	return s, err
}

func (s *Switch) Config() models.ConfigHelper {
	return models.ConfigHelper{
		PinType: "pwm",
		Pins: pins.Pins["gpio"],
		Fields: map[string][]string{
			"edge": []string{"rising", "falling", "both"},
		},
	}
}

//The GPIO does the real waiting here.  This wraps it and adds
//a delay so that the inevitable bounce in the signal from the
//physical device is ignored.
func (s *Switch) wait(out chan<- interface{}, err chan<- error) {
	for {
		val, e := s.GPIO.Wait()
		if e != nil {
			err <- e
			return
		}
		switch v := s.TrueValue.(type) {
		case bool:
			out <- val
		default:
			if val {
				out <- v
			} else {
				out <- 0.0
			}
		}
		time.Sleep(100 * time.Millisecond)
	}
}

func (s *Switch) SendValue() {
	s.out <- models.Value{
		Value: s.Value,
		Units: s.Units,
	}
}

func (s *Switch) GetValue() *models.Value {
	return &models.Value{
		Value: s.Value,
		Units: s.Units,
	}
}

func (s *Switch) Start(in <-chan models.Message, out chan<- models.Value) {
	s.out = out
	value := make(chan interface{})
	err := make(chan error)
	keepGoing := true
	go s.wait(value, err)
	for keepGoing {
		select {
		case <-in:
			//do nothing
		case val := <-value:
			s.Value = val
			s.SendValue()
		case e := <-err:
			log.Println(e)
		}
	}
}

<commit_msg>switch is not poller<commit_after>package input

import (
	"errors"
	"fmt"
	"log"
	"time"
	"bitbucket.org/cswank/gogadgets/models"
	"bitbucket.org/cswank/gogadgets/output"
	"bitbucket.org/cswank/gogadgets/pins"
)

//Switch is an input device that waits for a GPIO pin
//to change value (1 to 0 or 0 to 1).  When that change
//happens it sends an update to the rest of the system.
type Switch struct {
	Value     interface{}
	TrueValue interface{}
	Units     string
	out       chan<- models.Value
}

func NewSwitch(pin *models.Pin) (InputDevice, error) {
	pin.Direction = "in"
	var err error
	var s *Switch
	gpio, err := output.NewGPIO(pin)
	poller, ok := gpio.(Poller)
	if !ok {
		return nil, errors.New(fmt.Sprintf("couldn't create a poller: %s", pin))
	}
	if err == nil {
		s = &Switch{
			GPIO:      poller,
			TrueValue: pin.Value,
			Units:     pin.Units,
		}
		switch s.TrueValue.(type) {
		case bool:
			s.Value = false
		default:
			s.Value = float64(0.0)
		}
	}
	return s, err
}

func (s *Switch) Config() models.ConfigHelper {
	return models.ConfigHelper{
		PinType: "pwm",
		Pins: pins.Pins["gpio"],
		Fields: map[string][]string{
			"edge": []string{"rising", "falling", "both"},
		},
	}
}

//The GPIO does the real waiting here.  This wraps it and adds
//a delay so that the inevitable bounce in the signal from the
//physical device is ignored.
func (s *Switch) wait(out chan<- interface{}, err chan<- error) {
	for {
		val, e := s.GPIO.Wait()
		if e != nil {
			err <- e
			return
		}
		switch v := s.TrueValue.(type) {
		case bool:
			out <- val
		default:
			if val {
				out <- v
			} else {
				out <- 0.0
			}
		}
		time.Sleep(100 * time.Millisecond)
	}
}

func (s *Switch) SendValue() {
	s.out <- models.Value{
		Value: s.Value,
		Units: s.Units,
	}
}

func (s *Switch) GetValue() *models.Value {
	return &models.Value{
		Value: s.Value,
		Units: s.Units,
	}
}

func (s *Switch) Start(in <-chan models.Message, out chan<- models.Value) {
	s.out = out
	value := make(chan interface{})
	err := make(chan error)
	keepGoing := true
	go s.wait(value, err)
	for keepGoing {
		select {
		case <-in:
			//do nothing
		case val := <-value:
			s.Value = val
			s.SendValue()
		case e := <-err:
			log.Println(e)
		}
	}
}


{'fraction_non_alphanumeric': 0.14742014742014742, 'fraction_numerical': 0.005807460352914898, 'mean_word_length': 2.833904109589041, 'pattern_counts': {'":': 2, '<': 29, '<?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': '4690211', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1703, 'n_words': 505}