From 20ee809e540556801fca87a573852d16c2ac7388 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Fri, 18 May 2018 22:49:25 +0000 Subject: Go server now working --- FakeUnicorn.go | 94 ---------------------------------------------------------- RealUnicorn.go | 27 ++++++++++------- Server.go | 11 +++---- Unicorn.go | 13 ++++---- 4 files changed, 28 insertions(+), 117 deletions(-) delete mode 100644 FakeUnicorn.go diff --git a/FakeUnicorn.go b/FakeUnicorn.go deleted file mode 100644 index 2258e58..0000000 --- a/FakeUnicorn.go +++ /dev/null @@ -1,94 +0,0 @@ -package main - -import ( - "github.com/veandco/go-sdl2/sdl" -) - -type FakeUnicorn struct { - BaseUnicorn - displayWidth int32 - displayHeight int32 - window *sdl.Window - renderer *sdl.Renderer -} - -// NewFake ... -// Constructs a new fake unicorn out of paint and glue -func NewFake(width, height uint8) (*FakeUnicorn, error) { - if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil { - return nil, err - } - - unicorn := &FakeUnicorn{ - BaseUnicorn{ - pixels: makePixels(width, height), - }, - 300, - 300, - nil, - nil, - } - if err := unicorn.createWindow(); err != nil { - unicorn.Close() - return nil, err - } - if err := unicorn.createRenderer(); err != nil { - unicorn.Close() - return nil, err - } - return unicorn, nil -} - -func (f *FakeUnicorn) createWindow() error { - window, err := sdl.CreateWindow("Fake Unicorn", - sdl.WINDOWPOS_UNDEFINED, - sdl.WINDOWPOS_UNDEFINED, - f.displayWidth, - f.displayHeight, - sdl.WINDOW_SHOWN) - f.window = window - return err -} - -func (f *FakeUnicorn) createRenderer() error { - renderer, err := sdl.CreateRenderer(f.window, -1, sdl.RENDERER_ACCELERATED) - f.renderer = renderer - return err -} - -func (f *FakeUnicorn) Close() error { - if f.window != nil { - f.window.Destroy() - } - if f.renderer != nil { - f.renderer.Destroy() - } - return nil -} - -func (f *FakeUnicorn) Show() { - width, height := f.GetWidth(), f.GetHeight() - for x := uint8(0); x < width; x++ { - for y := uint8(0); y < height; y++ { - r, g, b := rgb(f.pixels[x][y]) - if err := f.renderer.SetDrawColor(r, g, b, uint8(255)); err != nil { - panic(err) - } - cellWidth := f.displayWidth / int32(width) - cellHeight := f.displayHeight / int32(height) - if err := f.renderer.FillRect(&sdl.Rect{ - X: cellWidth * int32(x), - Y: f.displayHeight - (cellHeight * int32(y)) - cellHeight, // SDL Y coordinate is from the top - W: cellWidth, - H: cellHeight, - }); err != nil { - panic(err) - } - } - } - f.renderer.Present() -} - -func (f *FakeUnicorn) Off() { - f.Close() -} diff --git a/RealUnicorn.go b/RealUnicorn.go index d9665d8..31fd229 100644 --- a/RealUnicorn.go +++ b/RealUnicorn.go @@ -1,7 +1,8 @@ package main import ( - "golang.org/x/exp/io/spi" + //"golang.org/x/exp/io/spi" + "github.com/ecc1/spi" "log" ) @@ -13,14 +14,16 @@ type RealUnicorn struct { // NewReal ... // Constructs a new real unicorn from fairy dust and sprinkles func NewReal() (*RealUnicorn, error) { - dev, err := spi.Open(&spi.Devfs{ + /*dev, err := spi.Open(&spi.Devfs{ Dev: "/dev/spidev0.0", - Mode: spi.Mode0, + Mode: spi.Mode3, MaxSpeed: 9000000, - }) + })*/ + dev, err := spi.Open("/dev/spidev0.0", 9000000, 0) if err != nil { return nil, err } + //dev.SetBitOrder(spi.LSBFirst) return &RealUnicorn{ BaseUnicorn{ @@ -32,24 +35,26 @@ func NewReal() (*RealUnicorn, error) { func (u *RealUnicorn) Show() { // Width * height * colours + leading bit - width := u.GetWidth() - height := u.GetHeight() - write := make([]byte, (width*height*3)+1) + width := int(u.GetWidth()) + height := int(u.GetHeight()) + sz := (width*height*3)+1 + write := make([]byte, sz) // Add the leading bit write[0] = 0x72 // Add all the pixel values ix := 1 - for x := uint8(0); x < width; x++ { - for y := uint8(0); y < height; y++ { + for x := 0; x < width; x++ { + for y := 0; y < height; y++ { for j := 0; j < 3; j++ { - write[ix] = u.pixels[x][y][j] + write[ix] = byte(u.pixels[x][y][j]) ix++ } } } // Write to the device - err := u.device.Tx(write, nil) + //err := u.device.Tx(write, nil) + err := u.device.Transfer(write) if err != nil { log.Printf("Error writing to SPI device %v", err) } diff --git a/Server.go b/Server.go index f25376c..eee725d 100644 --- a/Server.go +++ b/Server.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" "github.com/gorilla/websocket" - "github.com/veandco/go-sdl2/sdl" + //"github.com/veandco/go-sdl2/sdl" "io/ioutil" "log" "net/http" @@ -185,7 +185,7 @@ func upgradeHandler(w http.ResponseWriter, r *http.Request) { } } -func handleSdlEvents() { +/*func handleSdlEvents() { running := true for running { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { @@ -196,7 +196,7 @@ func handleSdlEvents() { } } } -} +}*/ func handleClients() { for { @@ -222,8 +222,9 @@ func main() { http.Handle("/", http.FileServer(http.Dir("build"))) http.HandleFunc("/ws", upgradeHandler) go http.ListenAndServe(":3001", nil) - go handleClients() - handleSdlEvents() + //go handleClients() + handleClients() + //handleSdlEvents() } func doBroadcast(obj interface{}) { diff --git a/Unicorn.go b/Unicorn.go index 905cc0a..8042358 100644 --- a/Unicorn.go +++ b/Unicorn.go @@ -1,7 +1,7 @@ package main import ( - "log" + // "log" ) // Unicorn ... @@ -32,11 +32,10 @@ type Unicorn interface { // if it can't find one then gives you a fake one. func GetUnicorn() (unicorn Unicorn, err error) { unicorn, err = NewReal() - if err != nil { - log.Println("Couldn't get a real unicorn, trying a fake one") - unicorn, err = NewFake(uint8(16), uint8(16)) - } - unicorn, err = NewFake(uint8(16), uint8(16)) + //if err != nil { + // log.Println("Couldn't get a real unicorn, trying a fake one") + // unicorn, err = NewFake(uint8(16), uint8(16)) + //} return } @@ -77,4 +76,4 @@ func makePixels(width, height uint8) [][][]uint8 { func rgb(pixel []uint8) (uint8, uint8, uint8) { return pixel[0], pixel[1], pixel[2] -} \ No newline at end of file +} -- cgit v1.2.3-ZIG