Skip to content

Commit

Permalink
errores de clase
Browse files Browse the repository at this point in the history
  • Loading branch information
sasilva1998 committed Mar 19, 2019
1 parent f38bf31 commit baf17e5
Showing 1 changed file with 126 additions and 128 deletions.
254 changes: 126 additions & 128 deletions ov2640.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,155 +8,155 @@
import gc

class ov2640(object):
def __init__(self, sclpin=22, sdapin=21, cspin=15, sckpin=14, mosipin=13, misopin=12, resolution=OV2640_320x240_JPEG, IMAGEDECODE=OV2640_YUV422):
def __init__(self, sclpin=22, sdapin=21, cspin=15, sckpin=14, mosipin=13, misopin=12, resolution=OV2640_320x240_JPEG, IMAGEDECODE=OV2640_YUV422):

gc.enable()
gc.enable()

#I2C pins
self.sclpin=sclpin
self.sdapin=sdapin
self.cspin=cspin
self.sclpin=sclpin
self.sdapin=sdapin
self.cspin=cspin

#SPI pins
self.sckpin=sckpin
self.mosipin=mosipin
self.misopin=misopin

self.standby=False #variable para control de estado de camara
self.standby=False #variable para control de estado de camara

#iniciacion de buses para la comunicacion
self.hspi = machine.SPI(1, baudrate=80000000, polarity=0, phase=0, sck=machine.Pin(self.sckpin), mosi=machine.Pin(self.mosipin), miso=machine.Pin(self.misopin))
self.i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21), freq=1000000)
self.hspi.init(baudrate=2000000)
self.hspi = machine.SPI(1, baudrate=80000000, polarity=0, phase=0, sck=machine.Pin(self.sckpin), mosi=machine.Pin(self.mosipin), miso=machine.Pin(self.misopin))
self.i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21), freq=1000000)
self.hspi.init(baudrate=2000000)

#cs pin para la comunicacion spi, tener en cuenta que este puede ser cualquier gpio
self.cspin = machine.Pin(self.cspin, machine.Pin.OUT)
self.cspin.value(1)
self.cspin = machine.Pin(self.cspin, machine.Pin.OUT)
self.cspin.value(1)

#deteccion de la camara
addrs = self.i2c.scan()
print('ov2640_init: devices detected on on i2c:')
for a in addrs:
print('0x%x' % a)
time.sleep(1)
addrs = self.i2c.scan()
print('ov2640_init: devices detected on on i2c:')
for a in addrs:
print('0x%x' % a)
time.sleep(1)


self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
# initiate system reset
self.i2c.writeto_mem(SENSORADDR, 0x12, b'\x80')
self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
# initiate system reset
self.i2c.writeto_mem(SENSORADDR, 0x12, b'\x80')

# let it come up
time.sleep_ms(100)
# let it come up
time.sleep_ms(100)

# jpg init registers
cam_write_register_set(self.i2c, SENSORADDR, OV2640_JPEG_INIT)
cam_write_register_set(self.i2c, SENSORADDR, IMAGEDECODE)
cam_write_register_set(self.i2c, SENSORADDR, OV2640_JPEG)
# jpg init registers
cam_write_register_set(self.i2c, SENSORADDR, OV2640_JPEG_INIT)
cam_write_register_set(self.i2c, SENSORADDR, IMAGEDECODE)
cam_write_register_set(self.i2c, SENSORADDR, OV2640_JPEG)

self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
self.i2c.writeto_mem(SENSORADDR, 0x15, b'\x00')
self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
self.i2c.writeto_mem(SENSORADDR, 0x15, b'\x00')

cam_write_register_set(i2c, SENSORADDR, OV2640_1600x1200_JPEG)

cam_spi_write(b'\x00', b'\x55', self.hspi, self.cspin)
res = cam_spi_read(b'\x00', self.hspi, self.cspin)
print(res)
print("ov2640 init: register test return bytes %s" % ubinascii.hexlify(res))
if (res == b'\x55'):
print("ov2640_init: register test successful")
else:
print("ov2640_init: register test failed!")
time.sleep_us(10)

self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
# check the camera type
time.sleep_us(50)
parta = self.i2c.readfrom_mem(SENSORADDR, 0x0a, 1)
time.sleep_us(50)
partb = self.i2c.readfrom_mem(SENSORADDR, 0x0b, 1)
if ((parta != b'\x26') or (partb != b'\x42')):
print("ov2640_init: device type does not appear to be ov2640, bytes: %s/%s" % \
(ubinascii.hexlify(parta), ubinascii.hexlify(partb)))
else:
print("ov2640_init: device type looks correct, bytes: %s/%s" % \
(ubinascii.hexlify(parta), ubinascii.hexlify(partb)))
time.sleep_us(50)


def capture_to_file(self, fn, overwrite):
# bit 0 - clear FIFO write done flag
cam_spi_write(b'\x04', b'\x01', self.hspi, self.cspin)

# bit 1 - start capture then read status
cam_spi_write(b'\x04', b'\x02', self.hspi, self.cspin)
time.sleep_ms(10)

# read status
cam_write_register_set(self.i2c, SENSORADDR, OV2640_1600x1200_JPEG)

cam_spi_write(b'\x00', b'\x55', self.hspi, self.cspin)
res = cam_spi_read(b'\x00', self.hspi, self.cspin)
print(res)
print("ov2640 init: register test return bytes %s" % ubinascii.hexlify(res))
if (res == b'\x55'):
print("ov2640_init: register test successful")
else:
print("ov2640_init: register test failed!")
time.sleep_us(10)

