changeset 899:2d718e542198

laundry add .go files Ignore-this: 34b9f9b1ab2830da75757ffedb93ead7 darcs-hash:20130825055434-312f9-317d7333fabed38a3d3f0b5b18009ee486813081
author drewp <drewp@bigasterisk.com>
date Sat, 24 Aug 2013 22:54:34 -0700
parents 8a5312d5b5eb
children 531fce163cbd
files service/laundry/gui.js service/laundry/index.html service/laundry/laundry.go service/laundry/makefile service/laundry/static/gui.js
diffstat 5 files changed, 147 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/service/laundry/gui.js	Sat Aug 24 22:11:55 2013 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-'use strict';
-
-function Ctrl($scope, $http) {
-    $http.get("status").success(function (data) {
-        $scope.status = data;
-    });
-}
--- a/service/laundry/index.html	Sat Aug 24 22:11:55 2013 -0700
+++ b/service/laundry/index.html	Sat Aug 24 22:54:34 2013 -0700
@@ -3,7 +3,7 @@
   <head>
     <title>laundry pi</title>
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular.min.js"></script>
-    <script src="gui.js"></script>
+    <script src="static/gui.js"></script>
   </head>
   <body ng-controller="Ctrl">
     <h1>laundry room pi</h1>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/laundry/laundry.go	Sat Aug 24 22:54:34 2013 -0700
