#!/bin/python

import time
import pigpio
import socket

DEV_ADDR = 0x58
pi = pigpio.pi()

def initDevice():

  h = pi.i2c_open(1, DEV_ADDR)
  pi.i2c_write_device(h, [0x30, 0x01])
  time.sleep(0.1)
  pi.i2c_write_device(h, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90])
  time.sleep(0.1)
  pi.i2c_write_device(h, [0x70, 0x00, 0x41])
  time.sleep(0.1)
  pi.i2c_write_device(h, [0x1A, 0x40, 0x00])
  time.sleep(0.1)
  pi.i2c_write_device(h, [0x33, 0x03])
  time.sleep(0.1)
  pi.i2c_write_device(h, [0x30, 0x08])
  time.sleep(0.1)
  return h

# x, y, size
def parseBlob(b):
  if (b[0] == 0xff and b[1] == 0xff and b[2] == 0xff):
    return None
  x = b[0] + ((b[2] & 0x30) << 4)
  y = b[1] + ((b[2] & 0xC0) << 2)
  s = b[2] & 0x0F
  return (x, y, s)

def sample(h):
  pi.i2c_write_device(h, [0x37])
  time.sleep(25/1000000)
  (count, data1) = pi.i2c_read_device(h, 8)
  time.sleep(380/1000000)
  (count, data2) = pi.i2c_read_device(h, 4)
  data = data1 + data2

  return (parseBlob(data[0:3]), parseBlob(data[3:6]), parseBlob(data[6:9]), parseBlob(data[9:12]))

# Set pin 4 to a 25 MHz clock
pi.hardware_clock(4, 20000000)

# Set pin 17 to 1 to disable the active-low reset pin
pi.set_mode(17, pigpio.OUTPUT)
pi.write(17, 1)

# Initialize i2c camera
h = initDevice()

client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.bind(('0.0.0.0', 56050))
print socket.gethostname()
(data, address) = client.recvfrom(4096)
print address

def control():
  pass

def sample(outbytes):
  pass

def stream(inbytes):
  pass
i = 0;
prevS = sample(h);
while True:
  t = time.clock()
  s = sample(h);
  while (s == prevS):
    s = sample(h);
  prevS = s;
  client.sendto(str((i, s)) + '\n', address)
  i += 1;
  time.sleep(0.01)

@@ -47,16 +47,39 @@
 t socket
+%0Aimport multiprocessing
 %0A%0ADEV_AD
@@ -124,17 +124,16 @@
 vice():%0A
-%0A
   h = pi
@@ -557,16 +557,276 @@
 turn h%0A%0A
+def sampleToBuffer(outbytes):%0A  while True:%0A    pi.i2c_write_device(h, %5B0x37%5D)%0A    time.sleep(25/1000000)%0A    (count, data1) = pi.i2c_read_device(h, 8)%0A    time.sleep(380/1000000)%0A    (count, data2) = pi.i2c_read_device(h, 4)%0A    outbytes%5B:%5D = data1 + data2;%0A%0A
 # x, y, 
@@ -1034,190 +1034,205 @@
 ef s
-ample(h):%0A  pi.i2c_write_device(h, %5B0x37%5D)%0A  time.sleep(25/1000000)%0A  (count, data1) = pi.i2c_read_device(h, 8)%0A  time.sleep(380/1000000)%0A  (count, data2) = pi.i2c_read_device(h, 4)%0A
+treamFromBuffer(inbytes):%0A  client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)%0A  client.bind(('', 56050))%0A  (data, address) = client.recvfrom(4096)%0A  print address%0A%0A  i = 0;%0A  while True:%0A  
   da
@@ -1240,31 +1240,26 @@
 a = 
-data1 + data2%0A%0A  return
+inbytes%5B:%5D%0A    s =
  (pa
@@ -1344,16 +1344,119 @@
 %5B9:12%5D))
+%0A    client.sendto(str((i, s)) + '%5Cn', address) %0A    print str((i, s))%0A    i += 1;%0A    time.sleep(0.01)
 %0A%0A# Set 
@@ -1659,459 +1659,249 @@
 ()%0A%0A
-client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)%0Aclient.bind(('0.0.0.0', 56050))%0Aprint socket.gethostname()%0A(data, address) = client.recvfrom(4096)%0Aprint address%0A%0Adef control():%0A  pass%0A%0Adef sample(outbytes):%0A  pass%0A%0Adef stream(inbytes):%0A  pass%0Ai = 0;%0AprevS = sample(h);%0Awhile True:%0A  t = time.clock()%0A  s = sample(h);%0A  while (s == prevS):%0A    s = sample(h);%0A  prevS = s;%0A  client.sendto(str((i, s)) + '%5Cn', address)%0A  i += 1;%0A  time.sleep(0.01
+# Begin sampling data from the camera%0Abytes = multiprocessing.Array('i', 12)%0Ap = multiprocessing.Process(target=sampleToBuffer, args=(bytes,))%0Ap.start()%0A%0A# Wait for an incoming http connection%0A%0Aserver_address = ('', 80)%0A%0AstreamFromBuffer(bytes
 )%0A

use multiprocessing for streaming data