Merge pull request #429 from adafruit/develop

Added image eink upload
This commit is contained in:
Ha Thach authored and GitHub committed 2020-01-31 17:15:34 +07:00
commit cc69ecc95d
7 files changed
+426 -37

No files matched your search

+3 -1
View File
@@ -51,7 +51,9 @@ jobs:
BSP_VERSION=`eval ls $HOME/$BSP_PATH`
rm -r $HOME/$BSP_PATH/*
ln -s $GITHUB_WORKSPACE $HOME/$BSP_PATH/$BSP_VERSION
arduino-cli lib install "Adafruit NeoPixel" "Adafruit NeoMatrix" "Adafruit GFX Library" "Adafruit SSD1306" "MIDI Library" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit Circuit Playground" "Firmata"
# Install library dependency
arduino-cli lib install "Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD"
arduino-cli lib install "Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix" "MIDI Library" "Firmata"
- name: Build examples
run: python3 tools/build_all.py ${{ matrix.arduino-platform }}
+10
View File
@@ -39,6 +39,16 @@ void SoftwareTimer::begin(uint32_t ms, TimerCallbackFunction_t callback, void* t
_handle = xTimerCreate(NULL, ms2tick(ms), repeating, timerID, callback);
}
void SoftwareTimer::setID(void* id)
{
vTimerSetTimerID(_handle, id);
}
void* SoftwareTimer::getID(void)
{
return pvTimerGetTimerID(_handle);
}
bool SoftwareTimer::start(void)
{
bool ret = true;
+3
View File
@@ -50,6 +50,9 @@ class SoftwareTimer
void begin(uint32_t ms, TimerCallbackFunction_t callback, void* timerID = NULL, bool repeating = true);
TimerHandle_t getHandle(void) { return _handle; }
void setID(void* id);
void* getID(void);
bool start(void);
bool stop (void);
bool reset (void);
@@ -0,0 +1,336 @@
/*********************************************************************
This is an example for our nRF52 based Bluefruit LE modules
Pick one up today in the adafruit shop!
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <bluefruit.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include "Adafruit_EPD.h"
/* This sketch demonstrates the "Image Upload" feature of Bluefruit Mobile App.
* Following TFT Display are supported
* - https://www.adafruit.com/product/4428
*/
#define EINK_GIZMO 1
// one of above supported TFT add-on
#define DISPLAY_IN_USE EINK_GIZMO
#define EPD_CS 0
#define EPD_DC 1
#define SRAM_CS -1
#define EPD_RESET PIN_A3 // can set to -1 and share with microcontroller Reset!
#define EPD_BUSY -1 // can set to -1 to not use a pin (will wait a fixed delay)
Adafruit_IL0373 display(152, 152, EPD_DC, EPD_RESET, EPD_CS, SRAM_CS, EPD_BUSY);
// Declaring Uart over BLE with large buffer to hold image data
// Depending on the Image Resolution and Transfer Mode especially without response
// or Interleaved with high ratio. You may need to increase this buffer size
BLEUart bleuart(10*1024);
/* The Image Transfer module sends the image of your choice to Bluefruit LE over UART.
* Each image sent begins with
* - A single byte char '!' (0x21) followed by 'I' helper for image
* - Color depth: 24-bit for RGB 888, 16-bit for RGB 565
* - Image width (uint16 little endian, 2 bytes)
* - Image height (uint16 little endian, 2 bytes)
* - Pixel data encoded as RGB 16/24 bit and suffixed by a single byte CRC.
*
* Format: [ '!' ] [ 'I' ] [uint8_t color bit] [ uint16 width ] [ uint16 height ] [ r g b ] [ r g b ] [ r g b ] … [ CRC ]
*/
uint16_t imageWidth = 0;
uint16_t imageHeight = 0;
uint8_t imageColorBit = 0;
uint32_t totalPixel = 0; // received pixel
// pixel line buffer, should be large enough to hold an image width
uint16_t pixel_buf[512];
// Use software timer to schedule bandwidth negotiation a few seconds after connection
SoftwareTimer negoTimer;
// Statistics for speed testing
uint32_t rxStartTime = 0;
uint32_t rxLastTime = 0;
void setup()
{
Serial.begin(115200);
display.begin();
display.clearBuffer();
#if DISPLAY_IN_USE == EINK_GIZMO
display.setRotation(3);
#else
// todo more eink displays
#endif
// Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice
// Note: All config***() function must be called before begin()
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.begin();
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
Bluefruit.setName("Bluefruit52");
Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms
// Configure and Start BLE Uart Service
bleuart.begin();
// Due to huge amount of image data
// NRF52832 doesn't have enough SRAM to queue up received packets using deferred callbacks.
// Therefore it must process data as soon as it comes, this can be done by
// changing the default "deferred" option to false to invoke callback immediately.
// However, the transfer speed will be affected since immediate callback will block BLE task
// to process data especially when tft.drawRGBBitmap() is calling.
#ifdef NRF52840_XXAA
// 2nd argument is true to deferred callbacks i.e queue it up in separated callback Task
bleuart.setRxCallback(bleuart_rx_callback, true);
#else
// 2nd argument is false to invoke callbacks immediately (thus blocking other ble events)
bleuart.setRxCallback(bleuart_rx_callback, false);
#endif
bleuart.setRxOverflowCallback(bleuart_overflow_callback);
// one-shot (non-repeating) 2 seconds timer
negoTimer.begin(2000, negotiate_bandwidth, NULL, false);
// Set up and start advertising
startAdv();
Serial.println("Advertising ... ");
}
void startAdv(void)
{
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_GENERIC_CLOCK);
// Include bleuart 128-bit uuid
Bluefruit.Advertising.addService(bleuart);
// There is no room for Name in Advertising packet
// Use Scan response for Name
Bluefruit.ScanResponse.addName();
/* Start Advertising
* - Enable auto advertising if disconnected
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
* - Timeout for fast mode is 30 seconds
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
*
* For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
void loop()
{
// nothing to do
}
// Invoked when receiving data from bleuart
// Pull data from bleuart fifo & draw image as soon as possible,
// Otherwise bleuart fifo can be overflowed
void bleuart_rx_callback(uint16_t conn_hdl)
{
(void) conn_hdl;
rxLastTime = millis();
// Received new Image
if ( (imageWidth == 0) && (imageHeight == 0) )
{
// take note of time of first packet
rxStartTime = millis();
// Skip all data until '!I' is found
while( bleuart.available() && bleuart.read() != '!' ) { }
if (bleuart.read() != 'I') return;
if ( !bleuart.available() ) return;
imageColorBit = bleuart.read8();
imageWidth = bleuart.read16();
imageHeight = bleuart.read16();
totalPixel = 0;
display.clearBuffer();
display.fillScreen(EPD_WHITE);
display.setCursor(0, 0);
// Print out the current connection info
BLEConnection* conn = Bluefruit.Connection(conn_hdl);
Serial.printf("Connection Info: PHY = %d Mbps, Conn Interval = %.2f ms, Data Length = %d, MTU = %d\n",
conn->getPHY(), conn->getConnectionInterval()*1.25f, conn->getDataLength(), conn->getMtu());
Serial.printf("Receving an %dx%d Image with %d bit color\n", imageWidth, imageHeight, imageColorBit);
}
// Extract pixel data to buffer and draw image line by line
while ( bleuart.available() >= 3 )
{
uint8_t red, green, blue;
if ( imageColorBit == 24 )
{
// Application send 24-bit color
red = bleuart.read();
green = bleuart.read();
blue = bleuart.read();
}
else if ( imageColorBit == 16 )
{
// Application send 16-bit 565 color
uint16_t c565 = bleuart.read16();
red = (c565 & 0xf800) >> 8;
green = (c565 & 0x07e0) >> 3;
blue = (c565 & 0x001f) << 3;
}
// Convert RGB into Eink Color
uint8_t c = 0;
if ((red < 0x80) && (green < 0x80) && (blue < 0x80)) {
c = EPD_BLACK; // try to infer black
} else if ((red >= 0x80) && (green >= 0x80) && (blue >= 0x80)) {
c = EPD_WHITE;
} else if (red >= 0x80) {
c = EPD_RED; // try to infer red color
}
// store to pixel buffer
pixel_buf[totalPixel % imageWidth] = c;
totalPixel++;
// have enough to draw an image line
if ( (totalPixel % imageWidth) == 0 )
{
display.drawRGBBitmap(0, totalPixel/imageWidth, pixel_buf, imageWidth, 1);
}
}
// all pixel data is received
if ( totalPixel == imageWidth*imageHeight )
{
uint8_t crc = bleuart.read();
(void) crc;
// do checksum later
// print speed summary
print_summary(totalPixel*(imageColorBit/8) + 8, rxLastTime-rxStartTime);
// Display on Eink, will probably take dozens of seconds
Serial.println("Displaying image (~20 seconds) .....");
display.display();
// reset and waiting for new image
imageColorBit = 0;
imageWidth = imageHeight = 0;
totalPixel = 0;
Serial.println("Ready to receive new image");
}
}
void negotiate_bandwidth(TimerHandle_t xTimer)
{
(void) xTimer;
uint16_t conn_hdl = (uint16_t) ((uint32_t) negoTimer.getID());
BLEConnection* conn = Bluefruit.Connection(conn_hdl);
// Switching from 1 Mb to 2 Mb PHY if needed
if ( conn->connected() )
{
// Requesting to Switching to 2MB PHY, larger data length and bigger MTU
// will increase the throughput on supported central. This should already
// be done with latest Bluefruit app.
//
// However, some Android devices require 2Mb PHY switching must be initiated
// from nRF side
if ( conn->getPHY() == BLE_GAP_PHY_1MBPS )
{
Serial.println("Requesting PHY change from 1 Mb to 2Mb");
conn->requestPHY();
// Data Length and MTU should already be done by Bluefruit app
// conn->requestDataLengthUpdate();
// conn->requestMtuExchange(247);
}
}
}
void connect_callback(uint16_t conn_handle)
{
// Set connection handle as timer ID
// Then schedule negotiation after a few seconds, we should not
// negotiate here since it increases chance to conflict with other
// on-going negotiation from central after connection
negoTimer.setID((void*) conn_handle);
negoTimer.start();
Serial.println("Connected");
Serial.println("Ready to receive new image");
}
void print_summary(uint32_t count, uint32_t ms)
{
float sec = ms / 1000.0F;
Serial.printf("Received %d bytes in %.2f seconds\n", count, sec);
Serial.printf("Speed: %.2f KB/s\n\n", (count / 1024.0F) / sec);
}
void bleuart_overflow_callback(uint16_t conn_hdl, uint16_t leftover)
{
(void) conn_hdl;
(void) leftover;
Serial.println("BLEUART rx buffer OVERFLOWED!");
Serial.println("Please increase buffer size for bleuart");
}
/**
* invoked when a connection is dropped
* @param conn_handle connection where this event happens
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
*/
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
imageColorBit = 0;
imageWidth = imageHeight = 0;
totalPixel = 0;
bleuart.flush();
}
@@ -12,8 +12,12 @@
any redistribution
*********************************************************************/
#include <bluefruit.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
/* This sketch demonstrates the "Image Upload" feature of Bluefruit Mobile App.
* Either 2.4" or 3.5" TFT FeatherWing is used to display uploaded image
* Following TFT Display are supported
* - https://www.adafruit.com/product/3315
* - https://www.adafruit.com/product/3651
* - https://www.adafruit.com/product/4367
@@ -21,14 +25,11 @@
#define TFT_35_FEATHERWING 1
#define TFT_24_FEATHERWING 2
#define TFT_15_GIZMO 3
#define TFT_GIZMO 3
// one of above supported TFT add-on
// [Configurable] Please select one of above supported Display to match your hardware setup
#define TFT_IN_USE TFT_35_FEATHERWING
#include <bluefruit.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#if defined(ARDUINO_NRF52832_FEATHER)
// Feather nRF52832
@@ -56,7 +57,7 @@
#include <Adafruit_ILI9341.h>
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#elif TFT_IN_USE == TFT_15_GIZMO
#elif TFT_IN_USE == TFT_GIZMO
#include "Adafruit_ST7789.h"
Adafruit_ST7789 tft = Adafruit_ST7789(&SPI, TFT_CS, TFT_DC, -1);
@@ -78,10 +79,10 @@ BLEUart bleuart(10*1024);
/* The Image Transfer module sends the image of your choice to Bluefruit LE over UART.
* Each image sent begins with
* - A single byte char '!' (0x21) followed by 'I' helper for image
* - Color depth: 24-bit for RGB 888, 16-bit for RGB 655
* - Color depth: 24-bit for RGB 888, 16-bit for RGB 565
* - Image width (uint16 little endian, 2 bytes)
* - Image height (uint16 little endian, 2 bytes)
* - Pixel data encoded as RGB 24-bit and suffixed by a single byte CRC.
* - Pixel data encoded as RGB 16/24 bit and suffixed by a single byte CRC.
*
* Format: [ '!' ] [ 'I' ] [uint8_t color bit] [ uint16 width ] [ uint16 height ] [ r g b ] [ r g b ] [ r g b ] … [ CRC ]
*/
@@ -95,6 +96,9 @@ uint32_t totalPixel = 0; // received pixel
// pixel line buffer, should be large enough to hold an image width
uint16_t pixel_buf[512];
// Use software timer to schedule bandwidth negotiation a few seconds after connection
SoftwareTimer negoTimer;
// Statistics for speed testing
uint32_t rxStartTime = 0;
uint32_t rxLastTime = 0;
@@ -106,7 +110,7 @@ void setup()
{
Serial.begin(115200);
#if TFT_IN_USE == TFT_15_GIZMO
#if TFT_IN_USE == TFT_GIZMO
tft.init(240, 240);
tft.setRotation(2);
pinMode(TFT_BACKLIGHT, OUTPUT);
@@ -143,7 +147,7 @@ void setup()
// However, the transfer speed will be affected since immediate callback will block BLE task
// to process data especially when tft.drawRGBBitmap() is calling.
#ifdef NRF52840_XXAA
// 2nd argument is true to deferred callbacks i.e queue it up in seperated callback Task
// 2nd argument is true to deferred callbacks i.e queue it up in separated callback Task
bleuart.setRxCallback(bleuart_rx_callback, true);
#else
// 2nd argument is false to invoke callbacks immediately (thus blocking other ble events)
@@ -152,6 +156,9 @@ void setup()
bleuart.setRxOverflowCallback(bleuart_overflow_callback);
// one-shot (non-repeating) 2 seconds timer
negoTimer.begin(2000, negotiate_bandwidth, NULL, false);
// Set up and start advertising
startAdv();
@@ -219,12 +226,15 @@ void bleuart_rx_callback(uint16_t conn_hdl)
totalPixel = 0;
PRINT_INT(imageColorBit);
PRINT_INT(imageWidth);
PRINT_INT(imageHeight);
tft.fillScreen(COLOR_BLACK);
tft.setCursor(0, 0);
// Print out the current connection info
BLEConnection* conn = Bluefruit.Connection(conn_hdl);
Serial.printf("Connection Info: PHY = %d Mbps, Conn Interval = %.2f ms, Data Length = %d, MTU = %d\n",
conn->getPHY(), conn->getConnectionInterval()*1.25f, conn->getDataLength(), conn->getMtu());
Serial.printf("Receiving an %dx%d Image with %d-bit color\n", imageWidth, imageHeight, imageColorBit);
}
// Extract pixel data to buffer and draw image line by line
@@ -245,7 +255,7 @@ void bleuart_rx_callback(uint16_t conn_hdl)
totalPixel++;
// enough to draw an image line
// have enough to draw an image line
if ( (totalPixel % imageWidth) == 0 )
{
tft.drawRGBBitmap(0, totalPixel/imageWidth, pixel_buf, imageWidth, 1);
@@ -269,24 +279,44 @@ void bleuart_rx_callback(uint16_t conn_hdl)
}
}
void negotiate_bandwidth(TimerHandle_t xTimer)
{
(void) xTimer;
uint16_t conn_hdl = (uint16_t) ((uint32_t) negoTimer.getID());
BLEConnection* conn = Bluefruit.Connection(conn_hdl);
// Switching from 1 Mb to 2 Mb PHY if needed
if ( conn->connected() )
{
// Requesting to Switching to 2MB PHY, larger data length and bigger MTU
// will increase the throughput on supported central. This should already
// be done with latest Bluefruit app.
//
// However, some Android devices require 2Mb PHY switching must be initiated
// from nRF side
if ( conn->getPHY() == BLE_GAP_PHY_1MBPS )
{
Serial.println("Requesting PHY change from 1 Mb to 2Mb");
conn->requestPHY();
// Data Length and MTU should already be done by Bluefruit app
// conn->requestDataLengthUpdate();
// conn->requestMtuExchange(247);
}
}
}
void connect_callback(uint16_t conn_handle)
{
BLEConnection* conn = Bluefruit.Connection(conn_handle);
// Set connection handle as timer ID
// Then schedule negotiation after a few seconds, we should not
// negotiate here since it increases chance to conflict with other
// on-going negotiation from central after connection
negoTimer.setID((void*) conn_handle);
negoTimer.start();
tft.println("Connected");
// Requesting to Switching to 2MB PHY, larger data length and MTU
// will increase the throughput on supported central. This should be already done
// with latest Bluefruit app, but still put here for user reference
conn->requestPHY();
tft.println("Switching PHY");
conn->requestDataLengthUpdate();
tft.println("Updating Data Length");
conn->requestMtuExchange(247);
tft.println("Exchanging MTU");
tft.setTextColor(COLOR_GREEN);
tft.println("Ready to receive new image");
tft.setTextColor(COLOR_WHITE);
@@ -294,6 +324,14 @@ void connect_callback(uint16_t conn_handle)
void print_summary(uint32_t count, uint32_t ms)
{
float sec = ms / 1000.0F;
// Print to serial
Serial.printf("Received %d bytes in %.2f seconds\n", count, sec);
Serial.printf("Speed: %.2f KB/s\n\n", (count / 1024.0F) / sec);
Serial.println("Ready to receive new image");
// Also print to TFT with color text
tft.setCursor(0, imageHeight+5);
tft.print("Received ");
@@ -304,14 +342,14 @@ void print_summary(uint32_t count, uint32_t ms)
tft.print(" bytes in ");
tft.setTextColor(COLOR_YELLOW);
tft.print(ms / 1000.0F, 2);
tft.print(sec, 2);
tft.setTextColor(COLOR_WHITE);
tft.println(" seconds");
tft.print("Speed: ");
tft.setTextColor(COLOR_YELLOW);
tft.print( (count / 1000.0F) / (ms / 1000.0F), 2);
tft.print( (count / 1024.0F) / sec, 2);
tft.setTextColor(COLOR_WHITE);
tft.print(" KB/s for ");
@@ -134,8 +134,8 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar
{
for (uint8_t i=0; i<count; i++)
{
if ( chr[i]->discovered() )
continue;
// Skip if output chr is already discovered, happens with multiple instances of same UUIDs
if ( chr[i]->discovered() ) continue;
if ( chr[i]->uuid == disc_chr->chars[d].uuid )
{
+2 -2
View File
@@ -183,7 +183,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
// disconnect Client Services & Characteristics of the disconnected handle
if ( evt_id == BLE_GAP_EVT_DISCONNECTED )
{
// Client
// Client Chr
for(uint8_t i=0; i<_client.chr_count; i++)
{
if ( evt_conn_hdl == _client.chr_list[i]->connHandle() )
@@ -192,7 +192,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt)
}
}
// TODO merge to above loop
// Client Service TODO merge to above loop
for(uint8_t i=0; i<_client.svc_count; i++)
{
if ( evt_conn_hdl == _client.svc_list[i]->_conn_hdl)