Skip to content

Commit

Permalink
Fixed code rot, compatible with latest EOR
Browse files Browse the repository at this point in the history
  • Loading branch information
kanflo committed Sep 20, 2017
1 parent 82c6119 commit e15a73e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -2,5 +2,5 @@ PROGRAM = capture
PROGRAM_SRC_DIR = . arducam
PROGRAM_INC_DIR = . arducam
OTA=1
EXTRA_COMPONENTS=extras/stdin_uart_interrupt extras/i2c extras/spi extras/spiflash extras/http-upload extras/rboot-ota
EXTRA_COMPONENTS=extras/stdin_uart_interrupt extras/i2c extras/spi extras/http-upload extras/rboot-ota
include $(EOR_ROOT)/common.mk
14 changes: 9 additions & 5 deletions arducam/arducam_arch_esp8266.c
Expand Up @@ -39,8 +39,9 @@ static uint8_t _sensor_addr;

//#define CONFIG_VERIFY

#define I2C_BUS (0)
#define I2C_SDA_PIN (2)
#define I2C_SCK_PIN (0)
#define I2C_SCL_PIN (0)


static void spi_chip_select(uint8_t cs_pin);
Expand All @@ -59,7 +60,7 @@ bool arducam_spi_init(void)
bool arducam_i2c_init(uint8_t sensor_addr)
{
printf("arducam_i2c_init\n");
i2c_init(I2C_SCK_PIN, I2C_SDA_PIN);
i2c_init(I2C_BUS, I2C_SCL_PIN, I2C_SDA_PIN, I2C_FREQ_80K);
_sensor_addr = sensor_addr;
return true;
}
Expand Down Expand Up @@ -103,13 +104,16 @@ uint8_t arducam_spi_read(uint8_t address)

uint8_t arducam_i2c_write(uint8_t regID, uint8_t regDat)
{
uint8_t data[] = {regID, regDat};
return i2c_slave_write(_sensor_addr, data, sizeof(data));
const uint8_t data[] = {regID, regDat};
return i2c_slave_write(I2C_BUS, _sensor_addr, 0, data, sizeof(data)) == 0;
}

uint8_t arducam_i2c_read(uint8_t regID, uint8_t* regDat)
{
return i2c_slave_read(_sensor_addr, regID, regDat, 1);
const uint8_t data[] = {regID};
return i2c_slave_read(I2C_BUS, _sensor_addr, data, regDat, 1) == 0;

int i2c_slave_read(uint8_t bus, uint8_t slave_addr, const uint8_t *data, uint8_t *buf, uint32_t len);
}

uint8_t arducam_i2c_write16(uint8_t regID, uint16_t regDat)
Expand Down
19 changes: 9 additions & 10 deletions camdriver.c
Expand Up @@ -25,7 +25,6 @@
#include <arducam.h>
#include <unistd.h>
#include <string.h>
#include <lwip/sockets.h> // write(...) will dump to uart if this include is missing :-/
#include <http_upload.h>
#include "timeutils.h"
#include "camdriver.h"
Expand Down Expand Up @@ -97,7 +96,7 @@ bool arducam_capture(void)
return true;
}

