Skip to content

Commit 20d67c1

Browse files
author
sasilva1998
committed
correccion de errores, libreria usable
1 parent a732fd3 commit 20d67c1

File tree

5 files changed

+113
-71
lines changed

5 files changed

+113
-71
lines changed

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
# OV2640_uPy
2-
Libreria para camara OV2640 con MicroPython
3-
Una mejora a partir de la libreria de namato la cual pueden encontrar en el siguiente link:
4-
https://github.com/namato/micropython-ov2640
2+
Libreria para camara OV2640 con MicroPython, especificamente para el ESP32 el cual tiene una mayor capacidad de procesamiento.
3+
Ha sido creada a partir de la de namato encontrada en el siguiente link: https://github.com/namato/micropython-ov2640
54

6-
El proposito de este repo es mejorar la libreria de namato para dar mas configuraciones para el ESP32 el cual tiene mas procesamiento.
5+
## Conexiones para la comunicación
6+
Para la comunicacion en la librería ya se ha escecificado que pines se usan, los cuales agrego a continucacion, sin embargo puede ser cambiado en el constructor:
7+
~~~~ python cam = ov2640(sclpin=22, sdapin=21, cspin=15, sckpin=14, mosipin=13, misopin=12 resolution=OV2640_320x240_JPEG, IMAGEDECODE=OV2640_YUV422) ~~~~
8+
9+
### I²C
10+
SCL -> GPIO22
11+
SDA -> GPIO21
12+
13+
### SPI
14+
CS -> GPIO15
15+
SCK -> GPIO14
16+
MOSI -> GPIO13
17+
MISO -> GPIO12
18+
19+
20+
## Proximas actualizaciones
21+
Proximamente se agregaran ciertas configuraciones posibles para la camara y ademas el ejemplo de un web server para video streaming

ov2640.py

Lines changed: 92 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,32 @@
88
import gc
99

1010
class ov2640(object):
11-
def __init__(self, sclpin=22, sdapin=21, cspin=15, resolution=OV2640_320x240_JPEG):
11+
def __init__(self, sclpin=22, sdapin=21, cspin=15, sckpin=14, mosipin=13, misopin=12 resolution=OV2640_320x240_JPEG, IMAGEDECODE=OV2640_YUV422):
12+
1213
gc.enable()
14+
15+
#I2C pins
1316
self.sclpin=sclpin
1417
self.sdapin=sdapin
1518
self.cspin=cspin
16-
self.standby=False
1719

18-
self.hspi = machine.SPI(1, baudrate=80000000, polarity=0, phase=0, sck=machine.Pin(14), mosi=machine.Pin(13), miso=machine.Pin(12))
20+
#SPI pins
21+
self.sckpin=sckpin
22+
self.mosipin=mosipin
23+
self.misopin=misopin
24+
25+
self.standby=False #variable para control de estado de camara
26+
27+
#iniciacion de buses para la comunicacion
28+
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))
1929
self.i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21), freq=1000000)
2030
self.hspi.init(baudrate=2000000)
2131

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

