8
0
Fork 0

Do nothing when idle

When you're programming inefficient crap, you're programming climate change.
This commit is contained in:
polyfloyd 2022-08-21 16:26:39 +02:00
parent 64bc680f07
commit 9c269f2612
2 changed files with 34 additions and 28 deletions

View file

@ -15,10 +15,10 @@ const (
LedpanelHeight = 48 LedpanelHeight = 48
) )
func Listen() (chan image.Image, chan error) { func Listen() (<-chan BitvisImage, chan error) {
errs := make(chan error, 1) errs := make(chan error, 1)
out := make(chan image.Image, 1) out := make(chan BitvisImage, 1)
out <- bitvisImage{} out <- BitvisImage{}
go func() { go func() {
defer close(errs) defer close(errs)
defer close(out) defer close(out)
@ -43,7 +43,7 @@ func Listen() (chan image.Image, chan error) {
return out, errs return out, errs
} }
func handleConnection(conn net.Conn, out chan<- image.Image) error { func handleConnection(conn net.Conn, out chan<- BitvisImage) error {
for { for {
if err := conn.SetReadDeadline(time.Now().Add(time.Second * 4)); err != nil { if err := conn.SetReadDeadline(time.Now().Add(time.Second * 4)); err != nil {
return err return err
@ -58,7 +58,7 @@ func handleConnection(conn net.Conn, out chan<- image.Image) error {
} }
io.CopyN(ioutil.Discard, conn, 2) io.CopyN(ioutil.Discard, conn, 2)
var img bitvisImage var img BitvisImage
if _, err := io.ReadFull(conn, img[:]); err != nil { if _, err := io.ReadFull(conn, img[:]); err != nil {
return err return err
} }
@ -69,20 +69,20 @@ func handleConnection(conn net.Conn, out chan<- image.Image) error {
} }
} }
type bitvisImage [LedpanelWidth * LedpanelHeight / 4]uint8 type BitvisImage [LedpanelWidth * LedpanelHeight / 4]uint8
func (img bitvisImage) ColorModel() color.Model { func (img BitvisImage) ColorModel() color.Model {
return color.RGBAModel return color.RGBAModel
} }
func (img bitvisImage) Bounds() image.Rectangle { func (img BitvisImage) Bounds() image.Rectangle {
return image.Rectangle{ return image.Rectangle{
Min: image.Point{X: 0, Y: 0}, Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: LedpanelWidth, Y: LedpanelHeight}, Max: image.Point{X: LedpanelWidth, Y: LedpanelHeight},
} }
} }
func (img bitvisImage) At(x, y int) color.Color { func (img BitvisImage) At(x, y int) color.Color {
return bitvisColor(img[y*LedpanelWidth/4+x/4] >> ((3 - uint(x)%4) * 2)) return bitvisColor(img[y*LedpanelWidth/4+x/4] >> ((3 - uint(x)%4) * 2))
} }

44
main.go
View file

@ -8,7 +8,6 @@ import (
"log" "log"
"net/http" "net/http"
"sync" "sync"
"time"
) )
func main() { func main() {
@ -17,8 +16,9 @@ func main() {
err := <-errors err := <-errors
log.Fatal(err) log.Fatal(err)
}() }()
var currentFrame []byte var currentFrame BitvisImage
var currentFrameLock sync.RWMutex var currentFrameLock sync.RWMutex
currentFrameUpdate := sync.NewCond(currentFrameLock.RLocker())
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
@ -42,39 +42,45 @@ func main() {
</html> </html>
`)) `))
}) })
mux.HandleFunc("/stream.mpng", func(res http.ResponseWriter, req *http.Request) { mux.HandleFunc("/stream.mpng", func(w http.ResponseWriter, r *http.Request) {
res.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary=--pngboundary") w.Header().Set("Content-Type", "multipart/x-mixed-replace; boundary=--pngboundary")
res.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
for { for {
currentFrameLock.RLock() currentFrameLock.RLock()
buf := currentFrame currentFrameUpdate.Wait()
img := currentFrame
currentFrameLock.RUnlock() currentFrameLock.RUnlock()
res.Write([]byte("--pngboundary")) buf := encodeImage(&img)
res.Write([]byte("Content-Type: image/png\n")) w.Write([]byte("--pngboundary"))
res.Write([]byte(fmt.Sprintf("Content-Length: %d\n\n", len(buf)))) w.Write([]byte("Content-Type: image/png\n"))
if _, err := io.Copy(res, bytes.NewReader(buf)); err != nil { w.Write([]byte(fmt.Sprintf("Content-Length: %d\n\n", len(buf))))
if _, err := io.Copy(w, bytes.NewReader(buf)); err != nil {
return return
} }
time.Sleep(time.Millisecond * 2)
} }
}) })
mux.HandleFunc("/frame.png", func(res http.ResponseWriter, req *http.Request) { mux.HandleFunc("/frame.png", func(w http.ResponseWriter, r *http.Request) {
res.Header().Set("Content-Type", "image/png") w.Header().Set("Content-Type", "image/png")
currentFrameLock.RLock() currentFrameLock.RLock()
buf := currentFrame img := currentFrame
currentFrameLock.RUnlock() currentFrameLock.RUnlock()
io.Copy(res, bytes.NewReader(buf)) w.Write(encodeImage(&img))
}) })
go func() { go func() {
log.Fatal(http.ListenAndServe(":13378", mux)) log.Fatal(http.ListenAndServe(":13378", mux))
}() }()
for img := range images { for img := range images {
var buf bytes.Buffer
enc := png.Encoder{CompressionLevel: png.BestSpeed}
enc.Encode(&buf, img)
currentFrameLock.Lock() currentFrameLock.Lock()
currentFrame = buf.Bytes() currentFrame = img
currentFrameUpdate.Broadcast()
currentFrameLock.Unlock() currentFrameLock.Unlock()
} }
} }
func encodeImage(img *BitvisImage) []byte {
var buf bytes.Buffer
enc := png.Encoder{CompressionLevel: png.BestSpeed}
enc.Encode(&buf, img)
return buf.Bytes()
}