#!/usr/bin/env python

'''

Improved dac.py and talk.py (by Jacob Potter). Pimped by Sam Neurohack.

- python3 
- works with nannou visualisers
- can talk to a given etherdream knowing it's IP.

v0.1.0

'''

#
# j4cDAC test code
#
# Copyright 2011 Jacob Potter
#
# 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/>.

import dac3 as dac
import argparse
import sys

argsparser = argparse.ArgumentParser(description="Draw a square on a laser via Etherdream DAC")

argsparser.add_argument("-i","--ip",help="Etherdream IP (default : first etherdream broadcast received)",default="True",type=str)



args        = argsparser.parse_args()
etherIP     = args.ip



class SquarePointStream(object):
    '''
    def produce(self):
        pmax = 15600
        pstep = 100
        cmax = 65535
        while True:
            for x in xrange(-pmax, pmax, pstep):
                yield (x, pmax, cmax, 0, 0, cmax)
            for y in xrange(pmax, -pmax, -pstep):
                yield (pmax, y, 0, cmax, 0, cmax)
            for x in xrange(pmax, -pmax, -pstep):
                yield (x, -pmax, 0, 0, cmax, cmax)
            for y in xrange(-pmax, pmax, pstep):
                yield (-pmax, y, cmax, cmax, cmax, cmax)
    '''

    def produce(self):

        pmax = 15600
        pstep = 100
        Cmax = 65535

        
        while True:

            print("Cmax:",Cmax)
                        
            for x in range(-pmax, pmax, pstep):

                yield (x, pmax,  Cmax, 0, 0)            # pure Red
                #yield (x, pmax, 0, Cmax, 0)            # pure Green
                #yield (x, pmax, 0, 0, Cmax)            # pure Blue
                #yield (x, pmax, Cmax, Cmax, Cmax)       # pure White

            
            for y in range(pmax, -pmax, -pstep):

                #yield (pmax, y,  Cmax, 0, 0)            # pure Red
                yield (pmax, y, 0, Cmax, 0)              # pure Green
                #yield (pmax, y,  0, 0, Cmax)           # pure Blue
                #yield (pmax, y, Cmax, Cmax, Cmax)       # pure White
            
            for x in range(pmax, -pmax, -pstep):
                #yield (x, -pmax,  Cmax, 0, 0)          # pure Red
                #yield (x, -pmax, 0, Cmax, 0)           # pure Green
                yield (x, -pmax, 0, 0, Cmax)           # pure Blue
                #yield (x, -pmax, Cmax, Cmax, Cmax)     # pure White
                
            
            for y in range(-pmax, pmax, pstep):
                #yield (-pmax, y,   Cmax, 0, 0)         # pure Red
                #yield (-pmax, y,0, Cmax, 0)            # pure Green
                #yield (-pmax, y, 0, 0, Cmax)           # pure Blue
                yield (-pmax, y, Cmax, Cmax, Cmax)      # pure White




    def __init__(self):
        self.stream = self.produce()

    def read(self, n):
        return [next(self.stream) for i in range(n)]

class NullPointStream(object):
    def read(self, n):
        return [(0, 0, 0, 0, 0)] * n

#dac.find_dac()
if etherIP == "True":
    print("Waiting for the first DAC broadcast...")
    d = dac.DAC(dac.find_first_dac())

else:
    print("Using Etherdream :", etherIP)
    d = dac.DAC(etherIP)


print("Sending points...")
d.play_stream(SquarePointStream())