36+
#deteccion de la camara
2537
addrs = self.i2c.scan()
2638
print('ov2640_init: devices detected on on i2c:')
2739
for a in addrs:
@@ -38,7 +50,7 @@ def __init__(self, sclpin=22, sdapin=21, cspin=15, resolution=OV2640_320x240_JPE
3850

3951
# jpg init registers
4052
cam_write_register_set(self.i2c, SENSORADDR, OV2640_JPEG_INIT)
41-
cam_write_register_set(self.i2c, SENSORADDR, OV2640_YUV422)
53+
cam_write_register_set(self.i2c, SENSORADDR, IMAGEDECODE)
4254
cam_write_register_set(self.i2c, SENSORADDR, OV2640_JPEG)
4355

4456
self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
@@ -70,6 +82,82 @@ def __init__(self, sclpin=22, sdapin=21, cspin=15, resolution=OV2640_320x240_JPE
7082
(ubinascii.hexlify(parta), ubinascii.hexlify(partb)))
7183
time.sleep_us(50)
7284

85+
86+
def capture_to_file(self, fn, overwrite):
87+
# bit 0 - clear FIFO write done flag
88+
cam_spi_write(b'\x04', b'\x01', self.hspi, self.cspin)
89+
90+
# bit 1 - start capture then read status
91+
cam_spi_write(b'\x04', b'\x02', self.hspi, self.cspin)
92+
time.sleep_ms(10)
93+
94+
# read status
95+
res = cam_spi_read(b'\x41', self.hspi, self.cspin)
96+
cnt = 0
97+
#if (res == b'\x00'):
98+
# print("initiate capture may have failed, return byte: %s" % ubinascii.hexlify(res))
99+
100+
# read the image from the camera fifo
101+
while True:
102+
res = cam_spi_read(b'\x41', self.hspi, self.cspin)
103+
mask = b'\x08'
104+
if (res[0] & mask[0]):
105+
break
106+
#print("continuing, res register %s" % ubinascii.hexlify(res))
107+
time.sleep_ms(10)
108+
cnt += 1
109+
#print("slept in loop %d times" % cnt)
110+
111+
# read the fifo size
112+
b1 = cam_spi_read(b'\x44', self.hspi, self.cspin)
113+
b2 = cam_spi_read(b'\x43', self.hspi, self.cspin)
114+
b3 = cam_spi_read(b'\x42', self.hspi, self.cspin)
115+
val = b1[0] << 16 | b2[0] << 8 | b3[0]
116+
print("ov2640_capture: %d bytes in fifo" % val)
117+
gc.collect()
118+
119+
bytebuf = [ 0, 0 ]
120+
picbuf = [ b'\x00' ] * PICBUFSIZE
121+
l = 0
122+
bp = 0
123+
if (overwrite == True):
124+
#print("deleting old file %s" % fn)
125+
try:
126+
uos.remove(fn)
127+
except OSError:
128+
pass
129+
while ((bytebuf[0] != b'\xd9') or (bytebuf[1] != b'\xff')):
130+
bytebuf[1] = bytebuf[0]
131+
if (bp > (len(picbuf) - 1)):
132+
#print("appending buffer to %s" % fn)
133+
appendbuf(fn, picbuf, bp)
134+
bp = 0
135+
136+
bytebuf[0] = cam_spi_read(b'\x3d', self.hspi, self.cspin)
137+
l += 1
138+
#print("read so far: %d, next byte: %s" % (l, ubinascii.hexlify(bytebuf[0])))
139+
picbuf[bp] = bytebuf[0]
140+
bp += 1
141+
if (bp > 0):
142+
#print("appending final buffer to %s" % fn)
143+
appendbuf(fn, picbuf, bp)
144+
print("read %d bytes from fifo, camera said %d were available" % (l, val))
145+
return (l)
146+
147+
def standby(self):
148+
# register set select
149+
self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
150+
# standby mode
151+
self.i2c.writeto_mem(SENSORADDR, 0x09, b'\x10')
152+
self.standby = True
153+
154+
def wake(self):
155+
# register set select
156+
self.i2c.writeto_mem(SENSORADDR, 0xff, b'\x01')
157+
# standby mode
158+
self.i2c.writeto_mem(SENSORADDR, 0x09, b'\x00')
159+
self.standby = False
160+
73161
def cam_write_register_set(i, addr, set):
74162
for el in set:
75163
raddr = el[0]
@@ -99,66 +187,7 @@ def appendbuf(fn, picbuf, howmany):
99187
print("error writing file")
100188
print("write %d bytes from buffer" % howmany)
101189

102-
def capture_to_file(fn, overwrite):
103-
# bit 0 - clear FIFO write done flag
104-
cam_spi_write(b'\x04', b'\x01', hspi, cspin)
105-
106-
# bit 1 - start capture then read status
107-
cam_spi_write(b'\x04', b'\x02', hspi, cspin)
108-
time.sleep_ms(10)
109-
110-
# read status
111-
res = cam_spi_read(b'\x41', hspi, cspin)
112-
cnt = 0
113-
#if (res == b'\x00'):
114-
# print("initiate capture may have failed, return byte: %s" % ubinascii.hexlify(res))
115-
116-
# read the image from the camera fifo
117-
while True:
118-
res = cam_spi_read(b'\x41', hspi, cspin)
119-
mask = b'\x08'
120-
if (res[0] & mask[0]):
121-
break
122-
#print("continuing, res register %s" % ubinascii.hexlify(res))
123-
time.sleep_ms(10)
124-
cnt += 1
125-
#print("slept in loop %d times" % cnt)
126-
127-
# read the fifo size
128-
b1 = cam_spi_read(b'\x44', hspi, cspin)
129-
b2 = cam_spi_read(b'\x43', hspi, cspin)
130-
b3 = cam_spi_read(b'\x42', hspi, cspin)
131-
val = b1[0] << 16 | b2[0] << 8 | b3[0]
132-
print("ov2640_capture: %d bytes in fifo" % val)
133-
gc.collect()
134-
135-
bytebuf = [ 0, 0 ]
136-
picbuf = [ b'\x00' ] * PICBUFSIZE
137-
l = 0
138-
bp = 0
139-
if (overwrite == True):
140-
#print("deleting old file %s" % fn)
141-
try:
142-
uos.remove(fn)
143-
except OSError:
144-
pass
145-
while ((bytebuf[0] != b'\xd9') or (bytebuf[1] != b'\xff')):
146-
bytebuf[1] = bytebuf[0]
147-
if (bp > (len(picbuf) - 1)):
148-
#print("appending buffer to %s" % fn)
149-
appendbuf(fn, picbuf, bp)
150-
bp = 0
151190

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

163192
def cam_spi_read(address, hspi, cspin):
164193
cspin.value(0)

ov2640_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,4 @@
424424
]
425425

426426
SENSORADDR = 0x30
427-
PICBUFSIZE = 128
427+
PICBUFSIZE = 128

ov2640_hires_constants.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11

2-
# 800x600 removed to save memory
3-
42
OV2640_1024x768_JPEG = [
53
[0xff, b'\x01'],
64
[0x11, b'\x01'],

ov2640_lores_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
[ 0x5b, b'\x3c' ],
4141
[ 0x5c, b'\x00' ],
4242
[ 0xe0, b'\x00' ],
43-
[ 0xff, b'\xff' ],
43+
[ 0xff, b'\xff' ]
4444
]
4545

4646
OV2640_352x288_JPEG = [

0 commit comments

Comments
 (0)