|
| 1 | +/************************************************** |
| 2 | + * ESPNowCam video Receiver |
| 3 | + * by @hpsaturn Copyright (C) 2024 |
| 4 | + * This file is part ESPNowCam tests project: |
| 5 | + * https://github.com/hpsaturn/ESPNowCam |
| 6 | +**************************************************/ |
| 7 | + |
| 8 | +#include <Arduino.h> |
| 9 | +#include <Wire.h> |
| 10 | +#include <ESPNowCam.h> |
| 11 | +#include "lgfx-esp32c3-crowpanel.h" |
| 12 | +#include <LGFX_TFT_eSPI.hpp> |
| 13 | +#include "Utils.h" |
| 14 | + |
| 15 | +ESPNowCam radio; |
| 16 | +LGFX tft; |
| 17 | + |
| 18 | +#define I2C_SDA 4 |
| 19 | +#define I2C_SCL 5 |
| 20 | +#define TP_INT 0 |
| 21 | +#define TP_RST -1 |
| 22 | +#define LCD_CS 10 |
| 23 | +#define LCD_BLK -1 |
| 24 | +#define PI4IO_I2C_ADDR 0x43 |
| 25 | + |
| 26 | +// frame buffer |
| 27 | +uint8_t *fb; |
| 28 | +// display globals |
| 29 | +int32_t dw, dh; |
| 30 | + |
| 31 | +static uint32_t frame_camera = 0; |
| 32 | +static uint_fast64_t time_stamp_camera = 0; |
| 33 | + |
| 34 | +static void print_FPS(int x, int y, const char *msg, uint32_t &frame, uint_fast64_t &time_stamp, uint32_t len) { |
| 35 | + frame++; |
| 36 | + if (millis() - time_stamp > 1000) { |
| 37 | + time_stamp = millis(); |
| 38 | + char output[40]; |
| 39 | + sprintf(output, "%s%2d FPS JPG: %05d\r\n",msg, frame, len); |
| 40 | + tft.drawString(output, x, y); |
| 41 | + frame = 0; |
| 42 | + Serial.print(output); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void onDataReady(uint32_t lenght) { |
| 47 | + tft.drawJpg(fb, lenght , 0, 0, dw, dh); |
| 48 | + print_FPS(5, 250, "CAM:", frame_camera, time_stamp_camera, lenght); |
| 49 | +} |
| 50 | + |
| 51 | +//Extended IO function |
| 52 | +void init_IO_extender() { |
| 53 | + Wire.beginTransmission(PI4IO_I2C_ADDR); |
| 54 | + Wire.write(0x01); // test register |
| 55 | + Wire.endTransmission(); |
| 56 | + Wire.requestFrom(PI4IO_I2C_ADDR, 1); |
| 57 | + uint8_t rxdata = Wire.read(); |
| 58 | + Serial.print("Device ID: "); |
| 59 | + Serial.println(rxdata, HEX); |
| 60 | + |
| 61 | + Wire.beginTransmission(PI4IO_I2C_ADDR); |
| 62 | + Wire.write(0x03); // IO direction register |
| 63 | + Wire.write((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4)); // set pins 0, 1, 2 as outputs |
| 64 | + Wire.endTransmission(); |
| 65 | + |
| 66 | + Wire.beginTransmission(PI4IO_I2C_ADDR); |
| 67 | + Wire.write(0x07); // Output Hi-Z register |
| 68 | + Wire.write(~((1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4))); // set pins 0, 1, 2 low |
| 69 | + Wire.endTransmission(); |
| 70 | +} |
| 71 | + |
| 72 | +void set_pin_io(uint8_t pin_number, bool value) { |
| 73 | + |
| 74 | + Wire.beginTransmission(PI4IO_I2C_ADDR); |
| 75 | + Wire.write(0x05); // test register |
| 76 | + Wire.endTransmission(); |
| 77 | + Wire.requestFrom(PI4IO_I2C_ADDR, 1); |
| 78 | + uint8_t rxdata = Wire.read(); |
| 79 | + Serial.print("Before the change: "); |
| 80 | + Serial.println(rxdata, HEX); |
| 81 | + |
| 82 | + Wire.beginTransmission(PI4IO_I2C_ADDR); |
| 83 | + Wire.write(0x05); // Output register |
| 84 | + |
| 85 | + if (!value) |
| 86 | + Wire.write((~(1 << pin_number)) & rxdata); // set pin low |
| 87 | + else |
| 88 | + Wire.write((1 << pin_number) | rxdata); // set pin high |
| 89 | + Wire.endTransmission(); |
| 90 | + |
| 91 | + Wire.beginTransmission(PI4IO_I2C_ADDR); |
| 92 | + Wire.write(0x05); // test register |
| 93 | + Wire.endTransmission(); |
| 94 | + Wire.requestFrom(PI4IO_I2C_ADDR, 1); |
| 95 | + rxdata = Wire.read(); |
| 96 | + Serial.print("after the change: "); |
| 97 | + Serial.println(rxdata, HEX); |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +void setup() { |
| 102 | + Serial.begin(115200); |
| 103 | + |
| 104 | + Wire.begin(4, 5); |
| 105 | + init_IO_extender(); |
| 106 | + delay(100); |
| 107 | + set_pin_io(3, true); |
| 108 | + set_pin_io(4, true); |
| 109 | + set_pin_io(2, true); |
| 110 | + |
| 111 | + pinMode(3, OUTPUT); |
| 112 | + digitalWrite(3, LOW); |
| 113 | + |
| 114 | + // ticker.attach(1, tcr1s); |
| 115 | + tft.init(); |
| 116 | + tft.initDMA(); |
| 117 | + tft.startWrite(); |
| 118 | + tft.setColor(0, 0, 0); |
| 119 | + |
| 120 | + tft.fillScreen(TFT_BLACK); |
| 121 | + |
| 122 | + dw = tft.width(); |
| 123 | + dh = tft.height(); |
| 124 | + |
| 125 | + // if(psramFound()){ |
| 126 | + // size_t psram_size = esp_spiram_get_size() / 1048576; |
| 127 | + // Serial.printf("PSRAM size: %dMb\r\n", psram_size); |
| 128 | + // } |
| 129 | + |
| 130 | + // BE CAREFUL WITH IT, IF JPG LEVEL CHANGES, INCREASE IT |
| 131 | + fb = static_cast<uint8_t*>(malloc(10000 * sizeof(uint8_t))); |
| 132 | + |
| 133 | + radio.setRecvBuffer(fb); |
| 134 | + radio.setRecvCallback(onDataReady); |
| 135 | + |
| 136 | + if (radio.init()) { |
| 137 | + tft.setTextSize(2); |
| 138 | + tft.drawString("ESPNow Init Success", 5, 2); |
| 139 | + } |
| 140 | + delay(1000); |
| 141 | +} |
| 142 | + |
| 143 | +void loop() { |
| 144 | +} |
0 commit comments