@@ -0,0 +1,129 @@
+package main
+
+import (
+	"io/ioutil"
+	"log"
+	"net/http"
+	"strconv"
+	"encoding/json"
+	"github.com/bmizerany/pat"
+	"github.com/mrmorphic/hwio"
+)
+
+/*
+hwio.DebugPinMap() wrote this:
+
+Pin 1: 3.3V,  cap:
+Pin 2: 5V,  cap:
+Pin 3: SDA,GPIO0  cap:output,input,input_pullup,input_pulldown
+Pin 5: SCL,GPIO1  cap:output,input,input_pullup,input_pulldown
+Pin 6: GROUND,  cap:
+Pin 7: GPIO4  cap:output,input,input_pullup,input_pulldown
+Pin 8: TXD,GPIO14  cap:output,input,input_pullup,input_pulldown
+Pin 10: RXD,GPIO15  cap:output,input,input_pullup,input_pulldown
+Pin 11: GPIO17  cap:output,input,input_pullup,input_pulldown
+Pin 12: GPIO18  cap:output,input,input_pullup,input_pulldown
+Pin 13: GPIO21  cap:output,input,input_pullup,input_pulldown
+Pin 15: GPIO22  cap:output,input,input_pullup,input_pulldown
+Pin 16: GPIO23  cap:output,input,input_pullup,input_pulldown
+Pin 18: GPIO24  cap:output,input,input_pullup,input_pulldown
+Pin 19: MOSI,GPIO10  cap:output,input,input_pullup,input_pulldown
+Pin 21: MISO,GPIO9  cap:output,input,input_pullup,input_pulldown
+Pin 22: GPIO25  cap:output,input,input_pullup,input_pulldown
+Pin 23: SCLK,GPIO11  cap:output,input,input_pullup,input_pulldown
+Pin 24: CE0N,GPIO8  cap:output,input,input_pullup,input_pulldown
+Pin 26: CE1N,GPIO7  cap:output,input,input_pullup,input_pulldown
+*/
+
+type Pins struct {
+	InMotion, InSwitch3, InSwitch1, InSwitch2, OutLed, OutSpeaker, InDoorClosed, OutStrike hwio.Pin
+	LastOutLed, LastOutStrike int
+}
+
+// hwio.GetPin with a panic instead of an error return
+func GetPin(id string) hwio.Pin {
+	p, e := hwio.GetPin(id)
+	if e != nil {
+		panic(e)
+	}
+	return p
+}
+
+func DigitalRead(p hwio.Pin) int {
+	v, err := hwio.DigitalRead(p)
+	if err != nil {
+		panic(err)
+	}
+	return v
+}
+
+func SetupIo() Pins {
+	pins := Pins{
+		InMotion:		GetPin("GPIO0"),
+		InSwitch3:		GetPin("GPIO1"),
+		InSwitch1:		GetPin("GPIO4"),
+		InSwitch2:		GetPin("GPIO17"),
+		OutLed:			GetPin("GPIO21"),
+		OutSpeaker:		GetPin("GPIO22"),
+		InDoorClosed:	GetPin("GPIO10"),
+		OutStrike:      GetPin("GPIO9"),
+	}
+	
+	if err := hwio.PinMode(pins.InMotion,		hwio.INPUT_PULLUP); err != nil { panic(err) }
+	if err := hwio.PinMode(pins.InSwitch1,		hwio.INPUT_PULLUP); err != nil { panic(err) }
+	if err := hwio.PinMode(pins.InSwitch2,		hwio.INPUT_PULLUP); err != nil { panic(err) }
+	if err := hwio.PinMode(pins.InSwitch3,		hwio.INPUT_PULLUP); err != nil { panic(err) }
+	if err := hwio.PinMode(pins.InDoorClosed,	hwio.INPUT_PULLUP); err != nil { panic(err) }
+	if err := hwio.PinMode(pins.OutLed,			hwio.OUTPUT); err != nil { panic(err) }
+	if err := hwio.PinMode(pins.OutSpeaker,		hwio.OUTPUT); err != nil { panic(err) }
+	if err := hwio.PinMode(pins.OutStrike,		hwio.OUTPUT); err != nil { panic(err) }
+	return pins
+}
+	
+func main() {
+	pins := SetupIo()
+
+	m := pat.New()
+	
+	m.Get("/", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
+		http.ServeFile(w, r, "index.html")
+	}));
+
+	m.Get("/static/:any", http.FileServer(http.Dir("./")));
+
+	m.Get("/status", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
+		jsonEncode := json.NewEncoder(w)
+		jsonEncode.Encode(map[string]int{
+			"motion": DigitalRead(pins.InMotion),
+			"switch1": DigitalRead(pins.InSwitch1),
+			"switch2": DigitalRead(pins.InSwitch2),
+			"switch3": DigitalRead(pins.InSwitch3),
+			"doorClosed": DigitalRead(pins.InDoorClosed),
+			"led": pins.LastOutLed,
+		})
+	}));
+
+	m.Put("/led", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
+		body, err := ioutil.ReadAll(r.Body)
+		if err != nil {
+			panic(err)
+		}
+		level, err := strconv.Atoi(string(body[:]))
+		if err != nil {
+			http.Error(w, "body must be '0' or '1'", http.StatusBadRequest)
+			return
+		}
+
+		hwio.DigitalWrite(pins.OutLed, level)
+		pins.LastOutLed = level
+
+		http.Error(w, "", http.StatusAccepted)
+	}));
+
+	http.Handle("/", m)
+	log.Printf("Listening on port 8080")
+	err := http.ListenAndServe(":8080", nil)
+	if err != nil {
+		log.Fatal("ListenAndServe: ", err)
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/laundry/makefile	Sat Aug 24 22:54:34 2013 -0700
@@ -0,0 +1,10 @@
+
+run: laundry
+	./laundry
+
+laundry: laundry.go
+	GOPATH=`pwd` /opt/go/bin/go build laundry.go
+
+deps:
+	GOPATH=`pwd` /opt/go/bin/go get github.com/mrmorphic/hwio
+	GOPATH=`pwd` /opt/go/bin/go get github.com/bmizerany/pat
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/service/laundry/static/gui.js	Sat Aug 24 22:54:34 2013 -0700
@@ -0,0 +1,7 @@
+'use strict';
+
+function Ctrl($scope, $http) {
+    $http.get("status").success(function (data) {
+        $scope.status = data;
+    });
+}