self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
# check the camera type
time.sleep_us(50)
parta = self.i2c.readfrom_mem(SENSORADDR, 0x0a, 1)
time.sleep_us(50)
partb = self.i2c.readfrom_mem(SENSORADDR, 0x0b, 1)
if ((parta != b'\x26') or (partb != b'\x42')):
print("ov2640_init: device type does not appear to be ov2640, bytes: %s/%s" % \
(ubinascii.hexlify(parta), ubinascii.hexlify(partb)))
else:
print("ov2640_init: device type looks correct, bytes: %s/%s" % \
(ubinascii.hexlify(parta), ubinascii.hexlify(partb)))
time.sleep_us(50)


def capture_to_file(self, fn, overwrite):
# bit 0 - clear FIFO write done flag
cam_spi_write(b'\x04', b'\x01', self.hspi, self.cspin)

# bit 1 - start capture then read status
cam_spi_write(b'\x04', b'\x02', self.hspi, self.cspin)
time.sleep_ms(10)

# read status
res = cam_spi_read(b'\x41', self.hspi, self.cspin)
cnt = 0
#if (res == b'\x00'):
# print("initiate capture may have failed, return byte: %s" % ubinascii.hexlify(res))

# read the image from the camera fifo
while True:
res = cam_spi_read(b'\x41', self.hspi, self.cspin)
cnt = 0
#if (res == b'\x00'):
# print("initiate capture may have failed, return byte: %s" % ubinascii.hexlify(res))

# read the image from the camera fifo
while True:
res = cam_spi_read(b'\x41', self.hspi, self.cspin)
mask = b'\x08'
if (res[0] & mask[0]):
break
#print("continuing, res register %s" % ubinascii.hexlify(res))
time.sleep_ms(10)
cnt += 1
#print("slept in loop %d times" % cnt)

# read the fifo size
b1 = cam_spi_read(b'\x44', self.hspi, self.cspin)
b2 = cam_spi_read(b'\x43', self.hspi, self.cspin)
b3 = cam_spi_read(b'\x42', self.hspi, self.cspin)
val = b1[0] << 16 | b2[0] << 8 | b3[0]
print("ov2640_capture: %d bytes in fifo" % val)
gc.collect()

bytebuf = [ 0, 0 ]
picbuf = [ b'\x00' ] * PICBUFSIZE
l = 0
bp = 0
if (overwrite == True):
#print("deleting old file %s" % fn)
try:
uos.remove(fn)
except OSError:
pass
while ((bytebuf[0] != b'\xd9') or (bytebuf[1] != b'\xff')):
bytebuf[1] = bytebuf[0]
if (bp > (len(picbuf) - 1)):
#print("appending buffer to %s" % fn)
appendbuf(fn, picbuf, bp)
bp = 0

bytebuf[0] = cam_spi_read(b'\x3d', self.hspi, self.cspin)
l += 1
#print("read so far: %d, next byte: %s" % (l, ubinascii.hexlify(bytebuf[0])))
picbuf[bp] = bytebuf[0]
bp += 1
if (bp > 0):
#print("appending final buffer to %s" % fn)
mask = b'\x08'
if (res[0] & mask[0]):
break
#print("continuing, res register %s" % ubinascii.hexlify(res))
time.sleep_ms(10)
cnt += 1
#print("slept in loop %d times" % cnt)

# read the fifo size
b1 = cam_spi_read(b'\x44', self.hspi, self.cspin)
b2 = cam_spi_read(b'\x43', self.hspi, self.cspin)
b3 = cam_spi_read(b'\x42', self.hspi, self.cspin)
val = b1[0] << 16 | b2[0] << 8 | b3[0]
print("ov2640_capture: %d bytes in fifo" % val)
gc.collect()

bytebuf = [ 0, 0 ]
picbuf = [ b'\x00' ] * PICBUFSIZE
l = 0
bp = 0
if (overwrite == True):
#print("deleting old file %s" % fn)
try:
uos.remove(fn)
except OSError:
pass
while ((bytebuf[0] != b'\xd9') or (bytebuf[1] != b'\xff')):
bytebuf[1] = bytebuf[0]
if (bp > (len(picbuf) - 1)):
#print("appending buffer to %s" % fn)
appendbuf(fn, picbuf, bp)
print("read %d bytes from fifo, camera said %d were available" % (l, val))
return (l)

def standby(self):
# register set select
self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
# standby mode
self.i2c.writeto_mem(SENSORADDR, 0x09, b'\x10')
self.standby = True

def wake(self):
# register set select
self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
# standby mode
self.i2c.writeto_mem(SENSORADDR, 0x09, b'\x00')
self.standby = False
bp = 0

bytebuf[0] = cam_spi_read(b'\x3d', self.hspi, self.cspin)
l += 1
#print("read so far: %d, next byte: %s" % (l, ubinascii.hexlify(bytebuf[0])))
picbuf[bp] = bytebuf[0]
bp += 1
if (bp > 0):
#print("appending final buffer to %s" % fn)
appendbuf(fn, picbuf, bp)
print("read %d bytes from fifo, camera said %d were available" % (l, val))
return (l)

def standby(self):
# register set select
self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
# standby mode
self.i2c.writeto_mem(SENSORADDR, 0x09, b'\x10')
self.standby = True

def wake(self):
# register set select
self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
# standby mode
self.i2c.writeto_mem(SENSORADDR, 0x09, b'\x00')
self.standby = False

def cam_write_register_set(i, addr, set):
for el in set:
Expand Down Expand Up @@ -187,8 +187,6 @@ def appendbuf(fn, picbuf, howmany):
print("error writing file")
print("write %d bytes from buffer" % howmany)



def cam_spi_read(address, hspi, cspin):
cspin.value(0)
maskbits = b'\x7f'
Expand Down

0 comments on commit baf17e5

Please sign in to comment.