diff --git a/c328.c b/c328.c index 3c0b9d0..9c2e3f8 100644 --- a/c328.c +++ b/c328.c @@ -11,6 +11,7 @@ #include "config.h" #include +#include #include #include "c328.h" @@ -216,3 +217,85 @@ char c3_finish_picture(void) return(0); } +/****************** Simple interface ******************/ + +static uint16_t image_len; +static uint16_t image_read; + +static uint8_t *package; +static uint16_t package_len; +static uint16_t package_id; + +char c3_open(uint8_t jr) +{ + /* Open, setup and take the image */ + if(c3_sync() != 0) return(-1); + if(c3_setup(CT_JPEG, 0, jr) != 0) return(-2); + if(c3_set_package_size(RXBUF_LEN) != 0) return(-3); + if(c3_snapshot(ST_JPEG, 0) != 0) return(-4); + if(c3_get_picture(PT_SNAPSHOT, &image_len) != 0) return(-5); + + image_read = 0; + package = NULL; + package_len = 0; + package_id = 0; + + return(0); +} + +char c3_close(void) +{ + c3_finish_picture(); + return(0); +} + +uint16_t c3_read(uint8_t *ptr, uint16_t length) +{ + uint16_t r; + + /* Don't read past the end of the image */ + r = image_len - image_read; + if(length > r) length = r; + + r = length; + while(r) + { + if(package_len == 0) + { + char i = c3_get_package(package_id++, &package, &package_len); + if(i != 0) return(length - r); + + /* Skip the package headers and checksum */ + package += 4; + package_len -= 6; + } + else + { + uint16_t b = r; + if(b > package_len) b = package_len; + + memcpy(ptr, package, b); + + package += b; + package_len -= b; + + ptr += b; + r -= b; + + image_read += b; + } + } + + return(length); +} + +uint16_t c3_filesize(void) +{ + return(image_len); +} + +char c3_eof(void) +{ + return(image_read >= image_len); +} + diff --git a/c328.h b/c328.h index 5a36d10..cb8b6b7 100644 --- a/c328.h +++ b/c328.h @@ -100,4 +100,10 @@ extern char c3_get_picture(uint8_t pt, uint16_t *length); extern char c3_get_package(uint16_t id, uint8_t **dst, uint16_t *length); extern char c3_finish_picture(void); +extern char c3_open(uint8_t jr); +extern char c3_close(void); +extern uint16_t c3_read(uint8_t *ptr, uint16_t length); +extern uint16_t c3_filesize(void); +extern char c3_eof(void); + #endif diff --git a/hadie.c b/hadie.c index 82ba185..5bd975d 100644 --- a/hadie.c +++ b/hadie.c @@ -30,8 +30,6 @@ char msg[MSG_SIZE]; #define PKT_SIZE_RSCODES (0x20) #define PKT_SIZE_PAYLOAD (PKT_SIZE - PKT_SIZE_HEADER - PKT_SIZE_RSCODES) uint8_t pkt[PKT_SIZE]; -uint16_t pkt_len; -uint16_t image_len; uint8_t image_pkts; void init_packet(uint8_t *packet, uint8_t imageid, uint8_t pktid, uint8_t pkts, uint16_t width, uint16_t height) @@ -49,129 +47,41 @@ void init_packet(uint8_t *packet, uint8_t imageid, uint8_t pktid, uint8_t pkts, memset(&packet[PKT_SIZE_HEADER], 0, PKT_SIZE_PAYLOAD); } -char setup_camera(void) -{ - if(c3_sync() != 0) - { - rtx_string_P(PSTR(PREFIX CALLSIGN ":Camera sync failed...\n")); - return(-1); - } - - /* Setup the camera */ - if(c3_setup(CT_JPEG, 0, SR_320x240) != 0) - { - rtx_string_P(PSTR(PREFIX CALLSIGN ":Camera setup failed...\n")); - return(-1); - } - - /* Set the package size */ - if(c3_set_package_size(PKT_SIZE_PAYLOAD + 6) != 0) - { - rtx_string_P(PSTR(PREFIX CALLSIGN ":Package size set failed!\n")); - return(-1); - } - - /* Take the image */ - if(c3_snapshot(ST_JPEG, 0) != 0) - { - rtx_string_P(PSTR(PREFIX CALLSIGN ":Snapshot failed!\n")); - return(-1); - } - - /* Get the image size and begin the transfer */ - if(c3_get_picture(PT_SNAPSHOT, &image_len) != 0) - { - rtx_string_P(PSTR(PREFIX CALLSIGN ":Get picture failed\n")); - return(-1); - } - - /* Calculate the number of packets needed for this image */ - image_pkts = image_len / PKT_SIZE_PAYLOAD; - image_pkts += (image_len % PKT_SIZE_PAYLOAD > 0 ? 1 : 0); - - /* Camera is ready to transfer the image data */ - - return(0); -} - char tx_image(void) { static char setup = 0; - - static uint16_t pkg_id; - static uint8_t *pkg; - static uint16_t pkg_len; - static uint8_t img_id = 0; - static uint16_t img_tx; static uint8_t pkt_id; if(!setup) { - if(setup_camera() != 0) return(setup); - setup = -1; + uint16_t image_len; + if(c3_open(SR_320x240) != 0) return(setup); + setup = -1; pkt_id = 0; - pkg_len = 0; - pkg_id = 0; - img_tx = 0; img_id++; + + /* Calculate the number of packets needed for this image */ + image_len = c3_filesize(); + image_pkts = image_len / PKT_SIZE_PAYLOAD; + image_pkts += (image_len % PKT_SIZE_PAYLOAD > 0 ? 1 : 0); } - /* Initialise the packet -- make sure previous packet has finished TX'ing! */ + /* Initialise the packet */ init_packet(pkt, img_id, pkt_id++, image_pkts, 320, 240); - pkt_len = 0; - while(pkt_len < PKT_SIZE_PAYLOAD) + c3_read(&pkt[PKT_SIZE_HEADER], PKT_SIZE_PAYLOAD); + + if(c3_eof()) { - if(pkg_len == 0) - { - char msg[100]; - char i; - if((i = c3_get_package(pkg_id++, &pkg, &pkg_len)) != 0) - { - snprintf(msg, 100, PREFIX CALLSIGN ",Get package %i failed (%i)\n", pkg_id - 1, i); - rtx_string(msg); - rtx_wait(); - - setup = 0; - return(setup); - } - - /* Skip the package header */ - pkg += 4; - pkg_len -= 6; - } - - if(pkg_len > 0) - { - uint16_t l = PKT_SIZE_PAYLOAD - pkt_len; - if(pkg_len < l) l = pkg_len; - - /* TODO: Copy with the JPEG filter */ - memcpy(pkt + PKT_SIZE_HEADER + pkt_len, pkg, l); - - pkg += l; - pkg_len -= l; - pkt_len += l; - img_tx += l; - } - - /* Have we reached the end of the image? */ - if(img_tx >= image_len) - { - c3_finish_picture(); - setup = 0; - break; - } + c3_close(); + setup = 0; } encode_rs_8(&pkt[1], &pkt[PKT_SIZE_HEADER + PKT_SIZE_PAYLOAD], 0); rtx_string_P(PSTR("UUU")); /* U = 0x55 */ rtx_data(pkt, PKT_SIZE); - //rtx_wait(); - - //c3_ping(); return(setup); }