Skip to content

Commit

Permalink
Big improvements, including websocket listening.
Browse files Browse the repository at this point in the history
  • Loading branch information
xStrom committed Apr 16, 2017
1 parent 6c3ec5d commit 473f0b8
Show file tree
Hide file tree
Showing 8 changed files with 544 additions and 182 deletions.
67 changes: 67 additions & 0 deletions art/art.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2017 Kaur Kuut
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package art

import (
"bytes"
"image"
"image/color"
"image/png"

"github.com/pkg/errors"
)

const (
White = iota
LightGray
Gray
Black
Pink
Red
Orange
Brown
Yellow
LightGreen
Green
Cyan
MediumBlue
DarkBlue
LightPurple
DarkPurple
)

type Pixel struct {
X int
Y int
C int
}

func ParseImage(data []byte) (image.Image, error) {
buf := bytes.NewBuffer(data)
img, err := png.Decode(buf)
if err != nil {
return nil, errors.Wrap(err, "Failed to decode image")
}
if img.Bounds().Min.X != 0 || img.Bounds().Min.Y != 0 || img.Bounds().Max.X != 1000 || img.Bounds().Max.Y != 1000 {
return nil, errors.New("Unexpected image bounds")
}
return img, nil
}

func SameColor(c1, c2 color.Color) bool {
r1, g1, b1, a1 := c1.RGBA()
r2, g2, b2, a2 := c2.RGBA()
return r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2
}
75 changes: 75 additions & 0 deletions art/estflag/estflag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2017 Kaur Kuut
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package estflag

import (
"image"
"image/color"

"github.com/xStrom/patriot/art"
"github.com/xStrom/patriot/painter"
)

var x0, y0 = 75, 36
var x1, y1 = 107, 56

var blue = color.RGBA64{0, 0, 60138, 65535}
var black = color.RGBA64{8738, 8738, 8738, 65535}
var white = color.RGBA64{65535, 65535, 65535, 65535}

// Fixes any broken pixels in the provided image
func CheckImage(image image.Image) {
for x := x0; x <= x1; x++ {
for y := y0; y <= y1; y++ {
c := image.At(x, y)

switch (y - y0) / 7 {
case 0:
if !art.SameColor(c, blue) {
painter.SetPixel(&art.Pixel{x, y, art.DarkBlue})
}
case 1:
if !art.SameColor(c, black) {
painter.SetPixel(&art.Pixel{x, y, art.Black})
}
case 2:
if !art.SameColor(c, white) {
painter.SetPixel(&art.Pixel{x, y, art.White})
}
}
}
}
}

// Fixes the provided pixel if needed
func CheckPixel(x, y, c int) {
// Make sure the pixel is even in bounds
if x >= x0 && x <= x1 && y >= y0 && y <= y1 {
switch (y - y0) / 7 {
case 0:
if c != art.DarkBlue {
painter.SetPixel(&art.Pixel{x, y, art.DarkBlue})
}
case 1:
if c != art.Black {
painter.SetPixel(&art.Pixel{x, y, art.Black})
}
case 2:
if c != art.White {
painter.SetPixel(&art.Pixel{x, y, art.White})
}
}
}
}
66 changes: 66 additions & 0 deletions painter/painter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2017 Kaur Kuut
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package painter

import (
"fmt"
"sync"
"time"

"github.com/xStrom/patriot/art"
"github.com/xStrom/patriot/sp"
"github.com/xStrom/patriot/work/shutdown"
)

// TODO: Instead of a dumb queue use a coordinate based map so we always only draw the latest requested value

var queue []*art.Pixel
var queueLock sync.Mutex

func Work(wg *sync.WaitGroup) {
var p *art.Pixel
for {
shutdown.ShutdownLock.RLock()
if shutdown.Shutdown {
shutdown.ShutdownLock.RUnlock()
fmt.Printf("Shutting down painter\n")
wg.Done()
break
}
shutdown.ShutdownLock.RUnlock()

queueLock.Lock()
if len(queue) > 0 {
p, queue = queue[0], queue[1:]
}
queueLock.Unlock()
if p != nil {
if err := sp.DrawPixel(p.X, p.Y, p.C); err != nil {
fmt.Printf("Failed drawing %v:%v to %v, because: %v", p.X, p.Y, p.C, err)
queueLock.Lock()
queue = append(queue, p)
queueLock.Unlock()
}
p = nil
}
time.Sleep(1 * time.Second) // Non-white pixels limited to 1/sec by server (white to 2.5/sec)
}
}

func SetPixel(p *art.Pixel) {
queueLock.Lock()
queue = append(queue, p)
queueLock.Unlock()
}
Loading

0 comments on commit 473f0b8

Please sign in to comment.