void arudcam_fifo_to_socket(int client_sock)
void arudcam_fifo_to_netcon(struct netconn *client)
{
uint8_t temp, temp_last = 0, buf_idx = 0;
uint32_t fifo_size, bytes_read, start_time = systime_ms();
Expand All @@ -114,11 +113,10 @@ void arudcam_fifo_to_socket(int client_sock)
temp_last = temp;
temp = arducam_read_fifo();
buffer[buf_idx++] = temp;
if (client_sock && buf_idx == BUF_SIZE) {
int res = write(client_sock, buffer, buf_idx);
if (res < 0) {
printf("\nERROR: write returned %d\n", res);
break;
if (client && buf_idx == BUF_SIZE) {
if (ERR_OK != netconn_write(client, buffer, buf_idx, NETCONN_COPY)) {
printf("netconn_write failed\n");
return;
}
buf_idx = 0;
}
Expand All @@ -128,9 +126,10 @@ void arudcam_fifo_to_socket(int client_sock)
break;
}
}
if (client_sock && buf_idx > 0) {
int res = write(client_sock, buffer, buf_idx);
(void) res;
if (client && buf_idx > 0) {
if (ERR_OK != netconn_write(client, buffer, buf_idx, NETCONN_COPY)) {
printf("netconn_write failed\n");
}
}
printf("Done, read %u bytes in %ums\n", bytes_read, systime_ms()-start_time);
}
Expand Down
7 changes: 5 additions & 2 deletions camdriver.h
Expand Up @@ -25,6 +25,9 @@
#ifndef _CAMDRIVER_H_
#define _CAMDRIVER_H_

#include <lwip/api.h>
#include <lwip/sockets.h>

#define OV2640_CHIPID_HIGH 0x0A
#define OV2640_CHIPID_LOW 0x0B

Expand All @@ -34,8 +37,8 @@ bool arducam_setup(void);
// Capture, return true if success
bool arducam_capture(void);

// Read FIFO and write to client socket
void arudcam_fifo_to_socket(int client_sock);
// Read FIFO and write to lwip netconn
void arudcam_fifo_to_netcon(struct netconn *client);

// Read FIFO and write to, well, nowhere
void arudcam_fifo_to_devnull(void);
Expand Down
113 changes: 48 additions & 65 deletions capture.c
Expand Up @@ -105,89 +105,72 @@ void server_task(void *p)
camera_ok = arducam_setup();
if (!camera_ok) {
printf("Camera init failed!\n");
return;
}

struct netconn *client = NULL;
struct netconn *nc = netconn_new(NETCONN_TCP);
if (nc == NULL) {
printf("Failed to allocate socket\n");
vTaskDelete(NULL);
}
netconn_bind(nc, IP_ADDR_ANY, 80);
netconn_listen(nc);
while (1) {
struct sockaddr_in server_addr, client_addr;
int server_sock, client_sock;
socklen_t sin_size;
bzero(&server_addr, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(80);

int recbytes;

do {
if (-1 == (server_sock = socket(AF_INET, SOCK_STREAM, 0))) {
printf("Socket error\n");
break;
}

if (-1 == bind(server_sock, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr))) {
printf("bind fail\n");
break;
}

printf(" bind port: %d\n", ntohs(server_addr.sin_port));

if (-1 == listen(server_sock, 5)) {
printf("listen fail\n");
break;
}

sin_size = sizeof(client_addr);

for (;;) {
if ((client_sock = accept(server_sock, (struct sockaddr *) &client_addr, &sin_size)) < 0) {
printf("accept fail\n");
continue;
}

printf("Client from %s:%d\n", inet_ntoa(client_addr.sin_addr), htons(client_addr.sin_port));

int opt = 50;
if (lwip_setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, &opt, sizeof(int)) < 0) {
printf("failed to set timeout on socket\n");
}
while ((recbytes = read(client_sock, buffer, BUF_SIZE)) > 0) {
buffer[recbytes] = 0;
err_t err = netconn_accept(nc, &client);
if (err == ERR_OK) {
struct netbuf *nb;

ip_addr_t client_addr;
uint16_t port_ignore;
netconn_peer(client, &client_addr, &port_ignore);
printf("---\n");
printf("Client from %d.%d.%d.%d\n", ip4_addr1(&client_addr), ip4_addr2(&client_addr), ip4_addr3(&client_addr), ip4_addr4(&client_addr));

if ((err = netconn_recv(client, &nb)) == ERR_OK) {
char *data;
uint16_t len;
err = netbuf_data(nb, (void*) &data, &len);
if (err == ERR_OK) {
printf("Received %d bytes\n", len);
} else {
printf("netbuf_data returned error %d\n", err);
}

sprintf(buffer, "HTTP/1.1 200 OK\nContent-Type: image/jpeg; charset=utf-8\nConnection: close\n\n");

#ifdef CONFIG_TARGET_ESPARDUCAM_MINI
gpio_write(LED_nPWR, 0);
#endif // CONFIG_TARGET_ESPARDUCAM_MINI
if (write(client_sock, buffer, strlen(buffer)) > 0) {
if (camera_ok) {
uint32_t retries = 4;
bool success;
while(retries--) {
success = arducam_capture();
if (success) {
break;
} else {
printf("Capture retry\n");
delay_ms(10); // Arbitrary value
}
}

netconn_write(client, buffer, strlen(buffer), NETCONN_COPY);

if (camera_ok) {
uint32_t retries = 4;
bool success;
while(retries--) {
success = arducam_capture();
if (success) {
printf("Reading fifo\n");
arudcam_fifo_to_socket(client_sock);
printf("---\n");
break;
} else {
printf("Capture retry\n");
delay_ms(10); // Arbitrary value
}
}
if (success) {
printf("Reading fifo\n");
arudcam_fifo_to_netcon(client);
}
}
#ifdef CONFIG_TARGET_ESPARDUCAM_MINI
gpio_write(LED_nPWR, 1);
#endif // CONFIG_TARGET_ESPARDUCAM_MINI

if (recbytes <= 0) {
close(client_sock);
}
}
} while (0);
netbuf_delete(nb);
}
printf("Closing connection\n");
netconn_close(client);
netconn_delete(client);
}
}

Expand Down

0 comments on commit e15a73e

Please sign in to comment.