Skip to content

Commit

Permalink
Merge pull request #11 from tgreiser/tng-working
Browse files Browse the repository at this point in the history
New drawing techniques and lissajous example.
  • Loading branch information
tgreiser authored May 2, 2017
2 parents dc9ad94 + a05ed53 commit bc77b1d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
7 changes: 7 additions & 0 deletions draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"image/color"
"io"
"log"
"math"
"time"

"github.com/tgreiser/ln/ln"
Expand Down Expand Up @@ -99,3 +100,9 @@ func BlankPath(w io.WriteCloser, p ln.Path) *Point {
}
return pt
}

// Osc is an oscillator value - send it frame counts / point counts
func Osc(cur, max int, amplitude, frequency, offset float64) float64 {
rad := float64(cur+1) / float64(max) * 2.0 * math.Pi
return math.Sin(rad*frequency)*amplitude + offset
}
98 changes: 98 additions & 0 deletions examples/lissajous/lissa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
# Copyright 2016 Tim Greiser
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package main

import (
"image/color"
"io"
"log"
"math"

"flag"

"github.com/tgreiser/etherdream"
"github.com/tgreiser/ln/ln"
)

func main() {
log.Printf("Listening...\n")
addr, bp, err := etherdream.FindFirstDAC()
if err != nil {
log.Fatalf("Network error: %v", err)
}

log.Printf("Found DAC at %v\n", addr)
log.Printf("BP: %v\n\n", bp)

dac, err := etherdream.NewDAC(addr.IP.String())
if err != nil {
log.Fatal(err)
}
defer dac.Close()
log.Printf("Initialized: %v\n\n", dac.LastStatus)
log.Printf("Firmware String: %v\n\n", dac.FirmwareString)

dac.Play(pointStream)
}

var max = flag.Int("speed", 500, "Speed to run the oscillation (1-20000)")
var xAmp = flag.Float64("x-amp", 20.0, "X amplitude (1.0 - 50.0)")
var yAmp = flag.Float64("y-amp", 5.0, "Y amplitude (1.0 - 50.0)")

func pointStream(w io.WriteCloser) {
defer w.Close()
pcount := etherdream.FramePoints() - *etherdream.BlankCount
xx := 0
xy := 0

var pt *etherdream.Point
for {
xRate := etherdream.Osc(xx%*max, *max, *xAmp, 1, 2.5)
yRate := etherdream.Osc(xy%*max, *max, *yAmp, 1, 2.5)
xx++
xy++
for iX := 0; iX < pcount; iX++ {
if iX == 0 && pt != nil {
nextPt := graph(w, pcount, iX, xRate, yRate)
blank := ln.Path{pt.ToVector(), nextPt.ToVector()}
etherdream.BlankPath(w, blank)
pt = nextPt
} else {
pt = graph(w, pcount, iX, xRate, yRate)
}
}
}
}

var xyMin = -5600
var xyMax = 5600
var xyRange = xyMax - xyMin

func graph(w io.WriteCloser, max, cur int, xRate, yRate float64) *etherdream.Point {
// 0 - max = 0 - 2 radians
rad := float64(cur+1) / float64(max) * 2.0 * math.Pi
x := math.Sin(rad*xRate)*float64(xyRange) + float64(xyMin)
y := math.Sin(rad*yRate)*float64(xyRange) + float64(xyMin)

pt := etherdream.NewPoint(int(x), int(y), colorOsc(max, cur))
w.Write(pt.Encode())
return pt
}

func colorOsc(max, cur int) color.Color {
return color.RGBA{0x00, 0x00, 0x55, 0xff}
}
6 changes: 6 additions & 0 deletions point.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"image/color"
"io"

"github.com/tgreiser/ln/ln"
)

// PointStream is the interface clients should implement to
Expand Down Expand Up @@ -92,6 +94,10 @@ func (p Point) Encode() []byte {
return enc
}

func (p Point) ToVector() ln.Vector {
return ln.Vector{float64(p.X), float64(p.Y), 0.0}
}

// Points - Point list
type Points struct {
Points []Point
Expand Down

0 comments on commit bc77b1d

Please sign in to comment.