|
| 1 | +#include "camera.h" |
| 2 | + |
| 3 | +#ifndef ARDUINO_NICLA_VISION |
| 4 | +#error "This sketch only works on Nicla Vision." |
| 5 | +#endif |
| 6 | + |
| 7 | +#include "gc2145.h" |
| 8 | +GC2145 galaxyCore; |
| 9 | +Camera cam(galaxyCore); |
| 10 | +#define IMAGE_MODE CAMERA_RGB565 |
| 11 | + |
| 12 | +#define CHUNK_SIZE 512 // Size of chunks in bytes |
| 13 | +#define RESOLUTION CAMERA_R1600x1200 |
| 14 | +#define ZOOM_WINDOW_RESOLUTION CAMERA_R320x240 |
| 15 | + |
| 16 | +constexpr uint16_t ZOOM_WINDOW_WIDTH = 320; |
| 17 | +constexpr uint16_t ZOOM_WINDOW_HEIGHT = 240; |
| 18 | +constexpr uint16_t ZOOM_X_STEPS = 100; |
| 19 | +constexpr uint16_t ZOOM_Y_STEPS = 100; |
| 20 | + |
| 21 | +FrameBuffer frameBuffer; |
| 22 | +uint32_t currentZoomX = 0; |
| 23 | +uint32_t currentZoomY = 0; |
| 24 | +uint32_t maxZoomX = 0; // Will be calculated in setup() |
| 25 | +uint32_t maxZoomY = 0; // Will be calculated in setup() |
| 26 | + |
| 27 | + |
| 28 | +void blinkLED(uint32_t count = 0xFFFFFFFF) |
| 29 | +{ |
| 30 | + pinMode(LED_BUILTIN, OUTPUT); |
| 31 | + |
| 32 | + while (count--) { |
| 33 | + digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level) |
| 34 | + delay(50); // wait for a second |
| 35 | + digitalWrite(LED_BUILTIN, HIGH); // turn the LED off by making the voltage LOW |
| 36 | + delay(50); // wait for a second |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +void setup() { |
| 41 | + // Init the cam QVGA, 30FPS |
| 42 | + if (!cam.begin(RESOLUTION, IMAGE_MODE, 30)) { |
| 43 | + blinkLED(); |
| 44 | + } |
| 45 | + |
| 46 | + blinkLED(5); |
| 47 | + |
| 48 | + pinMode(LEDB, OUTPUT); |
| 49 | + digitalWrite(LEDB, HIGH); |
| 50 | + |
| 51 | + // Flips the image vertically |
| 52 | + cam.setVerticalFlip(true); |
| 53 | + |
| 54 | + // Mirrors the image horizontally |
| 55 | + cam.setHorizontalMirror(true); |
| 56 | + |
| 57 | + // Calculate the max zoom window position |
| 58 | + maxZoomX = cam.getResolutionWidth() - ZOOM_WINDOW_WIDTH; |
| 59 | + maxZoomY = cam.getResolutionHeight() - ZOOM_WINDOW_HEIGHT; |
| 60 | + |
| 61 | + // Set the zoom window to 0,0 |
| 62 | + cam.zoomTo(ZOOM_WINDOW_RESOLUTION, currentZoomX, currentZoomY); |
| 63 | +} |
| 64 | + |
| 65 | +void sendFrame(){ |
| 66 | + // Grab frame and write to serial |
| 67 | + if (cam.grabFrame(frameBuffer, 3000) == 0) { |
| 68 | + byte* buffer = frameBuffer.getBuffer(); |
| 69 | + size_t bufferSize = cam.frameSize(); |
| 70 | + digitalWrite(LEDB, LOW); |
| 71 | + |
| 72 | + // Split buffer into chunks |
| 73 | + for(size_t i = 0; i < bufferSize; i += CHUNK_SIZE) { |
| 74 | + size_t chunkSize = min(bufferSize - i, CHUNK_SIZE); |
| 75 | + Serial.write(buffer + i, chunkSize); |
| 76 | + Serial.flush(); |
| 77 | + delay(1); // Small delay to allow the receiver to process the data |
| 78 | + } |
| 79 | + |
| 80 | + digitalWrite(LEDB, HIGH); |
| 81 | + } else { |
| 82 | + blinkLED(20); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void loop() { |
| 87 | + if(!Serial) { |
| 88 | + Serial.begin(115200); |
| 89 | + while(!Serial); |
| 90 | + } |
| 91 | + |
| 92 | + if(!Serial.available()) return; |
| 93 | + byte request = Serial.read(); |
| 94 | + |
| 95 | + if(request == 1){ |
| 96 | + sendFrame(); |
| 97 | + currentZoomX += ZOOM_X_STEPS; |
| 98 | + |
| 99 | + if(currentZoomX > maxZoomX){ |
| 100 | + currentZoomX = 0; |
| 101 | + currentZoomY += ZOOM_Y_STEPS; |
| 102 | + if(currentZoomY > maxZoomY){ |
| 103 | + currentZoomY = 0; |
| 104 | + } |
| 105 | + } |
| 106 | + cam.zoomTo(ZOOM_WINDOW_RESOLUTION, currentZoomX, currentZoomY); |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments