Advertisement
imonbayazid

JPEGCamera.cpp

Sep 4th, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. /* Arduino JPEGCamera Library
  2. * Copyright 2010 SparkFun Electronic
  3. * Written by Ryan Owens
  4. * Modified by arms22
  5. * Ported to mbed by yamaguch
  6. */
  7.  
  8. #include "JPEGCamera.h"
  9.  
  10. #define min(x, y) ((x) < (y)) ? (x) : (y)
  11.  
  12.  
  13. const int RESPONSE_TIMEOUT = 500;
  14. const int DATA_TIMEOUT = 1500;
  15.  
  16. Serial bt(p13, p14);
  17.  
  18. JPEGCamera::JPEGCamera(PinName tx, PinName rx) : Serial(tx, rx) {
  19. baud(115200);
  20. bt.baud(115200);
  21. state = READY;
  22. }
  23.  
  24. bool JPEGCamera::setPictureSize(JPEGCamera::PictureSize size, bool doReset) {
  25. char buf[5] = {0x56, 0x00, 0x54, 0x01, (char) size};
  26. int ret = sendReceive(buf, sizeof buf, 5);
  27.  
  28. if (ret == 5 && buf[0] == 0x76) {
  29. if (doReset)
  30. reset();
  31. return true;
  32. } else
  33. return false;
  34. }
  35.  
  36. bool JPEGCamera::isReady() {
  37. return state == READY;
  38. }
  39.  
  40. bool JPEGCamera::isProcessing() {
  41. return state == PROCESSING;
  42. }
  43.  
  44. bool JPEGCamera::takePicture(char *filename) {
  45. if (state == READY) {
  46. fp = fopen(filename, "wb");
  47. if (fp != 0) {
  48. if (takePicture()) {
  49. imageSize = getImageSize();
  50. address = 0;
  51. state = PROCESSING;
  52. } else {
  53. fclose(fp);
  54. printf("takePicture(%s) failed", filename);
  55. state = ERROR;
  56. }
  57. } else {
  58. printf("fopen() failed");
  59. state = ERROR;
  60. }
  61. }
  62. return state != ERROR;
  63. }
  64.  
  65. bool JPEGCamera::processPicture() {
  66. if (state == PROCESSING) {
  67. bt.printf("\n\nNew Image \n\n");
  68. if (address < imageSize) {
  69. char data[2048];
  70. int size = readData(data, min(sizeof(data), imageSize - address), address);
  71. int ret = fwrite(data, size, 1, fp);
  72. for (int i=0; i<size; i++) bt.printf("%x ",data[i]);
  73. if (ret > 0)
  74. address += size;
  75. if (ret == 0 || address >= imageSize) {
  76. stopPictures();
  77. fclose(fp);
  78. wait(0.1); // ????
  79. state = ret > 0 ? READY : ERROR;
  80. }
  81. }
  82. }
  83.  
  84. return state == PROCESSING || state == READY;
  85. }
  86.  
  87. bool JPEGCamera::reset() {
  88. char buf[4] = {0x56, 0x00, 0x26, 0x00};
  89. int ret = sendReceive(buf, sizeof buf, 12);
  90. if (ret == 12 && buf[0] == 0x0D) {
  91. wait(4.0);
  92. state = READY;
  93. } else {
  94. state = ERROR;
  95. }
  96. return state == READY;
  97. }
  98.  
  99. bool JPEGCamera::takePicture() {
  100. char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x00};
  101. int ret = sendReceive(buf, sizeof buf, 5);
  102.  
  103. return ret == 5 && buf[0] == 0x76;
  104. }
  105.  
  106. bool JPEGCamera::stopPictures() {
  107. char buf[5] = {0x56, 0x00, 0x36, 0x01, 0x03};
  108. int ret = sendReceive(buf, sizeof buf, 5);
  109.  
  110. return ret == 4 && buf[0] == 0x76;
  111. }
  112.  
  113. int JPEGCamera::getImageSize() {
  114. char buf[9] = {0x56, 0x00, 0x34, 0x01, 0x00};
  115. int ret = sendReceive(buf, sizeof buf, 9);
  116.  
  117. //The size is in the last 2 characters of the response.
  118. return (ret == 9 && buf[0] == 0x76) ? (buf[7] << 8 | buf[8]) : 0;
  119. }
  120.  
  121. int JPEGCamera::readData(char *dataBuf, int size, int address) {
  122. char buf[16] = {0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00,
  123. address >> 8, address & 255, 0x00, 0x00, size >> 8, size & 255, 0x00, 0x0A
  124. };
  125. int ret = sendReceive(buf, sizeof buf, 5);
  126.  
  127. return (ret == 5 && buf[0] == 0x76) ? receive(dataBuf, size, DATA_TIMEOUT) : 0;
  128. }
  129.  
  130. int JPEGCamera::sendReceive(char *buf, int sendSize, int receiveSize) {
  131. while (readable()) getc();
  132.  
  133. for (int i = 0; i < sendSize; i++) putc(buf[i]);
  134.  
  135. return receive(buf, receiveSize, RESPONSE_TIMEOUT);
  136. }
  137.  
  138. int JPEGCamera::receive(char *buf, int size, int timeout) {
  139. timer.start();
  140. timer.reset();
  141.  
  142. int i = 0;
  143. while (i < size && timer.read_ms() < timeout) {
  144. if (readable())
  145. buf[i++] = getc();
  146. }
  147.  
  148. return i;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement