From fbaa6aeecb2ce02f9bae6eee0ce3c7a0346c8157 Mon Sep 17 00:00:00 2001 From: Jim Bennett Date: Sat, 5 Jun 2021 17:08:00 -0700 Subject: [PATCH] Adding wio terminal code --- .../fruit-quality-detector/.gitignore | 5 + .../.vscode/extensions.json | 7 + .../fruit-quality-detector/include/README | 39 +++++ .../fruit-quality-detector/lib/README | 46 +++++ .../fruit-quality-detector/platformio.ini | 24 +++ .../fruit-quality-detector/src/camera.h | 160 ++++++++++++++++++ .../fruit-quality-detector/src/config.h | 9 + .../fruit-quality-detector/src/main.cpp | 112 ++++++++++++ .../fruit-quality-detector/test/README | 11 ++ .../wio-terminal-camera.md | 2 +- 10 files changed, 414 insertions(+), 1 deletion(-) create mode 100644 4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/.gitignore create mode 100644 4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/.vscode/extensions.json create mode 100644 4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/include/README create mode 100644 4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/lib/README create mode 100644 4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/platformio.ini create mode 100644 4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/camera.h create mode 100644 4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/config.h create mode 100644 4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/main.cpp create mode 100644 4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/test/README diff --git a/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/.gitignore b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/.gitignore new file mode 100644 index 00000000..89cc49cb --- /dev/null +++ b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/.vscode/extensions.json b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/.vscode/extensions.json new file mode 100644 index 00000000..0f0d7401 --- /dev/null +++ b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/include/README b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/include/README new file mode 100644 index 00000000..194dcd43 --- /dev/null +++ b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/lib/README b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/lib/README new file mode 100644 index 00000000..6debab1e --- /dev/null +++ b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/platformio.ini b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/platformio.ini new file mode 100644 index 00000000..57efb3ca --- /dev/null +++ b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/platformio.ini @@ -0,0 +1,24 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:seeed_wio_terminal] +platform = atmelsam +board = seeed_wio_terminal +framework = arduino +lib_deps = + seeed-studio/Seeed Arduino rpcWiFi @ 1.0.3 + seeed-studio/Seeed Arduino FS @ 2.0.2 + seeed-studio/Seeed Arduino SFUD @ 2.0.1 + seeed-studio/Seeed Arduino rpcUnified @ 2.1.3 + seeed-studio/Seeed_Arduino_mbedtls @ 3.0.1 + seeed-studio/Seeed Arduino RTC @ 2.0.0 +build_flags = + -DARDUCAM_SHIELD_V2 + -DOV2640_CAM \ No newline at end of file diff --git a/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/camera.h b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/camera.h new file mode 100644 index 00000000..2028039f --- /dev/null +++ b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/camera.h @@ -0,0 +1,160 @@ +#pragma once + +#include +#include + +class Camera +{ +public: + Camera(int format, int image_size) : _arducam(OV2640, PIN_SPI_SS) + { + _format = format; + _image_size = image_size; + } + + bool init() + { + // Reset the CPLD + _arducam.write_reg(0x07, 0x80); + delay(100); + + _arducam.write_reg(0x07, 0x00); + delay(100); + + // Check if the ArduCAM SPI bus is OK + _arducam.write_reg(ARDUCHIP_TEST1, 0x55); + if (_arducam.read_reg(ARDUCHIP_TEST1) != 0x55) + { + return false; + } + + // Change MCU mode + _arducam.set_mode(MCU2LCD_MODE); + + uint8_t vid, pid; + + // Check if the camera module type is OV2640 + _arducam.wrSensorReg8_8(0xff, 0x01); + _arducam.rdSensorReg8_8(OV2640_CHIPID_HIGH, &vid); + _arducam.rdSensorReg8_8(OV2640_CHIPID_LOW, &pid); + if ((vid != 0x26) && ((pid != 0x41) || (pid != 0x42))) + { + return false; + } + + _arducam.set_format(_format); + _arducam.InitCAM(); + _arducam.OV2640_set_JPEG_size(_image_size); + _arducam.OV2640_set_Light_Mode(Auto); + _arducam.OV2640_set_Special_effects(Normal); + delay(1000); + + return true; + } + + void startCapture() + { + _arducam.flush_fifo(); + _arducam.clear_fifo_flag(); + _arducam.start_capture(); + } + + bool captureReady() + { + return _arducam.get_bit(ARDUCHIP_TRIG, CAP_DONE_MASK); + } + + bool readImageToBuffer(byte **buffer, uint32_t &buffer_length) + { + if (!captureReady()) return false; + + // Get the image file length + uint32_t length = _arducam.read_fifo_length(); + buffer_length = length; + + if (length >= MAX_FIFO_SIZE) + { + return false; + } + if (length == 0) + { + return false; + } + + // create the buffer + byte *buf = new byte[length]; + + uint8_t temp = 0, temp_last = 0; + int i = 0; + uint32_t buffer_pos = 0; + bool is_header = false; + + _arducam.CS_LOW(); + _arducam.set_fifo_burst(); + + while (length--) + { + temp_last = temp; + temp = SPI.transfer(0x00); + //Read JPEG data from FIFO + if ((temp == 0xD9) && (temp_last == 0xFF)) //If find the end ,break while, + { + buf[buffer_pos] = temp; + + buffer_pos++; + i++; + + _arducam.CS_HIGH(); + } + if (is_header == true) + { + //Write image data to buffer if not full + if (i < 256) + { + buf[buffer_pos] = temp; + buffer_pos++; + i++; + } + else + { + _arducam.CS_HIGH(); + + i = 0; + buf[buffer_pos] = temp; + + buffer_pos++; + i++; + + _arducam.CS_LOW(); + _arducam.set_fifo_burst(); + } + } + else if ((temp == 0xD8) & (temp_last == 0xFF)) + { + is_header = true; + + buf[buffer_pos] = temp_last; + buffer_pos++; + i++; + + buf[buffer_pos] = temp; + buffer_pos++; + i++; + } + } + + _arducam.clear_fifo_flag(); + + _arducam.set_format(_format); + _arducam.InitCAM(); + _arducam.OV2640_set_JPEG_size(_image_size); + + // return the buffer + *buffer = buf; + } + +private: + ArduCAM _arducam; + int _format; + int _image_size; +}; diff --git a/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/config.h b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/config.h new file mode 100644 index 00000000..3eec3910 --- /dev/null +++ b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/config.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +using namespace std; + +// WiFi credentials +const char *SSID = ""; +const char *PASSWORD = ""; diff --git a/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/main.cpp b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/main.cpp new file mode 100644 index 00000000..14ae8f51 --- /dev/null +++ b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/src/main.cpp @@ -0,0 +1,112 @@ +#include +#include +#include "SD/Seeed_SD.h" +#include +#include + +#include "config.h" +#include "camera.h" + +Camera camera = Camera(JPEG, OV2640_640x480); + +void setupCamera() +{ + pinMode(PIN_SPI_SS, OUTPUT); + digitalWrite(PIN_SPI_SS, HIGH); + + Wire.begin(); + SPI.begin(); + + if (!camera.init()) + { + Serial.println("Error setting up the camera!"); + } +} + +void connectWiFi() +{ + while (WiFi.status() != WL_CONNECTED) + { + Serial.println("Connecting to WiFi.."); + WiFi.begin(SSID, PASSWORD); + delay(500); + } + + Serial.println("Connected!"); +} + +void setupSDCard() +{ + while (!SD.begin(SDCARD_SS_PIN, SDCARD_SPI)) + { + Serial.println("SD Card Error"); + } +} + +void setup() +{ + Serial.begin(9600); + + while (!Serial) + ; // Wait for Serial to be ready + + delay(1000); + + connectWiFi(); + + setupCamera(); + + pinMode(WIO_KEY_C, INPUT_PULLUP); + + setupSDCard(); +} + +int fileNum = 1; + +void saveToSDCard(byte *buffer, uint32_t length) +{ + char buff[16]; + sprintf(buff, "%d.jpg", fileNum); + fileNum++; + + File outFile = SD.open(buff, FILE_WRITE ); + outFile.write(buffer, length); + outFile.close(); + + Serial.print("Image written to file "); + Serial.println(buff); +} + +void buttonPressed() +{ + camera.startCapture(); + + while (!camera.captureReady()) + delay(100); + + Serial.println("Image captured"); + + byte *buffer; + uint32_t length; + + if (camera.readImageToBuffer(&buffer, length)) + { + Serial.print("Image read to buffer with length "); + Serial.println(length); + + saveToSDCard(buffer, length); + + delete(buffer); + } +} + +void loop() +{ + if (digitalRead(WIO_KEY_C) == LOW) + { + buttonPressed(); + delay(2000); + } + + delay(200); +} \ No newline at end of file diff --git a/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/test/README b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/test/README new file mode 100644 index 00000000..b94d0890 --- /dev/null +++ b/4-manufacturing/lessons/2-check-fruit-from-device/code-camera/wio-terminal/fruit-quality-detector/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html diff --git a/4-manufacturing/lessons/2-check-fruit-from-device/wio-terminal-camera.md b/4-manufacturing/lessons/2-check-fruit-from-device/wio-terminal-camera.md index 23e3cc67..703c0eb0 100644 --- a/4-manufacturing/lessons/2-check-fruit-from-device/wio-terminal-camera.md +++ b/4-manufacturing/lessons/2-check-fruit-from-device/wio-terminal-camera.md @@ -455,4 +455,4 @@ The Wio Terminal only supports microSD cards of up to 16GB in size. If you have ![A picture of a banana captured using the ArduCam](../../../images/banana-arducam.jpg) - > 💁 It may take a few images for the white balance of the camera to adjust itself. You will notice this based on the color of the images captured, the first few may look off color. + > 💁 It may take a few images for the white balance of the camera to adjust itself. You will notice this based on the color of the images captured, the first few may look off color. You can always work around this by changing the code to capture a few images that are ignored during the setup.