Compare commits
71 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1763821b5 | ||
|
|
c0b83b626e | ||
|
|
f8c0b1acea | ||
|
|
f9b604f04e | ||
|
|
3dc5f6fec4 | ||
|
|
41c93e4eba | ||
|
|
1c33162368 | ||
|
|
27d42fbef3 | ||
|
|
dd280bdc97 | ||
|
|
bf031fd999 | ||
|
|
02350c6a9f | ||
|
|
9023b262a1 | ||
|
|
eabd149371 | ||
|
|
838246d147 | ||
|
|
f96b6ab29c | ||
|
|
e3d0201365 | ||
|
|
286b47f489 | ||
|
|
aff4dc6628 | ||
|
|
98a39374e8 | ||
|
|
e8c0fb42d4 | ||
|
|
b77af16caa | ||
|
|
e3c1e28b8f | ||
|
|
dc7544d944 | ||
|
|
504c7b307d | ||
|
|
b6bc1f7ed3 | ||
|
|
ea0abaf351 | ||
|
|
2771579007 | ||
|
|
27035b2b91 | ||
|
|
1107590b56 | ||
|
|
66ddb52103 | ||
|
|
9f4f71fabe | ||
|
|
d23020e268 | ||
|
|
f4491875ab | ||
|
|
6fe28da41b | ||
|
|
689b539c6b | ||
|
|
ce37c80c2d | ||
|
|
b39ce22e54 | ||
|
|
77c655fcf5 | ||
|
|
246afae6ef | ||
|
|
fcfa10bb1f | ||
|
|
febf79a98a | ||
|
|
424104f8ff | ||
|
|
955c78de64 | ||
|
|
958508eb6b | ||
|
|
6aa5d41a42 | ||
|
|
2a27c6d068 | ||
|
|
b73ae7fe74 | ||
|
|
f264efdb12 | ||
|
|
0d32d21d75 | ||
|
|
9b4dfbd180 | ||
|
|
926c786705 | ||
|
|
299623927e | ||
|
|
9a3bb81337 | ||
|
|
73d1839ddd | ||
|
|
cc86533e86 | ||
|
|
bf3f270067 | ||
|
|
cfe838e03b | ||
|
|
7484fe478c | ||
|
|
d41d539435 | ||
|
|
cf6fec78dc | ||
|
|
10d76dde12 | ||
|
|
7b5a63d220 | ||
|
|
c1d5f5d562 | ||
|
|
adfeee063f | ||
|
|
2d3928ed81 | ||
|
|
48249fbd1e | ||
|
|
1a53dccebd | ||
|
|
3e28724b62 | ||
|
|
d86b3fe134 | ||
|
|
1a3d6b125d | ||
|
|
b2020f5512 |
8
.github/workflows/ci.yml
vendored
@@ -12,12 +12,6 @@ jobs:
|
||||
- uses: actions/checkout@v6
|
||||
with:
|
||||
submodules: recursive
|
||||
- uses: actions/cache@v5
|
||||
with:
|
||||
path: |
|
||||
~/.cache/pip
|
||||
~/.platformio/.cache
|
||||
key: ${{ runner.os }}-pio
|
||||
- uses: actions/setup-python@v6
|
||||
with:
|
||||
python-version: '3.14'
|
||||
@@ -34,7 +28,7 @@ jobs:
|
||||
sudo apt-get install -y clang-format-21
|
||||
|
||||
- name: Run cppcheck
|
||||
run: pio check --fail-on-defect medium --fail-on-defect high
|
||||
run: pio check --fail-on-defect low --fail-on-defect medium --fail-on-defect high
|
||||
|
||||
- name: Run clang-format
|
||||
run: PATH="/usr/lib/llvm-21/bin:$PATH" ./bin/clang-format-fix && git diff --exit-code || (echo "Please run 'bin/clang-format-fix' to fix formatting issues" && exit 1)
|
||||
|
||||
1
.gitignore
vendored
@@ -3,3 +3,4 @@
|
||||
.DS_Store
|
||||
.vscode
|
||||
lib/EpdFont/fontsrc
|
||||
*.generated.h
|
||||
|
||||
19
README.md
@@ -23,18 +23,23 @@ CrossPoint Reader aims to:
|
||||
|
||||
This project is **not affiliated with Xteink**; it's built as a community project.
|
||||
|
||||
## Features
|
||||
## Features & Usage
|
||||
|
||||
- [x] EPUB parsing and rendering
|
||||
- [ ] Image support within EPUB
|
||||
- [x] Saved reading position
|
||||
- [ ] File explorer with file picker
|
||||
- [x] File explorer with file picker
|
||||
- [x] Basic EPUB picker from root directory
|
||||
- [x] Support nested folders
|
||||
- [ ] EPUB picker with cover art
|
||||
- [ ] Image support within EPUB
|
||||
- [x] Custom sleep screen
|
||||
- [ ] Cover sleep screen
|
||||
- [x] Wifi book upload
|
||||
- [ ] Wifi OTA updates
|
||||
- [ ] Configurable font, layout, and display options
|
||||
- [ ] WiFi connectivity
|
||||
- [ ] BLE connectivity
|
||||
- [ ] Screen rotation
|
||||
|
||||
See [the user guide](./USER_GUIDE.md) for instructions on operating CrossPoint.
|
||||
|
||||
## Installing
|
||||
|
||||
@@ -59,10 +64,6 @@ back to the other partition using the "Swap boot partition" button here https://
|
||||
|
||||
See [Development](#development) below.
|
||||
|
||||
## Usage
|
||||
|
||||
See [the user guide](./USER_GUIDE.md) for instructions on operating CrossPoint.
|
||||
|
||||
## Development
|
||||
|
||||
### Prerequisites
|
||||
|
||||
@@ -19,24 +19,72 @@ The device utilises the standard buttons on the Xtink X4 in the same layout:
|
||||
|
||||
### Power On / Off
|
||||
|
||||
To turn the device on or off, **press and hold the Power button for 1 full second**.
|
||||
To turn the device on or off, **press and hold the Power button for half a second**. In **Settings** you can configure
|
||||
the power button to trigger on a short press instead of a long one.
|
||||
|
||||
### First Launch
|
||||
|
||||
Upon turning the device on for the first time, you will be placed on the **Book Selection Screen** (File Browser).
|
||||
Upon turning the device on for the first time, you will be placed on the **Home** screen.
|
||||
|
||||
> **Note:** On subsequent restarts, the firmware will automatically reopen the last book you were reading.
|
||||
|
||||
---
|
||||
|
||||
## 3. Book Selection
|
||||
## 3. Screens
|
||||
|
||||
The Home Screen acts as a folder and file browser.
|
||||
### 3.1 Home Screen
|
||||
|
||||
The Home Screen is the main entry point to the firmware. From here you can navigate to the **Book Selection** screen,
|
||||
**Settings** screen, or **File Upload** screen.
|
||||
|
||||
### 3.2 Book Selection (Read)
|
||||
|
||||
The Book Selection acts as a folder and file browser.
|
||||
|
||||
* **Navigate List:** Use **Left** (or **Volume Up**), or **Right** (or **Volume Down**) to move the selection cursor up
|
||||
and down through folders and books.
|
||||
* **Open Selection:** Press **Confirm** to open a folder or read a selected book.
|
||||
|
||||
### 3.3 Reading Screen
|
||||
|
||||
See [4. Reading Mode](#4-reading-mode) below for more information.
|
||||
|
||||
### 3.4 File Upload Screen
|
||||
|
||||
The File Upload screen allows you to upload new e-books to the device. When you enter the screen you'll be prompted with
|
||||
a WiFi selection dialog and then your X4 will start hosting a web server.
|
||||
|
||||
See the [webserver docs](./docs/webserver.md) for more information on how to connect to the web server and upload files.
|
||||
|
||||
### 3.5 Settings
|
||||
|
||||
The Settings screen allows you to configure the device's behavior. There are a few settings you can adjust:
|
||||
- **Sleep Screen**: Which sleep screen to display when the device sleeps, options are:
|
||||
- "Dark" (default) - The default dark sleep screen
|
||||
- "Light" - The same default sleep screen, on a white background
|
||||
- "Custom" - Custom images from the SD card, see [3.6 Sleep Screen](#36-sleep-screen) below for more information
|
||||
- "Cover" - The book cover image (Note: this is experimental and may not work as expected)
|
||||
- **Extra Paragraph Spacing**: If enabled, vertical space will be added between paragraphs in the book, if disabled,
|
||||
paragraphs will not have vertical space between them, but will have first word indentation.
|
||||
- **Short Power Button Click**: Whether to trigger the power button on a short press or a long press.
|
||||
|
||||
### 3.6 Sleep Screen
|
||||
|
||||
You can customize the sleep screen by placing custom images in specific locations on the SD card:
|
||||
|
||||
- **Single Image:** Place a file named `sleep.bmp` in the root directory.
|
||||
- **Multiple Images:** Create a `sleep` directory in the root of the SD card and place any number of `.bmp` images
|
||||
inside. If images are found in this directory, they will take priority over the `sleep.png` file, and one will be
|
||||
randomly selected each time the device sleeps.
|
||||
|
||||
> [!NOTE]
|
||||
> You'll need to set the **Sleep Screen** setting to **Custom** in order to use these images.
|
||||
|
||||
> [!TIP]
|
||||
> For best results:
|
||||
> - Use uncompressed BMP files with 24-bit color depth
|
||||
> - Use a resolution of 480x800 pixels to match the device's screen resolution.
|
||||
|
||||
---
|
||||
|
||||
## 4. Reading Mode
|
||||
@@ -76,3 +124,4 @@ are planned for future updates:
|
||||
|
||||
* **Images:** Embedded images in e-books will not render.
|
||||
* **Text Formatting:** There are currently no settings to adjust font type, size, line spacing, or margins.
|
||||
* **Rotation**: Different rotation options are not supported.
|
||||
|
||||
|
Before Width: | Height: | Size: 2.2 MiB After Width: | Height: | Size: 899 KiB |
BIN
docs/images/wifi/webserver_files.png
Normal file
|
After Width: | Height: | Size: 193 KiB |
BIN
docs/images/wifi/webserver_homepage.png
Normal file
|
After Width: | Height: | Size: 144 KiB |
BIN
docs/images/wifi/webserver_upload.png
Normal file
|
After Width: | Height: | Size: 135 KiB |
BIN
docs/images/wifi/wifi_connected.jpeg
Normal file
|
After Width: | Height: | Size: 2.0 MiB |
BIN
docs/images/wifi/wifi_networks.jpeg
Normal file
|
After Width: | Height: | Size: 1.7 MiB |
BIN
docs/images/wifi/wifi_password.jpeg
Normal file
|
After Width: | Height: | Size: 2.1 MiB |
272
docs/webserver.md
Normal file
@@ -0,0 +1,272 @@
|
||||
# Web Server Guide
|
||||
|
||||
This guide explains how to connect your CrossPoint Reader to WiFi and use the built-in web server to upload EPUB files from your computer or phone.
|
||||
|
||||
## Overview
|
||||
|
||||
CrossPoint Reader includes a built-in web server that allows you to:
|
||||
|
||||
- Upload EPUB files wirelessly from any device on the same WiFi network
|
||||
- Browse and manage files on your device's SD card
|
||||
- Create folders to organize your ebooks
|
||||
- Delete files and folders
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Your CrossPoint Reader device
|
||||
- A WiFi network
|
||||
- A computer, phone, or tablet connected to the **same WiFi network**
|
||||
|
||||
---
|
||||
|
||||
## Step 1: Accessing the WiFi Screen
|
||||
|
||||
1. From the main menu or file browser, navigate to the **Settings** screen
|
||||
2. Select the **WiFi** option
|
||||
3. The device will automatically start scanning for available networks
|
||||
|
||||
---
|
||||
|
||||
## Step 2: Connecting to WiFi
|
||||
|
||||
### Viewing Available Networks
|
||||
|
||||
Once the scan completes, you'll see a list of available WiFi networks with the following indicators:
|
||||
|
||||
- **Signal strength bars** (`||||`, `|||`, `||`, `|`) - Shows connection quality
|
||||
- **`*` symbol** - Indicates the network is password-protected (encrypted)
|
||||
- **`+` symbol** - Indicates you have previously saved credentials for this network
|
||||
|
||||
<img src="./images/wifi/wifi_networks.jpeg" height="500">
|
||||
|
||||
### Selecting a Network
|
||||
|
||||
1. Use the **Left/Right** (or **Volume Up/Down**) buttons to navigate through the network list
|
||||
2. Press **Confirm** to select the highlighted network
|
||||
|
||||
### Entering Password (for encrypted networks)
|
||||
|
||||
If the network requires a password:
|
||||
|
||||
1. An on-screen keyboard will appear
|
||||
2. Use the navigation buttons to select characters
|
||||
3. Press **Confirm** to enter each character
|
||||
4. When complete, select the **Done** option on the keyboard
|
||||
|
||||
<img src="./images/wifi/wifi_password.jpeg" height="500">
|
||||
|
||||
**Note:** If you've previously connected to this network, the saved password will be used automatically.
|
||||
|
||||
### Connection Process
|
||||
|
||||
The device will display "Connecting..." while establishing the connection. This typically takes 5-10 seconds.
|
||||
|
||||
### Saving Credentials
|
||||
|
||||
If this is a new network, you'll be prompted to save the password:
|
||||
|
||||
- Select **Yes** to save credentials for automatic connection next time (NOTE: These are stored in plaintext on the device's SD card. Do not use this for sensitive networks.)
|
||||
- Select **No** to connect without saving
|
||||
|
||||
---
|
||||
|
||||
## Step 3: Connection Success
|
||||
|
||||
Once connected, the screen will display:
|
||||
|
||||
- **Network name** (SSID)
|
||||
- **IP Address** (e.g., `192.168.1.102`)
|
||||
- **Web server URL** (e.g., `http://192.168.1.102/`)
|
||||
|
||||
<img src="./images/wifi/wifi_connected.jpeg" height="500">
|
||||
|
||||
**Important:** Make note of the IP address - you'll need this to access the web interface from your computer or phone.
|
||||
|
||||
---
|
||||
|
||||
## Step 4: Accessing the Web Interface
|
||||
|
||||
### From a Computer
|
||||
|
||||
1. Ensure your computer is connected to the **same WiFi network** as your CrossPoint Reader
|
||||
2. Open any web browser (Chrome is recommended)
|
||||
3. Type the IP address shown on your device into the browser's address bar
|
||||
- Example: `http://192.168.1.102/`
|
||||
4. Press Enter
|
||||
|
||||
### From a Phone or Tablet
|
||||
|
||||
1. Ensure your phone/tablet is connected to the **same WiFi network** as your CrossPoint Reader
|
||||
2. Open your mobile browser (Safari, Chrome, etc.)
|
||||
3. Type the IP address into the address bar
|
||||
- Example: `http://192.168.1.102/`
|
||||
4. Tap Go
|
||||
|
||||
---
|
||||
|
||||
## Step 5: Using the Web Interface
|
||||
|
||||
### Home Page
|
||||
|
||||
The home page displays:
|
||||
|
||||
- Device status and version information
|
||||
- WiFi connection status
|
||||
- Current IP address
|
||||
- Available memory
|
||||
|
||||
Navigation links:
|
||||
|
||||
- **Home** - Returns to the status page
|
||||
- **File Manager** - Access file management features
|
||||
|
||||
<img src="./images/wifi/webserver_homepage.png" width="600">
|
||||
|
||||
### File Manager
|
||||
|
||||
Click **File Manager** to access file management features.
|
||||
|
||||
#### Browsing Files
|
||||
|
||||
- The file manager displays all files and folders on your SD card
|
||||
- **Folders** are highlighted in yellow with a 📁 icon
|
||||
- **EPUB files** are highlighted in green with a 📗 icon
|
||||
- Click on a folder name to navigate into it
|
||||
- Use the breadcrumb navigation at the top to go back to parent folders
|
||||
|
||||
<img src="./images/wifi/webserver_files.png" width="600">
|
||||
|
||||
#### Uploading EPUB Files
|
||||
|
||||
1. Click the **+ Add** button in the top-right corner
|
||||
2. Select **Upload eBook** from the dropdown menu
|
||||
3. Click **Choose File** and select an `.epub` file from your device
|
||||
4. Click **Upload**
|
||||
5. A progress bar will show the upload status
|
||||
6. The page will automatically refresh when the upload is complete
|
||||
|
||||
**Note:** Only `.epub` files are accepted. Other file types will be rejected.
|
||||
|
||||
<img src="./images/wifi/webserver_upload.png" width="600">
|
||||
|
||||
#### Creating Folders
|
||||
|
||||
1. Click the **+ Add** button in the top-right corner
|
||||
2. Select **New Folder** from the dropdown menu
|
||||
3. Enter a folder name (letters, numbers, underscores, and hyphens only)
|
||||
4. Click **Create Folder**
|
||||
|
||||
This is useful for organizing your ebooks by genre, author, or series.
|
||||
|
||||
#### Deleting Files and Folders
|
||||
|
||||
1. Click the **🗑️** (trash) icon next to any file or folder
|
||||
2. Confirm the deletion in the popup dialog
|
||||
3. Click **Delete** to permanently remove the item
|
||||
|
||||
**Warning:** Deletion is permanent and cannot be undone!
|
||||
|
||||
**Note:** Folders must be empty before they can be deleted.
|
||||
|
||||
---
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Cannot See the Device on the Network
|
||||
|
||||
**Problem:** Browser shows "Cannot connect" or "Site can't be reached"
|
||||
|
||||
**Solutions:**
|
||||
|
||||
1. Verify both devices are on the **same WiFi network**
|
||||
- Check your computer/phone WiFi settings
|
||||
- Confirm the CrossPoint Reader shows "Connected" status
|
||||
2. Double-check the IP address
|
||||
- Make sure you typed it correctly
|
||||
- Include `http://` at the beginning
|
||||
3. Try disabling VPN if you're using one
|
||||
4. Some networks have "client isolation" enabled - check with your network administrator
|
||||
|
||||
### Connection Drops or Times Out
|
||||
|
||||
**Problem:** WiFi connection is unstable
|
||||
|
||||
**Solutions:**
|
||||
|
||||
1. Move closer to the WiFi router
|
||||
2. Check signal strength on the device (should be at least `||` or better)
|
||||
3. Avoid interference from other devices
|
||||
4. Try a different WiFi network if available
|
||||
|
||||
### Upload Fails
|
||||
|
||||
**Problem:** File upload doesn't complete or shows an error
|
||||
|
||||
**Solutions:**
|
||||
|
||||
1. Ensure the file is a valid `.epub` file
|
||||
2. Check that the SD card has enough free space
|
||||
3. Try uploading a smaller file first to test
|
||||
4. Refresh the browser page and try again
|
||||
|
||||
### Saved Password Not Working
|
||||
|
||||
**Problem:** Device fails to connect with saved credentials
|
||||
|
||||
**Solutions:**
|
||||
|
||||
1. When connection fails, you'll be prompted to "Forget Network"
|
||||
2. Select **Yes** to remove the saved password
|
||||
3. Reconnect and enter the password again
|
||||
4. Choose to save the new password
|
||||
|
||||
---
|
||||
|
||||
## Security Notes
|
||||
|
||||
- The web server runs on port 80 (standard HTTP)
|
||||
- **No authentication is required** - anyone on the same network can access the interface
|
||||
- The web server is only accessible while the WiFi screen shows "Connected"
|
||||
- The web server automatically stops when you exit the WiFi screen
|
||||
- For security, only use on trusted private networks
|
||||
|
||||
---
|
||||
|
||||
## Technical Details
|
||||
|
||||
- **Supported WiFi:** 2.4GHz networks (802.11 b/g/n)
|
||||
- **Web Server Port:** 80 (HTTP)
|
||||
- **Maximum Upload Size:** Limited by available SD card space
|
||||
- **Supported File Format:** `.epub` only
|
||||
- **Browser Compatibility:** All modern browsers (Chrome, Firefox, Safari, Edge)
|
||||
|
||||
---
|
||||
|
||||
## Tips and Best Practices
|
||||
|
||||
1. **Organize with folders** - Create folders before uploading to keep your library organized
|
||||
2. **Check signal strength** - Stronger signals (`|||` or `||||`) provide faster, more reliable uploads
|
||||
3. **Upload multiple files** - You can upload files one at a time; the page refreshes after each upload
|
||||
4. **Use descriptive names** - Name your folders clearly (e.g., "SciFi", "Mystery", "Non-Fiction")
|
||||
5. **Keep credentials saved** - Save your WiFi password for quick reconnection in the future
|
||||
6. **Exit when done** - Press **Back** to exit the WiFi screen and save battery
|
||||
|
||||
---
|
||||
|
||||
## Exiting WiFi Mode
|
||||
|
||||
When you're finished uploading files:
|
||||
|
||||
1. Press the **Back** button on your CrossPoint Reader
|
||||
2. The web server will automatically stop
|
||||
3. WiFi will disconnect to conserve battery
|
||||
4. You'll return to the previous screen
|
||||
|
||||
Your uploaded files will be immediately available in the file browser!
|
||||
|
||||
---
|
||||
|
||||
## Related Documentation
|
||||
|
||||
- [User Guide](../USER_GUIDE.md) - General device operation
|
||||
- [README](../README.md) - Project overview and features
|
||||
@@ -59,14 +59,28 @@ bool EpdFont::hasPrintableChars(const char* string) const {
|
||||
|
||||
const EpdGlyph* EpdFont::getGlyph(const uint32_t cp) const {
|
||||
const EpdUnicodeInterval* intervals = data->intervals;
|
||||
for (int i = 0; i < data->intervalCount; i++) {
|
||||
const EpdUnicodeInterval* interval = &intervals[i];
|
||||
if (cp >= interval->first && cp <= interval->last) {
|
||||
const int count = data->intervalCount;
|
||||
|
||||
if (count == 0) return nullptr;
|
||||
|
||||
// Binary search for O(log n) lookup instead of O(n)
|
||||
// Critical for Korean fonts with many unicode intervals
|
||||
int left = 0;
|
||||
int right = count - 1;
|
||||
|
||||
while (left <= right) {
|
||||
const int mid = left + (right - left) / 2;
|
||||
const EpdUnicodeInterval* interval = &intervals[mid];
|
||||
|
||||
if (cp < interval->first) {
|
||||
right = mid - 1;
|
||||
} else if (cp > interval->last) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
// Found: cp >= interval->first && cp <= interval->last
|
||||
return &data->glyph[interval->offset + (cp - interval->first)];
|
||||
}
|
||||
if (cp < interval->first) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#include "Epub.h"
|
||||
|
||||
#include <FsHelpers.h>
|
||||
#include <HardwareSerial.h>
|
||||
#include <JpegToBmpConverter.h>
|
||||
#include <SD.h>
|
||||
#include <ZipFile.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "Epub/FsHelpers.h"
|
||||
#include "Epub/parsers/ContainerParser.h"
|
||||
#include "Epub/parsers/ContentOpfParser.h"
|
||||
#include "Epub/parsers/TocNcxParser.h"
|
||||
@@ -30,31 +29,39 @@ bool Epub::findContentOpfFile(std::string* contentOpfFile) const {
|
||||
// Stream read (reusing your existing stream logic)
|
||||
if (!readItemContentsToStream(containerPath, containerParser, 512)) {
|
||||
Serial.printf("[%lu] [EBP] Could not read META-INF/container.xml\n", millis());
|
||||
containerParser.teardown();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Extract the result
|
||||
if (containerParser.fullPath.empty()) {
|
||||
Serial.printf("[%lu] [EBP] Could not find valid rootfile in container.xml\n", millis());
|
||||
containerParser.teardown();
|
||||
return false;
|
||||
}
|
||||
|
||||
*contentOpfFile = std::move(containerParser.fullPath);
|
||||
|
||||
containerParser.teardown();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Epub::parseContentOpf(const std::string& contentOpfFilePath) {
|
||||
bool Epub::parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata) {
|
||||
std::string contentOpfFilePath;
|
||||
if (!findContentOpfFile(&contentOpfFilePath)) {
|
||||
Serial.printf("[%lu] [EBP] Could not find content.opf in zip\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
contentBasePath = contentOpfFilePath.substr(0, contentOpfFilePath.find_last_of('/') + 1);
|
||||
|
||||
Serial.printf("[%lu] [EBP] Parsing content.opf: %s\n", millis(), contentOpfFilePath.c_str());
|
||||
|
||||
size_t contentOpfSize;
|
||||
if (!getItemSize(contentOpfFilePath, &contentOpfSize)) {
|
||||
Serial.printf("[%lu] [EBP] Could not get size of content.opf\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
ContentOpfParser opfParser(getBasePath(), contentOpfSize);
|
||||
ContentOpfParser opfParser(getCachePath(), getBasePath(), contentOpfSize, bookMetadataCache.get());
|
||||
Serial.printf("[%lu] [MEM] Free: %d bytes, Total: %d bytes, Min Free: %d bytes\n", millis(), ESP.getFreeHeap(),
|
||||
ESP.getHeapSize(), ESP.getMinFreeHeap());
|
||||
|
||||
if (!opfParser.setup()) {
|
||||
Serial.printf("[%lu] [EBP] Could not setup content.opf parser\n", millis());
|
||||
@@ -63,137 +70,154 @@ bool Epub::parseContentOpf(const std::string& contentOpfFilePath) {
|
||||
|
||||
if (!readItemContentsToStream(contentOpfFilePath, opfParser, 1024)) {
|
||||
Serial.printf("[%lu] [EBP] Could not read content.opf\n", millis());
|
||||
opfParser.teardown();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Grab data from opfParser into epub
|
||||
title = opfParser.title;
|
||||
if (!opfParser.coverItemId.empty() && opfParser.items.count(opfParser.coverItemId) > 0) {
|
||||
coverImageItem = opfParser.items.at(opfParser.coverItemId);
|
||||
}
|
||||
bookMetadata.title = opfParser.title;
|
||||
// TODO: Parse author
|
||||
bookMetadata.author = "";
|
||||
bookMetadata.coverItemHref = opfParser.coverItemHref;
|
||||
|
||||
if (!opfParser.tocNcxPath.empty()) {
|
||||
tocNcxItem = opfParser.tocNcxPath;
|
||||
}
|
||||
|
||||
for (auto& spineRef : opfParser.spineRefs) {
|
||||
if (opfParser.items.count(spineRef)) {
|
||||
spine.emplace_back(spineRef, opfParser.items.at(spineRef));
|
||||
}
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [EBP] Successfully parsed content.opf\n", millis());
|
||||
|
||||
opfParser.teardown();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Epub::parseTocNcxFile() {
|
||||
bool Epub::parseTocNcxFile() const {
|
||||
// the ncx file should have been specified in the content.opf file
|
||||
if (tocNcxItem.empty()) {
|
||||
Serial.printf("[%lu] [EBP] No ncx file specified\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t tocSize;
|
||||
if (!getItemSize(tocNcxItem, &tocSize)) {
|
||||
Serial.printf("[%lu] [EBP] Could not get size of toc ncx\n", millis());
|
||||
Serial.printf("[%lu] [EBP] Parsing toc ncx file: %s\n", millis(), tocNcxItem.c_str());
|
||||
|
||||
const auto tmpNcxPath = getCachePath() + "/toc.ncx";
|
||||
File tempNcxFile;
|
||||
if (!FsHelpers::openFileForWrite("EBP", tmpNcxPath, tempNcxFile)) {
|
||||
return false;
|
||||
}
|
||||
readItemContentsToStream(tocNcxItem, tempNcxFile, 1024);
|
||||
tempNcxFile.close();
|
||||
if (!FsHelpers::openFileForRead("EBP", tmpNcxPath, tempNcxFile)) {
|
||||
return false;
|
||||
}
|
||||
const auto ncxSize = tempNcxFile.size();
|
||||
|
||||
TocNcxParser ncxParser(contentBasePath, tocSize);
|
||||
TocNcxParser ncxParser(contentBasePath, ncxSize, bookMetadataCache.get());
|
||||
|
||||
if (!ncxParser.setup()) {
|
||||
Serial.printf("[%lu] [EBP] Could not setup toc ncx parser\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!readItemContentsToStream(tocNcxItem, ncxParser, 1024)) {
|
||||
Serial.printf("[%lu] [EBP] Could not read toc ncx stream\n", millis());
|
||||
ncxParser.teardown();
|
||||
const auto ncxBuffer = static_cast<uint8_t*>(malloc(1024));
|
||||
if (!ncxBuffer) {
|
||||
Serial.printf("[%lu] [EBP] Could not allocate memory for toc ncx parser\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
this->toc = std::move(ncxParser.toc);
|
||||
while (tempNcxFile.available()) {
|
||||
const auto readSize = tempNcxFile.read(ncxBuffer, 1024);
|
||||
const auto processedSize = ncxParser.write(ncxBuffer, readSize);
|
||||
|
||||
Serial.printf("[%lu] [EBP] Parsed %d TOC items\n", millis(), this->toc.size());
|
||||
if (processedSize != readSize) {
|
||||
Serial.printf("[%lu] [EBP] Could not process all toc ncx data\n", millis());
|
||||
free(ncxBuffer);
|
||||
tempNcxFile.close();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
ncxParser.teardown();
|
||||
free(ncxBuffer);
|
||||
tempNcxFile.close();
|
||||
SD.remove(tmpNcxPath.c_str());
|
||||
|
||||
Serial.printf("[%lu] [EBP] Parsed TOC items\n", millis());
|
||||
return true;
|
||||
}
|
||||
|
||||
// load in the meta data for the epub file
|
||||
bool Epub::load() {
|
||||
Serial.printf("[%lu] [EBP] Loading ePub: %s\n", millis(), filepath.c_str());
|
||||
ZipFile zip("/sd" + filepath);
|
||||
|
||||
std::string contentOpfFilePath;
|
||||
if (!findContentOpfFile(&contentOpfFilePath)) {
|
||||
Serial.printf("[%lu] [EBP] Could not find content.opf in zip\n", millis());
|
||||
// Initialize spine/TOC cache
|
||||
bookMetadataCache.reset(new BookMetadataCache(cachePath));
|
||||
|
||||
// Try to load existing cache first
|
||||
if (bookMetadataCache->load()) {
|
||||
Serial.printf("[%lu] [EBP] Loaded ePub: %s\n", millis(), filepath.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
// Cache doesn't exist or is invalid, build it
|
||||
Serial.printf("[%lu] [EBP] Cache not found, building spine/TOC cache\n", millis());
|
||||
setupCacheDir();
|
||||
|
||||
// Begin building cache - stream entries to disk immediately
|
||||
if (!bookMetadataCache->beginWrite()) {
|
||||
Serial.printf("[%lu] [EBP] Could not begin writing cache\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [EBP] Found content.opf at: %s\n", millis(), contentOpfFilePath.c_str());
|
||||
|
||||
contentBasePath = contentOpfFilePath.substr(0, contentOpfFilePath.find_last_of('/') + 1);
|
||||
|
||||
if (!parseContentOpf(contentOpfFilePath)) {
|
||||
// OPF Pass
|
||||
BookMetadataCache::BookMetadata bookMetadata;
|
||||
if (!bookMetadataCache->beginContentOpfPass()) {
|
||||
Serial.printf("[%lu] [EBP] Could not begin writing content.opf pass\n", millis());
|
||||
return false;
|
||||
}
|
||||
if (!parseContentOpf(bookMetadata)) {
|
||||
Serial.printf("[%lu] [EBP] Could not parse content.opf\n", millis());
|
||||
return false;
|
||||
}
|
||||
if (!bookMetadataCache->endContentOpfPass()) {
|
||||
Serial.printf("[%lu] [EBP] Could not end writing content.opf pass\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
// TOC Pass
|
||||
if (!bookMetadataCache->beginTocPass()) {
|
||||
Serial.printf("[%lu] [EBP] Could not begin writing toc pass\n", millis());
|
||||
return false;
|
||||
}
|
||||
if (!parseTocNcxFile()) {
|
||||
Serial.printf("[%lu] [EBP] Could not parse toc\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
initializeSpineItemSizes();
|
||||
Serial.printf("[%lu] [EBP] Loaded ePub: %s\n", millis(), filepath.c_str());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Epub::initializeSpineItemSizes() {
|
||||
setupCacheDir();
|
||||
|
||||
size_t spineItemsCount = getSpineItemsCount();
|
||||
size_t cumSpineItemSize = 0;
|
||||
if (SD.exists((getCachePath() + "/spine_size.bin").c_str())) {
|
||||
File f = SD.open((getCachePath() + "/spine_size.bin").c_str());
|
||||
uint8_t data[4];
|
||||
for (size_t i = 0; i < spineItemsCount; i++) {
|
||||
f.read(data, 4);
|
||||
cumSpineItemSize = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
|
||||
cumulativeSpineItemSize.emplace_back(cumSpineItemSize);
|
||||
// Serial.printf("[%lu] [EBP] Loading item %d size %u to %u %u\n", millis(),
|
||||
// i, cumSpineItemSize, data[1], data[0]);
|
||||
}
|
||||
f.close();
|
||||
} else {
|
||||
File f = SD.open((getCachePath() + "/spine_size.bin").c_str(), FILE_WRITE);
|
||||
uint8_t data[4];
|
||||
// determine size of spine items
|
||||
for (size_t i = 0; i < spineItemsCount; i++) {
|
||||
std::string spineItem = getSpineItem(i);
|
||||
size_t s = 0;
|
||||
getItemSize(spineItem, &s);
|
||||
cumSpineItemSize += s;
|
||||
cumulativeSpineItemSize.emplace_back(cumSpineItemSize);
|
||||
|
||||
// and persist to cache
|
||||
data[0] = cumSpineItemSize & 0xFF;
|
||||
data[1] = (cumSpineItemSize >> 8) & 0xFF;
|
||||
data[2] = (cumSpineItemSize >> 16) & 0xFF;
|
||||
data[3] = (cumSpineItemSize >> 24) & 0xFF;
|
||||
// Serial.printf("[%lu] [EBP] Persisting item %d size %u to %u %u\n", millis(),
|
||||
// i, cumSpineItemSize, data[1], data[0]);
|
||||
f.write(data, 4);
|
||||
}
|
||||
|
||||
f.close();
|
||||
if (!bookMetadataCache->endTocPass()) {
|
||||
Serial.printf("[%lu] [EBP] Could not end writing toc pass\n", millis());
|
||||
return false;
|
||||
}
|
||||
Serial.printf("[%lu] [EBP] Book size: %lu\n", millis(), cumSpineItemSize);
|
||||
|
||||
// Close the cache files
|
||||
if (!bookMetadataCache->endWrite()) {
|
||||
Serial.printf("[%lu] [EBP] Could not end writing cache\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Build final book.bin
|
||||
if (!bookMetadataCache->buildBookBin(filepath, bookMetadata)) {
|
||||
Serial.printf("[%lu] [EBP] Could not update mappings and sizes\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!bookMetadataCache->cleanupTmpFiles()) {
|
||||
Serial.printf("[%lu] [EBP] Could not cleanup tmp files - ignoring\n", millis());
|
||||
}
|
||||
|
||||
// Reload the cache from disk so it's in the correct state
|
||||
bookMetadataCache.reset(new BookMetadataCache(cachePath));
|
||||
if (!bookMetadataCache->load()) {
|
||||
Serial.printf("[%lu] [EBP] Failed to reload cache after writing\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [EBP] Loaded ePub: %s\n", millis(), filepath.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Epub::clearCache() const {
|
||||
@@ -229,49 +253,76 @@ const std::string& Epub::getCachePath() const { return cachePath; }
|
||||
|
||||
const std::string& Epub::getPath() const { return filepath; }
|
||||
|
||||
const std::string& Epub::getTitle() const { return title; }
|
||||
|
||||
const std::string& Epub::getCoverImageItem() const { return coverImageItem; }
|
||||
|
||||
std::string normalisePath(const std::string& path) {
|
||||
std::vector<std::string> components;
|
||||
std::string component;
|
||||
|
||||
for (const auto c : path) {
|
||||
if (c == '/') {
|
||||
if (!component.empty()) {
|
||||
if (component == "..") {
|
||||
if (!components.empty()) {
|
||||
components.pop_back();
|
||||
}
|
||||
} else {
|
||||
components.push_back(component);
|
||||
}
|
||||
component.clear();
|
||||
}
|
||||
} else {
|
||||
component += c;
|
||||
}
|
||||
const std::string& Epub::getTitle() const {
|
||||
static std::string blank;
|
||||
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
|
||||
return blank;
|
||||
}
|
||||
|
||||
if (!component.empty()) {
|
||||
components.push_back(component);
|
||||
}
|
||||
|
||||
std::string result;
|
||||
for (const auto& c : components) {
|
||||
if (!result.empty()) {
|
||||
result += "/";
|
||||
}
|
||||
result += c;
|
||||
}
|
||||
|
||||
return result;
|
||||
return bookMetadataCache->coreMetadata.title;
|
||||
}
|
||||
|
||||
uint8_t* Epub::readItemContentsToBytes(const std::string& itemHref, size_t* size, bool trailingNullByte) const {
|
||||
std::string Epub::getCoverBmpPath() const { return cachePath + "/cover.bmp"; }
|
||||
|
||||
bool Epub::generateCoverBmp() const {
|
||||
// Already generated, return true
|
||||
if (SD.exists(getCoverBmpPath().c_str())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
|
||||
Serial.printf("[%lu] [EBP] Cannot generate cover BMP, cache not loaded\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto coverImageHref = bookMetadataCache->coreMetadata.coverItemHref;
|
||||
if (coverImageHref.empty()) {
|
||||
Serial.printf("[%lu] [EBP] No known cover image\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (coverImageHref.substr(coverImageHref.length() - 4) == ".jpg" ||
|
||||
coverImageHref.substr(coverImageHref.length() - 5) == ".jpeg") {
|
||||
Serial.printf("[%lu] [EBP] Generating BMP from JPG cover image\n", millis());
|
||||
const auto coverJpgTempPath = getCachePath() + "/.cover.jpg";
|
||||
|
||||
File coverJpg;
|
||||
if (!FsHelpers::openFileForWrite("EBP", coverJpgTempPath, coverJpg)) {
|
||||
return false;
|
||||
}
|
||||
readItemContentsToStream(coverImageHref, coverJpg, 1024);
|
||||
coverJpg.close();
|
||||
|
||||
if (!FsHelpers::openFileForRead("EBP", coverJpgTempPath, coverJpg)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
File coverBmp;
|
||||
if (!FsHelpers::openFileForWrite("EBP", getCoverBmpPath(), coverBmp)) {
|
||||
coverJpg.close();
|
||||
return false;
|
||||
}
|
||||
const bool success = JpegToBmpConverter::jpegFileToBmpStream(coverJpg, coverBmp);
|
||||
coverJpg.close();
|
||||
coverBmp.close();
|
||||
SD.remove(coverJpgTempPath.c_str());
|
||||
|
||||
if (!success) {
|
||||
Serial.printf("[%lu] [EBP] Failed to generate BMP from JPG cover image\n", millis());
|
||||
SD.remove(getCoverBmpPath().c_str());
|
||||
}
|
||||
Serial.printf("[%lu] [EBP] Generated BMP from JPG cover image, success: %s\n", millis(), success ? "yes" : "no");
|
||||
return success;
|
||||
} else {
|
||||
Serial.printf("[%lu] [EBP] Cover image is not a JPG, skipping\n", millis());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t* Epub::readItemContentsToBytes(const std::string& itemHref, size_t* size, const bool trailingNullByte) const {
|
||||
const ZipFile zip("/sd" + filepath);
|
||||
const std::string path = normalisePath(itemHref);
|
||||
const std::string path = FsHelpers::normalisePath(itemHref);
|
||||
|
||||
const auto content = zip.readFileToMemory(path.c_str(), size, trailingNullByte);
|
||||
if (!content) {
|
||||
@@ -284,77 +335,104 @@ uint8_t* Epub::readItemContentsToBytes(const std::string& itemHref, size_t* size
|
||||
|
||||
bool Epub::readItemContentsToStream(const std::string& itemHref, Print& out, const size_t chunkSize) const {
|
||||
const ZipFile zip("/sd" + filepath);
|
||||
const std::string path = normalisePath(itemHref);
|
||||
const std::string path = FsHelpers::normalisePath(itemHref);
|
||||
|
||||
return zip.readFileToStream(path.c_str(), out, chunkSize);
|
||||
}
|
||||
|
||||
bool Epub::getItemSize(const std::string& itemHref, size_t* size) const {
|
||||
const ZipFile zip("/sd" + filepath);
|
||||
const std::string path = normalisePath(itemHref);
|
||||
return getItemSize(zip, itemHref, size);
|
||||
}
|
||||
|
||||
bool Epub::getItemSize(const ZipFile& zip, const std::string& itemHref, size_t* size) {
|
||||
const std::string path = FsHelpers::normalisePath(itemHref);
|
||||
return zip.getInflatedFileSize(path.c_str(), size);
|
||||
}
|
||||
|
||||
int Epub::getSpineItemsCount() const { return spine.size(); }
|
||||
int Epub::getSpineItemsCount() const {
|
||||
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
|
||||
return 0;
|
||||
}
|
||||
return bookMetadataCache->getSpineCount();
|
||||
}
|
||||
|
||||
size_t Epub::getCumulativeSpineItemSize(const int spineIndex) const { return cumulativeSpineItemSize.at(spineIndex); }
|
||||
size_t Epub::getCumulativeSpineItemSize(const int spineIndex) const { return getSpineItem(spineIndex).cumulativeSize; }
|
||||
|
||||
std::string& Epub::getSpineItem(const int spineIndex) {
|
||||
if (spineIndex < 0 || spineIndex >= spine.size()) {
|
||||
BookMetadataCache::SpineEntry Epub::getSpineItem(const int spineIndex) const {
|
||||
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
|
||||
Serial.printf("[%lu] [EBP] getSpineItem called but cache not loaded\n", millis());
|
||||
return {};
|
||||
}
|
||||
|
||||
if (spineIndex < 0 || spineIndex >= bookMetadataCache->getSpineCount()) {
|
||||
Serial.printf("[%lu] [EBP] getSpineItem index:%d is out of range\n", millis(), spineIndex);
|
||||
return spine.at(0).second;
|
||||
return bookMetadataCache->getSpineEntry(0);
|
||||
}
|
||||
|
||||
return spine.at(spineIndex).second;
|
||||
return bookMetadataCache->getSpineEntry(spineIndex);
|
||||
}
|
||||
|
||||
EpubTocEntry& Epub::getTocItem(const int tocTndex) {
|
||||
if (tocTndex < 0 || tocTndex >= toc.size()) {
|
||||
Serial.printf("[%lu] [EBP] getTocItem index:%d is out of range\n", millis(), tocTndex);
|
||||
return toc.at(0);
|
||||
BookMetadataCache::TocEntry Epub::getTocItem(const int tocIndex) const {
|
||||
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
|
||||
Serial.printf("[%lu] [EBP] getTocItem called but cache not loaded\n", millis());
|
||||
return {};
|
||||
}
|
||||
|
||||
return toc.at(tocTndex);
|
||||
if (tocIndex < 0 || tocIndex >= bookMetadataCache->getTocCount()) {
|
||||
Serial.printf("[%lu] [EBP] getTocItem index:%d is out of range\n", millis(), tocIndex);
|
||||
return {};
|
||||
}
|
||||
|
||||
return bookMetadataCache->getTocEntry(tocIndex);
|
||||
}
|
||||
|
||||
int Epub::getTocItemsCount() const { return toc.size(); }
|
||||
int Epub::getTocItemsCount() const {
|
||||
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return bookMetadataCache->getTocCount();
|
||||
}
|
||||
|
||||
// work out the section index for a toc index
|
||||
int Epub::getSpineIndexForTocIndex(const int tocIndex) const {
|
||||
// the toc entry should have an href that matches the spine item
|
||||
// so we can find the spine index by looking for the href
|
||||
for (int i = 0; i < spine.size(); i++) {
|
||||
if (spine[i].second == toc[tocIndex].href) {
|
||||
return i;
|
||||
}
|
||||
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
|
||||
Serial.printf("[%lu] [EBP] getSpineIndexForTocIndex called but cache not loaded\n", millis());
|
||||
return 0;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [EBP] Section not found\n", millis());
|
||||
// not found - default to the start of the book
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Epub::getTocIndexForSpineIndex(const int spineIndex) const {
|
||||
// the toc entry should have an href that matches the spine item
|
||||
// so we can find the toc index by looking for the href
|
||||
for (int i = 0; i < toc.size(); i++) {
|
||||
if (toc[i].href == spine[spineIndex].second) {
|
||||
return i;
|
||||
}
|
||||
if (tocIndex < 0 || tocIndex >= bookMetadataCache->getTocCount()) {
|
||||
Serial.printf("[%lu] [EBP] getSpineIndexForTocIndex: tocIndex %d out of range\n", millis(), tocIndex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [EBP] TOC item not found\n", millis());
|
||||
return -1;
|
||||
const int spineIndex = bookMetadataCache->getTocEntry(tocIndex).spineIndex;
|
||||
if (spineIndex < 0) {
|
||||
Serial.printf("[%lu] [EBP] Section not found for TOC index %d\n", millis(), tocIndex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return spineIndex;
|
||||
}
|
||||
|
||||
size_t Epub::getBookSize() const { return getCumulativeSpineItemSize(getSpineItemsCount() - 1); }
|
||||
int Epub::getTocIndexForSpineIndex(const int spineIndex) const { return getSpineItem(spineIndex).tocIndex; }
|
||||
|
||||
size_t Epub::getBookSize() const {
|
||||
if (!bookMetadataCache || !bookMetadataCache->isLoaded() || bookMetadataCache->getSpineCount() == 0) {
|
||||
return 0;
|
||||
}
|
||||
return getCumulativeSpineItemSize(getSpineItemsCount() - 1);
|
||||
}
|
||||
|
||||
// Calculate progress in book
|
||||
uint8_t Epub::calculateProgress(const int currentSpineIndex, const float currentSpineRead) {
|
||||
size_t prevChapterSize = (currentSpineIndex >= 1) ? getCumulativeSpineItemSize(currentSpineIndex - 1) : 0;
|
||||
size_t curChapterSize = getCumulativeSpineItemSize(currentSpineIndex) - prevChapterSize;
|
||||
size_t bookSize = getBookSize();
|
||||
size_t sectionProgSize = currentSpineRead * curChapterSize;
|
||||
uint8_t Epub::calculateProgress(const int currentSpineIndex, const float currentSpineRead) const {
|
||||
const size_t bookSize = getBookSize();
|
||||
if (bookSize == 0) {
|
||||
return 0;
|
||||
}
|
||||
const size_t prevChapterSize = (currentSpineIndex >= 1) ? getCumulativeSpineItemSize(currentSpineIndex - 1) : 0;
|
||||
const size_t curChapterSize = getCumulativeSpineItemSize(currentSpineIndex) - prevChapterSize;
|
||||
const size_t sectionProgSize = currentSpineRead * curChapterSize;
|
||||
return round(static_cast<float>(prevChapterSize + sectionProgSize) / bookSize * 100.0);
|
||||
}
|
||||
|
||||
@@ -1,38 +1,30 @@
|
||||
#pragma once
|
||||
#include <Print.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "Epub/EpubTocEntry.h"
|
||||
#include "Epub/BookMetadataCache.h"
|
||||
|
||||
class ZipFile;
|
||||
|
||||
class Epub {
|
||||
// the title read from the EPUB meta data
|
||||
std::string title;
|
||||
// the cover image
|
||||
std::string coverImageItem;
|
||||
// the ncx file
|
||||
std::string tocNcxItem;
|
||||
// where is the EPUBfile?
|
||||
std::string filepath;
|
||||
// the spine of the EPUB file
|
||||
std::vector<std::pair<std::string, std::string>> spine;
|
||||
// the file size of the spine items (proxy to book progress)
|
||||
std::vector<size_t> cumulativeSpineItemSize;
|
||||
// the toc of the EPUB file
|
||||
std::vector<EpubTocEntry> toc;
|
||||
// the base path for items in the EPUB file
|
||||
std::string contentBasePath;
|
||||
// Uniq cache key based on filepath
|
||||
std::string cachePath;
|
||||
// Spine and TOC cache
|
||||
std::unique_ptr<BookMetadataCache> bookMetadataCache;
|
||||
|
||||
bool findContentOpfFile(std::string* contentOpfFile) const;
|
||||
bool parseContentOpf(const std::string& contentOpfFilePath);
|
||||
bool parseTocNcxFile();
|
||||
void initializeSpineItemSizes();
|
||||
bool parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata);
|
||||
bool parseTocNcxFile() const;
|
||||
static bool getItemSize(const ZipFile& zip, const std::string& itemHref, size_t* size);
|
||||
|
||||
public:
|
||||
explicit Epub(std::string filepath, const std::string& cacheDir) : filepath(std::move(filepath)) {
|
||||
@@ -47,19 +39,20 @@ class Epub {
|
||||
const std::string& getCachePath() const;
|
||||
const std::string& getPath() const;
|
||||
const std::string& getTitle() const;
|
||||
const std::string& getCoverImageItem() const;
|
||||
std::string getCoverBmpPath() const;
|
||||
bool generateCoverBmp() const;
|
||||
uint8_t* readItemContentsToBytes(const std::string& itemHref, size_t* size = nullptr,
|
||||
bool trailingNullByte = false) const;
|
||||
bool readItemContentsToStream(const std::string& itemHref, Print& out, size_t chunkSize) const;
|
||||
bool getItemSize(const std::string& itemHref, size_t* size) const;
|
||||
std::string& getSpineItem(int spineIndex);
|
||||
BookMetadataCache::SpineEntry getSpineItem(int spineIndex) const;
|
||||
BookMetadataCache::TocEntry getTocItem(int tocIndex) const;
|
||||
int getSpineItemsCount() const;
|
||||
size_t getCumulativeSpineItemSize(const int spineIndex) const;
|
||||
EpubTocEntry& getTocItem(int tocIndex);
|
||||
int getTocItemsCount() const;
|
||||
int getSpineIndexForTocIndex(int tocIndex) const;
|
||||
int getTocIndexForSpineIndex(int spineIndex) const;
|
||||
size_t getCumulativeSpineItemSize(int spineIndex) const;
|
||||
|
||||
size_t getBookSize() const;
|
||||
uint8_t calculateProgress(const int currentSpineIndex, const float currentSpineRead);
|
||||
uint8_t calculateProgress(const int currentSpineIndex, const float currentSpineRead) const;
|
||||
};
|
||||
|
||||
326
lib/Epub/Epub/BookMetadataCache.cpp
Normal file
@@ -0,0 +1,326 @@
|
||||
#include "BookMetadataCache.h"
|
||||
|
||||
#include <HardwareSerial.h>
|
||||
#include <SD.h>
|
||||
#include <Serialization.h>
|
||||
#include <ZipFile.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "FsHelpers.h"
|
||||
|
||||
namespace {
|
||||
constexpr uint8_t BOOK_CACHE_VERSION = 1;
|
||||
constexpr char bookBinFile[] = "/book.bin";
|
||||
constexpr char tmpSpineBinFile[] = "/spine.bin.tmp";
|
||||
constexpr char tmpTocBinFile[] = "/toc.bin.tmp";
|
||||
} // namespace
|
||||
|
||||
/* ============= WRITING / BUILDING FUNCTIONS ================ */
|
||||
|
||||
bool BookMetadataCache::beginWrite() {
|
||||
buildMode = true;
|
||||
spineCount = 0;
|
||||
tocCount = 0;
|
||||
Serial.printf("[%lu] [BMC] Entering write mode\n", millis());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BookMetadataCache::beginContentOpfPass() {
|
||||
Serial.printf("[%lu] [BMC] Beginning content opf pass\n", millis());
|
||||
|
||||
// Open spine file for writing
|
||||
return FsHelpers::openFileForWrite("BMC", cachePath + tmpSpineBinFile, spineFile);
|
||||
}
|
||||
|
||||
bool BookMetadataCache::endContentOpfPass() {
|
||||
spineFile.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BookMetadataCache::beginTocPass() {
|
||||
Serial.printf("[%lu] [BMC] Beginning toc pass\n", millis());
|
||||
|
||||
// Open spine file for reading
|
||||
if (!FsHelpers::openFileForRead("BMC", cachePath + tmpSpineBinFile, spineFile)) {
|
||||
return false;
|
||||
}
|
||||
if (!FsHelpers::openFileForWrite("BMC", cachePath + tmpTocBinFile, tocFile)) {
|
||||
spineFile.close();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BookMetadataCache::endTocPass() {
|
||||
tocFile.close();
|
||||
spineFile.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BookMetadataCache::endWrite() {
|
||||
if (!buildMode) {
|
||||
Serial.printf("[%lu] [BMC] endWrite called but not in build mode\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
buildMode = false;
|
||||
Serial.printf("[%lu] [BMC] Wrote %d spine, %d TOC entries\n", millis(), spineCount, tocCount);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BookMetadataCache::buildBookBin(const std::string& epubPath, const BookMetadata& metadata) {
|
||||
// Open all three files, writing to meta, reading from spine and toc
|
||||
if (!FsHelpers::openFileForWrite("BMC", cachePath + bookBinFile, bookFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!FsHelpers::openFileForRead("BMC", cachePath + tmpSpineBinFile, spineFile)) {
|
||||
bookFile.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!FsHelpers::openFileForRead("BMC", cachePath + tmpTocBinFile, tocFile)) {
|
||||
bookFile.close();
|
||||
spineFile.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
constexpr size_t headerASize =
|
||||
sizeof(BOOK_CACHE_VERSION) + /* LUT Offset */ sizeof(size_t) + sizeof(spineCount) + sizeof(tocCount);
|
||||
const size_t metadataSize =
|
||||
metadata.title.size() + metadata.author.size() + metadata.coverItemHref.size() + sizeof(uint32_t) * 3;
|
||||
const size_t lutSize = sizeof(size_t) * spineCount + sizeof(size_t) * tocCount;
|
||||
const size_t lutOffset = headerASize + metadataSize;
|
||||
|
||||
// Header A
|
||||
serialization::writePod(bookFile, BOOK_CACHE_VERSION);
|
||||
serialization::writePod(bookFile, lutOffset);
|
||||
serialization::writePod(bookFile, spineCount);
|
||||
serialization::writePod(bookFile, tocCount);
|
||||
// Metadata
|
||||
serialization::writeString(bookFile, metadata.title);
|
||||
serialization::writeString(bookFile, metadata.author);
|
||||
serialization::writeString(bookFile, metadata.coverItemHref);
|
||||
|
||||
// Loop through spine entries, writing LUT positions
|
||||
spineFile.seek(0);
|
||||
for (int i = 0; i < spineCount; i++) {
|
||||
auto pos = spineFile.position();
|
||||
auto spineEntry = readSpineEntry(spineFile);
|
||||
serialization::writePod(bookFile, pos + lutOffset + lutSize);
|
||||
}
|
||||
|
||||
// Loop through toc entries, writing LUT positions
|
||||
tocFile.seek(0);
|
||||
for (int i = 0; i < tocCount; i++) {
|
||||
auto pos = tocFile.position();
|
||||
auto tocEntry = readTocEntry(tocFile);
|
||||
serialization::writePod(bookFile, pos + lutOffset + lutSize + spineFile.position());
|
||||
}
|
||||
|
||||
// LUTs complete
|
||||
// Loop through spines from spine file matching up TOC indexes, calculating cumulative size and writing to book.bin
|
||||
|
||||
const ZipFile zip("/sd" + epubPath);
|
||||
size_t cumSize = 0;
|
||||
spineFile.seek(0);
|
||||
for (int i = 0; i < spineCount; i++) {
|
||||
auto spineEntry = readSpineEntry(spineFile);
|
||||
|
||||
tocFile.seek(0);
|
||||
for (int j = 0; j < tocCount; j++) {
|
||||
auto tocEntry = readTocEntry(tocFile);
|
||||
if (tocEntry.spineIndex == i) {
|
||||
spineEntry.tocIndex = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Not a huge deal if we don't fine a TOC entry for the spine entry, this is expected behaviour for EPUBs
|
||||
// Logging here is for debugging
|
||||
if (spineEntry.tocIndex == -1) {
|
||||
Serial.printf("[%lu] [BMC] Warning: Could not find TOC entry for spine item %d: %s\n", millis(), i,
|
||||
spineEntry.href.c_str());
|
||||
}
|
||||
|
||||
// Calculate size for cumulative size
|
||||
size_t itemSize = 0;
|
||||
const std::string path = FsHelpers::normalisePath(spineEntry.href);
|
||||
if (zip.getInflatedFileSize(path.c_str(), &itemSize)) {
|
||||
cumSize += itemSize;
|
||||
spineEntry.cumulativeSize = cumSize;
|
||||
} else {
|
||||
Serial.printf("[%lu] [BMC] Warning: Could not get size for spine item: %s\n", millis(), path.c_str());
|
||||
}
|
||||
|
||||
// Write out spine data to book.bin
|
||||
writeSpineEntry(bookFile, spineEntry);
|
||||
}
|
||||
|
||||
// Loop through toc entries from toc file writing to book.bin
|
||||
tocFile.seek(0);
|
||||
for (int i = 0; i < tocCount; i++) {
|
||||
auto tocEntry = readTocEntry(tocFile);
|
||||
writeTocEntry(bookFile, tocEntry);
|
||||
}
|
||||
|
||||
bookFile.close();
|
||||
spineFile.close();
|
||||
tocFile.close();
|
||||
|
||||
Serial.printf("[%lu] [BMC] Successfully built book.bin\n", millis());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BookMetadataCache::cleanupTmpFiles() const {
|
||||
if (SD.exists((cachePath + tmpSpineBinFile).c_str())) {
|
||||
SD.remove((cachePath + tmpSpineBinFile).c_str());
|
||||
}
|
||||
if (SD.exists((cachePath + tmpTocBinFile).c_str())) {
|
||||
SD.remove((cachePath + tmpTocBinFile).c_str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t BookMetadataCache::writeSpineEntry(File& file, const SpineEntry& entry) const {
|
||||
const auto pos = file.position();
|
||||
serialization::writeString(file, entry.href);
|
||||
serialization::writePod(file, entry.cumulativeSize);
|
||||
serialization::writePod(file, entry.tocIndex);
|
||||
return pos;
|
||||
}
|
||||
|
||||
size_t BookMetadataCache::writeTocEntry(File& file, const TocEntry& entry) const {
|
||||
const auto pos = file.position();
|
||||
serialization::writeString(file, entry.title);
|
||||
serialization::writeString(file, entry.href);
|
||||
serialization::writeString(file, entry.anchor);
|
||||
serialization::writePod(file, entry.level);
|
||||
serialization::writePod(file, entry.spineIndex);
|
||||
return pos;
|
||||
}
|
||||
|
||||
// Note: for the LUT to be accurate, this **MUST** be called for all spine items before `addTocEntry` is ever called
|
||||
// this is because in this function we're marking positions of the items
|
||||
void BookMetadataCache::createSpineEntry(const std::string& href) {
|
||||
if (!buildMode || !spineFile) {
|
||||
Serial.printf("[%lu] [BMC] createSpineEntry called but not in build mode\n", millis());
|
||||
return;
|
||||
}
|
||||
|
||||
const SpineEntry entry(href, 0, -1);
|
||||
writeSpineEntry(spineFile, entry);
|
||||
spineCount++;
|
||||
}
|
||||
|
||||
void BookMetadataCache::createTocEntry(const std::string& title, const std::string& href, const std::string& anchor,
|
||||
const uint8_t level) {
|
||||
if (!buildMode || !tocFile || !spineFile) {
|
||||
Serial.printf("[%lu] [BMC] createTocEntry called but not in build mode\n", millis());
|
||||
return;
|
||||
}
|
||||
|
||||
int spineIndex = -1;
|
||||
// find spine index
|
||||
spineFile.seek(0);
|
||||
for (int i = 0; i < spineCount; i++) {
|
||||
auto spineEntry = readSpineEntry(spineFile);
|
||||
if (spineEntry.href == href) {
|
||||
spineIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (spineIndex == -1) {
|
||||
Serial.printf("[%lu] [BMC] addTocEntry: Could not find spine item for TOC href %s\n", millis(), href.c_str());
|
||||
}
|
||||
|
||||
const TocEntry entry(title, href, anchor, level, spineIndex);
|
||||
writeTocEntry(tocFile, entry);
|
||||
tocCount++;
|
||||
}
|
||||
|
||||
/* ============= READING / LOADING FUNCTIONS ================ */
|
||||
|
||||
bool BookMetadataCache::load() {
|
||||
if (!FsHelpers::openFileForRead("BMC", cachePath + bookBinFile, bookFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t version;
|
||||
serialization::readPod(bookFile, version);
|
||||
if (version != BOOK_CACHE_VERSION) {
|
||||
Serial.printf("[%lu] [BMC] Cache version mismatch: expected %d, got %d\n", millis(), BOOK_CACHE_VERSION, version);
|
||||
bookFile.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
serialization::readPod(bookFile, lutOffset);
|
||||
serialization::readPod(bookFile, spineCount);
|
||||
serialization::readPod(bookFile, tocCount);
|
||||
|
||||
serialization::readString(bookFile, coreMetadata.title);
|
||||
serialization::readString(bookFile, coreMetadata.author);
|
||||
serialization::readString(bookFile, coreMetadata.coverItemHref);
|
||||
|
||||
loaded = true;
|
||||
Serial.printf("[%lu] [BMC] Loaded cache data: %d spine, %d TOC entries\n", millis(), spineCount, tocCount);
|
||||
return true;
|
||||
}
|
||||
|
||||
BookMetadataCache::SpineEntry BookMetadataCache::getSpineEntry(const int index) {
|
||||
if (!loaded) {
|
||||
Serial.printf("[%lu] [BMC] getSpineEntry called but cache not loaded\n", millis());
|
||||
return {};
|
||||
}
|
||||
|
||||
if (index < 0 || index >= static_cast<int>(spineCount)) {
|
||||
Serial.printf("[%lu] [BMC] getSpineEntry index %d out of range\n", millis(), index);
|
||||
return {};
|
||||
}
|
||||
|
||||
// Seek to spine LUT item, read from LUT and get out data
|
||||
bookFile.seek(lutOffset + sizeof(size_t) * index);
|
||||
size_t spineEntryPos;
|
||||
serialization::readPod(bookFile, spineEntryPos);
|
||||
bookFile.seek(spineEntryPos);
|
||||
return readSpineEntry(bookFile);
|
||||
}
|
||||
|
||||
BookMetadataCache::TocEntry BookMetadataCache::getTocEntry(const int index) {
|
||||
if (!loaded) {
|
||||
Serial.printf("[%lu] [BMC] getTocEntry called but cache not loaded\n", millis());
|
||||
return {};
|
||||
}
|
||||
|
||||
if (index < 0 || index >= static_cast<int>(tocCount)) {
|
||||
Serial.printf("[%lu] [BMC] getTocEntry index %d out of range\n", millis(), index);
|
||||
return {};
|
||||
}
|
||||
|
||||
// Seek to TOC LUT item, read from LUT and get out data
|
||||
bookFile.seek(lutOffset + sizeof(size_t) * spineCount + sizeof(size_t) * index);
|
||||
size_t tocEntryPos;
|
||||
serialization::readPod(bookFile, tocEntryPos);
|
||||
bookFile.seek(tocEntryPos);
|
||||
return readTocEntry(bookFile);
|
||||
}
|
||||
|
||||
BookMetadataCache::SpineEntry BookMetadataCache::readSpineEntry(File& file) const {
|
||||
SpineEntry entry;
|
||||
serialization::readString(file, entry.href);
|
||||
serialization::readPod(file, entry.cumulativeSize);
|
||||
serialization::readPod(file, entry.tocIndex);
|
||||
return entry;
|
||||
}
|
||||
|
||||
BookMetadataCache::TocEntry BookMetadataCache::readTocEntry(File& file) const {
|
||||
TocEntry entry;
|
||||
serialization::readString(file, entry.title);
|
||||
serialization::readString(file, entry.href);
|
||||
serialization::readString(file, entry.anchor);
|
||||
serialization::readPod(file, entry.level);
|
||||
serialization::readPod(file, entry.spineIndex);
|
||||
return entry;
|
||||
}
|
||||
87
lib/Epub/Epub/BookMetadataCache.h
Normal file
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
|
||||
#include <SD.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
class BookMetadataCache {
|
||||
public:
|
||||
struct BookMetadata {
|
||||
std::string title;
|
||||
std::string author;
|
||||
std::string coverItemHref;
|
||||
};
|
||||
|
||||
struct SpineEntry {
|
||||
std::string href;
|
||||
size_t cumulativeSize;
|
||||
int16_t tocIndex;
|
||||
|
||||
SpineEntry() : cumulativeSize(0), tocIndex(-1) {}
|
||||
SpineEntry(std::string href, const size_t cumulativeSize, const int16_t tocIndex)
|
||||
: href(std::move(href)), cumulativeSize(cumulativeSize), tocIndex(tocIndex) {}
|
||||
};
|
||||
|
||||
struct TocEntry {
|
||||
std::string title;
|
||||
std::string href;
|
||||
std::string anchor;
|
||||
uint8_t level;
|
||||
int16_t spineIndex;
|
||||
|
||||
TocEntry() : level(0), spineIndex(-1) {}
|
||||
TocEntry(std::string title, std::string href, std::string anchor, const uint8_t level, const int16_t spineIndex)
|
||||
: title(std::move(title)),
|
||||
href(std::move(href)),
|
||||
anchor(std::move(anchor)),
|
||||
level(level),
|
||||
spineIndex(spineIndex) {}
|
||||
};
|
||||
|
||||
private:
|
||||
std::string cachePath;
|
||||
size_t lutOffset;
|
||||
uint16_t spineCount;
|
||||
uint16_t tocCount;
|
||||
bool loaded;
|
||||
bool buildMode;
|
||||
|
||||
File bookFile;
|
||||
// Temp file handles during build
|
||||
File spineFile;
|
||||
File tocFile;
|
||||
|
||||
size_t writeSpineEntry(File& file, const SpineEntry& entry) const;
|
||||
size_t writeTocEntry(File& file, const TocEntry& entry) const;
|
||||
SpineEntry readSpineEntry(File& file) const;
|
||||
TocEntry readTocEntry(File& file) const;
|
||||
|
||||
public:
|
||||
BookMetadata coreMetadata;
|
||||
|
||||
explicit BookMetadataCache(std::string cachePath)
|
||||
: cachePath(std::move(cachePath)), lutOffset(0), spineCount(0), tocCount(0), loaded(false), buildMode(false) {}
|
||||
~BookMetadataCache() = default;
|
||||
|
||||
// Building phase (stream to disk immediately)
|
||||
bool beginWrite();
|
||||
bool beginContentOpfPass();
|
||||
void createSpineEntry(const std::string& href);
|
||||
bool endContentOpfPass();
|
||||
bool beginTocPass();
|
||||
void createTocEntry(const std::string& title, const std::string& href, const std::string& anchor, uint8_t level);
|
||||
bool endTocPass();
|
||||
bool endWrite();
|
||||
bool cleanupTmpFiles() const;
|
||||
|
||||
// Post-processing to update mappings and sizes
|
||||
bool buildBookBin(const std::string& epubPath, const BookMetadata& metadata);
|
||||
|
||||
// Reading phase (read mode)
|
||||
bool load();
|
||||
SpineEntry getSpineEntry(int index);
|
||||
TocEntry getTocEntry(int index);
|
||||
int getSpineCount() const { return spineCount; }
|
||||
int getTocCount() const { return tocCount; }
|
||||
bool isLoaded() const { return loaded; }
|
||||
};
|
||||
@@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
class EpubTocEntry {
|
||||
public:
|
||||
std::string title;
|
||||
std::string href;
|
||||
std::string anchor;
|
||||
int level;
|
||||
EpubTocEntry(std::string title, std::string href, std::string anchor, const int level)
|
||||
: title(std::move(title)), href(std::move(href)), anchor(std::move(anchor)), level(level) {}
|
||||
};
|
||||
@@ -2,6 +2,26 @@
|
||||
|
||||
#include <SD.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
bool FsHelpers::openFileForRead(const char* moduleName, const std::string& path, File& file) {
|
||||
file = SD.open(path.c_str(), FILE_READ);
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [%s] Failed to open file for reading: %s\n", millis(), moduleName, path.c_str());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FsHelpers::openFileForWrite(const char* moduleName, const std::string& path, File& file) {
|
||||
file = SD.open(path.c_str(), FILE_WRITE, true);
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [%s] Failed to open file for writing: %s\n", millis(), moduleName, path.c_str());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FsHelpers::removeDir(const char* path) {
|
||||
// 1. Open the directory
|
||||
File dir = SD.open(path);
|
||||
@@ -34,3 +54,39 @@ bool FsHelpers::removeDir(const char* path) {
|
||||
|
||||
return SD.rmdir(path);
|
||||
}
|
||||
|
||||
std::string FsHelpers::normalisePath(const std::string& path) {
|
||||
std::vector<std::string> components;
|
||||
std::string component;
|
||||
|
||||
for (const auto c : path) {
|
||||
if (c == '/') {
|
||||
if (!component.empty()) {
|
||||
if (component == "..") {
|
||||
if (!components.empty()) {
|
||||
components.pop_back();
|
||||
}
|
||||
} else {
|
||||
components.push_back(component);
|
||||
}
|
||||
component.clear();
|
||||
}
|
||||
} else {
|
||||
component += c;
|
||||
}
|
||||
}
|
||||
|
||||
if (!component.empty()) {
|
||||
components.push_back(component);
|
||||
}
|
||||
|
||||
std::string result;
|
||||
for (const auto& c : components) {
|
||||
if (!result.empty()) {
|
||||
result += "/";
|
||||
}
|
||||
result += c;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
#pragma once
|
||||
#include <FS.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
class FsHelpers {
|
||||
public:
|
||||
static bool openFileForRead(const char* moduleName, const std::string& path, File& file);
|
||||
static bool openFileForWrite(const char* moduleName, const std::string& path, File& file);
|
||||
static bool removeDir(const char* path);
|
||||
static std::string normalisePath(const std::string& path);
|
||||
};
|
||||
|
||||
@@ -7,48 +7,50 @@ namespace {
|
||||
constexpr uint8_t PAGE_FILE_VERSION = 3;
|
||||
}
|
||||
|
||||
void PageLine::render(GfxRenderer& renderer, const int fontId) { block->render(renderer, fontId, xPos, yPos); }
|
||||
|
||||
void PageLine::serialize(std::ostream& os) {
|
||||
serialization::writePod(os, xPos);
|
||||
serialization::writePod(os, yPos);
|
||||
|
||||
// serialize TextBlock pointed to by PageLine
|
||||
block->serialize(os);
|
||||
void PageLine::render(GfxRenderer& renderer, const int fontId, const int xOffset, const int yOffset) {
|
||||
block->render(renderer, fontId, xPos + xOffset, yPos + yOffset);
|
||||
}
|
||||
|
||||
std::unique_ptr<PageLine> PageLine::deserialize(std::istream& is) {
|
||||
void PageLine::serialize(File& file) {
|
||||
serialization::writePod(file, xPos);
|
||||
serialization::writePod(file, yPos);
|
||||
|
||||
// serialize TextBlock pointed to by PageLine
|
||||
block->serialize(file);
|
||||
}
|
||||
|
||||
std::unique_ptr<PageLine> PageLine::deserialize(File& file) {
|
||||
int16_t xPos;
|
||||
int16_t yPos;
|
||||
serialization::readPod(is, xPos);
|
||||
serialization::readPod(is, yPos);
|
||||
serialization::readPod(file, xPos);
|
||||
serialization::readPod(file, yPos);
|
||||
|
||||
auto tb = TextBlock::deserialize(is);
|
||||
auto tb = TextBlock::deserialize(file);
|
||||
return std::unique_ptr<PageLine>(new PageLine(std::move(tb), xPos, yPos));
|
||||
}
|
||||
|
||||
void Page::render(GfxRenderer& renderer, const int fontId) const {
|
||||
void Page::render(GfxRenderer& renderer, const int fontId, const int xOffset, const int yOffset) const {
|
||||
for (auto& element : elements) {
|
||||
element->render(renderer, fontId);
|
||||
element->render(renderer, fontId, xOffset, yOffset);
|
||||
}
|
||||
}
|
||||
|
||||
void Page::serialize(std::ostream& os) const {
|
||||
serialization::writePod(os, PAGE_FILE_VERSION);
|
||||
void Page::serialize(File& file) const {
|
||||
serialization::writePod(file, PAGE_FILE_VERSION);
|
||||
|
||||
const uint32_t count = elements.size();
|
||||
serialization::writePod(os, count);
|
||||
serialization::writePod(file, count);
|
||||
|
||||
for (const auto& el : elements) {
|
||||
// Only PageLine exists currently
|
||||
serialization::writePod(os, static_cast<uint8_t>(TAG_PageLine));
|
||||
el->serialize(os);
|
||||
serialization::writePod(file, static_cast<uint8_t>(TAG_PageLine));
|
||||
el->serialize(file);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Page> Page::deserialize(std::istream& is) {
|
||||
std::unique_ptr<Page> Page::deserialize(File& file) {
|
||||
uint8_t version;
|
||||
serialization::readPod(is, version);
|
||||
serialization::readPod(file, version);
|
||||
if (version != PAGE_FILE_VERSION) {
|
||||
Serial.printf("[%lu] [PGE] Deserialization failed: Unknown version %u\n", millis(), version);
|
||||
return nullptr;
|
||||
@@ -57,14 +59,14 @@ std::unique_ptr<Page> Page::deserialize(std::istream& is) {
|
||||
auto page = std::unique_ptr<Page>(new Page());
|
||||
|
||||
uint32_t count;
|
||||
serialization::readPod(is, count);
|
||||
serialization::readPod(file, count);
|
||||
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
uint8_t tag;
|
||||
serialization::readPod(is, tag);
|
||||
serialization::readPod(file, tag);
|
||||
|
||||
if (tag == TAG_PageLine) {
|
||||
auto pl = PageLine::deserialize(is);
|
||||
auto pl = PageLine::deserialize(file);
|
||||
page->elements.push_back(std::move(pl));
|
||||
} else {
|
||||
Serial.printf("[%lu] [PGE] Deserialization failed: Unknown tag %u\n", millis(), tag);
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#pragma once
|
||||
#include <FS.h>
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -15,8 +17,8 @@ class PageElement {
|
||||
int16_t yPos;
|
||||
explicit PageElement(const int16_t xPos, const int16_t yPos) : xPos(xPos), yPos(yPos) {}
|
||||
virtual ~PageElement() = default;
|
||||
virtual void render(GfxRenderer& renderer, int fontId) = 0;
|
||||
virtual void serialize(std::ostream& os) = 0;
|
||||
virtual void render(GfxRenderer& renderer, int fontId, int xOffset, int yOffset) = 0;
|
||||
virtual void serialize(File& file) = 0;
|
||||
};
|
||||
|
||||
// a line from a block element
|
||||
@@ -26,16 +28,16 @@ class PageLine final : public PageElement {
|
||||
public:
|
||||
PageLine(std::shared_ptr<TextBlock> block, const int16_t xPos, const int16_t yPos)
|
||||
: PageElement(xPos, yPos), block(std::move(block)) {}
|
||||
void render(GfxRenderer& renderer, int fontId) override;
|
||||
void serialize(std::ostream& os) override;
|
||||
static std::unique_ptr<PageLine> deserialize(std::istream& is);
|
||||
void render(GfxRenderer& renderer, int fontId, int xOffset, int yOffset) override;
|
||||
void serialize(File& file) override;
|
||||
static std::unique_ptr<PageLine> deserialize(File& file);
|
||||
};
|
||||
|
||||
class Page {
|
||||
public:
|
||||
// the list of block index and line numbers on this page
|
||||
std::vector<std::shared_ptr<PageElement>> elements;
|
||||
void render(GfxRenderer& renderer, int fontId) const;
|
||||
void serialize(std::ostream& os) const;
|
||||
static std::unique_ptr<Page> deserialize(std::istream& is);
|
||||
void render(GfxRenderer& renderer, int fontId, int xOffset, int yOffset) const;
|
||||
void serialize(File& file) const;
|
||||
static std::unique_ptr<Page> deserialize(File& file);
|
||||
};
|
||||
|
||||
@@ -18,21 +18,36 @@ void ParsedText::addWord(std::string word, const EpdFontStyle fontStyle) {
|
||||
}
|
||||
|
||||
// Consumes data to minimize memory usage
|
||||
void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fontId, const int horizontalMargin,
|
||||
const std::function<void(std::shared_ptr<TextBlock>)>& processLine) {
|
||||
void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fontId, const int viewportWidth,
|
||||
const std::function<void(std::shared_ptr<TextBlock>)>& processLine,
|
||||
const bool includeLastLine) {
|
||||
if (words.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t totalWordCount = words.size();
|
||||
const int pageWidth = renderer.getScreenWidth() - horizontalMargin;
|
||||
const int pageWidth = viewportWidth;
|
||||
const int spaceWidth = renderer.getSpaceWidth(fontId);
|
||||
// width of 1em to indent first line of paragraph if Extra Spacing is enabled
|
||||
const int indentWidth = (!extraParagraphSpacing) ? 1 * renderer.getTextWidth(fontId, "m", REGULAR) : 0;
|
||||
const auto wordWidths = calculateWordWidths(renderer, fontId);
|
||||
const auto lineBreakIndices = computeLineBreaks(pageWidth, spaceWidth, wordWidths);
|
||||
const size_t lineCount = includeLastLine ? lineBreakIndices.size() : lineBreakIndices.size() - 1;
|
||||
|
||||
for (size_t i = 0; i < lineCount; ++i) {
|
||||
extractLine(i, pageWidth, spaceWidth, wordWidths, lineBreakIndices, processLine);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint16_t> ParsedText::calculateWordWidths(const GfxRenderer& renderer, const int fontId) {
|
||||
const size_t totalWordCount = words.size();
|
||||
|
||||
std::vector<uint16_t> wordWidths;
|
||||
wordWidths.reserve(totalWordCount);
|
||||
|
||||
// add em-space at the beginning of first word in paragraph to indent
|
||||
if (!extraParagraphSpacing) {
|
||||
std::string& first_word = words.front();
|
||||
first_word.insert(0, "\xe2\x80\x83");
|
||||
}
|
||||
|
||||
auto wordsIt = words.begin();
|
||||
auto wordStylesIt = wordStyles.begin();
|
||||
|
||||
@@ -43,6 +58,13 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo
|
||||
std::advance(wordStylesIt, 1);
|
||||
}
|
||||
|
||||
return wordWidths;
|
||||
}
|
||||
|
||||
std::vector<size_t> ParsedText::computeLineBreaks(const int pageWidth, const int spaceWidth,
|
||||
const std::vector<uint16_t>& wordWidths) const {
|
||||
const size_t totalWordCount = words.size();
|
||||
|
||||
// DP table to store the minimum badness (cost) of lines starting at index i
|
||||
std::vector<int> dp(totalWordCount);
|
||||
// 'ans[i]' stores the index 'j' of the *last word* in the optimal line starting at 'i'
|
||||
@@ -53,7 +75,7 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo
|
||||
ans[totalWordCount - 1] = totalWordCount - 1;
|
||||
|
||||
for (int i = totalWordCount - 2; i >= 0; --i) {
|
||||
int currlen = -spaceWidth + indentWidth;
|
||||
int currlen = -spaceWidth;
|
||||
dp[i] = MAX_COST;
|
||||
|
||||
for (size_t j = i; j < totalWordCount; ++j) {
|
||||
@@ -84,88 +106,90 @@ void ParsedText::layoutAndExtractLines(const GfxRenderer& renderer, const int fo
|
||||
ans[i] = j; // j is the index of the last word in this optimal line
|
||||
}
|
||||
}
|
||||
|
||||
// Handle oversized word: if no valid configuration found, force single-word line
|
||||
// This prevents cascade failure where one oversized word breaks all preceding words
|
||||
if (dp[i] == MAX_COST) {
|
||||
ans[i] = i; // Just this word on its own line
|
||||
// Inherit cost from next word to allow subsequent words to find valid configurations
|
||||
if (i + 1 < static_cast<int>(totalWordCount)) {
|
||||
dp[i] = dp[i + 1];
|
||||
} else {
|
||||
dp[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stores the index of the word that starts the next line (last_word_index + 1)
|
||||
std::vector<size_t> lineBreakIndices;
|
||||
size_t currentWordIndex = 0;
|
||||
constexpr size_t MAX_LINES = 1000;
|
||||
|
||||
while (currentWordIndex < totalWordCount) {
|
||||
if (lineBreakIndices.size() >= MAX_LINES) {
|
||||
break;
|
||||
size_t nextBreakIndex = ans[currentWordIndex] + 1;
|
||||
|
||||
// Safety check: prevent infinite loop if nextBreakIndex doesn't advance
|
||||
if (nextBreakIndex <= currentWordIndex) {
|
||||
// Force advance by at least one word to avoid infinite loop
|
||||
nextBreakIndex = currentWordIndex + 1;
|
||||
}
|
||||
|
||||
size_t nextBreakIndex = ans[currentWordIndex] + 1;
|
||||
lineBreakIndices.push_back(nextBreakIndex);
|
||||
|
||||
currentWordIndex = nextBreakIndex;
|
||||
}
|
||||
|
||||
// Initialize iterators for consumption
|
||||
auto wordStartIt = words.begin();
|
||||
auto wordStyleStartIt = wordStyles.begin();
|
||||
size_t wordWidthIndex = 0;
|
||||
|
||||
size_t lastBreakAt = 0;
|
||||
for (const size_t lineBreak : lineBreakIndices) {
|
||||
const size_t lineWordCount = lineBreak - lastBreakAt;
|
||||
|
||||
// Calculate end iterators for the range to splice
|
||||
auto wordEndIt = wordStartIt;
|
||||
auto wordStyleEndIt = wordStyleStartIt;
|
||||
std::advance(wordEndIt, lineWordCount);
|
||||
std::advance(wordStyleEndIt, lineWordCount);
|
||||
|
||||
// Calculate total word width for this line
|
||||
int lineWordWidthSum = 0;
|
||||
for (size_t i = 0; i < lineWordCount; ++i) {
|
||||
lineWordWidthSum += wordWidths[wordWidthIndex + i];
|
||||
}
|
||||
|
||||
// Calculate spacing
|
||||
int spareSpace = pageWidth - lineWordWidthSum;
|
||||
if (wordWidthIndex == 0) {
|
||||
spareSpace -= indentWidth;
|
||||
}
|
||||
|
||||
int spacing = spaceWidth;
|
||||
const bool isLastLine = lineBreak == totalWordCount;
|
||||
|
||||
if (style == TextBlock::JUSTIFIED && !isLastLine && lineWordCount >= 2) {
|
||||
spacing = spareSpace / (lineWordCount - 1);
|
||||
}
|
||||
|
||||
// Calculate initial x position
|
||||
uint16_t xpos = (wordWidthIndex == 0) ? indentWidth : 0;
|
||||
|
||||
if (style == TextBlock::RIGHT_ALIGN) {
|
||||
xpos = spareSpace - (lineWordCount - 1) * spaceWidth;
|
||||
} else if (style == TextBlock::CENTER_ALIGN) {
|
||||
xpos = (spareSpace - (lineWordCount - 1) * spaceWidth) / 2;
|
||||
}
|
||||
|
||||
// Pre-calculate X positions for words
|
||||
std::list<uint16_t> lineXPos;
|
||||
for (size_t i = 0; i < lineWordCount; ++i) {
|
||||
const uint16_t currentWordWidth = wordWidths[wordWidthIndex + i];
|
||||
lineXPos.push_back(xpos);
|
||||
xpos += currentWordWidth + spacing;
|
||||
}
|
||||
|
||||
// *** CRITICAL STEP: CONSUME DATA USING SPLICE ***
|
||||
std::list<std::string> lineWords;
|
||||
lineWords.splice(lineWords.begin(), words, wordStartIt, wordEndIt);
|
||||
std::list<EpdFontStyle> lineWordStyles;
|
||||
lineWordStyles.splice(lineWordStyles.begin(), wordStyles, wordStyleStartIt, wordStyleEndIt);
|
||||
|
||||
processLine(
|
||||
std::make_shared<TextBlock>(std::move(lineWords), std::move(lineXPos), std::move(lineWordStyles), style));
|
||||
|
||||
// Update pointers/indices for the next line
|
||||
wordStartIt = wordEndIt;
|
||||
wordStyleStartIt = wordStyleEndIt;
|
||||
wordWidthIndex += lineWordCount;
|
||||
lastBreakAt = lineBreak;
|
||||
}
|
||||
return lineBreakIndices;
|
||||
}
|
||||
|
||||
void ParsedText::extractLine(const size_t breakIndex, const int pageWidth, const int spaceWidth,
|
||||
const std::vector<uint16_t>& wordWidths, const std::vector<size_t>& lineBreakIndices,
|
||||
const std::function<void(std::shared_ptr<TextBlock>)>& processLine) {
|
||||
const size_t lineBreak = lineBreakIndices[breakIndex];
|
||||
const size_t lastBreakAt = breakIndex > 0 ? lineBreakIndices[breakIndex - 1] : 0;
|
||||
const size_t lineWordCount = lineBreak - lastBreakAt;
|
||||
|
||||
// Calculate total word width for this line
|
||||
int lineWordWidthSum = 0;
|
||||
for (size_t i = lastBreakAt; i < lineBreak; i++) {
|
||||
lineWordWidthSum += wordWidths[i];
|
||||
}
|
||||
|
||||
// Calculate spacing
|
||||
const int spareSpace = pageWidth - lineWordWidthSum;
|
||||
|
||||
int spacing = spaceWidth;
|
||||
const bool isLastLine = breakIndex == lineBreakIndices.size() - 1;
|
||||
|
||||
if (style == TextBlock::JUSTIFIED && !isLastLine && lineWordCount >= 2) {
|
||||
spacing = spareSpace / (lineWordCount - 1);
|
||||
}
|
||||
|
||||
// Calculate initial x position
|
||||
uint16_t xpos = 0;
|
||||
if (style == TextBlock::RIGHT_ALIGN) {
|
||||
xpos = spareSpace - (lineWordCount - 1) * spaceWidth;
|
||||
} else if (style == TextBlock::CENTER_ALIGN) {
|
||||
xpos = (spareSpace - (lineWordCount - 1) * spaceWidth) / 2;
|
||||
}
|
||||
|
||||
// Pre-calculate X positions for words
|
||||
std::list<uint16_t> lineXPos;
|
||||
for (size_t i = lastBreakAt; i < lineBreak; i++) {
|
||||
const uint16_t currentWordWidth = wordWidths[i];
|
||||
lineXPos.push_back(xpos);
|
||||
xpos += currentWordWidth + spacing;
|
||||
}
|
||||
|
||||
// Iterators always start at the beginning as we are moving content with splice below
|
||||
auto wordEndIt = words.begin();
|
||||
auto wordStyleEndIt = wordStyles.begin();
|
||||
std::advance(wordEndIt, lineWordCount);
|
||||
std::advance(wordStyleEndIt, lineWordCount);
|
||||
|
||||
// *** CRITICAL STEP: CONSUME DATA USING SPLICE ***
|
||||
std::list<std::string> lineWords;
|
||||
lineWords.splice(lineWords.begin(), words, words.begin(), wordEndIt);
|
||||
std::list<EpdFontStyle> lineWordStyles;
|
||||
lineWordStyles.splice(lineWordStyles.begin(), wordStyles, wordStyles.begin(), wordStyleEndIt);
|
||||
|
||||
processLine(std::make_shared<TextBlock>(std::move(lineWords), std::move(lineXPos), std::move(lineWordStyles), style));
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
#include <EpdFontFamily.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "blocks/TextBlock.h"
|
||||
|
||||
@@ -18,6 +18,12 @@ class ParsedText {
|
||||
TextBlock::BLOCK_STYLE style;
|
||||
bool extraParagraphSpacing;
|
||||
|
||||
std::vector<size_t> computeLineBreaks(int pageWidth, int spaceWidth, const std::vector<uint16_t>& wordWidths) const;
|
||||
void extractLine(size_t breakIndex, int pageWidth, int spaceWidth, const std::vector<uint16_t>& wordWidths,
|
||||
const std::vector<size_t>& lineBreakIndices,
|
||||
const std::function<void(std::shared_ptr<TextBlock>)>& processLine);
|
||||
std::vector<uint16_t> calculateWordWidths(const GfxRenderer& renderer, int fontId);
|
||||
|
||||
public:
|
||||
explicit ParsedText(const TextBlock::BLOCK_STYLE style, const bool extraParagraphSpacing)
|
||||
: style(style), extraParagraphSpacing(extraParagraphSpacing) {}
|
||||
@@ -26,7 +32,9 @@ class ParsedText {
|
||||
void addWord(std::string word, EpdFontStyle fontStyle);
|
||||
void setStyle(const TextBlock::BLOCK_STYLE style) { this->style = style; }
|
||||
TextBlock::BLOCK_STYLE getStyle() const { return style; }
|
||||
size_t size() const { return words.size(); }
|
||||
bool isEmpty() const { return words.empty(); }
|
||||
void layoutAndExtractLines(const GfxRenderer& renderer, int fontId, int horizontalMargin,
|
||||
const std::function<void(std::shared_ptr<TextBlock>)>& processLine);
|
||||
void layoutAndExtractLines(const GfxRenderer& renderer, int fontId, int viewportWidth,
|
||||
const std::function<void(std::shared_ptr<TextBlock>)>& processLine,
|
||||
bool includeLastLine = true);
|
||||
};
|
||||
|
||||
@@ -1,22 +1,23 @@
|
||||
#include "Section.h"
|
||||
|
||||
#include <FsHelpers.h>
|
||||
#include <SD.h>
|
||||
#include <Serialization.h>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "FsHelpers.h"
|
||||
#include "Page.h"
|
||||
#include "parsers/ChapterHtmlSlimParser.h"
|
||||
|
||||
namespace {
|
||||
constexpr uint8_t SECTION_FILE_VERSION = 5;
|
||||
}
|
||||
constexpr uint8_t SECTION_FILE_VERSION = 6;
|
||||
} // namespace
|
||||
|
||||
void Section::onPageComplete(std::unique_ptr<Page> page) {
|
||||
const auto filePath = cachePath + "/page_" + std::to_string(pageCount) + ".bin";
|
||||
|
||||
std::ofstream outputFile("/sd" + filePath);
|
||||
File outputFile;
|
||||
if (!FsHelpers::openFileForWrite("SCT", filePath, outputFile)) {
|
||||
return;
|
||||
}
|
||||
page->serialize(outputFile);
|
||||
outputFile.close();
|
||||
|
||||
@@ -25,36 +26,30 @@ void Section::onPageComplete(std::unique_ptr<Page> page) {
|
||||
pageCount++;
|
||||
}
|
||||
|
||||
void Section::writeCacheMetadata(const int fontId, const float lineCompression, const int marginTop,
|
||||
const int marginRight, const int marginBottom, const int marginLeft,
|
||||
const bool extraParagraphSpacing) const {
|
||||
std::ofstream outputFile(("/sd" + cachePath + "/section.bin").c_str());
|
||||
void Section::writeCacheMetadata(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
|
||||
const int viewportWidth, const int viewportHeight) const {
|
||||
File outputFile;
|
||||
if (!FsHelpers::openFileForWrite("SCT", cachePath + "/section.bin", outputFile)) {
|
||||
return;
|
||||
}
|
||||
serialization::writePod(outputFile, SECTION_FILE_VERSION);
|
||||
serialization::writePod(outputFile, fontId);
|
||||
serialization::writePod(outputFile, lineCompression);
|
||||
serialization::writePod(outputFile, marginTop);
|
||||
serialization::writePod(outputFile, marginRight);
|
||||
serialization::writePod(outputFile, marginBottom);
|
||||
serialization::writePod(outputFile, marginLeft);
|
||||
serialization::writePod(outputFile, extraParagraphSpacing);
|
||||
serialization::writePod(outputFile, viewportWidth);
|
||||
serialization::writePod(outputFile, viewportHeight);
|
||||
serialization::writePod(outputFile, pageCount);
|
||||
outputFile.close();
|
||||
}
|
||||
|
||||
bool Section::loadCacheMetadata(const int fontId, const float lineCompression, const int marginTop,
|
||||
const int marginRight, const int marginBottom, const int marginLeft,
|
||||
const bool extraParagraphSpacing) {
|
||||
if (!SD.exists(cachePath.c_str())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Section::loadCacheMetadata(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
|
||||
const int viewportWidth, const int viewportHeight) {
|
||||
const auto sectionFilePath = cachePath + "/section.bin";
|
||||
if (!SD.exists(sectionFilePath.c_str())) {
|
||||
File inputFile;
|
||||
if (!FsHelpers::openFileForRead("SCT", sectionFilePath, inputFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ifstream inputFile(("/sd" + sectionFilePath).c_str());
|
||||
|
||||
// Match parameters
|
||||
{
|
||||
uint8_t version;
|
||||
@@ -66,20 +61,18 @@ bool Section::loadCacheMetadata(const int fontId, const float lineCompression, c
|
||||
return false;
|
||||
}
|
||||
|
||||
int fileFontId, fileMarginTop, fileMarginRight, fileMarginBottom, fileMarginLeft;
|
||||
int fileFontId, fileViewportWidth, fileViewportHeight;
|
||||
float fileLineCompression;
|
||||
bool fileExtraParagraphSpacing;
|
||||
serialization::readPod(inputFile, fileFontId);
|
||||
serialization::readPod(inputFile, fileLineCompression);
|
||||
serialization::readPod(inputFile, fileMarginTop);
|
||||
serialization::readPod(inputFile, fileMarginRight);
|
||||
serialization::readPod(inputFile, fileMarginBottom);
|
||||
serialization::readPod(inputFile, fileMarginLeft);
|
||||
serialization::readPod(inputFile, fileExtraParagraphSpacing);
|
||||
serialization::readPod(inputFile, fileViewportWidth);
|
||||
serialization::readPod(inputFile, fileViewportHeight);
|
||||
|
||||
if (fontId != fileFontId || lineCompression != fileLineCompression || marginTop != fileMarginTop ||
|
||||
marginRight != fileMarginRight || marginBottom != fileMarginBottom || marginLeft != fileMarginLeft ||
|
||||
extraParagraphSpacing != fileExtraParagraphSpacing) {
|
||||
if (fontId != fileFontId || lineCompression != fileLineCompression ||
|
||||
extraParagraphSpacing != fileExtraParagraphSpacing || viewportWidth != fileViewportWidth ||
|
||||
viewportHeight != fileViewportHeight) {
|
||||
inputFile.close();
|
||||
Serial.printf("[%lu] [SCT] Deserialization failed: Parameters do not match\n", millis());
|
||||
clearCache();
|
||||
@@ -114,31 +107,58 @@ bool Section::clearCache() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Section::persistPageDataToSD(const int fontId, const float lineCompression, const int marginTop,
|
||||
const int marginRight, const int marginBottom, const int marginLeft,
|
||||
const bool extraParagraphSpacing) {
|
||||
const auto localPath = epub->getSpineItem(spineIndex);
|
||||
|
||||
// TODO: Should we get rid of this file all together?
|
||||
// It currently saves us a bit of memory by allowing for all the inflation bits to be released
|
||||
// before loading the XML parser
|
||||
bool Section::persistPageDataToSD(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
|
||||
const int viewportWidth, const int viewportHeight,
|
||||
const std::function<void()>& progressSetupFn,
|
||||
const std::function<void(int)>& progressFn) {
|
||||
constexpr size_t MIN_SIZE_FOR_PROGRESS = 50 * 1024; // 50KB
|
||||
const auto localPath = epub->getSpineItem(spineIndex).href;
|
||||
const auto tmpHtmlPath = epub->getCachePath() + "/.tmp_" + std::to_string(spineIndex) + ".html";
|
||||
File f = SD.open(tmpHtmlPath.c_str(), FILE_WRITE, true);
|
||||
bool success = epub->readItemContentsToStream(localPath, f, 1024);
|
||||
f.close();
|
||||
|
||||
// Retry logic for SD card timing issues
|
||||
bool success = false;
|
||||
size_t fileSize = 0;
|
||||
for (int attempt = 0; attempt < 3 && !success; attempt++) {
|
||||
if (attempt > 0) {
|
||||
Serial.printf("[%lu] [SCT] Retrying stream (attempt %d)...\n", millis(), attempt + 1);
|
||||
delay(50); // Brief delay before retry
|
||||
}
|
||||
|
||||
// Remove any incomplete file from previous attempt before retrying
|
||||
if (SD.exists(tmpHtmlPath.c_str())) {
|
||||
SD.remove(tmpHtmlPath.c_str());
|
||||
}
|
||||
|
||||
File tmpHtml;
|
||||
if (!FsHelpers::openFileForWrite("SCT", tmpHtmlPath, tmpHtml)) {
|
||||
continue;
|
||||
}
|
||||
success = epub->readItemContentsToStream(localPath, tmpHtml, 1024);
|
||||
fileSize = tmpHtml.size();
|
||||
tmpHtml.close();
|
||||
|
||||
// If streaming failed, remove the incomplete file immediately
|
||||
if (!success && SD.exists(tmpHtmlPath.c_str())) {
|
||||
SD.remove(tmpHtmlPath.c_str());
|
||||
Serial.printf("[%lu] [SCT] Removed incomplete temp file after failed attempt\n", millis());
|
||||
}
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
Serial.printf("[%lu] [SCT] Failed to stream item contents to temp file\n", millis());
|
||||
Serial.printf("[%lu] [SCT] Failed to stream item contents to temp file after retries\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [SCT] Streamed temp HTML to %s\n", millis(), tmpHtmlPath.c_str());
|
||||
Serial.printf("[%lu] [SCT] Streamed temp HTML to %s (%d bytes)\n", millis(), tmpHtmlPath.c_str(), fileSize);
|
||||
|
||||
const auto sdTmpHtmlPath = "/sd" + tmpHtmlPath;
|
||||
// Only show progress bar for larger chapters where rendering overhead is worth it
|
||||
if (progressSetupFn && fileSize >= MIN_SIZE_FOR_PROGRESS) {
|
||||
progressSetupFn();
|
||||
}
|
||||
|
||||
ChapterHtmlSlimParser visitor(sdTmpHtmlPath.c_str(), renderer, fontId, lineCompression, marginTop, marginRight,
|
||||
marginBottom, marginLeft, extraParagraphSpacing,
|
||||
[this](std::unique_ptr<Page> page) { this->onPageComplete(std::move(page)); });
|
||||
ChapterHtmlSlimParser visitor(
|
||||
tmpHtmlPath, renderer, fontId, lineCompression, extraParagraphSpacing, viewportWidth, viewportHeight,
|
||||
[this](std::unique_ptr<Page> page) { this->onPageComplete(std::move(page)); }, progressFn);
|
||||
success = visitor.parseAndBuildPages();
|
||||
|
||||
SD.remove(tmpHtmlPath.c_str());
|
||||
@@ -147,19 +167,18 @@ bool Section::persistPageDataToSD(const int fontId, const float lineCompression,
|
||||
return false;
|
||||
}
|
||||
|
||||
writeCacheMetadata(fontId, lineCompression, marginTop, marginRight, marginBottom, marginLeft, extraParagraphSpacing);
|
||||
writeCacheMetadata(fontId, lineCompression, extraParagraphSpacing, viewportWidth, viewportHeight);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::unique_ptr<Page> Section::loadPageFromSD() const {
|
||||
const auto filePath = "/sd" + cachePath + "/page_" + std::to_string(currentPage) + ".bin";
|
||||
if (!SD.exists(filePath.c_str() + 3)) {
|
||||
Serial.printf("[%lu] [SCT] Page file does not exist: %s\n", millis(), filePath.c_str());
|
||||
const auto filePath = cachePath + "/page_" + std::to_string(currentPage) + ".bin";
|
||||
|
||||
File inputFile;
|
||||
if (!FsHelpers::openFileForRead("SCT", filePath, inputFile)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::ifstream inputFile(filePath);
|
||||
auto page = Page::deserialize(inputFile);
|
||||
inputFile.close();
|
||||
return page;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "Epub.h"
|
||||
@@ -12,8 +13,8 @@ class Section {
|
||||
GfxRenderer& renderer;
|
||||
std::string cachePath;
|
||||
|
||||
void writeCacheMetadata(int fontId, float lineCompression, int marginTop, int marginRight, int marginBottom,
|
||||
int marginLeft, bool extraParagraphSpacing) const;
|
||||
void writeCacheMetadata(int fontId, float lineCompression, bool extraParagraphSpacing, int viewportWidth,
|
||||
int viewportHeight) const;
|
||||
void onPageComplete(std::unique_ptr<Page> page);
|
||||
|
||||
public:
|
||||
@@ -21,15 +22,17 @@ class Section {
|
||||
int currentPage = 0;
|
||||
|
||||
explicit Section(const std::shared_ptr<Epub>& epub, const int spineIndex, GfxRenderer& renderer)
|
||||
: epub(epub), spineIndex(spineIndex), renderer(renderer) {
|
||||
cachePath = epub->getCachePath() + "/" + std::to_string(spineIndex);
|
||||
}
|
||||
: epub(epub),
|
||||
spineIndex(spineIndex),
|
||||
renderer(renderer),
|
||||
cachePath(epub->getCachePath() + "/" + std::to_string(spineIndex)) {}
|
||||
~Section() = default;
|
||||
bool loadCacheMetadata(int fontId, float lineCompression, int marginTop, int marginRight, int marginBottom,
|
||||
int marginLeft, bool extraParagraphSpacing);
|
||||
bool loadCacheMetadata(int fontId, float lineCompression, bool extraParagraphSpacing, int viewportWidth,
|
||||
int viewportHeight);
|
||||
void setupCacheDir() const;
|
||||
bool clearCache() const;
|
||||
bool persistPageDataToSD(int fontId, float lineCompression, int marginTop, int marginRight, int marginBottom,
|
||||
int marginLeft, bool extraParagraphSpacing);
|
||||
bool persistPageDataToSD(int fontId, float lineCompression, bool extraParagraphSpacing, int viewportWidth,
|
||||
int viewportHeight, const std::function<void()>& progressSetupFn = nullptr,
|
||||
const std::function<void(int)>& progressFn = nullptr);
|
||||
std::unique_ptr<Page> loadPageFromSD() const;
|
||||
};
|
||||
|
||||
@@ -4,11 +4,18 @@
|
||||
#include <Serialization.h>
|
||||
|
||||
void TextBlock::render(const GfxRenderer& renderer, const int fontId, const int x, const int y) const {
|
||||
// Validate iterator bounds before rendering
|
||||
if (words.size() != wordXpos.size() || words.size() != wordStyles.size()) {
|
||||
Serial.printf("[%lu] [TXB] Render skipped: size mismatch (words=%u, xpos=%u, styles=%u)\n", millis(),
|
||||
(uint32_t)words.size(), (uint32_t)wordXpos.size(), (uint32_t)wordStyles.size());
|
||||
return;
|
||||
}
|
||||
|
||||
auto wordIt = words.begin();
|
||||
auto wordStylesIt = wordStyles.begin();
|
||||
auto wordXposIt = wordXpos.begin();
|
||||
|
||||
for (int i = 0; i < words.size(); i++) {
|
||||
for (size_t i = 0; i < words.size(); i++) {
|
||||
renderer.drawText(fontId, *wordXposIt + x, y, wordIt->c_str(), true, *wordStylesIt);
|
||||
|
||||
std::advance(wordIt, 1);
|
||||
@@ -17,27 +24,27 @@ void TextBlock::render(const GfxRenderer& renderer, const int fontId, const int
|
||||
}
|
||||
}
|
||||
|
||||
void TextBlock::serialize(std::ostream& os) const {
|
||||
void TextBlock::serialize(File& file) const {
|
||||
// words
|
||||
const uint32_t wc = words.size();
|
||||
serialization::writePod(os, wc);
|
||||
for (const auto& w : words) serialization::writeString(os, w);
|
||||
serialization::writePod(file, wc);
|
||||
for (const auto& w : words) serialization::writeString(file, w);
|
||||
|
||||
// wordXpos
|
||||
const uint32_t xc = wordXpos.size();
|
||||
serialization::writePod(os, xc);
|
||||
for (auto x : wordXpos) serialization::writePod(os, x);
|
||||
serialization::writePod(file, xc);
|
||||
for (auto x : wordXpos) serialization::writePod(file, x);
|
||||
|
||||
// wordStyles
|
||||
const uint32_t sc = wordStyles.size();
|
||||
serialization::writePod(os, sc);
|
||||
for (auto s : wordStyles) serialization::writePod(os, s);
|
||||
serialization::writePod(file, sc);
|
||||
for (auto s : wordStyles) serialization::writePod(file, s);
|
||||
|
||||
// style
|
||||
serialization::writePod(os, style);
|
||||
serialization::writePod(file, style);
|
||||
}
|
||||
|
||||
std::unique_ptr<TextBlock> TextBlock::deserialize(std::istream& is) {
|
||||
std::unique_ptr<TextBlock> TextBlock::deserialize(File& file) {
|
||||
uint32_t wc, xc, sc;
|
||||
std::list<std::string> words;
|
||||
std::list<uint16_t> wordXpos;
|
||||
@@ -45,22 +52,36 @@ std::unique_ptr<TextBlock> TextBlock::deserialize(std::istream& is) {
|
||||
BLOCK_STYLE style;
|
||||
|
||||
// words
|
||||
serialization::readPod(is, wc);
|
||||
serialization::readPod(file, wc);
|
||||
|
||||
// Sanity check: prevent allocation of unreasonably large lists (max 10000 words per block)
|
||||
if (wc > 10000) {
|
||||
Serial.printf("[%lu] [TXB] Deserialization failed: word count %u exceeds maximum\n", millis(), wc);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
words.resize(wc);
|
||||
for (auto& w : words) serialization::readString(is, w);
|
||||
for (auto& w : words) serialization::readString(file, w);
|
||||
|
||||
// wordXpos
|
||||
serialization::readPod(is, xc);
|
||||
serialization::readPod(file, xc);
|
||||
wordXpos.resize(xc);
|
||||
for (auto& x : wordXpos) serialization::readPod(is, x);
|
||||
for (auto& x : wordXpos) serialization::readPod(file, x);
|
||||
|
||||
// wordStyles
|
||||
serialization::readPod(is, sc);
|
||||
serialization::readPod(file, sc);
|
||||
wordStyles.resize(sc);
|
||||
for (auto& s : wordStyles) serialization::readPod(is, s);
|
||||
for (auto& s : wordStyles) serialization::readPod(file, s);
|
||||
|
||||
// Validate data consistency: all three lists must have the same size
|
||||
if (wc != xc || wc != sc) {
|
||||
Serial.printf("[%lu] [TXB] Deserialization failed: size mismatch (words=%u, xpos=%u, styles=%u)\n", millis(), wc,
|
||||
xc, sc);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// style
|
||||
serialization::readPod(is, style);
|
||||
serialization::readPod(file, style);
|
||||
|
||||
return std::unique_ptr<TextBlock>(new TextBlock(std::move(words), std::move(wordXpos), std::move(wordStyles), style));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <EpdFontFamily.h>
|
||||
#include <FS.h>
|
||||
|
||||
#include <list>
|
||||
#include <memory>
|
||||
@@ -35,6 +36,6 @@ class TextBlock final : public Block {
|
||||
// given a renderer works out where to break the words into lines
|
||||
void render(const GfxRenderer& renderer, int fontId, int x, int y) const;
|
||||
BlockType getType() override { return TEXT_BLOCK; }
|
||||
void serialize(std::ostream& os) const;
|
||||
static std::unique_ptr<TextBlock> deserialize(std::istream& is);
|
||||
void serialize(File& file) const;
|
||||
static std::unique_ptr<TextBlock> deserialize(File& file);
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "ChapterHtmlSlimParser.h"
|
||||
|
||||
#include <FsHelpers.h>
|
||||
#include <GfxRenderer.h>
|
||||
#include <HardwareSerial.h>
|
||||
#include <expat.h>
|
||||
@@ -10,13 +11,16 @@
|
||||
const char* HEADER_TAGS[] = {"h1", "h2", "h3", "h4", "h5", "h6"};
|
||||
constexpr int NUM_HEADER_TAGS = sizeof(HEADER_TAGS) / sizeof(HEADER_TAGS[0]);
|
||||
|
||||
const char* BLOCK_TAGS[] = {"p", "li", "div", "br"};
|
||||
// Minimum file size (in bytes) to show progress bar - smaller chapters don't benefit from it
|
||||
constexpr size_t MIN_SIZE_FOR_PROGRESS = 50 * 1024; // 50KB
|
||||
|
||||
const char* BLOCK_TAGS[] = {"p", "li", "div", "br", "blockquote"};
|
||||
constexpr int NUM_BLOCK_TAGS = sizeof(BLOCK_TAGS) / sizeof(BLOCK_TAGS[0]);
|
||||
|
||||
const char* BOLD_TAGS[] = {"b"};
|
||||
const char* BOLD_TAGS[] = {"b", "strong"};
|
||||
constexpr int NUM_BOLD_TAGS = sizeof(BOLD_TAGS) / sizeof(BOLD_TAGS[0]);
|
||||
|
||||
const char* ITALIC_TAGS[] = {"i"};
|
||||
const char* ITALIC_TAGS[] = {"i", "em"};
|
||||
constexpr int NUM_ITALIC_TAGS = sizeof(ITALIC_TAGS) / sizeof(ITALIC_TAGS[0]);
|
||||
|
||||
const char* IMAGE_TAGS[] = {"img"};
|
||||
@@ -75,6 +79,18 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
|
||||
return;
|
||||
}
|
||||
|
||||
// Skip blocks with role="doc-pagebreak" and epub:type="pagebreak"
|
||||
if (atts != nullptr) {
|
||||
for (int i = 0; atts[i]; i += 2) {
|
||||
if (strcmp(atts[i], "role") == 0 && strcmp(atts[i + 1], "doc-pagebreak") == 0 ||
|
||||
strcmp(atts[i], "epub:type") == 0 && strcmp(atts[i + 1], "pagebreak") == 0) {
|
||||
self->skipUntilDepth = self->depth;
|
||||
self->depth += 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (matches(name, HEADER_TAGS, NUM_HEADER_TAGS)) {
|
||||
self->startNewTextBlock(TextBlock::CENTER_ALIGN);
|
||||
self->boldUntilDepth = min(self->boldUntilDepth, self->depth);
|
||||
@@ -131,6 +147,17 @@ void XMLCALL ChapterHtmlSlimParser::characterData(void* userData, const XML_Char
|
||||
|
||||
self->partWordBuffer[self->partWordBufferIndex++] = s[i];
|
||||
}
|
||||
|
||||
// If we have > 750 words buffered up, perform the layout and consume out all but the last line
|
||||
// There should be enough here to build out 1-2 full pages and doing this will free up a lot of
|
||||
// memory.
|
||||
// Spotted when reading Intermezzo, there are some really long text blocks in there.
|
||||
if (self->currentTextBlock->size() > 750) {
|
||||
Serial.printf("[%lu] [EHP] Text block too long, splitting into multiple pages\n", millis());
|
||||
self->currentTextBlock->layoutAndExtractLines(
|
||||
self->renderer, self->fontId, self->viewportWidth,
|
||||
[self](const std::shared_ptr<TextBlock>& textBlock) { self->addLineToPage(textBlock); }, false);
|
||||
}
|
||||
}
|
||||
|
||||
void XMLCALL ChapterHtmlSlimParser::endElement(void* userData, const XML_Char* name) {
|
||||
@@ -191,48 +218,75 @@ bool ChapterHtmlSlimParser::parseAndBuildPages() {
|
||||
return false;
|
||||
}
|
||||
|
||||
XML_SetUserData(parser, this);
|
||||
XML_SetElementHandler(parser, startElement, endElement);
|
||||
XML_SetCharacterDataHandler(parser, characterData);
|
||||
|
||||
FILE* file = fopen(filepath, "r");
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [EHP] Couldn't open file %s\n", millis(), filepath);
|
||||
File file;
|
||||
if (!FsHelpers::openFileForRead("EHP", filepath, file)) {
|
||||
XML_ParserFree(parser);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get file size for progress calculation
|
||||
const size_t totalSize = file.size();
|
||||
size_t bytesRead = 0;
|
||||
int lastProgress = -1;
|
||||
|
||||
XML_SetUserData(parser, this);
|
||||
XML_SetElementHandler(parser, startElement, endElement);
|
||||
XML_SetCharacterDataHandler(parser, characterData);
|
||||
|
||||
do {
|
||||
void* const buf = XML_GetBuffer(parser, 1024);
|
||||
if (!buf) {
|
||||
Serial.printf("[%lu] [EHP] Couldn't allocate memory for buffer\n", millis());
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_SetCharacterDataHandler(parser, nullptr);
|
||||
XML_ParserFree(parser);
|
||||
fclose(file);
|
||||
file.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t len = fread(buf, 1, 1024, file);
|
||||
const size_t len = file.read(static_cast<uint8_t*>(buf), 1024);
|
||||
|
||||
if (ferror(file)) {
|
||||
if (len == 0) {
|
||||
Serial.printf("[%lu] [EHP] File read error\n", millis());
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_SetCharacterDataHandler(parser, nullptr);
|
||||
XML_ParserFree(parser);
|
||||
fclose(file);
|
||||
file.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
done = feof(file);
|
||||
// Update progress (call every 10% change to avoid too frequent updates)
|
||||
// Only show progress for larger chapters where rendering overhead is worth it
|
||||
bytesRead += len;
|
||||
if (progressFn && totalSize >= MIN_SIZE_FOR_PROGRESS) {
|
||||
const int progress = static_cast<int>((bytesRead * 100) / totalSize);
|
||||
if (lastProgress / 10 != progress / 10) {
|
||||
lastProgress = progress;
|
||||
progressFn(progress);
|
||||
}
|
||||
}
|
||||
|
||||
done = file.available() == 0;
|
||||
|
||||
if (XML_ParseBuffer(parser, static_cast<int>(len), done) == XML_STATUS_ERROR) {
|
||||
Serial.printf("[%lu] [EHP] Parse error at line %lu:\n%s\n", millis(), XML_GetCurrentLineNumber(parser),
|
||||
XML_ErrorString(XML_GetErrorCode(parser)));
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_SetCharacterDataHandler(parser, nullptr);
|
||||
XML_ParserFree(parser);
|
||||
fclose(file);
|
||||
file.close();
|
||||
return false;
|
||||
}
|
||||
} while (!done);
|
||||
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_SetCharacterDataHandler(parser, nullptr);
|
||||
XML_ParserFree(parser);
|
||||
fclose(file);
|
||||
file.close();
|
||||
|
||||
// Process last page if there is still text
|
||||
if (currentTextBlock) {
|
||||
@@ -247,15 +301,14 @@ bool ChapterHtmlSlimParser::parseAndBuildPages() {
|
||||
|
||||
void ChapterHtmlSlimParser::addLineToPage(std::shared_ptr<TextBlock> line) {
|
||||
const int lineHeight = renderer.getLineHeight(fontId) * lineCompression;
|
||||
const int pageHeight = GfxRenderer::getScreenHeight() - marginTop - marginBottom;
|
||||
|
||||
if (currentPageNextY + lineHeight > pageHeight) {
|
||||
if (currentPageNextY + lineHeight > viewportHeight) {
|
||||
completePageFn(std::move(currentPage));
|
||||
currentPage.reset(new Page());
|
||||
currentPageNextY = marginTop;
|
||||
currentPageNextY = 0;
|
||||
}
|
||||
|
||||
currentPage->elements.push_back(std::make_shared<PageLine>(line, marginLeft, currentPageNextY));
|
||||
currentPage->elements.push_back(std::make_shared<PageLine>(line, 0, currentPageNextY));
|
||||
currentPageNextY += lineHeight;
|
||||
}
|
||||
|
||||
@@ -267,12 +320,12 @@ void ChapterHtmlSlimParser::makePages() {
|
||||
|
||||
if (!currentPage) {
|
||||
currentPage.reset(new Page());
|
||||
currentPageNextY = marginTop;
|
||||
currentPageNextY = 0;
|
||||
}
|
||||
|
||||
const int lineHeight = renderer.getLineHeight(fontId) * lineCompression;
|
||||
currentTextBlock->layoutAndExtractLines(
|
||||
renderer, fontId, marginLeft + marginRight,
|
||||
renderer, fontId, viewportWidth,
|
||||
[this](const std::shared_ptr<TextBlock>& textBlock) { addLineToPage(textBlock); });
|
||||
// Extra paragraph spacing if enabled
|
||||
if (extraParagraphSpacing) {
|
||||
|
||||
@@ -15,9 +15,10 @@ class GfxRenderer;
|
||||
#define MAX_WORD_SIZE 200
|
||||
|
||||
class ChapterHtmlSlimParser {
|
||||
const char* filepath;
|
||||
const std::string& filepath;
|
||||
GfxRenderer& renderer;
|
||||
std::function<void(std::unique_ptr<Page>)> completePageFn;
|
||||
std::function<void(int)> progressFn; // Progress callback (0-100)
|
||||
int depth = 0;
|
||||
int skipUntilDepth = INT_MAX;
|
||||
int boldUntilDepth = INT_MAX;
|
||||
@@ -31,11 +32,9 @@ class ChapterHtmlSlimParser {
|
||||
int16_t currentPageNextY = 0;
|
||||
int fontId;
|
||||
float lineCompression;
|
||||
int marginTop;
|
||||
int marginRight;
|
||||
int marginBottom;
|
||||
int marginLeft;
|
||||
bool extraParagraphSpacing;
|
||||
int viewportWidth;
|
||||
int viewportHeight;
|
||||
|
||||
void startNewTextBlock(TextBlock::BLOCK_STYLE style);
|
||||
void makePages();
|
||||
@@ -45,20 +44,20 @@ class ChapterHtmlSlimParser {
|
||||
static void XMLCALL endElement(void* userData, const XML_Char* name);
|
||||
|
||||
public:
|
||||
explicit ChapterHtmlSlimParser(const char* filepath, GfxRenderer& renderer, const int fontId,
|
||||
const float lineCompression, const int marginTop, const int marginRight,
|
||||
const int marginBottom, const int marginLeft, const bool extraParagraphSpacing,
|
||||
const std::function<void(std::unique_ptr<Page>)>& completePageFn)
|
||||
explicit ChapterHtmlSlimParser(const std::string& filepath, GfxRenderer& renderer, const int fontId,
|
||||
const float lineCompression, const bool extraParagraphSpacing, const int viewportWidth,
|
||||
const int viewportHeight,
|
||||
const std::function<void(std::unique_ptr<Page>)>& completePageFn,
|
||||
const std::function<void(int)>& progressFn = nullptr)
|
||||
: filepath(filepath),
|
||||
renderer(renderer),
|
||||
fontId(fontId),
|
||||
lineCompression(lineCompression),
|
||||
marginTop(marginTop),
|
||||
marginRight(marginRight),
|
||||
marginBottom(marginBottom),
|
||||
marginLeft(marginLeft),
|
||||
extraParagraphSpacing(extraParagraphSpacing),
|
||||
completePageFn(completePageFn) {}
|
||||
viewportWidth(viewportWidth),
|
||||
viewportHeight(viewportHeight),
|
||||
completePageFn(completePageFn),
|
||||
progressFn(progressFn) {}
|
||||
~ChapterHtmlSlimParser() = default;
|
||||
bool parseAndBuildPages();
|
||||
void addLineToPage(std::shared_ptr<TextBlock> line);
|
||||
|
||||
@@ -14,12 +14,13 @@ bool ContainerParser::setup() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ContainerParser::teardown() {
|
||||
ContainerParser::~ContainerParser() {
|
||||
if (parser) {
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_ParserFree(parser);
|
||||
parser = nullptr;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t ContainerParser::write(const uint8_t data) { return write(&data, 1); }
|
||||
|
||||
@@ -23,9 +23,9 @@ class ContainerParser final : public Print {
|
||||
std::string fullPath;
|
||||
|
||||
explicit ContainerParser(const size_t xmlSize) : remainingSize(xmlSize) {}
|
||||
~ContainerParser() override;
|
||||
|
||||
bool setup();
|
||||
bool teardown();
|
||||
|
||||
size_t write(uint8_t) override;
|
||||
size_t write(const uint8_t* buffer, size_t size) override;
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
#include "ContentOpfParser.h"
|
||||
|
||||
#include <FsHelpers.h>
|
||||
#include <HardwareSerial.h>
|
||||
#include <Serialization.h>
|
||||
#include <ZipFile.h>
|
||||
|
||||
#include "../BookMetadataCache.h"
|
||||
|
||||
namespace {
|
||||
constexpr const char MEDIA_TYPE_NCX[] = "application/x-dtbncx+xml";
|
||||
}
|
||||
constexpr char MEDIA_TYPE_NCX[] = "application/x-dtbncx+xml";
|
||||
constexpr char itemCacheFile[] = "/.items.bin";
|
||||
} // namespace
|
||||
|
||||
bool ContentOpfParser::setup() {
|
||||
parser = XML_ParserCreate(nullptr);
|
||||
@@ -20,12 +25,20 @@ bool ContentOpfParser::setup() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ContentOpfParser::teardown() {
|
||||
ContentOpfParser::~ContentOpfParser() {
|
||||
if (parser) {
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_SetCharacterDataHandler(parser, nullptr);
|
||||
XML_ParserFree(parser);
|
||||
parser = nullptr;
|
||||
}
|
||||
return true;
|
||||
if (tempItemStore) {
|
||||
tempItemStore.close();
|
||||
}
|
||||
if (SD.exists((cachePath + itemCacheFile).c_str())) {
|
||||
SD.remove((cachePath + itemCacheFile).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
size_t ContentOpfParser::write(const uint8_t data) { return write(&data, 1); }
|
||||
@@ -41,6 +54,9 @@ size_t ContentOpfParser::write(const uint8_t* buffer, const size_t size) {
|
||||
|
||||
if (!buf) {
|
||||
Serial.printf("[%lu] [COF] Couldn't allocate memory for buffer\n", millis());
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_SetCharacterDataHandler(parser, nullptr);
|
||||
XML_ParserFree(parser);
|
||||
parser = nullptr;
|
||||
return 0;
|
||||
@@ -52,6 +68,9 @@ size_t ContentOpfParser::write(const uint8_t* buffer, const size_t size) {
|
||||
if (XML_ParseBuffer(parser, static_cast<int>(toRead), remainingSize == toRead) == XML_STATUS_ERROR) {
|
||||
Serial.printf("[%lu] [COF] Parse error at line %lu: %s\n", millis(), XML_GetCurrentLineNumber(parser),
|
||||
XML_ErrorString(XML_GetErrorCode(parser)));
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_SetCharacterDataHandler(parser, nullptr);
|
||||
XML_ParserFree(parser);
|
||||
parser = nullptr;
|
||||
return 0;
|
||||
@@ -86,11 +105,21 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
|
||||
|
||||
if (self->state == IN_PACKAGE && (strcmp(name, "manifest") == 0 || strcmp(name, "opf:manifest") == 0)) {
|
||||
self->state = IN_MANIFEST;
|
||||
if (!FsHelpers::openFileForWrite("COF", self->cachePath + itemCacheFile, self->tempItemStore)) {
|
||||
Serial.printf(
|
||||
"[%lu] [COF] Couldn't open temp items file for writing. This is probably going to be a fatal error.\n",
|
||||
millis());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (self->state == IN_PACKAGE && (strcmp(name, "spine") == 0 || strcmp(name, "opf:spine") == 0)) {
|
||||
self->state = IN_SPINE;
|
||||
if (!FsHelpers::openFileForRead("COF", self->cachePath + itemCacheFile, self->tempItemStore)) {
|
||||
Serial.printf(
|
||||
"[%lu] [COF] Couldn't open temp items file for reading. This is probably going to be a fatal error.\n",
|
||||
millis());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -127,7 +156,13 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
|
||||
}
|
||||
}
|
||||
|
||||
self->items[itemId] = href;
|
||||
// Write items down to SD card
|
||||
serialization::writeString(self->tempItemStore, itemId);
|
||||
serialization::writeString(self->tempItemStore, href);
|
||||
|
||||
if (itemId == self->coverItemId) {
|
||||
self->coverItemHref = href;
|
||||
}
|
||||
|
||||
if (mediaType == MEDIA_TYPE_NCX) {
|
||||
if (self->tocNcxPath.empty()) {
|
||||
@@ -140,14 +175,29 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
|
||||
return;
|
||||
}
|
||||
|
||||
if (self->state == IN_SPINE && (strcmp(name, "itemref") == 0 || strcmp(name, "opf:itemref") == 0)) {
|
||||
for (int i = 0; atts[i]; i += 2) {
|
||||
if (strcmp(atts[i], "idref") == 0) {
|
||||
self->spineRefs.emplace_back(atts[i + 1]);
|
||||
break;
|
||||
// NOTE: This relies on spine appearing after item manifest (which is pretty safe as it's part of the EPUB spec)
|
||||
// Only run the spine parsing if there's a cache to add it to
|
||||
if (self->cache) {
|
||||
if (self->state == IN_SPINE && (strcmp(name, "itemref") == 0 || strcmp(name, "opf:itemref") == 0)) {
|
||||
for (int i = 0; atts[i]; i += 2) {
|
||||
if (strcmp(atts[i], "idref") == 0) {
|
||||
const std::string idref = atts[i + 1];
|
||||
// Resolve the idref to href using items map
|
||||
self->tempItemStore.seek(0);
|
||||
std::string itemId;
|
||||
std::string href;
|
||||
while (self->tempItemStore.available()) {
|
||||
serialization::readString(self->tempItemStore, itemId);
|
||||
serialization::readString(self->tempItemStore, href);
|
||||
if (itemId == idref) {
|
||||
self->cache->createSpineEntry(href);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,11 +216,13 @@ void XMLCALL ContentOpfParser::endElement(void* userData, const XML_Char* name)
|
||||
|
||||
if (self->state == IN_SPINE && (strcmp(name, "spine") == 0 || strcmp(name, "opf:spine") == 0)) {
|
||||
self->state = IN_PACKAGE;
|
||||
self->tempItemStore.close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (self->state == IN_MANIFEST && (strcmp(name, "manifest") == 0 || strcmp(name, "opf:manifest") == 0)) {
|
||||
self->state = IN_PACKAGE;
|
||||
self->tempItemStore.close();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
#include <Print.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "Epub.h"
|
||||
#include "expat.h"
|
||||
|
||||
class BookMetadataCache;
|
||||
|
||||
class ContentOpfParser final : public Print {
|
||||
enum ParserState {
|
||||
START,
|
||||
@@ -16,10 +16,14 @@ class ContentOpfParser final : public Print {
|
||||
IN_SPINE,
|
||||
};
|
||||
|
||||
const std::string& cachePath;
|
||||
const std::string& baseContentPath;
|
||||
size_t remainingSize;
|
||||
XML_Parser parser = nullptr;
|
||||
ParserState state = START;
|
||||
BookMetadataCache* cache;
|
||||
File tempItemStore;
|
||||
std::string coverItemId;
|
||||
|
||||
static void startElement(void* userData, const XML_Char* name, const XML_Char** atts);
|
||||
static void characterData(void* userData, const XML_Char* s, int len);
|
||||
@@ -28,15 +32,14 @@ class ContentOpfParser final : public Print {
|
||||
public:
|
||||
std::string title;
|
||||
std::string tocNcxPath;
|
||||
std::string coverItemId;
|
||||
std::map<std::string, std::string> items;
|
||||
std::vector<std::string> spineRefs;
|
||||
std::string coverItemHref;
|
||||
|
||||
explicit ContentOpfParser(const std::string& baseContentPath, const size_t xmlSize)
|
||||
: baseContentPath(baseContentPath), remainingSize(xmlSize) {}
|
||||
explicit ContentOpfParser(const std::string& cachePath, const std::string& baseContentPath, const size_t xmlSize,
|
||||
BookMetadataCache* cache)
|
||||
: cachePath(cachePath), baseContentPath(baseContentPath), remainingSize(xmlSize), cache(cache) {}
|
||||
~ContentOpfParser() override;
|
||||
|
||||
bool setup();
|
||||
bool teardown();
|
||||
|
||||
size_t write(uint8_t) override;
|
||||
size_t write(const uint8_t* buffer, size_t size) override;
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <HardwareSerial.h>
|
||||
|
||||
#include "../BookMetadataCache.h"
|
||||
|
||||
bool TocNcxParser::setup() {
|
||||
parser = XML_ParserCreate(nullptr);
|
||||
if (!parser) {
|
||||
@@ -15,12 +17,14 @@ bool TocNcxParser::setup() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TocNcxParser::teardown() {
|
||||
TocNcxParser::~TocNcxParser() {
|
||||
if (parser) {
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_SetCharacterDataHandler(parser, nullptr);
|
||||
XML_ParserFree(parser);
|
||||
parser = nullptr;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t TocNcxParser::write(const uint8_t data) { return write(&data, 1); }
|
||||
@@ -35,6 +39,11 @@ size_t TocNcxParser::write(const uint8_t* buffer, const size_t size) {
|
||||
void* const buf = XML_GetBuffer(parser, 1024);
|
||||
if (!buf) {
|
||||
Serial.printf("[%lu] [TOC] Couldn't allocate memory for buffer\n", millis());
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_SetCharacterDataHandler(parser, nullptr);
|
||||
XML_ParserFree(parser);
|
||||
parser = nullptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -44,6 +53,11 @@ size_t TocNcxParser::write(const uint8_t* buffer, const size_t size) {
|
||||
if (XML_ParseBuffer(parser, static_cast<int>(toRead), remainingSize == toRead) == XML_STATUS_ERROR) {
|
||||
Serial.printf("[%lu] [TOC] Parse error at line %lu: %s\n", millis(), XML_GetCurrentLineNumber(parser),
|
||||
XML_ErrorString(XML_GetErrorCode(parser)));
|
||||
XML_StopParser(parser, XML_FALSE); // Stop any pending processing
|
||||
XML_SetElementHandler(parser, nullptr, nullptr); // Clear callbacks
|
||||
XML_SetCharacterDataHandler(parser, nullptr);
|
||||
XML_ParserFree(parser);
|
||||
parser = nullptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -154,8 +168,9 @@ void XMLCALL TocNcxParser::endElement(void* userData, const XML_Char* name) {
|
||||
href = href.substr(0, pos);
|
||||
}
|
||||
|
||||
// Push to vector
|
||||
self->toc.emplace_back(self->currentLabel, href, anchor, self->currentDepth);
|
||||
if (self->cache) {
|
||||
self->cache->createTocEntry(self->currentLabel, href, anchor, self->currentDepth);
|
||||
}
|
||||
|
||||
// Clear them so we don't re-add them if there are weird XML structures
|
||||
self->currentLabel.clear();
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
#pragma once
|
||||
#include <Print.h>
|
||||
#include <expat.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Epub/EpubTocEntry.h"
|
||||
#include "expat.h"
|
||||
class BookMetadataCache;
|
||||
|
||||
class TocNcxParser final : public Print {
|
||||
enum ParserState { START, IN_NCX, IN_NAV_MAP, IN_NAV_POINT, IN_NAV_LABEL, IN_NAV_LABEL_TEXT, IN_CONTENT };
|
||||
@@ -14,23 +13,22 @@ class TocNcxParser final : public Print {
|
||||
size_t remainingSize;
|
||||
XML_Parser parser = nullptr;
|
||||
ParserState state = START;
|
||||
BookMetadataCache* cache;
|
||||
|
||||
std::string currentLabel;
|
||||
std::string currentSrc;
|
||||
size_t currentDepth = 0;
|
||||
uint8_t currentDepth = 0;
|
||||
|
||||
static void startElement(void* userData, const XML_Char* name, const XML_Char** atts);
|
||||
static void characterData(void* userData, const XML_Char* s, int len);
|
||||
static void endElement(void* userData, const XML_Char* name);
|
||||
|
||||
public:
|
||||
std::vector<EpubTocEntry> toc;
|
||||
|
||||
explicit TocNcxParser(const std::string& baseContentPath, const size_t xmlSize)
|
||||
: baseContentPath(baseContentPath), remainingSize(xmlSize) {}
|
||||
explicit TocNcxParser(const std::string& baseContentPath, const size_t xmlSize, BookMetadataCache* cache)
|
||||
: baseContentPath(baseContentPath), remainingSize(xmlSize), cache(cache) {}
|
||||
~TocNcxParser() override;
|
||||
|
||||
bool setup();
|
||||
bool teardown();
|
||||
|
||||
size_t write(uint8_t) override;
|
||||
size_t write(const uint8_t* buffer, size_t size) override;
|
||||
|
||||
112
lib/FsHelpers/FsHelpers.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "FsHelpers.h"
|
||||
|
||||
#include <SD.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
bool FsHelpers::openFileForRead(const char* moduleName, const char* path, File& file) {
|
||||
if (!SD.exists(path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
file = SD.open(path, FILE_READ);
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [%s] Failed to open file for reading: %s\n", millis(), moduleName, path);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FsHelpers::openFileForRead(const char* moduleName, const std::string& path, File& file) {
|
||||
return openFileForRead(moduleName, path.c_str(), file);
|
||||
}
|
||||
|
||||
bool FsHelpers::openFileForRead(const char* moduleName, const String& path, File& file) {
|
||||
return openFileForRead(moduleName, path.c_str(), file);
|
||||
}
|
||||
|
||||
bool FsHelpers::openFileForWrite(const char* moduleName, const char* path, File& file) {
|
||||
file = SD.open(path, FILE_WRITE, true);
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [%s] Failed to open file for writing: %s\n", millis(), moduleName, path);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FsHelpers::openFileForWrite(const char* moduleName, const std::string& path, File& file) {
|
||||
return openFileForWrite(moduleName, path.c_str(), file);
|
||||
}
|
||||
|
||||
bool FsHelpers::openFileForWrite(const char* moduleName, const String& path, File& file) {
|
||||
return openFileForWrite(moduleName, path.c_str(), file);
|
||||
}
|
||||
|
||||
bool FsHelpers::removeDir(const char* path) {
|
||||
// 1. Open the directory
|
||||
File dir = SD.open(path);
|
||||
if (!dir) {
|
||||
return false;
|
||||
}
|
||||
if (!dir.isDirectory()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
File file = dir.openNextFile();
|
||||
while (file) {
|
||||
String filePath = path;
|
||||
if (!filePath.endsWith("/")) {
|
||||
filePath += "/";
|
||||
}
|
||||
filePath += file.name();
|
||||
|
||||
if (file.isDirectory()) {
|
||||
if (!removeDir(filePath.c_str())) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!SD.remove(filePath.c_str())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
file = dir.openNextFile();
|
||||
}
|
||||
|
||||
return SD.rmdir(path);
|
||||
}
|
||||
|
||||
std::string FsHelpers::normalisePath(const std::string& path) {
|
||||
std::vector<std::string> components;
|
||||
std::string component;
|
||||
|
||||
for (const auto c : path) {
|
||||
if (c == '/') {
|
||||
if (!component.empty()) {
|
||||
if (component == "..") {
|
||||
if (!components.empty()) {
|
||||
components.pop_back();
|
||||
}
|
||||
} else {
|
||||
components.push_back(component);
|
||||
}
|
||||
component.clear();
|
||||
}
|
||||
} else {
|
||||
component += c;
|
||||
}
|
||||
}
|
||||
|
||||
if (!component.empty()) {
|
||||
components.push_back(component);
|
||||
}
|
||||
|
||||
std::string result;
|
||||
for (const auto& c : components) {
|
||||
if (!result.empty()) {
|
||||
result += "/";
|
||||
}
|
||||
result += c;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
14
lib/FsHelpers/FsHelpers.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <FS.h>
|
||||
|
||||
class FsHelpers {
|
||||
public:
|
||||
static bool openFileForRead(const char* moduleName, const char* path, File& file);
|
||||
static bool openFileForRead(const char* moduleName, const std::string& path, File& file);
|
||||
static bool openFileForRead(const char* moduleName, const String& path, File& file);
|
||||
static bool openFileForWrite(const char* moduleName, const char* path, File& file);
|
||||
static bool openFileForWrite(const char* moduleName, const std::string& path, File& file);
|
||||
static bool openFileForWrite(const char* moduleName, const String& path, File& file);
|
||||
static bool removeDir(const char* path);
|
||||
static std::string normalisePath(const std::string& path);
|
||||
};
|
||||
372
lib/GfxRenderer/Bitmap.cpp
Normal file
@@ -0,0 +1,372 @@
|
||||
#include "Bitmap.h"
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
// ============================================================================
|
||||
// IMAGE PROCESSING OPTIONS - Toggle these to test different configurations
|
||||
// ============================================================================
|
||||
// Note: For cover images, dithering is done in JpegToBmpConverter.cpp
|
||||
// This file handles BMP reading - use simple quantization to avoid double-dithering
|
||||
constexpr bool USE_FLOYD_STEINBERG = false; // Disabled - dithering done at JPEG conversion
|
||||
constexpr bool USE_NOISE_DITHERING = false; // Hash-based noise dithering
|
||||
// Brightness adjustments:
|
||||
constexpr bool USE_BRIGHTNESS = false; // true: apply brightness/gamma adjustments
|
||||
constexpr int BRIGHTNESS_BOOST = 20; // Brightness offset (0-50), only if USE_BRIGHTNESS=true
|
||||
constexpr bool GAMMA_CORRECTION = false; // Gamma curve, only if USE_BRIGHTNESS=true
|
||||
// ============================================================================
|
||||
|
||||
// Integer approximation of gamma correction (brightens midtones)
|
||||
static inline int applyGamma(int gray) {
|
||||
if (!GAMMA_CORRECTION) return gray;
|
||||
const int product = gray * 255;
|
||||
int x = gray;
|
||||
if (x > 0) {
|
||||
x = (x + product / x) >> 1;
|
||||
x = (x + product / x) >> 1;
|
||||
}
|
||||
return x > 255 ? 255 : x;
|
||||
}
|
||||
|
||||
// Simple quantization without dithering - just divide into 4 levels
|
||||
static inline uint8_t quantizeSimple(int gray) {
|
||||
if (USE_BRIGHTNESS) {
|
||||
gray += BRIGHTNESS_BOOST;
|
||||
if (gray > 255) gray = 255;
|
||||
gray = applyGamma(gray);
|
||||
}
|
||||
return static_cast<uint8_t>(gray >> 6);
|
||||
}
|
||||
|
||||
// Hash-based noise dithering - survives downsampling without moiré artifacts
|
||||
static inline uint8_t quantizeNoise(int gray, int x, int y) {
|
||||
if (USE_BRIGHTNESS) {
|
||||
gray += BRIGHTNESS_BOOST;
|
||||
if (gray > 255) gray = 255;
|
||||
gray = applyGamma(gray);
|
||||
}
|
||||
|
||||
uint32_t hash = static_cast<uint32_t>(x) * 374761393u + static_cast<uint32_t>(y) * 668265263u;
|
||||
hash = (hash ^ (hash >> 13)) * 1274126177u;
|
||||
const int threshold = static_cast<int>(hash >> 24);
|
||||
|
||||
const int scaled = gray * 3;
|
||||
if (scaled < 255) {
|
||||
return (scaled + threshold >= 255) ? 1 : 0;
|
||||
} else if (scaled < 510) {
|
||||
return ((scaled - 255) + threshold >= 255) ? 2 : 1;
|
||||
} else {
|
||||
return ((scaled - 510) + threshold >= 255) ? 3 : 2;
|
||||
}
|
||||
}
|
||||
|
||||
// Main quantization function
|
||||
static inline uint8_t quantize(int gray, int x, int y) {
|
||||
if (USE_NOISE_DITHERING) {
|
||||
return quantizeNoise(gray, x, y);
|
||||
} else {
|
||||
return quantizeSimple(gray);
|
||||
}
|
||||
}
|
||||
|
||||
// Floyd-Steinberg quantization with error diffusion and serpentine scanning
|
||||
// Returns 2-bit value (0-3) and updates error buffers
|
||||
static inline uint8_t quantizeFloydSteinberg(int gray, int x, int width, int16_t* errorCurRow, int16_t* errorNextRow,
|
||||
bool reverseDir) {
|
||||
// Add accumulated error to this pixel
|
||||
int adjusted = gray + errorCurRow[x + 1];
|
||||
|
||||
// Clamp to valid range
|
||||
if (adjusted < 0) adjusted = 0;
|
||||
if (adjusted > 255) adjusted = 255;
|
||||
|
||||
// Quantize to 4 levels (0, 85, 170, 255)
|
||||
uint8_t quantized;
|
||||
int quantizedValue;
|
||||
if (adjusted < 43) {
|
||||
quantized = 0;
|
||||
quantizedValue = 0;
|
||||
} else if (adjusted < 128) {
|
||||
quantized = 1;
|
||||
quantizedValue = 85;
|
||||
} else if (adjusted < 213) {
|
||||
quantized = 2;
|
||||
quantizedValue = 170;
|
||||
} else {
|
||||
quantized = 3;
|
||||
quantizedValue = 255;
|
||||
}
|
||||
|
||||
// Calculate error
|
||||
int error = adjusted - quantizedValue;
|
||||
|
||||
// Distribute error to neighbors (serpentine: direction-aware)
|
||||
if (!reverseDir) {
|
||||
// Left to right
|
||||
errorCurRow[x + 2] += (error * 7) >> 4; // Right: 7/16
|
||||
errorNextRow[x] += (error * 3) >> 4; // Bottom-left: 3/16
|
||||
errorNextRow[x + 1] += (error * 5) >> 4; // Bottom: 5/16
|
||||
errorNextRow[x + 2] += (error) >> 4; // Bottom-right: 1/16
|
||||
} else {
|
||||
// Right to left (mirrored)
|
||||
errorCurRow[x] += (error * 7) >> 4; // Left: 7/16
|
||||
errorNextRow[x + 2] += (error * 3) >> 4; // Bottom-right: 3/16
|
||||
errorNextRow[x + 1] += (error * 5) >> 4; // Bottom: 5/16
|
||||
errorNextRow[x] += (error) >> 4; // Bottom-left: 1/16
|
||||
}
|
||||
|
||||
return quantized;
|
||||
}
|
||||
|
||||
Bitmap::~Bitmap() {
|
||||
delete[] errorCurRow;
|
||||
delete[] errorNextRow;
|
||||
}
|
||||
|
||||
uint16_t Bitmap::readLE16(File& f) {
|
||||
const int c0 = f.read();
|
||||
const int c1 = f.read();
|
||||
const auto b0 = static_cast<uint8_t>(c0 < 0 ? 0 : c0);
|
||||
const auto b1 = static_cast<uint8_t>(c1 < 0 ? 0 : c1);
|
||||
return static_cast<uint16_t>(b0) | (static_cast<uint16_t>(b1) << 8);
|
||||
}
|
||||
|
||||
uint32_t Bitmap::readLE32(File& f) {
|
||||
const int c0 = f.read();
|
||||
const int c1 = f.read();
|
||||
const int c2 = f.read();
|
||||
const int c3 = f.read();
|
||||
|
||||
const auto b0 = static_cast<uint8_t>(c0 < 0 ? 0 : c0);
|
||||
const auto b1 = static_cast<uint8_t>(c1 < 0 ? 0 : c1);
|
||||
const auto b2 = static_cast<uint8_t>(c2 < 0 ? 0 : c2);
|
||||
const auto b3 = static_cast<uint8_t>(c3 < 0 ? 0 : c3);
|
||||
|
||||
return static_cast<uint32_t>(b0) | (static_cast<uint32_t>(b1) << 8) | (static_cast<uint32_t>(b2) << 16) |
|
||||
(static_cast<uint32_t>(b3) << 24);
|
||||
}
|
||||
|
||||
const char* Bitmap::errorToString(BmpReaderError err) {
|
||||
switch (err) {
|
||||
case BmpReaderError::Ok:
|
||||
return "Ok";
|
||||
case BmpReaderError::FileInvalid:
|
||||
return "FileInvalid";
|
||||
case BmpReaderError::SeekStartFailed:
|
||||
return "SeekStartFailed";
|
||||
case BmpReaderError::NotBMP:
|
||||
return "NotBMP (missing 'BM')";
|
||||
case BmpReaderError::DIBTooSmall:
|
||||
return "DIBTooSmall (<40 bytes)";
|
||||
case BmpReaderError::BadPlanes:
|
||||
return "BadPlanes (!= 1)";
|
||||
case BmpReaderError::UnsupportedBpp:
|
||||
return "UnsupportedBpp (expected 1, 2, 8, 24, or 32)";
|
||||
case BmpReaderError::UnsupportedCompression:
|
||||
return "UnsupportedCompression (expected BI_RGB or BI_BITFIELDS for 32bpp)";
|
||||
case BmpReaderError::BadDimensions:
|
||||
return "BadDimensions";
|
||||
case BmpReaderError::ImageTooLarge:
|
||||
return "ImageTooLarge (max 2048x3072)";
|
||||
case BmpReaderError::PaletteTooLarge:
|
||||
return "PaletteTooLarge";
|
||||
|
||||
case BmpReaderError::SeekPixelDataFailed:
|
||||
return "SeekPixelDataFailed";
|
||||
case BmpReaderError::BufferTooSmall:
|
||||
return "BufferTooSmall";
|
||||
|
||||
case BmpReaderError::OomRowBuffer:
|
||||
return "OomRowBuffer";
|
||||
case BmpReaderError::ShortReadRow:
|
||||
return "ShortReadRow";
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
BmpReaderError Bitmap::parseHeaders() {
|
||||
if (!file) return BmpReaderError::FileInvalid;
|
||||
if (!file.seek(0)) return BmpReaderError::SeekStartFailed;
|
||||
|
||||
// --- BMP FILE HEADER ---
|
||||
const uint16_t bfType = readLE16(file);
|
||||
if (bfType != 0x4D42) return BmpReaderError::NotBMP;
|
||||
|
||||
file.seek(8, SeekCur);
|
||||
bfOffBits = readLE32(file);
|
||||
|
||||
// --- DIB HEADER ---
|
||||
const uint32_t biSize = readLE32(file);
|
||||
if (biSize < 40) return BmpReaderError::DIBTooSmall;
|
||||
|
||||
width = static_cast<int32_t>(readLE32(file));
|
||||
const auto rawHeight = static_cast<int32_t>(readLE32(file));
|
||||
topDown = rawHeight < 0;
|
||||
height = topDown ? -rawHeight : rawHeight;
|
||||
|
||||
const uint16_t planes = readLE16(file);
|
||||
bpp = readLE16(file);
|
||||
const uint32_t comp = readLE32(file);
|
||||
const bool validBpp = bpp == 1 || bpp == 2 || bpp == 8 || bpp == 24 || bpp == 32;
|
||||
|
||||
if (planes != 1) return BmpReaderError::BadPlanes;
|
||||
if (!validBpp) return BmpReaderError::UnsupportedBpp;
|
||||
// Allow BI_RGB (0) for all, and BI_BITFIELDS (3) for 32bpp which is common for BGRA masks.
|
||||
if (!(comp == 0 || (bpp == 32 && comp == 3))) return BmpReaderError::UnsupportedCompression;
|
||||
|
||||
file.seek(12, SeekCur); // biSizeImage, biXPelsPerMeter, biYPelsPerMeter
|
||||
const uint32_t colorsUsed = readLE32(file);
|
||||
if (colorsUsed > 256u) return BmpReaderError::PaletteTooLarge;
|
||||
file.seek(4, SeekCur); // biClrImportant
|
||||
|
||||
if (width <= 0 || height <= 0) return BmpReaderError::BadDimensions;
|
||||
|
||||
// Safety limits to prevent memory issues on ESP32
|
||||
constexpr int MAX_IMAGE_WIDTH = 2048;
|
||||
constexpr int MAX_IMAGE_HEIGHT = 3072;
|
||||
if (width > MAX_IMAGE_WIDTH || height > MAX_IMAGE_HEIGHT) {
|
||||
return BmpReaderError::ImageTooLarge;
|
||||
}
|
||||
|
||||
// Pre-calculate Row Bytes to avoid doing this every row
|
||||
rowBytes = (width * bpp + 31) / 32 * 4;
|
||||
|
||||
for (int i = 0; i < 256; i++) paletteLum[i] = static_cast<uint8_t>(i);
|
||||
if (colorsUsed > 0) {
|
||||
for (uint32_t i = 0; i < colorsUsed; i++) {
|
||||
uint8_t rgb[4];
|
||||
file.read(rgb, 4); // Read B, G, R, Reserved in one go
|
||||
paletteLum[i] = (77u * rgb[2] + 150u * rgb[1] + 29u * rgb[0]) >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
if (!file.seek(bfOffBits)) {
|
||||
return BmpReaderError::SeekPixelDataFailed;
|
||||
}
|
||||
|
||||
// Allocate Floyd-Steinberg error buffers if enabled
|
||||
if (USE_FLOYD_STEINBERG) {
|
||||
delete[] errorCurRow;
|
||||
delete[] errorNextRow;
|
||||
errorCurRow = new int16_t[width + 2](); // +2 for boundary handling
|
||||
errorNextRow = new int16_t[width + 2]();
|
||||
lastRowY = -1;
|
||||
}
|
||||
|
||||
return BmpReaderError::Ok;
|
||||
}
|
||||
|
||||
// packed 2bpp output, 0 = black, 1 = dark gray, 2 = light gray, 3 = white
|
||||
BmpReaderError Bitmap::readRow(uint8_t* data, uint8_t* rowBuffer, int rowY) const {
|
||||
// Note: rowBuffer should be pre-allocated by the caller to size 'rowBytes'
|
||||
if (file.read(rowBuffer, rowBytes) != rowBytes) return BmpReaderError::ShortReadRow;
|
||||
|
||||
// Handle Floyd-Steinberg error buffer progression
|
||||
const bool useFS = USE_FLOYD_STEINBERG && errorCurRow && errorNextRow;
|
||||
if (useFS) {
|
||||
// Check if we need to advance to next row (or reset if jumping)
|
||||
if (rowY != lastRowY + 1 && rowY != 0) {
|
||||
// Non-sequential row access - reset error buffers
|
||||
memset(errorCurRow, 0, (width + 2) * sizeof(int16_t));
|
||||
memset(errorNextRow, 0, (width + 2) * sizeof(int16_t));
|
||||
} else if (rowY > 0) {
|
||||
// Sequential access - swap buffers
|
||||
int16_t* temp = errorCurRow;
|
||||
errorCurRow = errorNextRow;
|
||||
errorNextRow = temp;
|
||||
memset(errorNextRow, 0, (width + 2) * sizeof(int16_t));
|
||||
}
|
||||
lastRowY = rowY;
|
||||
}
|
||||
|
||||
uint8_t* outPtr = data;
|
||||
uint8_t currentOutByte = 0;
|
||||
int bitShift = 6;
|
||||
int currentX = 0;
|
||||
|
||||
// Helper lambda to pack 2bpp color into the output stream
|
||||
auto packPixel = [&](const uint8_t lum) {
|
||||
uint8_t color;
|
||||
if (useFS) {
|
||||
// Floyd-Steinberg error diffusion
|
||||
color = quantizeFloydSteinberg(lum, currentX, width, errorCurRow, errorNextRow, false);
|
||||
} else {
|
||||
// Simple quantization or noise dithering
|
||||
color = quantize(lum, currentX, rowY);
|
||||
}
|
||||
currentOutByte |= (color << bitShift);
|
||||
if (bitShift == 0) {
|
||||
*outPtr++ = currentOutByte;
|
||||
currentOutByte = 0;
|
||||
bitShift = 6;
|
||||
} else {
|
||||
bitShift -= 2;
|
||||
}
|
||||
currentX++;
|
||||
};
|
||||
|
||||
uint8_t lum;
|
||||
|
||||
switch (bpp) {
|
||||
case 32: {
|
||||
const uint8_t* p = rowBuffer;
|
||||
for (int x = 0; x < width; x++) {
|
||||
lum = (77u * p[2] + 150u * p[1] + 29u * p[0]) >> 8;
|
||||
packPixel(lum);
|
||||
p += 4;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 24: {
|
||||
const uint8_t* p = rowBuffer;
|
||||
for (int x = 0; x < width; x++) {
|
||||
lum = (77u * p[2] + 150u * p[1] + 29u * p[0]) >> 8;
|
||||
packPixel(lum);
|
||||
p += 3;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 8: {
|
||||
for (int x = 0; x < width; x++) {
|
||||
packPixel(paletteLum[rowBuffer[x]]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
for (int x = 0; x < width; x++) {
|
||||
lum = paletteLum[(rowBuffer[x >> 2] >> (6 - ((x & 3) * 2))) & 0x03];
|
||||
packPixel(lum);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
for (int x = 0; x < width; x++) {
|
||||
lum = (rowBuffer[x >> 3] & (0x80 >> (x & 7))) ? 0xFF : 0x00;
|
||||
packPixel(lum);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return BmpReaderError::UnsupportedBpp;
|
||||
}
|
||||
|
||||
// Flush remaining bits if width is not a multiple of 4
|
||||
if (bitShift != 6) *outPtr = currentOutByte;
|
||||
|
||||
return BmpReaderError::Ok;
|
||||
}
|
||||
|
||||
BmpReaderError Bitmap::rewindToData() const {
|
||||
if (!file.seek(bfOffBits)) {
|
||||
return BmpReaderError::SeekPixelDataFailed;
|
||||
}
|
||||
|
||||
// Reset Floyd-Steinberg error buffers when rewinding
|
||||
if (USE_FLOYD_STEINBERG && errorCurRow && errorNextRow) {
|
||||
memset(errorCurRow, 0, (width + 2) * sizeof(int16_t));
|
||||
memset(errorNextRow, 0, (width + 2) * sizeof(int16_t));
|
||||
lastRowY = -1;
|
||||
}
|
||||
|
||||
return BmpReaderError::Ok;
|
||||
}
|
||||
59
lib/GfxRenderer/Bitmap.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <FS.h>
|
||||
|
||||
enum class BmpReaderError : uint8_t {
|
||||
Ok = 0,
|
||||
FileInvalid,
|
||||
SeekStartFailed,
|
||||
|
||||
NotBMP,
|
||||
DIBTooSmall,
|
||||
|
||||
BadPlanes,
|
||||
UnsupportedBpp,
|
||||
UnsupportedCompression,
|
||||
|
||||
BadDimensions,
|
||||
ImageTooLarge,
|
||||
PaletteTooLarge,
|
||||
|
||||
SeekPixelDataFailed,
|
||||
BufferTooSmall,
|
||||
OomRowBuffer,
|
||||
ShortReadRow,
|
||||
};
|
||||
|
||||
class Bitmap {
|
||||
public:
|
||||
static const char* errorToString(BmpReaderError err);
|
||||
|
||||
explicit Bitmap(File& file) : file(file) {}
|
||||
~Bitmap();
|
||||
BmpReaderError parseHeaders();
|
||||
BmpReaderError readRow(uint8_t* data, uint8_t* rowBuffer, int rowY) const;
|
||||
BmpReaderError rewindToData() const;
|
||||
int getWidth() const { return width; }
|
||||
int getHeight() const { return height; }
|
||||
bool isTopDown() const { return topDown; }
|
||||
bool hasGreyscale() const { return bpp > 1; }
|
||||
int getRowBytes() const { return rowBytes; }
|
||||
|
||||
private:
|
||||
static uint16_t readLE16(File& f);
|
||||
static uint32_t readLE32(File& f);
|
||||
|
||||
File& file;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
bool topDown = false;
|
||||
uint32_t bfOffBits = 0;
|
||||
uint16_t bpp = 0;
|
||||
int rowBytes = 0;
|
||||
uint8_t paletteLum[256] = {};
|
||||
|
||||
// Floyd-Steinberg dithering state (mutable for const methods)
|
||||
mutable int16_t* errorCurRow = nullptr;
|
||||
mutable int16_t* errorNextRow = nullptr;
|
||||
mutable int lastRowY = -1; // Track row progression for error propagation
|
||||
};
|
||||
@@ -4,6 +4,37 @@
|
||||
|
||||
void GfxRenderer::insertFont(const int fontId, EpdFontFamily font) { fontMap.insert({fontId, font}); }
|
||||
|
||||
void GfxRenderer::rotateCoordinates(const int x, const int y, int* rotatedX, int* rotatedY) const {
|
||||
switch (orientation) {
|
||||
case Portrait: {
|
||||
// Logical portrait (480x800) → panel (800x480)
|
||||
// Rotation: 90 degrees clockwise
|
||||
*rotatedX = y;
|
||||
*rotatedY = EInkDisplay::DISPLAY_HEIGHT - 1 - x;
|
||||
break;
|
||||
}
|
||||
case LandscapeClockwise: {
|
||||
// Logical landscape (800x480) rotated 180 degrees (swap top/bottom and left/right)
|
||||
*rotatedX = EInkDisplay::DISPLAY_WIDTH - 1 - x;
|
||||
*rotatedY = EInkDisplay::DISPLAY_HEIGHT - 1 - y;
|
||||
break;
|
||||
}
|
||||
case PortraitInverted: {
|
||||
// Logical portrait (480x800) → panel (800x480)
|
||||
// Rotation: 90 degrees counter-clockwise
|
||||
*rotatedX = EInkDisplay::DISPLAY_WIDTH - 1 - y;
|
||||
*rotatedY = x;
|
||||
break;
|
||||
}
|
||||
case LandscapeCounterClockwise: {
|
||||
// Logical landscape (800x480) aligned with panel orientation
|
||||
*rotatedX = x;
|
||||
*rotatedY = y;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GfxRenderer::drawPixel(const int x, const int y, const bool state) const {
|
||||
uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
|
||||
|
||||
@@ -13,15 +44,14 @@ void GfxRenderer::drawPixel(const int x, const int y, const bool state) const {
|
||||
return;
|
||||
}
|
||||
|
||||
// Rotate coordinates: portrait (480x800) -> landscape (800x480)
|
||||
// Rotation: 90 degrees clockwise
|
||||
const int rotatedX = y;
|
||||
const int rotatedY = EInkDisplay::DISPLAY_HEIGHT - 1 - x;
|
||||
int rotatedX = 0;
|
||||
int rotatedY = 0;
|
||||
rotateCoordinates(x, y, &rotatedX, &rotatedY);
|
||||
|
||||
// Bounds checking (portrait: 480x800)
|
||||
// Bounds checking against physical panel dimensions
|
||||
if (rotatedX < 0 || rotatedX >= EInkDisplay::DISPLAY_WIDTH || rotatedY < 0 ||
|
||||
rotatedY >= EInkDisplay::DISPLAY_HEIGHT) {
|
||||
Serial.printf("[%lu] [GFX] !! Outside range (%d, %d)\n", millis(), x, y);
|
||||
Serial.printf("[%lu] [GFX] !! Outside range (%d, %d) -> (%d, %d)\n", millis(), x, y, rotatedX, rotatedY);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -55,7 +85,7 @@ void GfxRenderer::drawCenteredText(const int fontId, const int y, const char* te
|
||||
|
||||
void GfxRenderer::drawText(const int fontId, const int x, const int y, const char* text, const bool black,
|
||||
const EpdFontStyle style) const {
|
||||
const int yPos = y + getLineHeight(fontId);
|
||||
const int yPos = y + getFontAscenderSize(fontId);
|
||||
int xpos = x;
|
||||
|
||||
// cannot draw a NULL / empty string
|
||||
@@ -115,14 +145,90 @@ void GfxRenderer::fillRect(const int x, const int y, const int width, const int
|
||||
}
|
||||
|
||||
void GfxRenderer::drawImage(const uint8_t bitmap[], const int x, const int y, const int width, const int height) const {
|
||||
// Flip X and Y for portrait mode
|
||||
einkDisplay.drawImage(bitmap, y, x, height, width);
|
||||
// TODO: Rotate bits
|
||||
int rotatedX = 0;
|
||||
int rotatedY = 0;
|
||||
rotateCoordinates(x, y, &rotatedX, &rotatedY);
|
||||
einkDisplay.drawImage(bitmap, rotatedX, rotatedY, width, height);
|
||||
}
|
||||
|
||||
void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, const int maxWidth,
|
||||
const int maxHeight) const {
|
||||
float scale = 1.0f;
|
||||
bool isScaled = false;
|
||||
if (maxWidth > 0 && bitmap.getWidth() > maxWidth) {
|
||||
scale = static_cast<float>(maxWidth) / static_cast<float>(bitmap.getWidth());
|
||||
isScaled = true;
|
||||
}
|
||||
if (maxHeight > 0 && bitmap.getHeight() > maxHeight) {
|
||||
scale = std::min(scale, static_cast<float>(maxHeight) / static_cast<float>(bitmap.getHeight()));
|
||||
isScaled = true;
|
||||
}
|
||||
|
||||
// Calculate output row size (2 bits per pixel, packed into bytes)
|
||||
// IMPORTANT: Use int, not uint8_t, to avoid overflow for images > 1020 pixels wide
|
||||
const int outputRowSize = (bitmap.getWidth() + 3) / 4;
|
||||
auto* outputRow = static_cast<uint8_t*>(malloc(outputRowSize));
|
||||
auto* rowBytes = static_cast<uint8_t*>(malloc(bitmap.getRowBytes()));
|
||||
|
||||
if (!outputRow || !rowBytes) {
|
||||
Serial.printf("[%lu] [GFX] !! Failed to allocate BMP row buffers\n", millis());
|
||||
free(outputRow);
|
||||
free(rowBytes);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int bmpY = 0; bmpY < bitmap.getHeight(); bmpY++) {
|
||||
// The BMP's (0, 0) is the bottom-left corner (if the height is positive, top-left if negative).
|
||||
// Screen's (0, 0) is the top-left corner.
|
||||
int screenY = y + (bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY);
|
||||
if (isScaled) {
|
||||
screenY = std::floor(screenY * scale);
|
||||
}
|
||||
if (screenY >= getScreenHeight()) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (bitmap.readRow(outputRow, rowBytes, bmpY) != BmpReaderError::Ok) {
|
||||
Serial.printf("[%lu] [GFX] Failed to read row %d from bitmap\n", millis(), bmpY);
|
||||
free(outputRow);
|
||||
free(rowBytes);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) {
|
||||
int screenX = x + bmpX;
|
||||
if (isScaled) {
|
||||
screenX = std::floor(screenX * scale);
|
||||
}
|
||||
if (screenX >= getScreenWidth()) {
|
||||
break;
|
||||
}
|
||||
|
||||
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
||||
|
||||
if (renderMode == BW && val < 3) {
|
||||
drawPixel(screenX, screenY);
|
||||
} else if (renderMode == GRAYSCALE_MSB && (val == 1 || val == 2)) {
|
||||
drawPixel(screenX, screenY, false);
|
||||
} else if (renderMode == GRAYSCALE_LSB && val == 1) {
|
||||
drawPixel(screenX, screenY, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(outputRow);
|
||||
free(rowBytes);
|
||||
}
|
||||
|
||||
void GfxRenderer::clearScreen(const uint8_t color) const { einkDisplay.clearScreen(color); }
|
||||
|
||||
void GfxRenderer::invertScreen() const {
|
||||
uint8_t* buffer = einkDisplay.getFrameBuffer();
|
||||
if (!buffer) {
|
||||
Serial.printf("[%lu] [GFX] !! No framebuffer in invertScreen\n", millis());
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < EInkDisplay::BUFFER_SIZE; i++) {
|
||||
buffer[i] = ~buffer[i];
|
||||
}
|
||||
@@ -132,23 +238,34 @@ void GfxRenderer::displayBuffer(const EInkDisplay::RefreshMode refreshMode) cons
|
||||
einkDisplay.displayBuffer(refreshMode);
|
||||
}
|
||||
|
||||
void GfxRenderer::displayWindow(const int x, const int y, const int width, const int height) const {
|
||||
// Rotate coordinates from portrait (480x800) to landscape (800x480)
|
||||
// Rotation: 90 degrees clockwise
|
||||
// Portrait coordinates: (x, y) with dimensions (width, height)
|
||||
// Landscape coordinates: (rotatedX, rotatedY) with dimensions (rotatedWidth, rotatedHeight)
|
||||
|
||||
const int rotatedX = y;
|
||||
const int rotatedY = EInkDisplay::DISPLAY_HEIGHT - 1 - x - width + 1;
|
||||
const int rotatedWidth = height;
|
||||
const int rotatedHeight = width;
|
||||
|
||||
einkDisplay.displayWindow(rotatedX, rotatedY, rotatedWidth, rotatedHeight);
|
||||
// Note: Internal driver treats screen in command orientation; this library exposes a logical orientation
|
||||
int GfxRenderer::getScreenWidth() const {
|
||||
switch (orientation) {
|
||||
case Portrait:
|
||||
case PortraitInverted:
|
||||
// 480px wide in portrait logical coordinates
|
||||
return EInkDisplay::DISPLAY_HEIGHT;
|
||||
case LandscapeClockwise:
|
||||
case LandscapeCounterClockwise:
|
||||
// 800px wide in landscape logical coordinates
|
||||
return EInkDisplay::DISPLAY_WIDTH;
|
||||
}
|
||||
return EInkDisplay::DISPLAY_HEIGHT;
|
||||
}
|
||||
|
||||
// Note: Internal driver treats screen in command orientation, this library treats in portrait orientation
|
||||
int GfxRenderer::getScreenWidth() { return EInkDisplay::DISPLAY_HEIGHT; }
|
||||
int GfxRenderer::getScreenHeight() { return EInkDisplay::DISPLAY_WIDTH; }
|
||||
int GfxRenderer::getScreenHeight() const {
|
||||
switch (orientation) {
|
||||
case Portrait:
|
||||
case PortraitInverted:
|
||||
// 800px tall in portrait logical coordinates
|
||||
return EInkDisplay::DISPLAY_WIDTH;
|
||||
case LandscapeClockwise:
|
||||
case LandscapeCounterClockwise:
|
||||
// 480px tall in landscape logical coordinates
|
||||
return EInkDisplay::DISPLAY_HEIGHT;
|
||||
}
|
||||
return EInkDisplay::DISPLAY_WIDTH;
|
||||
}
|
||||
|
||||
int GfxRenderer::getSpaceWidth(const int fontId) const {
|
||||
if (fontMap.count(fontId) == 0) {
|
||||
@@ -159,6 +276,15 @@ int GfxRenderer::getSpaceWidth(const int fontId) const {
|
||||
return fontMap.at(fontId).getGlyph(' ', REGULAR)->advanceX;
|
||||
}
|
||||
|
||||
int GfxRenderer::getFontAscenderSize(const int fontId) const {
|
||||
if (fontMap.count(fontId) == 0) {
|
||||
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return fontMap.at(fontId).getData(REGULAR)->ascender;
|
||||
}
|
||||
|
||||
int GfxRenderer::getLineHeight(const int fontId) const {
|
||||
if (fontMap.count(fontId) == 0) {
|
||||
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
||||
@@ -168,6 +294,28 @@ int GfxRenderer::getLineHeight(const int fontId) const {
|
||||
return fontMap.at(fontId).getData(REGULAR)->advanceY;
|
||||
}
|
||||
|
||||
void GfxRenderer::drawButtonHints(const int fontId, const char* btn1, const char* btn2, const char* btn3,
|
||||
const char* btn4) const {
|
||||
const int pageHeight = getScreenHeight();
|
||||
constexpr int buttonWidth = 106;
|
||||
constexpr int buttonHeight = 40;
|
||||
constexpr int buttonY = 40; // Distance from bottom
|
||||
constexpr int textYOffset = 7; // Distance from top of button to text baseline
|
||||
constexpr int buttonPositions[] = {25, 130, 245, 350};
|
||||
const char* labels[] = {btn1, btn2, btn3, btn4};
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
// Only draw if the label is non-empty
|
||||
if (labels[i] != nullptr && labels[i][0] != '\0') {
|
||||
const int x = buttonPositions[i];
|
||||
drawRect(x, pageHeight - buttonY, buttonWidth, buttonHeight);
|
||||
const int textWidth = getTextWidth(fontId, labels[i]);
|
||||
const int textX = x + (buttonWidth - 1 - textWidth) / 2;
|
||||
drawText(fontId, textX, pageHeight - buttonY + textYOffset, labels[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t* GfxRenderer::getFrameBuffer() const { return einkDisplay.getFrameBuffer(); }
|
||||
|
||||
size_t GfxRenderer::getBufferSize() { return EInkDisplay::BUFFER_SIZE; }
|
||||
@@ -193,9 +341,14 @@ void GfxRenderer::freeBwBufferChunks() {
|
||||
* This should be called before grayscale buffers are populated.
|
||||
* A `restoreBwBuffer` call should always follow the grayscale render if this method was called.
|
||||
* Uses chunked allocation to avoid needing 48KB of contiguous memory.
|
||||
* Returns true if buffer was stored successfully, false if allocation failed.
|
||||
*/
|
||||
void GfxRenderer::storeBwBuffer() {
|
||||
bool GfxRenderer::storeBwBuffer() {
|
||||
const uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
|
||||
if (!frameBuffer) {
|
||||
Serial.printf("[%lu] [GFX] !! No framebuffer in storeBwBuffer\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Allocate and copy each chunk
|
||||
for (size_t i = 0; i < BW_BUFFER_NUM_CHUNKS; i++) {
|
||||
@@ -215,7 +368,7 @@ void GfxRenderer::storeBwBuffer() {
|
||||
BW_BUFFER_CHUNK_SIZE);
|
||||
// Free previously allocated chunks
|
||||
freeBwBufferChunks();
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(bwBufferChunks[i], frameBuffer + offset, BW_BUFFER_CHUNK_SIZE);
|
||||
@@ -223,6 +376,7 @@ void GfxRenderer::storeBwBuffer() {
|
||||
|
||||
Serial.printf("[%lu] [GFX] Stored BW buffer in %zu chunks (%zu bytes each)\n", millis(), BW_BUFFER_NUM_CHUNKS,
|
||||
BW_BUFFER_CHUNK_SIZE);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,6 +400,12 @@ void GfxRenderer::restoreBwBuffer() {
|
||||
}
|
||||
|
||||
uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
|
||||
if (!frameBuffer) {
|
||||
Serial.printf("[%lu] [GFX] !! No framebuffer in restoreBwBuffer\n", millis());
|
||||
freeBwBufferChunks();
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < BW_BUFFER_NUM_CHUNKS; i++) {
|
||||
// Check if chunk is missing
|
||||
if (!bwBufferChunks[i]) {
|
||||
@@ -264,6 +424,17 @@ void GfxRenderer::restoreBwBuffer() {
|
||||
Serial.printf("[%lu] [GFX] Restored and freed BW buffer chunks\n", millis());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup grayscale buffers using the current frame buffer.
|
||||
* Use this when BW buffer was re-rendered instead of stored/restored.
|
||||
*/
|
||||
void GfxRenderer::cleanupGrayscaleWithFrameBuffer() const {
|
||||
uint8_t* frameBuffer = einkDisplay.getFrameBuffer();
|
||||
if (frameBuffer) {
|
||||
einkDisplay.cleanupGrayscaleBuffers(frameBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
void GfxRenderer::renderChar(const EpdFontFamily& fontFamily, const uint32_t cp, int* x, const int* y,
|
||||
const bool pixelState, const EpdFontStyle style) const {
|
||||
const EpdGlyph* glyph = fontFamily.getGlyph(cp, style);
|
||||
@@ -327,3 +498,32 @@ void GfxRenderer::renderChar(const EpdFontFamily& fontFamily, const uint32_t cp,
|
||||
|
||||
*x += glyph->advanceX;
|
||||
}
|
||||
|
||||
void GfxRenderer::getOrientedViewableTRBL(int* outTop, int* outRight, int* outBottom, int* outLeft) const {
|
||||
switch (orientation) {
|
||||
case Portrait:
|
||||
*outTop = VIEWABLE_MARGIN_TOP;
|
||||
*outRight = VIEWABLE_MARGIN_RIGHT;
|
||||
*outBottom = VIEWABLE_MARGIN_BOTTOM;
|
||||
*outLeft = VIEWABLE_MARGIN_LEFT;
|
||||
break;
|
||||
case LandscapeClockwise:
|
||||
*outTop = VIEWABLE_MARGIN_LEFT;
|
||||
*outRight = VIEWABLE_MARGIN_TOP;
|
||||
*outBottom = VIEWABLE_MARGIN_RIGHT;
|
||||
*outLeft = VIEWABLE_MARGIN_BOTTOM;
|
||||
break;
|
||||
case PortraitInverted:
|
||||
*outTop = VIEWABLE_MARGIN_BOTTOM;
|
||||
*outRight = VIEWABLE_MARGIN_LEFT;
|
||||
*outBottom = VIEWABLE_MARGIN_TOP;
|
||||
*outLeft = VIEWABLE_MARGIN_RIGHT;
|
||||
break;
|
||||
case LandscapeCounterClockwise:
|
||||
*outTop = VIEWABLE_MARGIN_RIGHT;
|
||||
*outRight = VIEWABLE_MARGIN_BOTTOM;
|
||||
*outBottom = VIEWABLE_MARGIN_LEFT;
|
||||
*outLeft = VIEWABLE_MARGIN_TOP;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,24 @@
|
||||
|
||||
#include <EInkDisplay.h>
|
||||
#include <EpdFontFamily.h>
|
||||
#include <FS.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "Bitmap.h"
|
||||
|
||||
class GfxRenderer {
|
||||
public:
|
||||
enum RenderMode { BW, GRAYSCALE_LSB, GRAYSCALE_MSB };
|
||||
|
||||
// Logical screen orientation from the perspective of callers
|
||||
enum Orientation {
|
||||
Portrait, // 480x800 logical coordinates (current default)
|
||||
LandscapeClockwise, // 800x480 logical coordinates, rotated 180° (swap top/bottom)
|
||||
PortraitInverted, // 480x800 logical coordinates, inverted
|
||||
LandscapeCounterClockwise // 800x480 logical coordinates, native panel orientation
|
||||
};
|
||||
|
||||
private:
|
||||
static constexpr size_t BW_BUFFER_CHUNK_SIZE = 8000; // 8KB chunks to allow for non-contiguous memory
|
||||
static constexpr size_t BW_BUFFER_NUM_CHUNKS = EInkDisplay::BUFFER_SIZE / BW_BUFFER_CHUNK_SIZE;
|
||||
@@ -17,24 +28,35 @@ class GfxRenderer {
|
||||
|
||||
EInkDisplay& einkDisplay;
|
||||
RenderMode renderMode;
|
||||
Orientation orientation;
|
||||
uint8_t* bwBufferChunks[BW_BUFFER_NUM_CHUNKS] = {nullptr};
|
||||
std::map<int, EpdFontFamily> fontMap;
|
||||
void renderChar(const EpdFontFamily& fontFamily, uint32_t cp, int* x, const int* y, bool pixelState,
|
||||
EpdFontStyle style) const;
|
||||
void freeBwBufferChunks();
|
||||
void rotateCoordinates(int x, int y, int* rotatedX, int* rotatedY) const;
|
||||
|
||||
public:
|
||||
explicit GfxRenderer(EInkDisplay& einkDisplay) : einkDisplay(einkDisplay), renderMode(BW) {}
|
||||
explicit GfxRenderer(EInkDisplay& einkDisplay) : einkDisplay(einkDisplay), renderMode(BW), orientation(Portrait) {}
|
||||
~GfxRenderer() = default;
|
||||
|
||||
static constexpr int VIEWABLE_MARGIN_TOP = 9;
|
||||
static constexpr int VIEWABLE_MARGIN_RIGHT = 3;
|
||||
static constexpr int VIEWABLE_MARGIN_BOTTOM = 3;
|
||||
static constexpr int VIEWABLE_MARGIN_LEFT = 3;
|
||||
|
||||
// Setup
|
||||
void insertFont(int fontId, EpdFontFamily font);
|
||||
|
||||
// Orientation control (affects logical width/height and coordinate transforms)
|
||||
void setOrientation(const Orientation o) { orientation = o; }
|
||||
Orientation getOrientation() const { return orientation; }
|
||||
|
||||
// Screen ops
|
||||
static int getScreenWidth();
|
||||
static int getScreenHeight();
|
||||
int getScreenWidth() const;
|
||||
int getScreenHeight() const;
|
||||
void displayBuffer(EInkDisplay::RefreshMode refreshMode = EInkDisplay::FAST_REFRESH) const;
|
||||
// EXPERIMENTAL: Windowed update - display only a rectangular region (portrait coordinates)
|
||||
// EXPERIMENTAL: Windowed update - display only a rectangular region
|
||||
void displayWindow(int x, int y, int width, int height) const;
|
||||
void invertScreen() const;
|
||||
void clearScreen(uint8_t color = 0xFF) const;
|
||||
@@ -45,24 +67,31 @@ class GfxRenderer {
|
||||
void drawRect(int x, int y, int width, int height, bool state = true) const;
|
||||
void fillRect(int x, int y, int width, int height, bool state = true) const;
|
||||
void drawImage(const uint8_t bitmap[], int x, int y, int width, int height) const;
|
||||
void drawBitmap(const Bitmap& bitmap, int x, int y, int maxWidth, int maxHeight) const;
|
||||
|
||||
// Text
|
||||
int getTextWidth(int fontId, const char* text, EpdFontStyle style = REGULAR) const;
|
||||
void drawCenteredText(int fontId, int y, const char* text, bool black = true, EpdFontStyle style = REGULAR) const;
|
||||
void drawText(int fontId, int x, int y, const char* text, bool black = true, EpdFontStyle style = REGULAR) const;
|
||||
int getSpaceWidth(int fontId) const;
|
||||
int getFontAscenderSize(int fontId) const;
|
||||
int getLineHeight(int fontId) const;
|
||||
|
||||
// UI Components
|
||||
void drawButtonHints(int fontId, const char* btn1, const char* btn2, const char* btn3, const char* btn4) const;
|
||||
|
||||
// Grayscale functions
|
||||
void setRenderMode(const RenderMode mode) { this->renderMode = mode; }
|
||||
void copyGrayscaleLsbBuffers() const;
|
||||
void copyGrayscaleMsbBuffers() const;
|
||||
void displayGrayBuffer() const;
|
||||
void storeBwBuffer();
|
||||
bool storeBwBuffer(); // Returns true if buffer was stored successfully
|
||||
void restoreBwBuffer();
|
||||
void cleanupGrayscaleWithFrameBuffer() const;
|
||||
|
||||
// Low level functions
|
||||
uint8_t* getFrameBuffer() const;
|
||||
static size_t getBufferSize();
|
||||
void grayscaleRevert() const;
|
||||
void getOrientedViewableTRBL(int* outTop, int* outRight, int* outBottom, int* outLeft) const;
|
||||
};
|
||||
|
||||
736
lib/JpegToBmpConverter/JpegToBmpConverter.cpp
Normal file
@@ -0,0 +1,736 @@
|
||||
#include "JpegToBmpConverter.h"
|
||||
|
||||
#include <picojpeg.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
// Context structure for picojpeg callback
|
||||
struct JpegReadContext {
|
||||
File& file;
|
||||
uint8_t buffer[512];
|
||||
size_t bufferPos;
|
||||
size_t bufferFilled;
|
||||
};
|
||||
|
||||
// ============================================================================
|
||||
// IMAGE PROCESSING OPTIONS - Toggle these to test different configurations
|
||||
// ============================================================================
|
||||
constexpr bool USE_8BIT_OUTPUT = false; // true: 8-bit grayscale (no quantization), false: 2-bit (4 levels)
|
||||
// Dithering method selection (only one should be true, or all false for simple quantization):
|
||||
constexpr bool USE_ATKINSON = true; // Atkinson dithering (cleaner than F-S, less error diffusion)
|
||||
constexpr bool USE_FLOYD_STEINBERG = false; // Floyd-Steinberg error diffusion (can cause "worm" artifacts)
|
||||
constexpr bool USE_NOISE_DITHERING = false; // Hash-based noise dithering (good for downsampling)
|
||||
// Brightness/Contrast adjustments:
|
||||
constexpr bool USE_BRIGHTNESS = true; // true: apply brightness/gamma adjustments
|
||||
constexpr int BRIGHTNESS_BOOST = 10; // Brightness offset (0-50)
|
||||
constexpr bool GAMMA_CORRECTION = true; // Gamma curve (brightens midtones)
|
||||
constexpr float CONTRAST_FACTOR = 1.15f; // Contrast multiplier (1.0 = no change, >1 = more contrast)
|
||||
// Pre-resize to target display size (CRITICAL: avoids dithering artifacts from post-downsampling)
|
||||
constexpr bool USE_PRESCALE = true; // true: scale image to target size before dithering
|
||||
constexpr int TARGET_MAX_WIDTH = 480; // Max width for cover images (portrait display width)
|
||||
constexpr int TARGET_MAX_HEIGHT = 800; // Max height for cover images (portrait display height)
|
||||
// ============================================================================
|
||||
|
||||
// Integer approximation of gamma correction (brightens midtones)
|
||||
// Uses a simple curve: out = 255 * sqrt(in/255) ≈ sqrt(in * 255)
|
||||
static inline int applyGamma(int gray) {
|
||||
if (!GAMMA_CORRECTION) return gray;
|
||||
// Fast integer square root approximation for gamma ~0.5 (brightening)
|
||||
// This brightens dark/mid tones while preserving highlights
|
||||
const int product = gray * 255;
|
||||
// Newton-Raphson integer sqrt (2 iterations for good accuracy)
|
||||
int x = gray;
|
||||
if (x > 0) {
|
||||
x = (x + product / x) >> 1;
|
||||
x = (x + product / x) >> 1;
|
||||
}
|
||||
return x > 255 ? 255 : x;
|
||||
}
|
||||
|
||||
// Apply contrast adjustment around midpoint (128)
|
||||
// factor > 1.0 increases contrast, < 1.0 decreases
|
||||
static inline int applyContrast(int gray) {
|
||||
// Integer-based contrast: (gray - 128) * factor + 128
|
||||
// Using fixed-point: factor 1.15 ≈ 115/100
|
||||
constexpr int factorNum = static_cast<int>(CONTRAST_FACTOR * 100);
|
||||
int adjusted = ((gray - 128) * factorNum) / 100 + 128;
|
||||
if (adjusted < 0) adjusted = 0;
|
||||
if (adjusted > 255) adjusted = 255;
|
||||
return adjusted;
|
||||
}
|
||||
|
||||
// Combined brightness/contrast/gamma adjustment
|
||||
static inline int adjustPixel(int gray) {
|
||||
if (!USE_BRIGHTNESS) return gray;
|
||||
|
||||
// Order: contrast first, then brightness, then gamma
|
||||
gray = applyContrast(gray);
|
||||
gray += BRIGHTNESS_BOOST;
|
||||
if (gray > 255) gray = 255;
|
||||
if (gray < 0) gray = 0;
|
||||
gray = applyGamma(gray);
|
||||
|
||||
return gray;
|
||||
}
|
||||
|
||||
// Simple quantization without dithering - just divide into 4 levels
|
||||
static inline uint8_t quantizeSimple(int gray) {
|
||||
gray = adjustPixel(gray);
|
||||
// Simple 2-bit quantization: 0-63=0, 64-127=1, 128-191=2, 192-255=3
|
||||
return static_cast<uint8_t>(gray >> 6);
|
||||
}
|
||||
|
||||
// Hash-based noise dithering - survives downsampling without moiré artifacts
|
||||
// Uses integer hash to generate pseudo-random threshold per pixel
|
||||
static inline uint8_t quantizeNoise(int gray, int x, int y) {
|
||||
gray = adjustPixel(gray);
|
||||
|
||||
// Generate noise threshold using integer hash (no regular pattern to alias)
|
||||
uint32_t hash = static_cast<uint32_t>(x) * 374761393u + static_cast<uint32_t>(y) * 668265263u;
|
||||
hash = (hash ^ (hash >> 13)) * 1274126177u;
|
||||
const int threshold = static_cast<int>(hash >> 24); // 0-255
|
||||
|
||||
// Map gray (0-255) to 4 levels with dithering
|
||||
const int scaled = gray * 3;
|
||||
|
||||
if (scaled < 255) {
|
||||
return (scaled + threshold >= 255) ? 1 : 0;
|
||||
} else if (scaled < 510) {
|
||||
return ((scaled - 255) + threshold >= 255) ? 2 : 1;
|
||||
} else {
|
||||
return ((scaled - 510) + threshold >= 255) ? 3 : 2;
|
||||
}
|
||||
}
|
||||
|
||||
// Main quantization function - selects between methods based on config
|
||||
static inline uint8_t quantize(int gray, int x, int y) {
|
||||
if (USE_NOISE_DITHERING) {
|
||||
return quantizeNoise(gray, x, y);
|
||||
} else {
|
||||
return quantizeSimple(gray);
|
||||
}
|
||||
}
|
||||
|
||||
// Atkinson dithering - distributes only 6/8 (75%) of error for cleaner results
|
||||
// Error distribution pattern:
|
||||
// X 1/8 1/8
|
||||
// 1/8 1/8 1/8
|
||||
// 1/8
|
||||
// Less error buildup = fewer artifacts than Floyd-Steinberg
|
||||
class AtkinsonDitherer {
|
||||
public:
|
||||
AtkinsonDitherer(int width) : width(width) {
|
||||
errorRow0 = new int16_t[width + 4](); // Current row
|
||||
errorRow1 = new int16_t[width + 4](); // Next row
|
||||
errorRow2 = new int16_t[width + 4](); // Row after next
|
||||
}
|
||||
|
||||
~AtkinsonDitherer() {
|
||||
delete[] errorRow0;
|
||||
delete[] errorRow1;
|
||||
delete[] errorRow2;
|
||||
}
|
||||
|
||||
uint8_t processPixel(int gray, int x) {
|
||||
// Apply brightness/contrast/gamma adjustments
|
||||
gray = adjustPixel(gray);
|
||||
|
||||
// Add accumulated error
|
||||
int adjusted = gray + errorRow0[x + 2];
|
||||
if (adjusted < 0) adjusted = 0;
|
||||
if (adjusted > 255) adjusted = 255;
|
||||
|
||||
// Quantize to 4 levels
|
||||
uint8_t quantized;
|
||||
int quantizedValue;
|
||||
if (adjusted < 43) {
|
||||
quantized = 0;
|
||||
quantizedValue = 0;
|
||||
} else if (adjusted < 128) {
|
||||
quantized = 1;
|
||||
quantizedValue = 85;
|
||||
} else if (adjusted < 213) {
|
||||
quantized = 2;
|
||||
quantizedValue = 170;
|
||||
} else {
|
||||
quantized = 3;
|
||||
quantizedValue = 255;
|
||||
}
|
||||
|
||||
// Calculate error (only distribute 6/8 = 75%)
|
||||
int error = (adjusted - quantizedValue) >> 3; // error/8
|
||||
|
||||
// Distribute 1/8 to each of 6 neighbors
|
||||
errorRow0[x + 3] += error; // Right
|
||||
errorRow0[x + 4] += error; // Right+1
|
||||
errorRow1[x + 1] += error; // Bottom-left
|
||||
errorRow1[x + 2] += error; // Bottom
|
||||
errorRow1[x + 3] += error; // Bottom-right
|
||||
errorRow2[x + 2] += error; // Two rows down
|
||||
|
||||
return quantized;
|
||||
}
|
||||
|
||||
void nextRow() {
|
||||
int16_t* temp = errorRow0;
|
||||
errorRow0 = errorRow1;
|
||||
errorRow1 = errorRow2;
|
||||
errorRow2 = temp;
|
||||
memset(errorRow2, 0, (width + 4) * sizeof(int16_t));
|
||||
}
|
||||
|
||||
void reset() {
|
||||
memset(errorRow0, 0, (width + 4) * sizeof(int16_t));
|
||||
memset(errorRow1, 0, (width + 4) * sizeof(int16_t));
|
||||
memset(errorRow2, 0, (width + 4) * sizeof(int16_t));
|
||||
}
|
||||
|
||||
private:
|
||||
int width;
|
||||
int16_t* errorRow0;
|
||||
int16_t* errorRow1;
|
||||
int16_t* errorRow2;
|
||||
};
|
||||
|
||||
// Floyd-Steinberg error diffusion dithering with serpentine scanning
|
||||
// Serpentine scanning alternates direction each row to reduce "worm" artifacts
|
||||
// Error distribution pattern (left-to-right):
|
||||
// X 7/16
|
||||
// 3/16 5/16 1/16
|
||||
// Error distribution pattern (right-to-left, mirrored):
|
||||
// 1/16 5/16 3/16
|
||||
// 7/16 X
|
||||
class FloydSteinbergDitherer {
|
||||
public:
|
||||
FloydSteinbergDitherer(int width) : width(width), rowCount(0) {
|
||||
errorCurRow = new int16_t[width + 2](); // +2 for boundary handling
|
||||
errorNextRow = new int16_t[width + 2]();
|
||||
}
|
||||
|
||||
~FloydSteinbergDitherer() {
|
||||
delete[] errorCurRow;
|
||||
delete[] errorNextRow;
|
||||
}
|
||||
|
||||
// Process a single pixel and return quantized 2-bit value
|
||||
// x is the logical x position (0 to width-1), direction handled internally
|
||||
uint8_t processPixel(int gray, int x, bool reverseDirection) {
|
||||
// Add accumulated error to this pixel
|
||||
int adjusted = gray + errorCurRow[x + 1];
|
||||
|
||||
// Clamp to valid range
|
||||
if (adjusted < 0) adjusted = 0;
|
||||
if (adjusted > 255) adjusted = 255;
|
||||
|
||||
// Quantize to 4 levels (0, 85, 170, 255)
|
||||
uint8_t quantized;
|
||||
int quantizedValue;
|
||||
if (adjusted < 43) {
|
||||
quantized = 0;
|
||||
quantizedValue = 0;
|
||||
} else if (adjusted < 128) {
|
||||
quantized = 1;
|
||||
quantizedValue = 85;
|
||||
} else if (adjusted < 213) {
|
||||
quantized = 2;
|
||||
quantizedValue = 170;
|
||||
} else {
|
||||
quantized = 3;
|
||||
quantizedValue = 255;
|
||||
}
|
||||
|
||||
// Calculate error
|
||||
int error = adjusted - quantizedValue;
|
||||
|
||||
// Distribute error to neighbors (serpentine: direction-aware)
|
||||
if (!reverseDirection) {
|
||||
// Left to right: standard distribution
|
||||
// Right: 7/16
|
||||
errorCurRow[x + 2] += (error * 7) >> 4;
|
||||
// Bottom-left: 3/16
|
||||
errorNextRow[x] += (error * 3) >> 4;
|
||||
// Bottom: 5/16
|
||||
errorNextRow[x + 1] += (error * 5) >> 4;
|
||||
// Bottom-right: 1/16
|
||||
errorNextRow[x + 2] += (error) >> 4;
|
||||
} else {
|
||||
// Right to left: mirrored distribution
|
||||
// Left: 7/16
|
||||
errorCurRow[x] += (error * 7) >> 4;
|
||||
// Bottom-right: 3/16
|
||||
errorNextRow[x + 2] += (error * 3) >> 4;
|
||||
// Bottom: 5/16
|
||||
errorNextRow[x + 1] += (error * 5) >> 4;
|
||||
// Bottom-left: 1/16
|
||||
errorNextRow[x] += (error) >> 4;
|
||||
}
|
||||
|
||||
return quantized;
|
||||
}
|
||||
|
||||
// Call at the end of each row to swap buffers
|
||||
void nextRow() {
|
||||
// Swap buffers
|
||||
int16_t* temp = errorCurRow;
|
||||
errorCurRow = errorNextRow;
|
||||
errorNextRow = temp;
|
||||
// Clear the next row buffer
|
||||
memset(errorNextRow, 0, (width + 2) * sizeof(int16_t));
|
||||
rowCount++;
|
||||
}
|
||||
|
||||
// Check if current row should be processed in reverse
|
||||
bool isReverseRow() const { return (rowCount & 1) != 0; }
|
||||
|
||||
// Reset for a new image or MCU block
|
||||
void reset() {
|
||||
memset(errorCurRow, 0, (width + 2) * sizeof(int16_t));
|
||||
memset(errorNextRow, 0, (width + 2) * sizeof(int16_t));
|
||||
rowCount = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
int width;
|
||||
int rowCount;
|
||||
int16_t* errorCurRow;
|
||||
int16_t* errorNextRow;
|
||||
};
|
||||
|
||||
inline void write16(Print& out, const uint16_t value) {
|
||||
out.write(value & 0xFF);
|
||||
out.write((value >> 8) & 0xFF);
|
||||
}
|
||||
|
||||
inline void write32(Print& out, const uint32_t value) {
|
||||
out.write(value & 0xFF);
|
||||
out.write((value >> 8) & 0xFF);
|
||||
out.write((value >> 16) & 0xFF);
|
||||
out.write((value >> 24) & 0xFF);
|
||||
}
|
||||
|
||||
inline void write32Signed(Print& out, const int32_t value) {
|
||||
out.write(value & 0xFF);
|
||||
out.write((value >> 8) & 0xFF);
|
||||
out.write((value >> 16) & 0xFF);
|
||||
out.write((value >> 24) & 0xFF);
|
||||
}
|
||||
|
||||
// Helper function: Write BMP header with 8-bit grayscale (256 levels)
|
||||
void writeBmpHeader8bit(Print& bmpOut, const int width, const int height) {
|
||||
// Calculate row padding (each row must be multiple of 4 bytes)
|
||||
const int bytesPerRow = (width + 3) / 4 * 4; // 8 bits per pixel, padded
|
||||
const int imageSize = bytesPerRow * height;
|
||||
const uint32_t paletteSize = 256 * 4; // 256 colors * 4 bytes (BGRA)
|
||||
const uint32_t fileSize = 14 + 40 + paletteSize + imageSize;
|
||||
|
||||
// BMP File Header (14 bytes)
|
||||
bmpOut.write('B');
|
||||
bmpOut.write('M');
|
||||
write32(bmpOut, fileSize);
|
||||
write32(bmpOut, 0); // Reserved
|
||||
write32(bmpOut, 14 + 40 + paletteSize); // Offset to pixel data
|
||||
|
||||
// DIB Header (BITMAPINFOHEADER - 40 bytes)
|
||||
write32(bmpOut, 40);
|
||||
write32Signed(bmpOut, width);
|
||||
write32Signed(bmpOut, -height); // Negative height = top-down bitmap
|
||||
write16(bmpOut, 1); // Color planes
|
||||
write16(bmpOut, 8); // Bits per pixel (8 bits)
|
||||
write32(bmpOut, 0); // BI_RGB (no compression)
|
||||
write32(bmpOut, imageSize);
|
||||
write32(bmpOut, 2835); // xPixelsPerMeter (72 DPI)
|
||||
write32(bmpOut, 2835); // yPixelsPerMeter (72 DPI)
|
||||
write32(bmpOut, 256); // colorsUsed
|
||||
write32(bmpOut, 256); // colorsImportant
|
||||
|
||||
// Color Palette (256 grayscale entries x 4 bytes = 1024 bytes)
|
||||
for (int i = 0; i < 256; i++) {
|
||||
bmpOut.write(static_cast<uint8_t>(i)); // Blue
|
||||
bmpOut.write(static_cast<uint8_t>(i)); // Green
|
||||
bmpOut.write(static_cast<uint8_t>(i)); // Red
|
||||
bmpOut.write(static_cast<uint8_t>(0)); // Reserved
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function: Write BMP header with 2-bit color depth
|
||||
void JpegToBmpConverter::writeBmpHeader(Print& bmpOut, const int width, const int height) {
|
||||
// Calculate row padding (each row must be multiple of 4 bytes)
|
||||
const int bytesPerRow = (width * 2 + 31) / 32 * 4; // 2 bits per pixel, round up
|
||||
const int imageSize = bytesPerRow * height;
|
||||
const uint32_t fileSize = 70 + imageSize; // 14 (file header) + 40 (DIB header) + 16 (palette) + image
|
||||
|
||||
// BMP File Header (14 bytes)
|
||||
bmpOut.write('B');
|
||||
bmpOut.write('M');
|
||||
write32(bmpOut, fileSize); // File size
|
||||
write32(bmpOut, 0); // Reserved
|
||||
write32(bmpOut, 70); // Offset to pixel data
|
||||
|
||||
// DIB Header (BITMAPINFOHEADER - 40 bytes)
|
||||
write32(bmpOut, 40);
|
||||
write32Signed(bmpOut, width);
|
||||
write32Signed(bmpOut, -height); // Negative height = top-down bitmap
|
||||
write16(bmpOut, 1); // Color planes
|
||||
write16(bmpOut, 2); // Bits per pixel (2 bits)
|
||||
write32(bmpOut, 0); // BI_RGB (no compression)
|
||||
write32(bmpOut, imageSize);
|
||||
write32(bmpOut, 2835); // xPixelsPerMeter (72 DPI)
|
||||
write32(bmpOut, 2835); // yPixelsPerMeter (72 DPI)
|
||||
write32(bmpOut, 4); // colorsUsed
|
||||
write32(bmpOut, 4); // colorsImportant
|
||||
|
||||
// Color Palette (4 colors x 4 bytes = 16 bytes)
|
||||
// Format: Blue, Green, Red, Reserved (BGRA)
|
||||
uint8_t palette[16] = {
|
||||
0x00, 0x00, 0x00, 0x00, // Color 0: Black
|
||||
0x55, 0x55, 0x55, 0x00, // Color 1: Dark gray (85)
|
||||
0xAA, 0xAA, 0xAA, 0x00, // Color 2: Light gray (170)
|
||||
0xFF, 0xFF, 0xFF, 0x00 // Color 3: White
|
||||
};
|
||||
for (const uint8_t i : palette) {
|
||||
bmpOut.write(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Callback function for picojpeg to read JPEG data
|
||||
unsigned char JpegToBmpConverter::jpegReadCallback(unsigned char* pBuf, const unsigned char buf_size,
|
||||
unsigned char* pBytes_actually_read, void* pCallback_data) {
|
||||
auto* context = static_cast<JpegReadContext*>(pCallback_data);
|
||||
|
||||
if (!context || !context->file) {
|
||||
return PJPG_STREAM_READ_ERROR;
|
||||
}
|
||||
|
||||
// Check if we need to refill our context buffer
|
||||
if (context->bufferPos >= context->bufferFilled) {
|
||||
context->bufferFilled = context->file.read(context->buffer, sizeof(context->buffer));
|
||||
context->bufferPos = 0;
|
||||
|
||||
if (context->bufferFilled == 0) {
|
||||
// EOF or error
|
||||
*pBytes_actually_read = 0;
|
||||
return 0; // Success (EOF is normal)
|
||||
}
|
||||
}
|
||||
|
||||
// Copy available bytes to picojpeg's buffer
|
||||
const size_t available = context->bufferFilled - context->bufferPos;
|
||||
const size_t toRead = available < buf_size ? available : buf_size;
|
||||
|
||||
memcpy(pBuf, context->buffer + context->bufferPos, toRead);
|
||||
context->bufferPos += toRead;
|
||||
*pBytes_actually_read = static_cast<unsigned char>(toRead);
|
||||
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
// Core function: Convert JPEG file to 2-bit BMP
|
||||
bool JpegToBmpConverter::jpegFileToBmpStream(File& jpegFile, Print& bmpOut) {
|
||||
Serial.printf("[%lu] [JPG] Converting JPEG to BMP\n", millis());
|
||||
|
||||
// Setup context for picojpeg callback
|
||||
JpegReadContext context = {.file = jpegFile, .bufferPos = 0, .bufferFilled = 0};
|
||||
|
||||
// Initialize picojpeg decoder
|
||||
pjpeg_image_info_t imageInfo;
|
||||
const unsigned char status = pjpeg_decode_init(&imageInfo, jpegReadCallback, &context, 0);
|
||||
if (status != 0) {
|
||||
Serial.printf("[%lu] [JPG] JPEG decode init failed with error code: %d\n", millis(), status);
|
||||
return false;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [JPG] JPEG dimensions: %dx%d, components: %d, MCUs: %dx%d\n", millis(), imageInfo.m_width,
|
||||
imageInfo.m_height, imageInfo.m_comps, imageInfo.m_MCUSPerRow, imageInfo.m_MCUSPerCol);
|
||||
|
||||
// Safety limits to prevent memory issues on ESP32
|
||||
constexpr int MAX_IMAGE_WIDTH = 2048;
|
||||
constexpr int MAX_IMAGE_HEIGHT = 3072;
|
||||
constexpr int MAX_MCU_ROW_BYTES = 65536;
|
||||
|
||||
if (imageInfo.m_width > MAX_IMAGE_WIDTH || imageInfo.m_height > MAX_IMAGE_HEIGHT) {
|
||||
Serial.printf("[%lu] [JPG] Image too large (%dx%d), max supported: %dx%d\n", millis(), imageInfo.m_width,
|
||||
imageInfo.m_height, MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Calculate output dimensions (pre-scale to fit display exactly)
|
||||
int outWidth = imageInfo.m_width;
|
||||
int outHeight = imageInfo.m_height;
|
||||
// Use fixed-point scaling (16.16) for sub-pixel accuracy
|
||||
uint32_t scaleX_fp = 65536; // 1.0 in 16.16 fixed point
|
||||
uint32_t scaleY_fp = 65536;
|
||||
bool needsScaling = false;
|
||||
|
||||
if (USE_PRESCALE && (imageInfo.m_width > TARGET_MAX_WIDTH || imageInfo.m_height > TARGET_MAX_HEIGHT)) {
|
||||
// Calculate scale to fit within target dimensions while maintaining aspect ratio
|
||||
const float scaleToFitWidth = static_cast<float>(TARGET_MAX_WIDTH) / imageInfo.m_width;
|
||||
const float scaleToFitHeight = static_cast<float>(TARGET_MAX_HEIGHT) / imageInfo.m_height;
|
||||
const float scale = (scaleToFitWidth < scaleToFitHeight) ? scaleToFitWidth : scaleToFitHeight;
|
||||
|
||||
outWidth = static_cast<int>(imageInfo.m_width * scale);
|
||||
outHeight = static_cast<int>(imageInfo.m_height * scale);
|
||||
|
||||
// Ensure at least 1 pixel
|
||||
if (outWidth < 1) outWidth = 1;
|
||||
if (outHeight < 1) outHeight = 1;
|
||||
|
||||
// Calculate fixed-point scale factors (source pixels per output pixel)
|
||||
// scaleX_fp = (srcWidth << 16) / outWidth
|
||||
scaleX_fp = (static_cast<uint32_t>(imageInfo.m_width) << 16) / outWidth;
|
||||
scaleY_fp = (static_cast<uint32_t>(imageInfo.m_height) << 16) / outHeight;
|
||||
needsScaling = true;
|
||||
|
||||
Serial.printf("[%lu] [JPG] Pre-scaling %dx%d -> %dx%d (fit to %dx%d)\n", millis(), imageInfo.m_width,
|
||||
imageInfo.m_height, outWidth, outHeight, TARGET_MAX_WIDTH, TARGET_MAX_HEIGHT);
|
||||
}
|
||||
|
||||
// Write BMP header with output dimensions
|
||||
int bytesPerRow;
|
||||
if (USE_8BIT_OUTPUT) {
|
||||
writeBmpHeader8bit(bmpOut, outWidth, outHeight);
|
||||
bytesPerRow = (outWidth + 3) / 4 * 4;
|
||||
} else {
|
||||
writeBmpHeader(bmpOut, outWidth, outHeight);
|
||||
bytesPerRow = (outWidth * 2 + 31) / 32 * 4;
|
||||
}
|
||||
|
||||
// Allocate row buffer
|
||||
auto* rowBuffer = static_cast<uint8_t*>(malloc(bytesPerRow));
|
||||
if (!rowBuffer) {
|
||||
Serial.printf("[%lu] [JPG] Failed to allocate row buffer\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Allocate a buffer for one MCU row worth of grayscale pixels
|
||||
// This is the minimal memory needed for streaming conversion
|
||||
const int mcuPixelHeight = imageInfo.m_MCUHeight;
|
||||
const int mcuRowPixels = imageInfo.m_width * mcuPixelHeight;
|
||||
|
||||
// Validate MCU row buffer size before allocation
|
||||
if (mcuRowPixels > MAX_MCU_ROW_BYTES) {
|
||||
Serial.printf("[%lu] [JPG] MCU row buffer too large (%d bytes), max: %d\n", millis(), mcuRowPixels,
|
||||
MAX_MCU_ROW_BYTES);
|
||||
free(rowBuffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
auto* mcuRowBuffer = static_cast<uint8_t*>(malloc(mcuRowPixels));
|
||||
if (!mcuRowBuffer) {
|
||||
Serial.printf("[%lu] [JPG] Failed to allocate MCU row buffer (%d bytes)\n", millis(), mcuRowPixels);
|
||||
free(rowBuffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create ditherer if enabled (only for 2-bit output)
|
||||
// Use OUTPUT dimensions for dithering (after prescaling)
|
||||
AtkinsonDitherer* atkinsonDitherer = nullptr;
|
||||
FloydSteinbergDitherer* fsDitherer = nullptr;
|
||||
if (!USE_8BIT_OUTPUT) {
|
||||
if (USE_ATKINSON) {
|
||||
atkinsonDitherer = new AtkinsonDitherer(outWidth);
|
||||
} else if (USE_FLOYD_STEINBERG) {
|
||||
fsDitherer = new FloydSteinbergDitherer(outWidth);
|
||||
}
|
||||
}
|
||||
|
||||
// For scaling: accumulate source rows into scaled output rows
|
||||
// We need to track which source Y maps to which output Y
|
||||
// Using fixed-point: srcY_fp = outY * scaleY_fp (gives source Y in 16.16 format)
|
||||
uint32_t* rowAccum = nullptr; // Accumulator for each output X (32-bit for larger sums)
|
||||
uint16_t* rowCount = nullptr; // Count of source pixels accumulated per output X
|
||||
int currentOutY = 0; // Current output row being accumulated
|
||||
uint32_t nextOutY_srcStart = 0; // Source Y where next output row starts (16.16 fixed point)
|
||||
|
||||
if (needsScaling) {
|
||||
rowAccum = new uint32_t[outWidth]();
|
||||
rowCount = new uint16_t[outWidth]();
|
||||
nextOutY_srcStart = scaleY_fp; // First boundary is at scaleY_fp (source Y for outY=1)
|
||||
}
|
||||
|
||||
// Process MCUs row-by-row and write to BMP as we go (top-down)
|
||||
const int mcuPixelWidth = imageInfo.m_MCUWidth;
|
||||
|
||||
for (int mcuY = 0; mcuY < imageInfo.m_MCUSPerCol; mcuY++) {
|
||||
// Clear the MCU row buffer
|
||||
memset(mcuRowBuffer, 0, mcuRowPixels);
|
||||
|
||||
// Decode one row of MCUs
|
||||
for (int mcuX = 0; mcuX < imageInfo.m_MCUSPerRow; mcuX++) {
|
||||
const unsigned char mcuStatus = pjpeg_decode_mcu();
|
||||
if (mcuStatus != 0) {
|
||||
if (mcuStatus == PJPG_NO_MORE_BLOCKS) {
|
||||
Serial.printf("[%lu] [JPG] Unexpected end of blocks at MCU (%d, %d)\n", millis(), mcuX, mcuY);
|
||||
} else {
|
||||
Serial.printf("[%lu] [JPG] JPEG decode MCU failed at (%d, %d) with error code: %d\n", millis(), mcuX, mcuY,
|
||||
mcuStatus);
|
||||
}
|
||||
free(mcuRowBuffer);
|
||||
free(rowBuffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
// picojpeg stores MCU data in 8x8 blocks
|
||||
// Block layout: H2V2(16x16)=0,64,128,192 H2V1(16x8)=0,64 H1V2(8x16)=0,128
|
||||
for (int blockY = 0; blockY < mcuPixelHeight; blockY++) {
|
||||
for (int blockX = 0; blockX < mcuPixelWidth; blockX++) {
|
||||
const int pixelX = mcuX * mcuPixelWidth + blockX;
|
||||
if (pixelX >= imageInfo.m_width) continue;
|
||||
|
||||
// Calculate proper block offset for picojpeg buffer
|
||||
const int blockCol = blockX / 8;
|
||||
const int blockRow = blockY / 8;
|
||||
const int localX = blockX % 8;
|
||||
const int localY = blockY % 8;
|
||||
const int blocksPerRow = mcuPixelWidth / 8;
|
||||
const int blockIndex = blockRow * blocksPerRow + blockCol;
|
||||
const int pixelOffset = blockIndex * 64 + localY * 8 + localX;
|
||||
|
||||
uint8_t gray;
|
||||
if (imageInfo.m_comps == 1) {
|
||||
gray = imageInfo.m_pMCUBufR[pixelOffset];
|
||||
} else {
|
||||
const uint8_t r = imageInfo.m_pMCUBufR[pixelOffset];
|
||||
const uint8_t g = imageInfo.m_pMCUBufG[pixelOffset];
|
||||
const uint8_t b = imageInfo.m_pMCUBufB[pixelOffset];
|
||||
gray = (r * 25 + g * 50 + b * 25) / 100;
|
||||
}
|
||||
|
||||
mcuRowBuffer[blockY * imageInfo.m_width + pixelX] = gray;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process source rows from this MCU row
|
||||
const int startRow = mcuY * mcuPixelHeight;
|
||||
const int endRow = (mcuY + 1) * mcuPixelHeight;
|
||||
|
||||
for (int y = startRow; y < endRow && y < imageInfo.m_height; y++) {
|
||||
const int bufferY = y - startRow;
|
||||
|
||||
if (!needsScaling) {
|
||||
// No scaling - direct output (1:1 mapping)
|
||||
memset(rowBuffer, 0, bytesPerRow);
|
||||
|
||||
if (USE_8BIT_OUTPUT) {
|
||||
for (int x = 0; x < outWidth; x++) {
|
||||
const uint8_t gray = mcuRowBuffer[bufferY * imageInfo.m_width + x];
|
||||
rowBuffer[x] = adjustPixel(gray);
|
||||
}
|
||||
} else {
|
||||
for (int x = 0; x < outWidth; x++) {
|
||||
const uint8_t gray = mcuRowBuffer[bufferY * imageInfo.m_width + x];
|
||||
uint8_t twoBit;
|
||||
if (atkinsonDitherer) {
|
||||
twoBit = atkinsonDitherer->processPixel(gray, x);
|
||||
} else if (fsDitherer) {
|
||||
twoBit = fsDitherer->processPixel(gray, x, fsDitherer->isReverseRow());
|
||||
} else {
|
||||
twoBit = quantize(gray, x, y);
|
||||
}
|
||||
const int byteIndex = (x * 2) / 8;
|
||||
const int bitOffset = 6 - ((x * 2) % 8);
|
||||
rowBuffer[byteIndex] |= (twoBit << bitOffset);
|
||||
}
|
||||
if (atkinsonDitherer)
|
||||
atkinsonDitherer->nextRow();
|
||||
else if (fsDitherer)
|
||||
fsDitherer->nextRow();
|
||||
}
|
||||
bmpOut.write(rowBuffer, bytesPerRow);
|
||||
} else {
|
||||
// Fixed-point area averaging for exact fit scaling
|
||||
// For each output pixel X, accumulate source pixels that map to it
|
||||
// srcX range for outX: [outX * scaleX_fp >> 16, (outX+1) * scaleX_fp >> 16)
|
||||
const uint8_t* srcRow = mcuRowBuffer + bufferY * imageInfo.m_width;
|
||||
|
||||
for (int outX = 0; outX < outWidth; outX++) {
|
||||
// Calculate source X range for this output pixel
|
||||
const int srcXStart = (static_cast<uint32_t>(outX) * scaleX_fp) >> 16;
|
||||
const int srcXEnd = (static_cast<uint32_t>(outX + 1) * scaleX_fp) >> 16;
|
||||
|
||||
// Accumulate all source pixels in this range
|
||||
int sum = 0;
|
||||
int count = 0;
|
||||
for (int srcX = srcXStart; srcX < srcXEnd && srcX < imageInfo.m_width; srcX++) {
|
||||
sum += srcRow[srcX];
|
||||
count++;
|
||||
}
|
||||
|
||||
// Handle edge case: if no pixels in range, use nearest
|
||||
if (count == 0 && srcXStart < imageInfo.m_width) {
|
||||
sum = srcRow[srcXStart];
|
||||
count = 1;
|
||||
}
|
||||
|
||||
rowAccum[outX] += sum;
|
||||
rowCount[outX] += count;
|
||||
}
|
||||
|
||||
// Check if we've crossed into the next output row
|
||||
// Current source Y in fixed point: y << 16
|
||||
const uint32_t srcY_fp = static_cast<uint32_t>(y + 1) << 16;
|
||||
|
||||
// Output row when source Y crosses the boundary
|
||||
if (srcY_fp >= nextOutY_srcStart && currentOutY < outHeight) {
|
||||
memset(rowBuffer, 0, bytesPerRow);
|
||||
|
||||
if (USE_8BIT_OUTPUT) {
|
||||
for (int x = 0; x < outWidth; x++) {
|
||||
const uint8_t gray = (rowCount[x] > 0) ? (rowAccum[x] / rowCount[x]) : 0;
|
||||
rowBuffer[x] = adjustPixel(gray);
|
||||
}
|
||||
} else {
|
||||
for (int x = 0; x < outWidth; x++) {
|
||||
const uint8_t gray = (rowCount[x] > 0) ? (rowAccum[x] / rowCount[x]) : 0;
|
||||
uint8_t twoBit;
|
||||
if (atkinsonDitherer) {
|
||||
twoBit = atkinsonDitherer->processPixel(gray, x);
|
||||
} else if (fsDitherer) {
|
||||
twoBit = fsDitherer->processPixel(gray, x, fsDitherer->isReverseRow());
|
||||
} else {
|
||||
twoBit = quantize(gray, x, currentOutY);
|
||||
}
|
||||
const int byteIndex = (x * 2) / 8;
|
||||
const int bitOffset = 6 - ((x * 2) % 8);
|
||||
rowBuffer[byteIndex] |= (twoBit << bitOffset);
|
||||
}
|
||||
if (atkinsonDitherer)
|
||||
atkinsonDitherer->nextRow();
|
||||
else if (fsDitherer)
|
||||
fsDitherer->nextRow();
|
||||
}
|
||||
|
||||
bmpOut.write(rowBuffer, bytesPerRow);
|
||||
currentOutY++;
|
||||
|
||||
// Reset accumulators for next output row
|
||||
memset(rowAccum, 0, outWidth * sizeof(uint32_t));
|
||||
memset(rowCount, 0, outWidth * sizeof(uint16_t));
|
||||
|
||||
// Update boundary for next output row
|
||||
nextOutY_srcStart = static_cast<uint32_t>(currentOutY + 1) * scaleY_fp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up
|
||||
if (rowAccum) {
|
||||
delete[] rowAccum;
|
||||
}
|
||||
if (rowCount) {
|
||||
delete[] rowCount;
|
||||
}
|
||||
if (atkinsonDitherer) {
|
||||
delete atkinsonDitherer;
|
||||
}
|
||||
if (fsDitherer) {
|
||||
delete fsDitherer;
|
||||
}
|
||||
free(mcuRowBuffer);
|
||||
free(rowBuffer);
|
||||
|
||||
Serial.printf("[%lu] [JPG] Successfully converted JPEG to BMP\n", millis());
|
||||
return true;
|
||||
}
|
||||
15
lib/JpegToBmpConverter/JpegToBmpConverter.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <FS.h>
|
||||
|
||||
class ZipFile;
|
||||
|
||||
class JpegToBmpConverter {
|
||||
static void writeBmpHeader(Print& bmpOut, int width, int height);
|
||||
// [COMMENTED OUT] static uint8_t grayscaleTo2Bit(uint8_t grayscale, int x, int y);
|
||||
static unsigned char jpegReadCallback(unsigned char* pBuf, unsigned char buf_size,
|
||||
unsigned char* pBytes_actually_read, void* pCallback_data);
|
||||
|
||||
public:
|
||||
static bool jpegFileToBmpStream(File& jpegFile, Print& bmpOut);
|
||||
};
|
||||
@@ -1,4 +1,6 @@
|
||||
#pragma once
|
||||
#include <FS.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace serialization {
|
||||
@@ -7,21 +9,44 @@ static void writePod(std::ostream& os, const T& value) {
|
||||
os.write(reinterpret_cast<const char*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void writePod(File& file, const T& value) {
|
||||
file.write(reinterpret_cast<const uint8_t*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void readPod(std::istream& is, T& value) {
|
||||
is.read(reinterpret_cast<char*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void readPod(File& file, T& value) {
|
||||
file.read(reinterpret_cast<uint8_t*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
static void writeString(std::ostream& os, const std::string& s) {
|
||||
const uint32_t len = s.size();
|
||||
writePod(os, len);
|
||||
os.write(s.data(), len);
|
||||
}
|
||||
|
||||
static void writeString(File& file, const std::string& s) {
|
||||
const uint32_t len = s.size();
|
||||
writePod(file, len);
|
||||
file.write(reinterpret_cast<const uint8_t*>(s.data()), len);
|
||||
}
|
||||
|
||||
static void readString(std::istream& is, std::string& s) {
|
||||
uint32_t len;
|
||||
readPod(is, len);
|
||||
s.resize(len);
|
||||
is.read(&s[0], len);
|
||||
}
|
||||
|
||||
static void readString(File& file, std::string& s) {
|
||||
uint32_t len;
|
||||
readPod(file, len);
|
||||
s.resize(len);
|
||||
file.read(reinterpret_cast<uint8_t*>(&s[0]), len);
|
||||
}
|
||||
} // namespace serialization
|
||||
|
||||
40
lib/Xtc/README
Normal file
@@ -0,0 +1,40 @@
|
||||
# XTC/XTCH Library
|
||||
|
||||
XTC ebook format support for CrossPoint Reader.
|
||||
|
||||
## Supported Formats
|
||||
|
||||
| Format | Extension | Description |
|
||||
|--------|-----------|----------------------------------------------|
|
||||
| XTC | `.xtc` | Container with XTG pages (1-bit monochrome) |
|
||||
| XTCH | `.xtch` | Container with XTH pages (2-bit grayscale) |
|
||||
|
||||
## Format Overview
|
||||
|
||||
XTC/XTCH are container formats designed for ESP32 e-paper displays. They store pre-rendered bitmap pages optimized for the XTeink X4 e-reader (480x800 resolution).
|
||||
|
||||
### Container Structure (XTC/XTCH)
|
||||
|
||||
- 56-byte header with metadata offsets
|
||||
- Optional metadata (title, author, etc.)
|
||||
- Page index table (16 bytes per page)
|
||||
- Page data (XTG or XTH format)
|
||||
|
||||
### Page Formats
|
||||
|
||||
#### XTG (1-bit monochrome)
|
||||
|
||||
- Row-major storage, 8 pixels per byte
|
||||
- MSB first (bit 7 = leftmost pixel)
|
||||
- 0 = Black, 1 = White
|
||||
|
||||
#### XTH (2-bit grayscale)
|
||||
|
||||
- Two bit planes stored sequentially
|
||||
- Column-major order (right to left)
|
||||
- 8 vertical pixels per byte
|
||||
- Grayscale: 0=White, 1=Dark Grey, 2=Light Grey, 3=Black
|
||||
|
||||
## Reference
|
||||
|
||||
Original format info: <https://gist.github.com/CrazyCoder/b125f26d6987c0620058249f59f1327d>
|
||||
337
lib/Xtc/Xtc.cpp
Normal file
@@ -0,0 +1,337 @@
|
||||
/**
|
||||
* Xtc.cpp
|
||||
*
|
||||
* Main XTC ebook class implementation
|
||||
* XTC ebook support for CrossPoint Reader
|
||||
*/
|
||||
|
||||
#include "Xtc.h"
|
||||
|
||||
#include <FsHelpers.h>
|
||||
#include <HardwareSerial.h>
|
||||
#include <SD.h>
|
||||
|
||||
bool Xtc::load() {
|
||||
Serial.printf("[%lu] [XTC] Loading XTC: %s\n", millis(), filepath.c_str());
|
||||
|
||||
// Initialize parser
|
||||
parser.reset(new xtc::XtcParser());
|
||||
|
||||
// Open XTC file
|
||||
xtc::XtcError err = parser->open(filepath.c_str());
|
||||
if (err != xtc::XtcError::OK) {
|
||||
Serial.printf("[%lu] [XTC] Failed to load: %s\n", millis(), xtc::errorToString(err));
|
||||
parser.reset();
|
||||
return false;
|
||||
}
|
||||
|
||||
loaded = true;
|
||||
Serial.printf("[%lu] [XTC] Loaded XTC: %s (%lu pages)\n", millis(), filepath.c_str(), parser->getPageCount());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Xtc::clearCache() const {
|
||||
if (!SD.exists(cachePath.c_str())) {
|
||||
Serial.printf("[%lu] [XTC] Cache does not exist, no action needed\n", millis());
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!FsHelpers::removeDir(cachePath.c_str())) {
|
||||
Serial.printf("[%lu] [XTC] Failed to clear cache\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [XTC] Cache cleared successfully\n", millis());
|
||||
return true;
|
||||
}
|
||||
|
||||
void Xtc::setupCacheDir() const {
|
||||
if (SD.exists(cachePath.c_str())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Create directories recursively
|
||||
for (size_t i = 1; i < cachePath.length(); i++) {
|
||||
if (cachePath[i] == '/') {
|
||||
SD.mkdir(cachePath.substr(0, i).c_str());
|
||||
}
|
||||
}
|
||||
SD.mkdir(cachePath.c_str());
|
||||
}
|
||||
|
||||
std::string Xtc::getTitle() const {
|
||||
if (!loaded || !parser) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Try to get title from XTC metadata first
|
||||
std::string title = parser->getTitle();
|
||||
if (!title.empty()) {
|
||||
return title;
|
||||
}
|
||||
|
||||
// Fallback: extract filename from path as title
|
||||
size_t lastSlash = filepath.find_last_of('/');
|
||||
size_t lastDot = filepath.find_last_of('.');
|
||||
|
||||
if (lastSlash == std::string::npos) {
|
||||
lastSlash = 0;
|
||||
} else {
|
||||
lastSlash++;
|
||||
}
|
||||
|
||||
if (lastDot == std::string::npos || lastDot <= lastSlash) {
|
||||
return filepath.substr(lastSlash);
|
||||
}
|
||||
|
||||
return filepath.substr(lastSlash, lastDot - lastSlash);
|
||||
}
|
||||
|
||||
std::string Xtc::getCoverBmpPath() const { return cachePath + "/cover.bmp"; }
|
||||
|
||||
bool Xtc::generateCoverBmp() const {
|
||||
// Already generated
|
||||
if (SD.exists(getCoverBmpPath().c_str())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!loaded || !parser) {
|
||||
Serial.printf("[%lu] [XTC] Cannot generate cover BMP, file not loaded\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (parser->getPageCount() == 0) {
|
||||
Serial.printf("[%lu] [XTC] No pages in XTC file\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Setup cache directory
|
||||
setupCacheDir();
|
||||
|
||||
// Get first page info for cover
|
||||
xtc::PageInfo pageInfo;
|
||||
if (!parser->getPageInfo(0, pageInfo)) {
|
||||
Serial.printf("[%lu] [XTC] Failed to get first page info\n", millis());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get bit depth
|
||||
const uint8_t bitDepth = parser->getBitDepth();
|
||||
|
||||
// Allocate buffer for page data
|
||||
// XTG (1-bit): Row-major, ((width+7)/8) * height bytes
|
||||
// XTH (2-bit): Two bit planes, column-major, ((width * height + 7) / 8) * 2 bytes
|
||||
size_t bitmapSize;
|
||||
if (bitDepth == 2) {
|
||||
bitmapSize = ((static_cast<size_t>(pageInfo.width) * pageInfo.height + 7) / 8) * 2;
|
||||
} else {
|
||||
bitmapSize = ((pageInfo.width + 7) / 8) * pageInfo.height;
|
||||
}
|
||||
uint8_t* pageBuffer = static_cast<uint8_t*>(malloc(bitmapSize));
|
||||
if (!pageBuffer) {
|
||||
Serial.printf("[%lu] [XTC] Failed to allocate page buffer (%lu bytes)\n", millis(), bitmapSize);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load first page (cover)
|
||||
size_t bytesRead = const_cast<xtc::XtcParser*>(parser.get())->loadPage(0, pageBuffer, bitmapSize);
|
||||
if (bytesRead == 0) {
|
||||
Serial.printf("[%lu] [XTC] Failed to load cover page\n", millis());
|
||||
free(pageBuffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create BMP file
|
||||
File coverBmp;
|
||||
if (!FsHelpers::openFileForWrite("XTC", getCoverBmpPath(), coverBmp)) {
|
||||
Serial.printf("[%lu] [XTC] Failed to create cover BMP file\n", millis());
|
||||
free(pageBuffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write BMP header
|
||||
// BMP file header (14 bytes)
|
||||
const uint32_t rowSize = ((pageInfo.width + 31) / 32) * 4; // Row size aligned to 4 bytes
|
||||
const uint32_t imageSize = rowSize * pageInfo.height;
|
||||
const uint32_t fileSize = 14 + 40 + 8 + imageSize; // Header + DIB + palette + data
|
||||
|
||||
// File header
|
||||
coverBmp.write('B');
|
||||
coverBmp.write('M');
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&fileSize), 4);
|
||||
uint32_t reserved = 0;
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&reserved), 4);
|
||||
uint32_t dataOffset = 14 + 40 + 8; // 1-bit palette has 2 colors (8 bytes)
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&dataOffset), 4);
|
||||
|
||||
// DIB header (BITMAPINFOHEADER - 40 bytes)
|
||||
uint32_t dibHeaderSize = 40;
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&dibHeaderSize), 4);
|
||||
int32_t width = pageInfo.width;
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&width), 4);
|
||||
int32_t height = -static_cast<int32_t>(pageInfo.height); // Negative for top-down
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&height), 4);
|
||||
uint16_t planes = 1;
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&planes), 2);
|
||||
uint16_t bitsPerPixel = 1; // 1-bit monochrome
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&bitsPerPixel), 2);
|
||||
uint32_t compression = 0; // BI_RGB (no compression)
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&compression), 4);
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&imageSize), 4);
|
||||
int32_t ppmX = 2835; // 72 DPI
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&ppmX), 4);
|
||||
int32_t ppmY = 2835;
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&ppmY), 4);
|
||||
uint32_t colorsUsed = 2;
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&colorsUsed), 4);
|
||||
uint32_t colorsImportant = 2;
|
||||
coverBmp.write(reinterpret_cast<const uint8_t*>(&colorsImportant), 4);
|
||||
|
||||
// Color palette (2 colors for 1-bit)
|
||||
// XTC uses inverted polarity: 0 = black, 1 = white
|
||||
// Color 0: Black (text/foreground in XTC)
|
||||
uint8_t black[4] = {0x00, 0x00, 0x00, 0x00};
|
||||
coverBmp.write(black, 4);
|
||||
// Color 1: White (background in XTC)
|
||||
uint8_t white[4] = {0xFF, 0xFF, 0xFF, 0x00};
|
||||
coverBmp.write(white, 4);
|
||||
|
||||
// Write bitmap data
|
||||
// BMP requires 4-byte row alignment
|
||||
const size_t dstRowSize = (pageInfo.width + 7) / 8; // 1-bit destination row size
|
||||
|
||||
if (bitDepth == 2) {
|
||||
// XTH 2-bit mode: Two bit planes, column-major order
|
||||
// - Columns scanned right to left (x = width-1 down to 0)
|
||||
// - 8 vertical pixels per byte (MSB = topmost pixel in group)
|
||||
// - First plane: Bit1, Second plane: Bit2
|
||||
// - Pixel value = (bit1 << 1) | bit2
|
||||
const size_t planeSize = (static_cast<size_t>(pageInfo.width) * pageInfo.height + 7) / 8;
|
||||
const uint8_t* plane1 = pageBuffer; // Bit1 plane
|
||||
const uint8_t* plane2 = pageBuffer + planeSize; // Bit2 plane
|
||||
const size_t colBytes = (pageInfo.height + 7) / 8; // Bytes per column
|
||||
|
||||
// Allocate a row buffer for 1-bit output
|
||||
uint8_t* rowBuffer = static_cast<uint8_t*>(malloc(dstRowSize));
|
||||
if (!rowBuffer) {
|
||||
free(pageBuffer);
|
||||
coverBmp.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint16_t y = 0; y < pageInfo.height; y++) {
|
||||
memset(rowBuffer, 0xFF, dstRowSize); // Start with all white
|
||||
|
||||
for (uint16_t x = 0; x < pageInfo.width; x++) {
|
||||
// Column-major, right to left: column index = (width - 1 - x)
|
||||
const size_t colIndex = pageInfo.width - 1 - x;
|
||||
const size_t byteInCol = y / 8;
|
||||
const size_t bitInByte = 7 - (y % 8); // MSB = topmost pixel
|
||||
|
||||
const size_t byteOffset = colIndex * colBytes + byteInCol;
|
||||
const uint8_t bit1 = (plane1[byteOffset] >> bitInByte) & 1;
|
||||
const uint8_t bit2 = (plane2[byteOffset] >> bitInByte) & 1;
|
||||
const uint8_t pixelValue = (bit1 << 1) | bit2;
|
||||
|
||||
// Threshold: 0=white (1); 1,2,3=black (0)
|
||||
if (pixelValue >= 1) {
|
||||
// Set bit to 0 (black) in BMP format
|
||||
const size_t dstByte = x / 8;
|
||||
const size_t dstBit = 7 - (x % 8);
|
||||
rowBuffer[dstByte] &= ~(1 << dstBit);
|
||||
}
|
||||
}
|
||||
|
||||
// Write converted row
|
||||
coverBmp.write(rowBuffer, dstRowSize);
|
||||
|
||||
// Pad to 4-byte boundary
|
||||
uint8_t padding[4] = {0, 0, 0, 0};
|
||||
size_t paddingSize = rowSize - dstRowSize;
|
||||
if (paddingSize > 0) {
|
||||
coverBmp.write(padding, paddingSize);
|
||||
}
|
||||
}
|
||||
|
||||
free(rowBuffer);
|
||||
} else {
|
||||
// 1-bit source: write directly with proper padding
|
||||
const size_t srcRowSize = (pageInfo.width + 7) / 8;
|
||||
|
||||
for (uint16_t y = 0; y < pageInfo.height; y++) {
|
||||
// Write source row
|
||||
coverBmp.write(pageBuffer + y * srcRowSize, srcRowSize);
|
||||
|
||||
// Pad to 4-byte boundary
|
||||
uint8_t padding[4] = {0, 0, 0, 0};
|
||||
size_t paddingSize = rowSize - srcRowSize;
|
||||
if (paddingSize > 0) {
|
||||
coverBmp.write(padding, paddingSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
coverBmp.close();
|
||||
free(pageBuffer);
|
||||
|
||||
Serial.printf("[%lu] [XTC] Generated cover BMP: %s\n", millis(), getCoverBmpPath().c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t Xtc::getPageCount() const {
|
||||
if (!loaded || !parser) {
|
||||
return 0;
|
||||
}
|
||||
return parser->getPageCount();
|
||||
}
|
||||
|
||||
uint16_t Xtc::getPageWidth() const {
|
||||
if (!loaded || !parser) {
|
||||
return 0;
|
||||
}
|
||||
return parser->getWidth();
|
||||
}
|
||||
|
||||
uint16_t Xtc::getPageHeight() const {
|
||||
if (!loaded || !parser) {
|
||||
return 0;
|
||||
}
|
||||
return parser->getHeight();
|
||||
}
|
||||
|
||||
uint8_t Xtc::getBitDepth() const {
|
||||
if (!loaded || !parser) {
|
||||
return 1; // Default to 1-bit
|
||||
}
|
||||
return parser->getBitDepth();
|
||||
}
|
||||
|
||||
size_t Xtc::loadPage(uint32_t pageIndex, uint8_t* buffer, size_t bufferSize) const {
|
||||
if (!loaded || !parser) {
|
||||
return 0;
|
||||
}
|
||||
return const_cast<xtc::XtcParser*>(parser.get())->loadPage(pageIndex, buffer, bufferSize);
|
||||
}
|
||||
|
||||
xtc::XtcError Xtc::loadPageStreaming(uint32_t pageIndex,
|
||||
std::function<void(const uint8_t* data, size_t size, size_t offset)> callback,
|
||||
size_t chunkSize) const {
|
||||
if (!loaded || !parser) {
|
||||
return xtc::XtcError::FILE_NOT_FOUND;
|
||||
}
|
||||
return const_cast<xtc::XtcParser*>(parser.get())->loadPageStreaming(pageIndex, callback, chunkSize);
|
||||
}
|
||||
|
||||
uint8_t Xtc::calculateProgress(uint32_t currentPage) const {
|
||||
if (!loaded || !parser || parser->getPageCount() == 0) {
|
||||
return 0;
|
||||
}
|
||||
return static_cast<uint8_t>((currentPage + 1) * 100 / parser->getPageCount());
|
||||
}
|
||||
|
||||
xtc::XtcError Xtc::getLastError() const {
|
||||
if (!parser) {
|
||||
return xtc::XtcError::FILE_NOT_FOUND;
|
||||
}
|
||||
return parser->getLastError();
|
||||
}
|
||||
97
lib/Xtc/Xtc.h
Normal file
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Xtc.h
|
||||
*
|
||||
* Main XTC ebook class for CrossPoint Reader
|
||||
* Provides EPUB-like interface for XTC file handling
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "Xtc/XtcParser.h"
|
||||
#include "Xtc/XtcTypes.h"
|
||||
|
||||
/**
|
||||
* XTC Ebook Handler
|
||||
*
|
||||
* Handles XTC file loading, page access, and cover image generation.
|
||||
* Interface is designed to be similar to Epub class for easy integration.
|
||||
*/
|
||||
class Xtc {
|
||||
std::string filepath;
|
||||
std::string cachePath;
|
||||
std::unique_ptr<xtc::XtcParser> parser;
|
||||
bool loaded;
|
||||
|
||||
public:
|
||||
explicit Xtc(std::string filepath, const std::string& cacheDir) : filepath(std::move(filepath)), loaded(false) {
|
||||
// Create cache key based on filepath (same as Epub)
|
||||
cachePath = cacheDir + "/xtc_" + std::to_string(std::hash<std::string>{}(this->filepath));
|
||||
}
|
||||
~Xtc() = default;
|
||||
|
||||
/**
|
||||
* Load XTC file
|
||||
* @return true on success
|
||||
*/
|
||||
bool load();
|
||||
|
||||
/**
|
||||
* Clear cached data
|
||||
* @return true on success
|
||||
*/
|
||||
bool clearCache() const;
|
||||
|
||||
/**
|
||||
* Setup cache directory
|
||||
*/
|
||||
void setupCacheDir() const;
|
||||
|
||||
// Path accessors
|
||||
const std::string& getCachePath() const { return cachePath; }
|
||||
const std::string& getPath() const { return filepath; }
|
||||
|
||||
// Metadata
|
||||
std::string getTitle() const;
|
||||
|
||||
// Cover image support (for sleep screen)
|
||||
std::string getCoverBmpPath() const;
|
||||
bool generateCoverBmp() const;
|
||||
|
||||
// Page access
|
||||
uint32_t getPageCount() const;
|
||||
uint16_t getPageWidth() const;
|
||||
uint16_t getPageHeight() const;
|
||||
uint8_t getBitDepth() const; // 1 = XTC (1-bit), 2 = XTCH (2-bit)
|
||||
|
||||
/**
|
||||
* Load page bitmap data
|
||||
* @param pageIndex Page index (0-based)
|
||||
* @param buffer Output buffer
|
||||
* @param bufferSize Buffer size
|
||||
* @return Number of bytes read
|
||||
*/
|
||||
size_t loadPage(uint32_t pageIndex, uint8_t* buffer, size_t bufferSize) const;
|
||||
|
||||
/**
|
||||
* Load page with streaming callback
|
||||
* @param pageIndex Page index
|
||||
* @param callback Callback for each chunk
|
||||
* @param chunkSize Chunk size
|
||||
* @return Error code
|
||||
*/
|
||||
xtc::XtcError loadPageStreaming(uint32_t pageIndex,
|
||||
std::function<void(const uint8_t* data, size_t size, size_t offset)> callback,
|
||||
size_t chunkSize = 1024) const;
|
||||
|
||||
// Progress calculation
|
||||
uint8_t calculateProgress(uint32_t currentPage) const;
|
||||
|
||||
// Check if file is loaded
|
||||
bool isLoaded() const { return loaded; }
|
||||
|
||||
// Error information
|
||||
xtc::XtcError getLastError() const;
|
||||
};
|
||||
316
lib/Xtc/Xtc/XtcParser.cpp
Normal file
@@ -0,0 +1,316 @@
|
||||
/**
|
||||
* XtcParser.cpp
|
||||
*
|
||||
* XTC file parsing implementation
|
||||
* XTC ebook support for CrossPoint Reader
|
||||
*/
|
||||
|
||||
#include "XtcParser.h"
|
||||
|
||||
#include <FsHelpers.h>
|
||||
#include <HardwareSerial.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace xtc {
|
||||
|
||||
XtcParser::XtcParser()
|
||||
: m_isOpen(false),
|
||||
m_defaultWidth(DISPLAY_WIDTH),
|
||||
m_defaultHeight(DISPLAY_HEIGHT),
|
||||
m_bitDepth(1),
|
||||
m_lastError(XtcError::OK) {
|
||||
memset(&m_header, 0, sizeof(m_header));
|
||||
}
|
||||
|
||||
XtcParser::~XtcParser() { close(); }
|
||||
|
||||
XtcError XtcParser::open(const char* filepath) {
|
||||
// Close if already open
|
||||
if (m_isOpen) {
|
||||
close();
|
||||
}
|
||||
|
||||
// Open file
|
||||
if (!FsHelpers::openFileForRead("XTC", filepath, m_file)) {
|
||||
m_lastError = XtcError::FILE_NOT_FOUND;
|
||||
return m_lastError;
|
||||
}
|
||||
|
||||
// Read header
|
||||
m_lastError = readHeader();
|
||||
if (m_lastError != XtcError::OK) {
|
||||
Serial.printf("[%lu] [XTC] Failed to read header: %s\n", millis(), errorToString(m_lastError));
|
||||
m_file.close();
|
||||
return m_lastError;
|
||||
}
|
||||
|
||||
// Read title if available
|
||||
readTitle();
|
||||
|
||||
// Read page table
|
||||
m_lastError = readPageTable();
|
||||
if (m_lastError != XtcError::OK) {
|
||||
Serial.printf("[%lu] [XTC] Failed to read page table: %s\n", millis(), errorToString(m_lastError));
|
||||
m_file.close();
|
||||
return m_lastError;
|
||||
}
|
||||
|
||||
m_isOpen = true;
|
||||
Serial.printf("[%lu] [XTC] Opened file: %s (%u pages, %dx%d)\n", millis(), filepath, m_header.pageCount,
|
||||
m_defaultWidth, m_defaultHeight);
|
||||
return XtcError::OK;
|
||||
}
|
||||
|
||||
void XtcParser::close() {
|
||||
if (m_isOpen) {
|
||||
m_file.close();
|
||||
m_isOpen = false;
|
||||
}
|
||||
m_pageTable.clear();
|
||||
m_title.clear();
|
||||
memset(&m_header, 0, sizeof(m_header));
|
||||
}
|
||||
|
||||
XtcError XtcParser::readHeader() {
|
||||
// Read first 56 bytes of header
|
||||
size_t bytesRead = m_file.read(reinterpret_cast<uint8_t*>(&m_header), sizeof(XtcHeader));
|
||||
if (bytesRead != sizeof(XtcHeader)) {
|
||||
return XtcError::READ_ERROR;
|
||||
}
|
||||
|
||||
// Verify magic number (accept both XTC and XTCH)
|
||||
if (m_header.magic != XTC_MAGIC && m_header.magic != XTCH_MAGIC) {
|
||||
Serial.printf("[%lu] [XTC] Invalid magic: 0x%08X (expected 0x%08X or 0x%08X)\n", millis(), m_header.magic,
|
||||
XTC_MAGIC, XTCH_MAGIC);
|
||||
return XtcError::INVALID_MAGIC;
|
||||
}
|
||||
|
||||
// Determine bit depth from file magic
|
||||
m_bitDepth = (m_header.magic == XTCH_MAGIC) ? 2 : 1;
|
||||
|
||||
// Check version
|
||||
if (m_header.version > 1) {
|
||||
Serial.printf("[%lu] [XTC] Unsupported version: %d\n", millis(), m_header.version);
|
||||
return XtcError::INVALID_VERSION;
|
||||
}
|
||||
|
||||
// Basic validation
|
||||
if (m_header.pageCount == 0) {
|
||||
return XtcError::CORRUPTED_HEADER;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [XTC] Header: magic=0x%08X (%s), ver=%u, pages=%u, bitDepth=%u\n", millis(), m_header.magic,
|
||||
(m_header.magic == XTCH_MAGIC) ? "XTCH" : "XTC", m_header.version, m_header.pageCount, m_bitDepth);
|
||||
|
||||
return XtcError::OK;
|
||||
}
|
||||
|
||||
XtcError XtcParser::readTitle() {
|
||||
// Title is usually at offset 0x38 (56) for 88-byte headers
|
||||
// Read title as null-terminated UTF-8 string
|
||||
if (m_header.titleOffset == 0) {
|
||||
m_header.titleOffset = 0x38; // Default offset
|
||||
}
|
||||
|
||||
if (!m_file.seek(m_header.titleOffset)) {
|
||||
return XtcError::READ_ERROR;
|
||||
}
|
||||
|
||||
char titleBuf[128] = {0};
|
||||
m_file.read(reinterpret_cast<uint8_t*>(titleBuf), sizeof(titleBuf) - 1);
|
||||
m_title = titleBuf;
|
||||
|
||||
Serial.printf("[%lu] [XTC] Title: %s\n", millis(), m_title.c_str());
|
||||
return XtcError::OK;
|
||||
}
|
||||
|
||||
XtcError XtcParser::readPageTable() {
|
||||
if (m_header.pageTableOffset == 0) {
|
||||
Serial.printf("[%lu] [XTC] Page table offset is 0, cannot read\n", millis());
|
||||
return XtcError::CORRUPTED_HEADER;
|
||||
}
|
||||
|
||||
// Seek to page table
|
||||
if (!m_file.seek(m_header.pageTableOffset)) {
|
||||
Serial.printf("[%lu] [XTC] Failed to seek to page table at %llu\n", millis(), m_header.pageTableOffset);
|
||||
return XtcError::READ_ERROR;
|
||||
}
|
||||
|
||||
m_pageTable.resize(m_header.pageCount);
|
||||
|
||||
// Read page table entries
|
||||
for (uint16_t i = 0; i < m_header.pageCount; i++) {
|
||||
PageTableEntry entry;
|
||||
size_t bytesRead = m_file.read(reinterpret_cast<uint8_t*>(&entry), sizeof(PageTableEntry));
|
||||
if (bytesRead != sizeof(PageTableEntry)) {
|
||||
Serial.printf("[%lu] [XTC] Failed to read page table entry %u\n", millis(), i);
|
||||
return XtcError::READ_ERROR;
|
||||
}
|
||||
|
||||
m_pageTable[i].offset = static_cast<uint32_t>(entry.dataOffset);
|
||||
m_pageTable[i].size = entry.dataSize;
|
||||
m_pageTable[i].width = entry.width;
|
||||
m_pageTable[i].height = entry.height;
|
||||
m_pageTable[i].bitDepth = m_bitDepth;
|
||||
|
||||
// Update default dimensions from first page
|
||||
if (i == 0) {
|
||||
m_defaultWidth = entry.width;
|
||||
m_defaultHeight = entry.height;
|
||||
}
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [XTC] Read %u page table entries\n", millis(), m_header.pageCount);
|
||||
return XtcError::OK;
|
||||
}
|
||||
|
||||
bool XtcParser::getPageInfo(uint32_t pageIndex, PageInfo& info) const {
|
||||
if (pageIndex >= m_pageTable.size()) {
|
||||
return false;
|
||||
}
|
||||
info = m_pageTable[pageIndex];
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t XtcParser::loadPage(uint32_t pageIndex, uint8_t* buffer, size_t bufferSize) {
|
||||
if (!m_isOpen) {
|
||||
m_lastError = XtcError::FILE_NOT_FOUND;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (pageIndex >= m_header.pageCount) {
|
||||
m_lastError = XtcError::PAGE_OUT_OF_RANGE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const PageInfo& page = m_pageTable[pageIndex];
|
||||
|
||||
// Seek to page data
|
||||
if (!m_file.seek(page.offset)) {
|
||||
Serial.printf("[%lu] [XTC] Failed to seek to page %u at offset %lu\n", millis(), pageIndex, page.offset);
|
||||
m_lastError = XtcError::READ_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read page header (XTG for 1-bit, XTH for 2-bit - same structure)
|
||||
XtgPageHeader pageHeader;
|
||||
size_t headerRead = m_file.read(reinterpret_cast<uint8_t*>(&pageHeader), sizeof(XtgPageHeader));
|
||||
if (headerRead != sizeof(XtgPageHeader)) {
|
||||
Serial.printf("[%lu] [XTC] Failed to read page header for page %u\n", millis(), pageIndex);
|
||||
m_lastError = XtcError::READ_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Verify page magic (XTG for 1-bit, XTH for 2-bit)
|
||||
const uint32_t expectedMagic = (m_bitDepth == 2) ? XTH_MAGIC : XTG_MAGIC;
|
||||
if (pageHeader.magic != expectedMagic) {
|
||||
Serial.printf("[%lu] [XTC] Invalid page magic for page %u: 0x%08X (expected 0x%08X)\n", millis(), pageIndex,
|
||||
pageHeader.magic, expectedMagic);
|
||||
m_lastError = XtcError::INVALID_MAGIC;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calculate bitmap size based on bit depth
|
||||
// XTG (1-bit): Row-major, ((width+7)/8) * height bytes
|
||||
// XTH (2-bit): Two bit planes, column-major, ((width * height + 7) / 8) * 2 bytes
|
||||
size_t bitmapSize;
|
||||
if (m_bitDepth == 2) {
|
||||
// XTH: two bit planes, each containing (width * height) bits rounded up to bytes
|
||||
bitmapSize = ((static_cast<size_t>(pageHeader.width) * pageHeader.height + 7) / 8) * 2;
|
||||
} else {
|
||||
bitmapSize = ((pageHeader.width + 7) / 8) * pageHeader.height;
|
||||
}
|
||||
|
||||
// Check buffer size
|
||||
if (bufferSize < bitmapSize) {
|
||||
Serial.printf("[%lu] [XTC] Buffer too small: need %u, have %u\n", millis(), bitmapSize, bufferSize);
|
||||
m_lastError = XtcError::MEMORY_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read bitmap data
|
||||
size_t bytesRead = m_file.read(buffer, bitmapSize);
|
||||
if (bytesRead != bitmapSize) {
|
||||
Serial.printf("[%lu] [XTC] Page read error: expected %u, got %u\n", millis(), bitmapSize, bytesRead);
|
||||
m_lastError = XtcError::READ_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
m_lastError = XtcError::OK;
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
XtcError XtcParser::loadPageStreaming(uint32_t pageIndex,
|
||||
std::function<void(const uint8_t* data, size_t size, size_t offset)> callback,
|
||||
size_t chunkSize) {
|
||||
if (!m_isOpen) {
|
||||
return XtcError::FILE_NOT_FOUND;
|
||||
}
|
||||
|
||||
if (pageIndex >= m_header.pageCount) {
|
||||
return XtcError::PAGE_OUT_OF_RANGE;
|
||||
}
|
||||
|
||||
const PageInfo& page = m_pageTable[pageIndex];
|
||||
|
||||
// Seek to page data
|
||||
if (!m_file.seek(page.offset)) {
|
||||
return XtcError::READ_ERROR;
|
||||
}
|
||||
|
||||
// Read and skip page header (XTG for 1-bit, XTH for 2-bit)
|
||||
XtgPageHeader pageHeader;
|
||||
size_t headerRead = m_file.read(reinterpret_cast<uint8_t*>(&pageHeader), sizeof(XtgPageHeader));
|
||||
const uint32_t expectedMagic = (m_bitDepth == 2) ? XTH_MAGIC : XTG_MAGIC;
|
||||
if (headerRead != sizeof(XtgPageHeader) || pageHeader.magic != expectedMagic) {
|
||||
return XtcError::READ_ERROR;
|
||||
}
|
||||
|
||||
// Calculate bitmap size based on bit depth
|
||||
// XTG (1-bit): Row-major, ((width+7)/8) * height bytes
|
||||
// XTH (2-bit): Two bit planes, ((width * height + 7) / 8) * 2 bytes
|
||||
size_t bitmapSize;
|
||||
if (m_bitDepth == 2) {
|
||||
bitmapSize = ((static_cast<size_t>(pageHeader.width) * pageHeader.height + 7) / 8) * 2;
|
||||
} else {
|
||||
bitmapSize = ((pageHeader.width + 7) / 8) * pageHeader.height;
|
||||
}
|
||||
|
||||
// Read in chunks
|
||||
std::vector<uint8_t> chunk(chunkSize);
|
||||
size_t totalRead = 0;
|
||||
|
||||
while (totalRead < bitmapSize) {
|
||||
size_t toRead = std::min(chunkSize, bitmapSize - totalRead);
|
||||
size_t bytesRead = m_file.read(chunk.data(), toRead);
|
||||
|
||||
if (bytesRead == 0) {
|
||||
return XtcError::READ_ERROR;
|
||||
}
|
||||
|
||||
callback(chunk.data(), bytesRead, totalRead);
|
||||
totalRead += bytesRead;
|
||||
}
|
||||
|
||||
return XtcError::OK;
|
||||
}
|
||||
|
||||
bool XtcParser::isValidXtcFile(const char* filepath) {
|
||||
File file = SD.open(filepath, FILE_READ);
|
||||
if (!file) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t magic = 0;
|
||||
size_t bytesRead = file.read(reinterpret_cast<uint8_t*>(&magic), sizeof(magic));
|
||||
file.close();
|
||||
|
||||
if (bytesRead != sizeof(magic)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (magic == XTC_MAGIC || magic == XTCH_MAGIC);
|
||||
}
|
||||
|
||||
} // namespace xtc
|
||||
96
lib/Xtc/Xtc/XtcParser.h
Normal file
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* XtcParser.h
|
||||
*
|
||||
* XTC file parsing and page data extraction
|
||||
* XTC ebook support for CrossPoint Reader
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SD.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "XtcTypes.h"
|
||||
|
||||
namespace xtc {
|
||||
|
||||
/**
|
||||
* XTC File Parser
|
||||
*
|
||||
* Reads XTC files from SD card and extracts page data.
|
||||
* Designed for ESP32-C3's limited RAM (~380KB) using streaming.
|
||||
*/
|
||||
class XtcParser {
|
||||
public:
|
||||
XtcParser();
|
||||
~XtcParser();
|
||||
|
||||
// File open/close
|
||||
XtcError open(const char* filepath);
|
||||
void close();
|
||||
bool isOpen() const { return m_isOpen; }
|
||||
|
||||
// Header information access
|
||||
const XtcHeader& getHeader() const { return m_header; }
|
||||
uint16_t getPageCount() const { return m_header.pageCount; }
|
||||
uint16_t getWidth() const { return m_defaultWidth; }
|
||||
uint16_t getHeight() const { return m_defaultHeight; }
|
||||
uint8_t getBitDepth() const { return m_bitDepth; } // 1 = XTC/XTG, 2 = XTCH/XTH
|
||||
|
||||
// Page information
|
||||
bool getPageInfo(uint32_t pageIndex, PageInfo& info) const;
|
||||
|
||||
/**
|
||||
* Load page bitmap (raw 1-bit data, skipping XTG header)
|
||||
*
|
||||
* @param pageIndex Page index (0-based)
|
||||
* @param buffer Output buffer (caller allocated)
|
||||
* @param bufferSize Buffer size
|
||||
* @return Number of bytes read on success, 0 on failure
|
||||
*/
|
||||
size_t loadPage(uint32_t pageIndex, uint8_t* buffer, size_t bufferSize);
|
||||
|
||||
/**
|
||||
* Streaming page load
|
||||
* Memory-efficient method that reads page data in chunks.
|
||||
*
|
||||
* @param pageIndex Page index
|
||||
* @param callback Callback function to receive data chunks
|
||||
* @param chunkSize Chunk size (default: 1024 bytes)
|
||||
* @return Error code
|
||||
*/
|
||||
XtcError loadPageStreaming(uint32_t pageIndex,
|
||||
std::function<void(const uint8_t* data, size_t size, size_t offset)> callback,
|
||||
size_t chunkSize = 1024);
|
||||
|
||||
// Get title from metadata
|
||||
std::string getTitle() const { return m_title; }
|
||||
|
||||
// Validation
|
||||
static bool isValidXtcFile(const char* filepath);
|
||||
|
||||
// Error information
|
||||
XtcError getLastError() const { return m_lastError; }
|
||||
|
||||
private:
|
||||
File m_file;
|
||||
bool m_isOpen;
|
||||
XtcHeader m_header;
|
||||
std::vector<PageInfo> m_pageTable;
|
||||
std::string m_title;
|
||||
uint16_t m_defaultWidth;
|
||||
uint16_t m_defaultHeight;
|
||||
uint8_t m_bitDepth; // 1 = XTC/XTG (1-bit), 2 = XTCH/XTH (2-bit)
|
||||
XtcError m_lastError;
|
||||
|
||||
// Internal helper functions
|
||||
XtcError readHeader();
|
||||
XtcError readPageTable();
|
||||
XtcError readTitle();
|
||||
};
|
||||
|
||||
} // namespace xtc
|
||||
147
lib/Xtc/Xtc/XtcTypes.h
Normal file
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* XtcTypes.h
|
||||
*
|
||||
* XTC file format type definitions
|
||||
* XTC ebook support for CrossPoint Reader
|
||||
*
|
||||
* XTC is the native binary ebook format for XTeink X4 e-reader.
|
||||
* It stores pre-rendered bitmap images per page.
|
||||
*
|
||||
* Format based on EPUB2XTC converter by Rafal-P-Mazur
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace xtc {
|
||||
|
||||
// XTC file magic numbers (little-endian)
|
||||
// "XTC\0" = 0x58, 0x54, 0x43, 0x00
|
||||
constexpr uint32_t XTC_MAGIC = 0x00435458; // "XTC\0" in little-endian (1-bit fast mode)
|
||||
// "XTCH" = 0x58, 0x54, 0x43, 0x48
|
||||
constexpr uint32_t XTCH_MAGIC = 0x48435458; // "XTCH" in little-endian (2-bit high quality mode)
|
||||
// "XTG\0" = 0x58, 0x54, 0x47, 0x00
|
||||
constexpr uint32_t XTG_MAGIC = 0x00475458; // "XTG\0" for 1-bit page data
|
||||
// "XTH\0" = 0x58, 0x54, 0x48, 0x00
|
||||
constexpr uint32_t XTH_MAGIC = 0x00485458; // "XTH\0" for 2-bit page data
|
||||
|
||||
// XTeink X4 display resolution
|
||||
constexpr uint16_t DISPLAY_WIDTH = 480;
|
||||
constexpr uint16_t DISPLAY_HEIGHT = 800;
|
||||
|
||||
// XTC file header (56 bytes)
|
||||
#pragma pack(push, 1)
|
||||
struct XtcHeader {
|
||||
uint32_t magic; // 0x00: Magic number "XTC\0" (0x00435458)
|
||||
uint16_t version; // 0x04: Format version (typically 1)
|
||||
uint16_t pageCount; // 0x06: Total page count
|
||||
uint32_t flags; // 0x08: Flags/reserved
|
||||
uint32_t headerSize; // 0x0C: Size of header section (typically 88)
|
||||
uint32_t reserved1; // 0x10: Reserved
|
||||
uint32_t tocOffset; // 0x14: TOC offset (0 if unused) - 4 bytes, not 8!
|
||||
uint64_t pageTableOffset; // 0x18: Page table offset
|
||||
uint64_t dataOffset; // 0x20: First page data offset
|
||||
uint64_t reserved2; // 0x28: Reserved
|
||||
uint32_t titleOffset; // 0x30: Title string offset
|
||||
uint32_t padding; // 0x34: Padding to 56 bytes
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
// Page table entry (16 bytes per page)
|
||||
#pragma pack(push, 1)
|
||||
struct PageTableEntry {
|
||||
uint64_t dataOffset; // 0x00: Absolute offset to page data
|
||||
uint32_t dataSize; // 0x08: Page data size in bytes
|
||||
uint16_t width; // 0x0C: Page width (480)
|
||||
uint16_t height; // 0x0E: Page height (800)
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
// XTG/XTH page data header (22 bytes)
|
||||
// Used for both 1-bit (XTG) and 2-bit (XTH) formats
|
||||
#pragma pack(push, 1)
|
||||
struct XtgPageHeader {
|
||||
uint32_t magic; // 0x00: File identifier (XTG: 0x00475458, XTH: 0x00485458)
|
||||
uint16_t width; // 0x04: Image width (pixels)
|
||||
uint16_t height; // 0x06: Image height (pixels)
|
||||
uint8_t colorMode; // 0x08: Color mode (0=monochrome)
|
||||
uint8_t compression; // 0x09: Compression (0=uncompressed)
|
||||
uint32_t dataSize; // 0x0A: Image data size (bytes)
|
||||
uint64_t md5; // 0x0E: MD5 checksum (first 8 bytes, optional)
|
||||
// Followed by bitmap data at offset 0x16 (22)
|
||||
//
|
||||
// XTG (1-bit): Row-major, 8 pixels/byte, MSB first
|
||||
// dataSize = ((width + 7) / 8) * height
|
||||
//
|
||||
// XTH (2-bit): Two bit planes, column-major (right-to-left), 8 vertical pixels/byte
|
||||
// dataSize = ((width * height + 7) / 8) * 2
|
||||
// First plane: Bit1 for all pixels
|
||||
// Second plane: Bit2 for all pixels
|
||||
// pixelValue = (bit1 << 1) | bit2
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
// Page information (internal use, optimized for memory)
|
||||
struct PageInfo {
|
||||
uint32_t offset; // File offset to page data (max 4GB file size)
|
||||
uint32_t size; // Data size (bytes)
|
||||
uint16_t width; // Page width
|
||||
uint16_t height; // Page height
|
||||
uint8_t bitDepth; // 1 = XTG (1-bit), 2 = XTH (2-bit grayscale)
|
||||
uint8_t padding; // Alignment padding
|
||||
}; // 16 bytes total
|
||||
|
||||
// Error codes
|
||||
enum class XtcError {
|
||||
OK = 0,
|
||||
FILE_NOT_FOUND,
|
||||
INVALID_MAGIC,
|
||||
INVALID_VERSION,
|
||||
CORRUPTED_HEADER,
|
||||
PAGE_OUT_OF_RANGE,
|
||||
READ_ERROR,
|
||||
WRITE_ERROR,
|
||||
MEMORY_ERROR,
|
||||
DECOMPRESSION_ERROR,
|
||||
};
|
||||
|
||||
// Convert error code to string
|
||||
inline const char* errorToString(XtcError err) {
|
||||
switch (err) {
|
||||
case XtcError::OK:
|
||||
return "OK";
|
||||
case XtcError::FILE_NOT_FOUND:
|
||||
return "File not found";
|
||||
case XtcError::INVALID_MAGIC:
|
||||
return "Invalid magic number";
|
||||
case XtcError::INVALID_VERSION:
|
||||
return "Unsupported version";
|
||||
case XtcError::CORRUPTED_HEADER:
|
||||
return "Corrupted header";
|
||||
case XtcError::PAGE_OUT_OF_RANGE:
|
||||
return "Page out of range";
|
||||
case XtcError::READ_ERROR:
|
||||
return "Read error";
|
||||
case XtcError::WRITE_ERROR:
|
||||
return "Write error";
|
||||
case XtcError::MEMORY_ERROR:
|
||||
return "Memory allocation error";
|
||||
case XtcError::DECOMPRESSION_ERROR:
|
||||
return "Decompression error";
|
||||
default:
|
||||
return "Unknown error";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if filename has XTC/XTCH extension
|
||||
*/
|
||||
inline bool isXtcExtension(const char* filename) {
|
||||
if (!filename) return false;
|
||||
const char* ext = strrchr(filename, '.');
|
||||
if (!ext) return false;
|
||||
return (strcasecmp(ext, ".xtc") == 0 || strcasecmp(ext, ".xtch") == 0);
|
||||
}
|
||||
|
||||
} // namespace xtc
|
||||
@@ -27,31 +27,28 @@ bool inflateOneShot(const uint8_t* inputBuf, const size_t deflatedSize, uint8_t*
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ZipFile::loadFileStat(const char* filename, mz_zip_archive_file_stat* fileStat) const {
|
||||
mz_zip_archive zipArchive = {};
|
||||
const bool status = mz_zip_reader_init_file(&zipArchive, filePath.c_str(), 0);
|
||||
ZipFile::ZipFile(std::string filePath) : filePath(std::move(filePath)) {
|
||||
const bool status = mz_zip_reader_init_file(&zipArchive, this->filePath.c_str(), 0);
|
||||
|
||||
if (!status) {
|
||||
Serial.printf("[%lu] [ZIP] mz_zip_reader_init_file() failed! Error: %s\n", millis(),
|
||||
Serial.printf("[%lu] [ZIP] mz_zip_reader_init_file() failed for %s! Error: %s\n", millis(), this->filePath.c_str(),
|
||||
mz_zip_get_error_string(zipArchive.m_last_error));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ZipFile::loadFileStat(const char* filename, mz_zip_archive_file_stat* fileStat) const {
|
||||
// find the file
|
||||
mz_uint32 fileIndex = 0;
|
||||
if (!mz_zip_reader_locate_file_v2(&zipArchive, filename, nullptr, 0, &fileIndex)) {
|
||||
Serial.printf("[%lu] [ZIP] Could not find file %s\n", millis(), filename);
|
||||
mz_zip_reader_end(&zipArchive);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!mz_zip_reader_file_stat(&zipArchive, fileIndex, fileStat)) {
|
||||
Serial.printf("[%lu] [ZIP] mz_zip_reader_file_stat() failed! Error: %s\n", millis(),
|
||||
mz_zip_get_error_string(zipArchive.m_last_error));
|
||||
mz_zip_reader_end(&zipArchive);
|
||||
return false;
|
||||
}
|
||||
mz_zip_reader_end(&zipArchive);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -62,6 +59,10 @@ long ZipFile::getDataOffset(const mz_zip_archive_file_stat& fileStat) const {
|
||||
const uint64_t fileOffset = fileStat.m_local_header_ofs;
|
||||
|
||||
FILE* file = fopen(filePath.c_str(), "r");
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [ZIP] Failed to open file for reading local header\n", millis());
|
||||
return -1;
|
||||
}
|
||||
fseek(file, fileOffset, SEEK_SET);
|
||||
const size_t read = fread(pLocalHeader, 1, localHeaderSize, file);
|
||||
fclose(file);
|
||||
@@ -104,12 +105,21 @@ uint8_t* ZipFile::readFileToMemory(const char* filename, size_t* size, const boo
|
||||
}
|
||||
|
||||
FILE* file = fopen(filePath.c_str(), "rb");
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [ZIP] Failed to open file for reading\n", millis());
|
||||
return nullptr;
|
||||
}
|
||||
fseek(file, fileOffset, SEEK_SET);
|
||||
|
||||
const auto deflatedDataSize = static_cast<size_t>(fileStat.m_comp_size);
|
||||
const auto inflatedDataSize = static_cast<size_t>(fileStat.m_uncomp_size);
|
||||
const auto dataSize = trailingNullByte ? inflatedDataSize + 1 : inflatedDataSize;
|
||||
const auto data = static_cast<uint8_t*>(malloc(dataSize));
|
||||
if (data == nullptr) {
|
||||
Serial.printf("[%lu] [ZIP] Failed to allocate memory for output buffer (%zu bytes)\n", millis(), dataSize);
|
||||
fclose(file);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (fileStat.m_method == MZ_NO_COMPRESSION) {
|
||||
// no deflation, just read content
|
||||
@@ -175,6 +185,10 @@ bool ZipFile::readFileToStream(const char* filename, Print& out, const size_t ch
|
||||
}
|
||||
|
||||
FILE* file = fopen(filePath.c_str(), "rb");
|
||||
if (!file) {
|
||||
Serial.printf("[%lu] [ZIP] Failed to open file for streaming\n", millis());
|
||||
return false;
|
||||
}
|
||||
fseek(file, fileOffset, SEEK_SET);
|
||||
|
||||
const auto deflatedDataSize = static_cast<size_t>(fileStat.m_comp_size);
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
#pragma once
|
||||
#include <Print.h>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "miniz.h"
|
||||
|
||||
class ZipFile {
|
||||
std::string filePath;
|
||||
mutable mz_zip_archive zipArchive = {};
|
||||
bool loadFileStat(const char* filename, mz_zip_archive_file_stat* fileStat) const;
|
||||
long getDataOffset(const mz_zip_archive_file_stat& fileStat) const;
|
||||
|
||||
public:
|
||||
explicit ZipFile(std::string filePath) : filePath(std::move(filePath)) {}
|
||||
~ZipFile() = default;
|
||||
explicit ZipFile(std::string filePath);
|
||||
~ZipFile() { mz_zip_reader_end(&zipArchive); }
|
||||
bool getInflatedFileSize(const char* filename, size_t* size) const;
|
||||
uint8_t* readFileToMemory(const char* filename, size_t* size = nullptr, bool trailingNullByte = false) const;
|
||||
bool readFileToStream(const char* filename, Print& out, size_t chunkSize) const;
|
||||
|
||||
2087
lib/picojpeg/picojpeg.c
Normal file
124
lib/picojpeg/picojpeg.h
Normal file
@@ -0,0 +1,124 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// picojpeg - Public domain, Rich Geldreich <richgel99@gmail.com>
|
||||
//------------------------------------------------------------------------------
|
||||
#ifndef PICOJPEG_H
|
||||
#define PICOJPEG_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Error codes
|
||||
enum {
|
||||
PJPG_NO_MORE_BLOCKS = 1,
|
||||
PJPG_BAD_DHT_COUNTS,
|
||||
PJPG_BAD_DHT_INDEX,
|
||||
PJPG_BAD_DHT_MARKER,
|
||||
PJPG_BAD_DQT_MARKER,
|
||||
PJPG_BAD_DQT_TABLE,
|
||||
PJPG_BAD_PRECISION,
|
||||
PJPG_BAD_HEIGHT,
|
||||
PJPG_BAD_WIDTH,
|
||||
PJPG_TOO_MANY_COMPONENTS,
|
||||
PJPG_BAD_SOF_LENGTH,
|
||||
PJPG_BAD_VARIABLE_MARKER,
|
||||
PJPG_BAD_DRI_LENGTH,
|
||||
PJPG_BAD_SOS_LENGTH,
|
||||
PJPG_BAD_SOS_COMP_ID,
|
||||
PJPG_W_EXTRA_BYTES_BEFORE_MARKER,
|
||||
PJPG_NO_ARITHMITIC_SUPPORT,
|
||||
PJPG_UNEXPECTED_MARKER,
|
||||
PJPG_NOT_JPEG,
|
||||
PJPG_UNSUPPORTED_MARKER,
|
||||
PJPG_BAD_DQT_LENGTH,
|
||||
PJPG_TOO_MANY_BLOCKS,
|
||||
PJPG_UNDEFINED_QUANT_TABLE,
|
||||
PJPG_UNDEFINED_HUFF_TABLE,
|
||||
PJPG_NOT_SINGLE_SCAN,
|
||||
PJPG_UNSUPPORTED_COLORSPACE,
|
||||
PJPG_UNSUPPORTED_SAMP_FACTORS,
|
||||
PJPG_DECODE_ERROR,
|
||||
PJPG_BAD_RESTART_MARKER,
|
||||
PJPG_ASSERTION_ERROR,
|
||||
PJPG_BAD_SOS_SPECTRAL,
|
||||
PJPG_BAD_SOS_SUCCESSIVE,
|
||||
PJPG_STREAM_READ_ERROR,
|
||||
PJPG_NOTENOUGHMEM,
|
||||
PJPG_UNSUPPORTED_COMP_IDENT,
|
||||
PJPG_UNSUPPORTED_QUANT_TABLE,
|
||||
PJPG_UNSUPPORTED_MODE, // picojpeg doesn't support progressive JPEG's
|
||||
};
|
||||
|
||||
// Scan types
|
||||
typedef enum { PJPG_GRAYSCALE, PJPG_YH1V1, PJPG_YH2V1, PJPG_YH1V2, PJPG_YH2V2 } pjpeg_scan_type_t;
|
||||
|
||||
typedef struct {
|
||||
// Image resolution
|
||||
int m_width;
|
||||
int m_height;
|
||||
|
||||
// Number of components (1 or 3)
|
||||
int m_comps;
|
||||
|
||||
// Total number of minimum coded units (MCU's) per row/col.
|
||||
int m_MCUSPerRow;
|
||||
int m_MCUSPerCol;
|
||||
|
||||
// Scan type
|
||||
pjpeg_scan_type_t m_scanType;
|
||||
|
||||
// MCU width/height in pixels (each is either 8 or 16 depending on the scan type)
|
||||
int m_MCUWidth;
|
||||
int m_MCUHeight;
|
||||
|
||||
// m_pMCUBufR, m_pMCUBufG, and m_pMCUBufB are pointers to internal MCU Y or RGB pixel component buffers.
|
||||
// Each time pjpegDecodeMCU() is called successfully these buffers will be filled with 8x8 pixel blocks of Y or RGB
|
||||
// pixels. Each MCU consists of (m_MCUWidth/8)*(m_MCUHeight/8) Y/RGB blocks: 1 for greyscale/no subsampling, 2 for
|
||||
// H1V2/H2V1, or 4 blocks for H2V2 sampling factors. Each block is a contiguous array of 64 (8x8) bytes of a single
|
||||
// component: either Y for grayscale images, or R, G or B components for color images.
|
||||
//
|
||||
// The 8x8 pixel blocks are organized in these byte arrays like this:
|
||||
//
|
||||
// PJPG_GRAYSCALE: Each MCU is decoded to a single block of 8x8 grayscale pixels.
|
||||
// Only the values in m_pMCUBufR are valid. Each 8 bytes is a row of pixels (raster order: left to right, top to
|
||||
// bottom) from the 8x8 block.
|
||||
//
|
||||
// PJPG_H1V1: Each MCU contains is decoded to a single block of 8x8 RGB pixels.
|
||||
//
|
||||
// PJPG_YH2V1: Each MCU is decoded to 2 blocks, or 16x8 pixels.
|
||||
// The 2 RGB blocks are at byte offsets: 0, 64
|
||||
//
|
||||
// PJPG_YH1V2: Each MCU is decoded to 2 blocks, or 8x16 pixels.
|
||||
// The 2 RGB blocks are at byte offsets: 0,
|
||||
// 128
|
||||
//
|
||||
// PJPG_YH2V2: Each MCU is decoded to 4 blocks, or 16x16 pixels.
|
||||
// The 2x2 block array is organized at byte offsets: 0, 64,
|
||||
// 128, 192
|
||||
//
|
||||
// It is up to the caller to copy or blit these pixels from these buffers into the destination bitmap.
|
||||
unsigned char* m_pMCUBufR;
|
||||
unsigned char* m_pMCUBufG;
|
||||
unsigned char* m_pMCUBufB;
|
||||
} pjpeg_image_info_t;
|
||||
|
||||
typedef unsigned char (*pjpeg_need_bytes_callback_t)(unsigned char* pBuf, unsigned char buf_size,
|
||||
unsigned char* pBytes_actually_read, void* pCallback_data);
|
||||
|
||||
// Initializes the decompressor. Returns 0 on success, or one of the above error codes on failure.
|
||||
// pNeed_bytes_callback will be called to fill the decompressor's internal input buffer.
|
||||
// If reduce is 1, only the first pixel of each block will be decoded. This mode is much faster because it skips the AC
|
||||
// dequantization, IDCT and chroma upsampling of every image pixel. Not thread safe.
|
||||
unsigned char pjpeg_decode_init(pjpeg_image_info_t* pInfo, pjpeg_need_bytes_callback_t pNeed_bytes_callback,
|
||||
void* pCallback_data, unsigned char reduce);
|
||||
|
||||
// Decompresses the file's next MCU. Returns 0 on success, PJPG_NO_MORE_BLOCKS if no more blocks are available, or an
|
||||
// error code. Must be called a total of m_MCUSPerRow*m_MCUSPerCol times to completely decompress the image. Not thread
|
||||
// safe.
|
||||
unsigned char pjpeg_decode_mcu(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // PICOJPEG_H
|
||||
@@ -1,16 +1,16 @@
|
||||
[platformio]
|
||||
crosspoint_version = 0.7.0
|
||||
crosspoint_version = 0.10.0
|
||||
default_envs = default
|
||||
|
||||
[base]
|
||||
platform = espressif32
|
||||
platform = espressif32 @ 6.12.0
|
||||
board = esp32-c3-devkitm-1
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
upload_speed = 921600
|
||||
check_tool = cppcheck
|
||||
check_flags = --enable=all --suppress=missingIncludeSystem --suppress=unusedFunction --suppress=unmatchedSuppression --suppress=*:*/.pio/* --inline-suppr
|
||||
check_skip_packages = yes
|
||||
check_severity = medium, high
|
||||
|
||||
board_upload.flash_size = 16MB
|
||||
board_upload.maximum_size = 16777216
|
||||
@@ -31,11 +31,16 @@ board_build.flash_mode = dio
|
||||
board_build.flash_size = 16MB
|
||||
board_build.partitions = partitions.csv
|
||||
|
||||
extra_scripts =
|
||||
pre:scripts/build_html.py
|
||||
|
||||
; Libraries
|
||||
lib_deps =
|
||||
BatteryMonitor=symlink://open-x4-sdk/libs/hardware/BatteryMonitor
|
||||
InputManager=symlink://open-x4-sdk/libs/hardware/InputManager
|
||||
EInkDisplay=symlink://open-x4-sdk/libs/display/EInkDisplay
|
||||
ArduinoJson @ 7.4.2
|
||||
QRCode @ 0.0.1
|
||||
|
||||
[env:default]
|
||||
extends = base
|
||||
|
||||
51
scripts/build_html.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
import re
|
||||
|
||||
SRC_DIR = "src"
|
||||
|
||||
def minify_html(html: str) -> str:
|
||||
# Tags where whitespace should be preserved
|
||||
preserve_tags = ['pre', 'code', 'textarea', 'script', 'style']
|
||||
preserve_regex = '|'.join(preserve_tags)
|
||||
|
||||
# Protect preserve blocks with placeholders
|
||||
preserve_blocks = []
|
||||
def preserve(match):
|
||||
preserve_blocks.append(match.group(0))
|
||||
return f"__PRESERVE_BLOCK_{len(preserve_blocks)-1}__"
|
||||
|
||||
html = re.sub(rf'<({preserve_regex})[\s\S]*?</\1>', preserve, html, flags=re.IGNORECASE)
|
||||
|
||||
# Remove HTML comments
|
||||
html = re.sub(r'<!--.*?-->', '', html, flags=re.DOTALL)
|
||||
|
||||
# Collapse all whitespace between tags
|
||||
html = re.sub(r'>\s+<', '><', html)
|
||||
|
||||
# Collapse multiple spaces inside tags
|
||||
html = re.sub(r'\s+', ' ', html)
|
||||
|
||||
# Restore preserved blocks
|
||||
for i, block in enumerate(preserve_blocks):
|
||||
html = html.replace(f"__PRESERVE_BLOCK_{i}__", block)
|
||||
|
||||
return html.strip()
|
||||
|
||||
for root, _, files in os.walk(SRC_DIR):
|
||||
for file in files:
|
||||
if file.endswith(".html"):
|
||||
html_path = os.path.join(root, file)
|
||||
with open(html_path, "r", encoding="utf-8") as f:
|
||||
html_content = f.read()
|
||||
|
||||
# minified = regex.sub("\g<1>", html_content)
|
||||
minified = minify_html(html_content)
|
||||
base_name = f"{os.path.splitext(file)[0]}Html"
|
||||
header_path = os.path.join(root, f"{base_name}.generated.h")
|
||||
|
||||
with open(header_path, "w", encoding="utf-8") as h:
|
||||
h.write(f"// THIS FILE IS AUTOGENERATED, DO NOT EDIT MANUALLY\n\n")
|
||||
h.write(f"#pragma once\n")
|
||||
h.write(f'constexpr char {base_name}[] PROGMEM = R"rawliteral({minified})rawliteral";\n')
|
||||
|
||||
print(f"Generated: {header_path}")
|
||||
@@ -1,30 +1,36 @@
|
||||
#include "CrossPointSettings.h"
|
||||
|
||||
#include <FsHelpers.h>
|
||||
#include <HardwareSerial.h>
|
||||
#include <SD.h>
|
||||
#include <Serialization.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
|
||||
// Initialize the static instance
|
||||
CrossPointSettings CrossPointSettings::instance;
|
||||
|
||||
namespace {
|
||||
constexpr uint8_t SETTINGS_FILE_VERSION = 1;
|
||||
constexpr uint8_t SETTINGS_COUNT = 2;
|
||||
constexpr char SETTINGS_FILE[] = "/sd/.crosspoint/settings.bin";
|
||||
// Increment this when adding new persisted settings fields
|
||||
constexpr uint8_t SETTINGS_COUNT = 5;
|
||||
constexpr char SETTINGS_FILE[] = "/.crosspoint/settings.bin";
|
||||
} // namespace
|
||||
|
||||
bool CrossPointSettings::saveToFile() const {
|
||||
// Make sure the directory exists
|
||||
SD.mkdir("/.crosspoint");
|
||||
|
||||
std::ofstream outputFile(SETTINGS_FILE);
|
||||
File outputFile;
|
||||
if (!FsHelpers::openFileForWrite("CPS", SETTINGS_FILE, outputFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
serialization::writePod(outputFile, SETTINGS_FILE_VERSION);
|
||||
serialization::writePod(outputFile, SETTINGS_COUNT);
|
||||
serialization::writePod(outputFile, whiteSleepScreen);
|
||||
serialization::writePod(outputFile, sleepScreen);
|
||||
serialization::writePod(outputFile, extraParagraphSpacing);
|
||||
serialization::writePod(outputFile, shortPwrBtn);
|
||||
serialization::writePod(outputFile, statusBar);
|
||||
serialization::writePod(outputFile, orientation);
|
||||
outputFile.close();
|
||||
|
||||
Serial.printf("[%lu] [CPS] Settings saved to file\n", millis());
|
||||
@@ -32,13 +38,11 @@ bool CrossPointSettings::saveToFile() const {
|
||||
}
|
||||
|
||||
bool CrossPointSettings::loadFromFile() {
|
||||
if (!SD.exists(SETTINGS_FILE + 3)) { // +3 to skip "/sd" prefix
|
||||
Serial.printf("[%lu] [CPS] Settings file does not exist, using defaults\n", millis());
|
||||
File inputFile;
|
||||
if (!FsHelpers::openFileForRead("CPS", SETTINGS_FILE, inputFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ifstream inputFile(SETTINGS_FILE);
|
||||
|
||||
uint8_t version;
|
||||
serialization::readPod(inputFile, version);
|
||||
if (version != SETTINGS_FILE_VERSION) {
|
||||
@@ -50,16 +54,20 @@ bool CrossPointSettings::loadFromFile() {
|
||||
uint8_t fileSettingsCount = 0;
|
||||
serialization::readPod(inputFile, fileSettingsCount);
|
||||
|
||||
// load settings that exist
|
||||
switch (fileSettingsCount) {
|
||||
case 1:
|
||||
serialization::readPod(inputFile, whiteSleepScreen);
|
||||
break;
|
||||
case 2:
|
||||
serialization::readPod(inputFile, whiteSleepScreen);
|
||||
serialization::readPod(inputFile, extraParagraphSpacing);
|
||||
break;
|
||||
}
|
||||
// load settings that exist (support older files with fewer fields)
|
||||
uint8_t settingsRead = 0;
|
||||
do {
|
||||
serialization::readPod(inputFile, sleepScreen);
|
||||
if (++settingsRead >= fileSettingsCount) break;
|
||||
serialization::readPod(inputFile, extraParagraphSpacing);
|
||||
if (++settingsRead >= fileSettingsCount) break;
|
||||
serialization::readPod(inputFile, shortPwrBtn);
|
||||
if (++settingsRead >= fileSettingsCount) break;
|
||||
serialization::readPod(inputFile, statusBar);
|
||||
if (++settingsRead >= fileSettingsCount) break;
|
||||
serialization::readPod(inputFile, orientation);
|
||||
if (++settingsRead >= fileSettingsCount) break;
|
||||
} while (false);
|
||||
|
||||
inputFile.close();
|
||||
Serial.printf("[%lu] [CPS] Settings loaded from file\n", millis());
|
||||
|
||||
@@ -15,17 +15,38 @@ class CrossPointSettings {
|
||||
CrossPointSettings(const CrossPointSettings&) = delete;
|
||||
CrossPointSettings& operator=(const CrossPointSettings&) = delete;
|
||||
|
||||
// Sleep screen settings
|
||||
uint8_t whiteSleepScreen = 0;
|
||||
// Should match with SettingsActivity text
|
||||
enum SLEEP_SCREEN_MODE { DARK = 0, LIGHT = 1, CUSTOM = 2, COVER = 3 };
|
||||
|
||||
// Status bar display type enum
|
||||
enum STATUS_BAR_MODE { NONE = 0, NO_PROGRESS = 1, FULL = 2 };
|
||||
|
||||
enum ORIENTATION {
|
||||
PORTRAIT = 0, // 480x800 logical coordinates (current default)
|
||||
LANDSCAPE_CW = 1, // 800x480 logical coordinates, rotated 180° (swap top/bottom)
|
||||
INVERTED = 2, // 480x800 logical coordinates, inverted
|
||||
LANDSCAPE_CCW = 3 // 800x480 logical coordinates, native panel orientation
|
||||
};
|
||||
|
||||
// Sleep screen settings
|
||||
uint8_t sleepScreen = DARK;
|
||||
// Status bar settings
|
||||
uint8_t statusBar = FULL;
|
||||
// Text rendering settings
|
||||
uint8_t extraParagraphSpacing = 1;
|
||||
// Duration of the power button press
|
||||
uint8_t shortPwrBtn = 0;
|
||||
// EPUB reading orientation settings
|
||||
// 0 = portrait (default), 1 = landscape clockwise, 2 = inverted, 3 = landscape counter-clockwise
|
||||
uint8_t orientation = PORTRAIT;
|
||||
|
||||
~CrossPointSettings() = default;
|
||||
|
||||
// Get singleton instance
|
||||
static CrossPointSettings& getInstance() { return instance; }
|
||||
|
||||
uint16_t getPowerButtonDuration() const { return shortPwrBtn ? 10 : 500; }
|
||||
|
||||
bool saveToFile() const;
|
||||
bool loadFromFile();
|
||||
};
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
#include "CrossPointState.h"
|
||||
|
||||
#include <FsHelpers.h>
|
||||
#include <HardwareSerial.h>
|
||||
#include <SD.h>
|
||||
#include <Serialization.h>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace {
|
||||
constexpr uint8_t STATE_FILE_VERSION = 1;
|
||||
constexpr char STATE_FILE[] = "/sd/.crosspoint/state.bin";
|
||||
constexpr char STATE_FILE[] = "/.crosspoint/state.bin";
|
||||
} // namespace
|
||||
|
||||
CrossPointState CrossPointState::instance;
|
||||
|
||||
bool CrossPointState::saveToFile() const {
|
||||
std::ofstream outputFile(STATE_FILE);
|
||||
File outputFile;
|
||||
if (!FsHelpers::openFileForWrite("CPS", STATE_FILE, outputFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
serialization::writePod(outputFile, STATE_FILE_VERSION);
|
||||
serialization::writeString(outputFile, openEpubPath);
|
||||
outputFile.close();
|
||||
@@ -22,7 +24,10 @@ bool CrossPointState::saveToFile() const {
|
||||
}
|
||||
|
||||
bool CrossPointState::loadFromFile() {
|
||||
std::ifstream inputFile(STATE_FILE);
|
||||
File inputFile;
|
||||
if (!FsHelpers::openFileForRead("CPS", STATE_FILE, inputFile)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t version;
|
||||
serialization::readPod(inputFile, version);
|
||||
|
||||
154
src/WifiCredentialStore.cpp
Normal file
@@ -0,0 +1,154 @@
|
||||
#include "WifiCredentialStore.h"
|
||||
|
||||
#include <FsHelpers.h>
|
||||
#include <HardwareSerial.h>
|
||||
#include <SD.h>
|
||||
#include <Serialization.h>
|
||||
|
||||
// Initialize the static instance
|
||||
WifiCredentialStore WifiCredentialStore::instance;
|
||||
|
||||
namespace {
|
||||
// File format version
|
||||
constexpr uint8_t WIFI_FILE_VERSION = 1;
|
||||
|
||||
// WiFi credentials file path
|
||||
constexpr char WIFI_FILE[] = "/.crosspoint/wifi.bin";
|
||||
|
||||
// Obfuscation key - "CrossPoint" in ASCII
|
||||
// This is NOT cryptographic security, just prevents casual file reading
|
||||
constexpr uint8_t OBFUSCATION_KEY[] = {0x43, 0x72, 0x6F, 0x73, 0x73, 0x50, 0x6F, 0x69, 0x6E, 0x74};
|
||||
constexpr size_t KEY_LENGTH = sizeof(OBFUSCATION_KEY);
|
||||
} // namespace
|
||||
|
||||
void WifiCredentialStore::obfuscate(std::string& data) const {
|
||||
Serial.printf("[%lu] [WCS] Obfuscating/deobfuscating %zu bytes\n", millis(), data.size());
|
||||
for (size_t i = 0; i < data.size(); i++) {
|
||||
data[i] ^= OBFUSCATION_KEY[i % KEY_LENGTH];
|
||||
}
|
||||
}
|
||||
|
||||
bool WifiCredentialStore::saveToFile() const {
|
||||
// Make sure the directory exists
|
||||
SD.mkdir("/.crosspoint");
|
||||
|
||||
File file;
|
||||
if (!FsHelpers::openFileForWrite("WCS", WIFI_FILE, file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Write header
|
||||
serialization::writePod(file, WIFI_FILE_VERSION);
|
||||
serialization::writePod(file, static_cast<uint8_t>(credentials.size()));
|
||||
|
||||
// Write each credential
|
||||
for (const auto& cred : credentials) {
|
||||
// Write SSID (plaintext - not sensitive)
|
||||
serialization::writeString(file, cred.ssid);
|
||||
Serial.printf("[%lu] [WCS] Saving SSID: %s, password length: %zu\n", millis(), cred.ssid.c_str(),
|
||||
cred.password.size());
|
||||
|
||||
// Write password (obfuscated)
|
||||
std::string obfuscatedPwd = cred.password;
|
||||
obfuscate(obfuscatedPwd);
|
||||
serialization::writeString(file, obfuscatedPwd);
|
||||
}
|
||||
|
||||
file.close();
|
||||
Serial.printf("[%lu] [WCS] Saved %zu WiFi credentials to file\n", millis(), credentials.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiCredentialStore::loadFromFile() {
|
||||
File file;
|
||||
if (!FsHelpers::openFileForRead("WCS", WIFI_FILE, file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read and verify version
|
||||
uint8_t version;
|
||||
serialization::readPod(file, version);
|
||||
if (version != WIFI_FILE_VERSION) {
|
||||
Serial.printf("[%lu] [WCS] Unknown file version: %u\n", millis(), version);
|
||||
file.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read credential count
|
||||
uint8_t count;
|
||||
serialization::readPod(file, count);
|
||||
|
||||
// Read credentials
|
||||
credentials.clear();
|
||||
for (uint8_t i = 0; i < count && i < MAX_NETWORKS; i++) {
|
||||
WifiCredential cred;
|
||||
|
||||
// Read SSID
|
||||
serialization::readString(file, cred.ssid);
|
||||
|
||||
// Read and deobfuscate password
|
||||
serialization::readString(file, cred.password);
|
||||
Serial.printf("[%lu] [WCS] Loaded SSID: %s, obfuscated password length: %zu\n", millis(), cred.ssid.c_str(),
|
||||
cred.password.size());
|
||||
obfuscate(cred.password); // XOR is symmetric, so same function deobfuscates
|
||||
Serial.printf("[%lu] [WCS] After deobfuscation, password length: %zu\n", millis(), cred.password.size());
|
||||
|
||||
credentials.push_back(cred);
|
||||
}
|
||||
|
||||
file.close();
|
||||
Serial.printf("[%lu] [WCS] Loaded %zu WiFi credentials from file\n", millis(), credentials.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WifiCredentialStore::addCredential(const std::string& ssid, const std::string& password) {
|
||||
// Check if this SSID already exists and update it
|
||||
const auto cred = find_if(credentials.begin(), credentials.end(),
|
||||
[&ssid](const WifiCredential& cred) { return cred.ssid == ssid; });
|
||||
if (cred != credentials.end()) {
|
||||
cred->password = password;
|
||||
Serial.printf("[%lu] [WCS] Updated credentials for: %s\n", millis(), ssid.c_str());
|
||||
return saveToFile();
|
||||
}
|
||||
|
||||
// Check if we've reached the limit
|
||||
if (credentials.size() >= MAX_NETWORKS) {
|
||||
Serial.printf("[%lu] [WCS] Cannot add more networks, limit of %zu reached\n", millis(), MAX_NETWORKS);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add new credential
|
||||
credentials.push_back({ssid, password});
|
||||
Serial.printf("[%lu] [WCS] Added credentials for: %s\n", millis(), ssid.c_str());
|
||||
return saveToFile();
|
||||
}
|
||||
|
||||
bool WifiCredentialStore::removeCredential(const std::string& ssid) {
|
||||
const auto cred = find_if(credentials.begin(), credentials.end(),
|
||||
[&ssid](const WifiCredential& cred) { return cred.ssid == ssid; });
|
||||
if (cred != credentials.end()) {
|
||||
credentials.erase(cred);
|
||||
Serial.printf("[%lu] [WCS] Removed credentials for: %s\n", millis(), ssid.c_str());
|
||||
return saveToFile();
|
||||
}
|
||||
return false; // Not found
|
||||
}
|
||||
|
||||
const WifiCredential* WifiCredentialStore::findCredential(const std::string& ssid) const {
|
||||
const auto cred = find_if(credentials.begin(), credentials.end(),
|
||||
[&ssid](const WifiCredential& cred) { return cred.ssid == ssid; });
|
||||
|
||||
if (cred != credentials.end()) {
|
||||
return &*cred;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool WifiCredentialStore::hasSavedCredential(const std::string& ssid) const { return findCredential(ssid) != nullptr; }
|
||||
|
||||
void WifiCredentialStore::clearAll() {
|
||||
credentials.clear();
|
||||
saveToFile();
|
||||
Serial.printf("[%lu] [WCS] Cleared all WiFi credentials\n", millis());
|
||||
}
|
||||
56
src/WifiCredentialStore.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct WifiCredential {
|
||||
std::string ssid;
|
||||
std::string password; // Stored obfuscated in file
|
||||
};
|
||||
|
||||
/**
|
||||
* Singleton class for storing WiFi credentials on the SD card.
|
||||
* Credentials are stored in /sd/.crosspoint/wifi.bin with basic
|
||||
* XOR obfuscation to prevent casual reading (not cryptographically secure).
|
||||
*/
|
||||
class WifiCredentialStore {
|
||||
private:
|
||||
static WifiCredentialStore instance;
|
||||
std::vector<WifiCredential> credentials;
|
||||
|
||||
static constexpr size_t MAX_NETWORKS = 8;
|
||||
|
||||
// Private constructor for singleton
|
||||
WifiCredentialStore() = default;
|
||||
|
||||
// XOR obfuscation (symmetric - same for encode/decode)
|
||||
void obfuscate(std::string& data) const;
|
||||
|
||||
public:
|
||||
// Delete copy constructor and assignment
|
||||
WifiCredentialStore(const WifiCredentialStore&) = delete;
|
||||
WifiCredentialStore& operator=(const WifiCredentialStore&) = delete;
|
||||
|
||||
// Get singleton instance
|
||||
static WifiCredentialStore& getInstance() { return instance; }
|
||||
|
||||
// Save/load from SD card
|
||||
bool saveToFile() const;
|
||||
bool loadFromFile();
|
||||
|
||||
// Credential management
|
||||
bool addCredential(const std::string& ssid, const std::string& password);
|
||||
bool removeCredential(const std::string& ssid);
|
||||
const WifiCredential* findCredential(const std::string& ssid) const;
|
||||
|
||||
// Get all stored credentials (for UI display)
|
||||
const std::vector<WifiCredential>& getCredentials() const { return credentials; }
|
||||
|
||||
// Check if a network is saved
|
||||
bool hasSavedCredential(const std::string& ssid) const;
|
||||
|
||||
// Clear all credentials
|
||||
void clearAll();
|
||||
};
|
||||
|
||||
// Helper macro to access credentials store
|
||||
#define WIFI_STORE WifiCredentialStore::getInstance()
|
||||
@@ -1,18 +1,25 @@
|
||||
#pragma once
|
||||
#include <InputManager.h>
|
||||
|
||||
#include <HardwareSerial.h>
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
class InputManager;
|
||||
class GfxRenderer;
|
||||
|
||||
class Activity {
|
||||
protected:
|
||||
std::string name;
|
||||
GfxRenderer& renderer;
|
||||
InputManager& inputManager;
|
||||
|
||||
public:
|
||||
explicit Activity(GfxRenderer& renderer, InputManager& inputManager)
|
||||
: renderer(renderer), inputManager(inputManager) {}
|
||||
explicit Activity(std::string name, GfxRenderer& renderer, InputManager& inputManager)
|
||||
: name(std::move(name)), renderer(renderer), inputManager(inputManager) {}
|
||||
virtual ~Activity() = default;
|
||||
virtual void onEnter() {}
|
||||
virtual void onExit() {}
|
||||
virtual void onEnter() { Serial.printf("[%lu] [ACT] Entering activity: %s\n", millis(), name.c_str()); }
|
||||
virtual void onExit() { Serial.printf("[%lu] [ACT] Exiting activity: %s\n", millis(), name.c_str()); }
|
||||
virtual void loop() {}
|
||||
virtual bool skipLoopDelay() { return false; }
|
||||
};
|
||||
|
||||
@@ -18,4 +18,7 @@ void ActivityWithSubactivity::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
void ActivityWithSubactivity::onExit() { exitActivity(); }
|
||||
void ActivityWithSubactivity::onExit() {
|
||||
Activity::onExit();
|
||||
exitActivity();
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ class ActivityWithSubactivity : public Activity {
|
||||
void enterNewActivity(Activity* activity);
|
||||
|
||||
public:
|
||||
explicit ActivityWithSubactivity(GfxRenderer& renderer, InputManager& inputManager)
|
||||
: Activity(renderer, inputManager) {}
|
||||
explicit ActivityWithSubactivity(std::string name, GfxRenderer& renderer, InputManager& inputManager)
|
||||
: Activity(std::move(name), renderer, inputManager) {}
|
||||
void loop() override;
|
||||
void onExit() override;
|
||||
};
|
||||
|
||||
@@ -6,11 +6,13 @@
|
||||
#include "images/CrossLarge.h"
|
||||
|
||||
void BootActivity::onEnter() {
|
||||
const auto pageWidth = GfxRenderer::getScreenWidth();
|
||||
const auto pageHeight = GfxRenderer::getScreenHeight();
|
||||
Activity::onEnter();
|
||||
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
|
||||
renderer.clearScreen();
|
||||
renderer.drawImage(CrossLarge, (pageWidth - 128) / 2, (pageHeight - 128) / 2, 128, 128);
|
||||
renderer.drawImage(CrossLarge, (pageWidth + 128) / 2, (pageHeight - 128) / 2, 128, 128);
|
||||
renderer.drawCenteredText(UI_FONT_ID, pageHeight / 2 + 70, "CrossPoint", true, BOLD);
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight / 2 + 95, "BOOTING");
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight - 30, CROSSPOINT_VERSION);
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
|
||||
class BootActivity final : public Activity {
|
||||
public:
|
||||
explicit BootActivity(GfxRenderer& renderer, InputManager& inputManager) : Activity(renderer, inputManager) {}
|
||||
explicit BootActivity(GfxRenderer& renderer, InputManager& inputManager) : Activity("Boot", renderer, inputManager) {}
|
||||
void onEnter() override;
|
||||
};
|
||||
|
||||
@@ -1,24 +1,237 @@
|
||||
#include "SleepActivity.h"
|
||||
|
||||
#include <Epub.h>
|
||||
#include <FsHelpers.h>
|
||||
#include <GfxRenderer.h>
|
||||
#include <SD.h>
|
||||
#include <Xtc.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "CrossPointSettings.h"
|
||||
#include "CrossPointState.h"
|
||||
#include "config.h"
|
||||
#include "images/CrossLarge.h"
|
||||
|
||||
namespace {
|
||||
// Check if path has XTC extension (.xtc or .xtch)
|
||||
bool isXtcFile(const std::string& path) {
|
||||
if (path.length() < 4) return false;
|
||||
std::string ext4 = path.substr(path.length() - 4);
|
||||
if (ext4 == ".xtc") return true;
|
||||
if (path.length() >= 5) {
|
||||
std::string ext5 = path.substr(path.length() - 5);
|
||||
if (ext5 == ".xtch") return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void SleepActivity::onEnter() {
|
||||
const auto pageWidth = GfxRenderer::getScreenWidth();
|
||||
const auto pageHeight = GfxRenderer::getScreenHeight();
|
||||
Activity::onEnter();
|
||||
renderPopup("Entering Sleep...");
|
||||
|
||||
if (SETTINGS.sleepScreen == CrossPointSettings::SLEEP_SCREEN_MODE::CUSTOM) {
|
||||
return renderCustomSleepScreen();
|
||||
}
|
||||
|
||||
if (SETTINGS.sleepScreen == CrossPointSettings::SLEEP_SCREEN_MODE::COVER) {
|
||||
return renderCoverSleepScreen();
|
||||
}
|
||||
|
||||
renderDefaultSleepScreen();
|
||||
}
|
||||
|
||||
void SleepActivity::renderPopup(const char* message) const {
|
||||
const int textWidth = renderer.getTextWidth(READER_FONT_ID, message);
|
||||
constexpr int margin = 20;
|
||||
const int x = (renderer.getScreenWidth() - textWidth - margin * 2) / 2;
|
||||
constexpr int y = 117;
|
||||
const int w = textWidth + margin * 2;
|
||||
const int h = renderer.getLineHeight(READER_FONT_ID) + margin * 2;
|
||||
// renderer.clearScreen();
|
||||
renderer.fillRect(x + 5, y + 5, w - 10, h - 10, false);
|
||||
renderer.drawText(READER_FONT_ID, x + margin, y + margin, message);
|
||||
renderer.drawRect(x + 5, y + 5, w - 10, h - 10);
|
||||
renderer.displayBuffer();
|
||||
}
|
||||
|
||||
void SleepActivity::renderCustomSleepScreen() const {
|
||||
// Check if we have a /sleep directory
|
||||
auto dir = SD.open("/sleep");
|
||||
if (dir && dir.isDirectory()) {
|
||||
std::vector<std::string> files;
|
||||
// collect all valid BMP files
|
||||
for (File file = dir.openNextFile(); file; file = dir.openNextFile()) {
|
||||
if (file.isDirectory()) {
|
||||
file.close();
|
||||
continue;
|
||||
}
|
||||
auto filename = std::string(file.name());
|
||||
if (filename[0] == '.') {
|
||||
file.close();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (filename.substr(filename.length() - 4) != ".bmp") {
|
||||
Serial.printf("[%lu] [SLP] Skipping non-.bmp file name: %s\n", millis(), file.name());
|
||||
file.close();
|
||||
continue;
|
||||
}
|
||||
Bitmap bitmap(file);
|
||||
if (bitmap.parseHeaders() != BmpReaderError::Ok) {
|
||||
Serial.printf("[%lu] [SLP] Skipping invalid BMP file: %s\n", millis(), file.name());
|
||||
file.close();
|
||||
continue;
|
||||
}
|
||||
files.emplace_back(filename);
|
||||
file.close();
|
||||
}
|
||||
const auto numFiles = files.size();
|
||||
if (numFiles > 0) {
|
||||
// Generate a random number between 1 and numFiles
|
||||
const auto randomFileIndex = random(numFiles);
|
||||
const auto filename = "/sleep/" + files[randomFileIndex];
|
||||
File file;
|
||||
if (FsHelpers::openFileForRead("SLP", filename, file)) {
|
||||
Serial.printf("[%lu] [SLP] Randomly loading: /sleep/%s\n", millis(), files[randomFileIndex].c_str());
|
||||
delay(100);
|
||||
Bitmap bitmap(file);
|
||||
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
||||
renderBitmapSleepScreen(bitmap);
|
||||
dir.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dir) dir.close();
|
||||
|
||||
// Look for sleep.bmp on the root of the sd card to determine if we should
|
||||
// render a custom sleep screen instead of the default.
|
||||
File file;
|
||||
if (FsHelpers::openFileForRead("SLP", "/sleep.bmp", file)) {
|
||||
Bitmap bitmap(file);
|
||||
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
||||
Serial.printf("[%lu] [SLP] Loading: /sleep.bmp\n", millis());
|
||||
renderBitmapSleepScreen(bitmap);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
renderDefaultSleepScreen();
|
||||
}
|
||||
|
||||
void SleepActivity::renderDefaultSleepScreen() const {
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
|
||||
renderer.clearScreen();
|
||||
renderer.drawImage(CrossLarge, (pageWidth - 128) / 2, (pageHeight - 128) / 2, 128, 128);
|
||||
renderer.drawImage(CrossLarge, (pageWidth + 128) / 2, (pageHeight - 128) / 2, 128, 128);
|
||||
renderer.drawCenteredText(UI_FONT_ID, pageHeight / 2 + 70, "CrossPoint", true, BOLD);
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight / 2 + 95, "SLEEPING");
|
||||
|
||||
// Apply white screen if enabled in settings
|
||||
if (!SETTINGS.whiteSleepScreen) {
|
||||
// Make sleep screen dark unless light is selected in settings
|
||||
if (SETTINGS.sleepScreen != CrossPointSettings::SLEEP_SCREEN_MODE::LIGHT) {
|
||||
renderer.invertScreen();
|
||||
}
|
||||
|
||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
||||
}
|
||||
|
||||
void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) const {
|
||||
int x, y;
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
|
||||
if (bitmap.getWidth() > pageWidth || bitmap.getHeight() > pageHeight) {
|
||||
// image will scale, make sure placement is right
|
||||
const float ratio = static_cast<float>(bitmap.getWidth()) / static_cast<float>(bitmap.getHeight());
|
||||
const float screenRatio = static_cast<float>(pageWidth) / static_cast<float>(pageHeight);
|
||||
|
||||
if (ratio > screenRatio) {
|
||||
// image wider than viewport ratio, scaled down image needs to be centered vertically
|
||||
x = 0;
|
||||
y = (pageHeight - pageWidth / ratio) / 2;
|
||||
} else {
|
||||
// image taller than viewport ratio, scaled down image needs to be centered horizontally
|
||||
x = (pageWidth - pageHeight * ratio) / 2;
|
||||
y = 0;
|
||||
}
|
||||
} else {
|
||||
// center the image
|
||||
x = (pageWidth - bitmap.getWidth()) / 2;
|
||||
y = (pageHeight - bitmap.getHeight()) / 2;
|
||||
}
|
||||
|
||||
renderer.clearScreen();
|
||||
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight);
|
||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
||||
|
||||
if (bitmap.hasGreyscale()) {
|
||||
bitmap.rewindToData();
|
||||
renderer.clearScreen(0x00);
|
||||
renderer.setRenderMode(GfxRenderer::GRAYSCALE_LSB);
|
||||
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight);
|
||||
renderer.copyGrayscaleLsbBuffers();
|
||||
|
||||
bitmap.rewindToData();
|
||||
renderer.clearScreen(0x00);
|
||||
renderer.setRenderMode(GfxRenderer::GRAYSCALE_MSB);
|
||||
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight);
|
||||
renderer.copyGrayscaleMsbBuffers();
|
||||
|
||||
renderer.displayGrayBuffer();
|
||||
renderer.setRenderMode(GfxRenderer::BW);
|
||||
}
|
||||
}
|
||||
|
||||
void SleepActivity::renderCoverSleepScreen() const {
|
||||
if (APP_STATE.openEpubPath.empty()) {
|
||||
return renderDefaultSleepScreen();
|
||||
}
|
||||
|
||||
std::string coverBmpPath;
|
||||
|
||||
// Check if the current book is XTC or EPUB
|
||||
if (isXtcFile(APP_STATE.openEpubPath)) {
|
||||
// Handle XTC file
|
||||
Xtc lastXtc(APP_STATE.openEpubPath, "/.crosspoint");
|
||||
if (!lastXtc.load()) {
|
||||
Serial.println("[SLP] Failed to load last XTC");
|
||||
return renderDefaultSleepScreen();
|
||||
}
|
||||
|
||||
if (!lastXtc.generateCoverBmp()) {
|
||||
Serial.println("[SLP] Failed to generate XTC cover bmp");
|
||||
return renderDefaultSleepScreen();
|
||||
}
|
||||
|
||||
coverBmpPath = lastXtc.getCoverBmpPath();
|
||||
} else {
|
||||
// Handle EPUB file
|
||||
Epub lastEpub(APP_STATE.openEpubPath, "/.crosspoint");
|
||||
if (!lastEpub.load()) {
|
||||
Serial.println("[SLP] Failed to load last epub");
|
||||
return renderDefaultSleepScreen();
|
||||
}
|
||||
|
||||
if (!lastEpub.generateCoverBmp()) {
|
||||
Serial.println("[SLP] Failed to generate cover bmp");
|
||||
return renderDefaultSleepScreen();
|
||||
}
|
||||
|
||||
coverBmpPath = lastEpub.getCoverBmpPath();
|
||||
}
|
||||
|
||||
File file;
|
||||
if (FsHelpers::openFileForRead("SLP", coverBmpPath, file)) {
|
||||
Bitmap bitmap(file);
|
||||
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
||||
renderBitmapSleepScreen(bitmap);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
renderDefaultSleepScreen();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
#pragma once
|
||||
#include "../Activity.h"
|
||||
|
||||
class Bitmap;
|
||||
|
||||
class SleepActivity final : public Activity {
|
||||
public:
|
||||
explicit SleepActivity(GfxRenderer& renderer, InputManager& inputManager) : Activity(renderer, inputManager) {}
|
||||
explicit SleepActivity(GfxRenderer& renderer, InputManager& inputManager)
|
||||
: Activity("Sleep", renderer, inputManager) {}
|
||||
void onEnter() override;
|
||||
|
||||
private:
|
||||
void renderPopup(const char* message) const;
|
||||
void renderDefaultSleepScreen() const;
|
||||
void renderCustomSleepScreen() const;
|
||||
void renderCoverSleepScreen() const;
|
||||
void renderBitmapSleepScreen(const Bitmap& bitmap) const;
|
||||
};
|
||||
|
||||
@@ -1,22 +1,27 @@
|
||||
#include "HomeActivity.h"
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <InputManager.h>
|
||||
#include <SD.h>
|
||||
|
||||
#include "CrossPointState.h"
|
||||
#include "config.h"
|
||||
|
||||
namespace {
|
||||
constexpr int menuItemCount = 2;
|
||||
}
|
||||
|
||||
void HomeActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<HomeActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
int HomeActivity::getMenuItemCount() const { return hasContinueReading ? 4 : 3; }
|
||||
|
||||
void HomeActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
// Check if we have a book to continue reading
|
||||
hasContinueReading = !APP_STATE.openEpubPath.empty() && SD.exists(APP_STATE.openEpubPath.c_str());
|
||||
|
||||
selectorIndex = 0;
|
||||
|
||||
// Trigger first update
|
||||
@@ -31,6 +36,8 @@ void HomeActivity::onEnter() {
|
||||
}
|
||||
|
||||
void HomeActivity::onExit() {
|
||||
Activity::onExit();
|
||||
|
||||
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
@@ -47,17 +54,35 @@ void HomeActivity::loop() {
|
||||
const bool nextPressed =
|
||||
inputManager.wasPressed(InputManager::BTN_DOWN) || inputManager.wasPressed(InputManager::BTN_RIGHT);
|
||||
|
||||
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
if (selectorIndex == 0) {
|
||||
onReaderOpen();
|
||||
} else if (selectorIndex == 1) {
|
||||
onSettingsOpen();
|
||||
const int menuCount = getMenuItemCount();
|
||||
|
||||
if (inputManager.wasReleased(InputManager::BTN_CONFIRM)) {
|
||||
if (hasContinueReading) {
|
||||
// Menu: Continue Reading, Browse, File transfer, Settings
|
||||
if (selectorIndex == 0) {
|
||||
onContinueReading();
|
||||
} else if (selectorIndex == 1) {
|
||||
onReaderOpen();
|
||||
} else if (selectorIndex == 2) {
|
||||
onFileTransferOpen();
|
||||
} else if (selectorIndex == 3) {
|
||||
onSettingsOpen();
|
||||
}
|
||||
} else {
|
||||
// Menu: Browse, File transfer, Settings
|
||||
if (selectorIndex == 0) {
|
||||
onReaderOpen();
|
||||
} else if (selectorIndex == 1) {
|
||||
onFileTransferOpen();
|
||||
} else if (selectorIndex == 2) {
|
||||
onSettingsOpen();
|
||||
}
|
||||
}
|
||||
} else if (prevPressed) {
|
||||
selectorIndex = (selectorIndex + menuItemCount - 1) % menuItemCount;
|
||||
selectorIndex = (selectorIndex + menuCount - 1) % menuCount;
|
||||
updateRequired = true;
|
||||
} else if (nextPressed) {
|
||||
selectorIndex = (selectorIndex + 1) % menuItemCount;
|
||||
selectorIndex = (selectorIndex + 1) % menuCount;
|
||||
updateRequired = true;
|
||||
}
|
||||
}
|
||||
@@ -77,27 +102,48 @@ void HomeActivity::displayTaskLoop() {
|
||||
void HomeActivity::render() const {
|
||||
renderer.clearScreen();
|
||||
|
||||
const auto pageWidth = GfxRenderer::getScreenWidth();
|
||||
const auto pageHeight = GfxRenderer::getScreenHeight();
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
renderer.drawCenteredText(READER_FONT_ID, 10, "CrossPoint Reader", true, BOLD);
|
||||
|
||||
// Draw selection
|
||||
renderer.fillRect(0, 60 + selectorIndex * 30 + 2, pageWidth - 1, 30);
|
||||
renderer.drawText(UI_FONT_ID, 20, 60, "Read", selectorIndex != 0);
|
||||
renderer.drawText(UI_FONT_ID, 20, 90, "Settings", selectorIndex != 1);
|
||||
renderer.fillRect(0, 60 + selectorIndex * 30 - 2, pageWidth - 1, 30);
|
||||
|
||||
renderer.drawRect(25, pageHeight - 40, 106, 40);
|
||||
renderer.drawText(UI_FONT_ID, 25 + (105 - renderer.getTextWidth(UI_FONT_ID, "Back")) / 2, pageHeight - 35, "Back");
|
||||
int menuY = 60;
|
||||
int menuIndex = 0;
|
||||
|
||||
renderer.drawRect(130, pageHeight - 40, 106, 40);
|
||||
renderer.drawText(UI_FONT_ID, 130 + (105 - renderer.getTextWidth(UI_FONT_ID, "Confirm")) / 2, pageHeight - 35,
|
||||
"Confirm");
|
||||
if (hasContinueReading) {
|
||||
// Extract filename from path for display
|
||||
std::string bookName = APP_STATE.openEpubPath;
|
||||
const size_t lastSlash = bookName.find_last_of('/');
|
||||
if (lastSlash != std::string::npos) {
|
||||
bookName = bookName.substr(lastSlash + 1);
|
||||
}
|
||||
// Remove .epub extension
|
||||
if (bookName.length() > 5 && bookName.substr(bookName.length() - 5) == ".epub") {
|
||||
bookName.resize(bookName.length() - 5);
|
||||
}
|
||||
// Truncate if too long
|
||||
if (bookName.length() > 25) {
|
||||
bookName.resize(22);
|
||||
bookName += "...";
|
||||
}
|
||||
std::string continueLabel = "Continue: " + bookName;
|
||||
renderer.drawText(UI_FONT_ID, 20, menuY, continueLabel.c_str(), selectorIndex != menuIndex);
|
||||
menuY += 30;
|
||||
menuIndex++;
|
||||
}
|
||||
|
||||
renderer.drawRect(245, pageHeight - 40, 106, 40);
|
||||
renderer.drawText(UI_FONT_ID, 245 + (105 - renderer.getTextWidth(UI_FONT_ID, "Left")) / 2, pageHeight - 35, "Left");
|
||||
renderer.drawText(UI_FONT_ID, 20, menuY, "Browse", selectorIndex != menuIndex);
|
||||
menuY += 30;
|
||||
menuIndex++;
|
||||
|
||||
renderer.drawRect(350, pageHeight - 40, 106, 40);
|
||||
renderer.drawText(UI_FONT_ID, 350 + (105 - renderer.getTextWidth(UI_FONT_ID, "Right")) / 2, pageHeight - 35, "Right");
|
||||
renderer.drawText(UI_FONT_ID, 20, menuY, "File transfer", selectorIndex != menuIndex);
|
||||
menuY += 30;
|
||||
menuIndex++;
|
||||
|
||||
renderer.drawText(UI_FONT_ID, 20, menuY, "Settings", selectorIndex != menuIndex);
|
||||
|
||||
renderer.drawButtonHints(UI_FONT_ID, "Back", "Confirm", "Left", "Right");
|
||||
|
||||
renderer.displayBuffer();
|
||||
}
|
||||
|
||||
@@ -12,17 +12,26 @@ class HomeActivity final : public Activity {
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
int selectorIndex = 0;
|
||||
bool updateRequired = false;
|
||||
bool hasContinueReading = false;
|
||||
const std::function<void()> onContinueReading;
|
||||
const std::function<void()> onReaderOpen;
|
||||
const std::function<void()> onSettingsOpen;
|
||||
const std::function<void()> onFileTransferOpen;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
int getMenuItemCount() const;
|
||||
|
||||
public:
|
||||
explicit HomeActivity(GfxRenderer& renderer, InputManager& inputManager, const std::function<void()>& onReaderOpen,
|
||||
const std::function<void()>& onSettingsOpen)
|
||||
: Activity(renderer, inputManager), onReaderOpen(onReaderOpen), onSettingsOpen(onSettingsOpen) {}
|
||||
explicit HomeActivity(GfxRenderer& renderer, InputManager& inputManager,
|
||||
const std::function<void()>& onContinueReading, const std::function<void()>& onReaderOpen,
|
||||
const std::function<void()>& onSettingsOpen, const std::function<void()>& onFileTransferOpen)
|
||||
: Activity("Home", renderer, inputManager),
|
||||
onContinueReading(onContinueReading),
|
||||
onReaderOpen(onReaderOpen),
|
||||
onSettingsOpen(onSettingsOpen),
|
||||
onFileTransferOpen(onFileTransferOpen) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
|
||||
432
src/activities/network/CrossPointWebServerActivity.cpp
Normal file
@@ -0,0 +1,432 @@
|
||||
#include "CrossPointWebServerActivity.h"
|
||||
|
||||
#include <DNSServer.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <GfxRenderer.h>
|
||||
#include <InputManager.h>
|
||||
#include <WiFi.h>
|
||||
#include <qrcode.h>
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "NetworkModeSelectionActivity.h"
|
||||
#include "WifiSelectionActivity.h"
|
||||
#include "config.h"
|
||||
|
||||
namespace {
|
||||
// AP Mode configuration
|
||||
constexpr const char* AP_SSID = "CrossPoint-Reader";
|
||||
constexpr const char* AP_PASSWORD = nullptr; // Open network for ease of use
|
||||
constexpr const char* AP_HOSTNAME = "crosspoint";
|
||||
constexpr uint8_t AP_CHANNEL = 1;
|
||||
constexpr uint8_t AP_MAX_CONNECTIONS = 4;
|
||||
|
||||
// DNS server for captive portal (redirects all DNS queries to our IP)
|
||||
DNSServer* dnsServer = nullptr;
|
||||
constexpr uint16_t DNS_PORT = 53;
|
||||
} // namespace
|
||||
|
||||
void CrossPointWebServerActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<CrossPointWebServerActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::onEnter() {
|
||||
ActivityWithSubactivity::onEnter();
|
||||
|
||||
Serial.printf("[%lu] [WEBACT] [MEM] Free heap at onEnter: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
// Reset state
|
||||
state = WebServerActivityState::MODE_SELECTION;
|
||||
networkMode = NetworkMode::JOIN_NETWORK;
|
||||
isApMode = false;
|
||||
connectedIP.clear();
|
||||
connectedSSID.clear();
|
||||
lastHandleClientTime = 0;
|
||||
updateRequired = true;
|
||||
|
||||
xTaskCreate(&CrossPointWebServerActivity::taskTrampoline, "WebServerActivityTask",
|
||||
2048, // Stack size
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
);
|
||||
|
||||
// Launch network mode selection subactivity
|
||||
Serial.printf("[%lu] [WEBACT] Launching NetworkModeSelectionActivity...\n", millis());
|
||||
enterNewActivity(new NetworkModeSelectionActivity(
|
||||
renderer, inputManager, [this](const NetworkMode mode) { onNetworkModeSelected(mode); },
|
||||
[this]() { onGoBack(); } // Cancel goes back to home
|
||||
));
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::onExit() {
|
||||
ActivityWithSubactivity::onExit();
|
||||
|
||||
Serial.printf("[%lu] [WEBACT] [MEM] Free heap at onExit start: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
state = WebServerActivityState::SHUTTING_DOWN;
|
||||
|
||||
// Stop the web server first (before disconnecting WiFi)
|
||||
stopWebServer();
|
||||
|
||||
// Stop mDNS
|
||||
MDNS.end();
|
||||
|
||||
// Stop DNS server if running (AP mode)
|
||||
if (dnsServer) {
|
||||
Serial.printf("[%lu] [WEBACT] Stopping DNS server...\n", millis());
|
||||
dnsServer->stop();
|
||||
delete dnsServer;
|
||||
dnsServer = nullptr;
|
||||
}
|
||||
|
||||
// CRITICAL: Wait for LWIP stack to flush any pending packets
|
||||
Serial.printf("[%lu] [WEBACT] Waiting 500ms for network stack to flush pending packets...\n", millis());
|
||||
delay(500);
|
||||
|
||||
// Disconnect WiFi gracefully
|
||||
if (isApMode) {
|
||||
Serial.printf("[%lu] [WEBACT] Stopping WiFi AP...\n", millis());
|
||||
WiFi.softAPdisconnect(true);
|
||||
} else {
|
||||
Serial.printf("[%lu] [WEBACT] Disconnecting WiFi (graceful)...\n", millis());
|
||||
WiFi.disconnect(false); // false = don't erase credentials, send disconnect frame
|
||||
}
|
||||
delay(100); // Allow disconnect frame to be sent
|
||||
|
||||
Serial.printf("[%lu] [WEBACT] Setting WiFi mode OFF...\n", millis());
|
||||
WiFi.mode(WIFI_OFF);
|
||||
delay(100); // Allow WiFi hardware to fully power down
|
||||
|
||||
Serial.printf("[%lu] [WEBACT] [MEM] Free heap after WiFi disconnect: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
// Acquire mutex before deleting task
|
||||
Serial.printf("[%lu] [WEBACT] Acquiring rendering mutex before task deletion...\n", millis());
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
|
||||
// Delete the display task
|
||||
Serial.printf("[%lu] [WEBACT] Deleting display task...\n", millis());
|
||||
if (displayTaskHandle) {
|
||||
vTaskDelete(displayTaskHandle);
|
||||
displayTaskHandle = nullptr;
|
||||
Serial.printf("[%lu] [WEBACT] Display task deleted\n", millis());
|
||||
}
|
||||
|
||||
// Delete the mutex
|
||||
Serial.printf("[%lu] [WEBACT] Deleting mutex...\n", millis());
|
||||
vSemaphoreDelete(renderingMutex);
|
||||
renderingMutex = nullptr;
|
||||
Serial.printf("[%lu] [WEBACT] Mutex deleted\n", millis());
|
||||
|
||||
Serial.printf("[%lu] [WEBACT] [MEM] Free heap at onExit end: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::onNetworkModeSelected(const NetworkMode mode) {
|
||||
Serial.printf("[%lu] [WEBACT] Network mode selected: %s\n", millis(),
|
||||
mode == NetworkMode::JOIN_NETWORK ? "Join Network" : "Create Hotspot");
|
||||
|
||||
networkMode = mode;
|
||||
isApMode = (mode == NetworkMode::CREATE_HOTSPOT);
|
||||
|
||||
// Exit mode selection subactivity
|
||||
exitActivity();
|
||||
|
||||
if (mode == NetworkMode::JOIN_NETWORK) {
|
||||
// STA mode - launch WiFi selection
|
||||
Serial.printf("[%lu] [WEBACT] Turning on WiFi (STA mode)...\n", millis());
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
state = WebServerActivityState::WIFI_SELECTION;
|
||||
Serial.printf("[%lu] [WEBACT] Launching WifiSelectionActivity...\n", millis());
|
||||
enterNewActivity(new WifiSelectionActivity(renderer, inputManager,
|
||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
||||
} else {
|
||||
// AP mode - start access point
|
||||
state = WebServerActivityState::AP_STARTING;
|
||||
updateRequired = true;
|
||||
startAccessPoint();
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::onWifiSelectionComplete(const bool connected) {
|
||||
Serial.printf("[%lu] [WEBACT] WifiSelectionActivity completed, connected=%d\n", millis(), connected);
|
||||
|
||||
if (connected) {
|
||||
// Get connection info before exiting subactivity
|
||||
connectedIP = static_cast<WifiSelectionActivity*>(subActivity.get())->getConnectedIP();
|
||||
connectedSSID = WiFi.SSID().c_str();
|
||||
isApMode = false;
|
||||
|
||||
exitActivity();
|
||||
|
||||
// Start mDNS for hostname resolution
|
||||
if (MDNS.begin(AP_HOSTNAME)) {
|
||||
Serial.printf("[%lu] [WEBACT] mDNS started: http://%s.local/\n", millis(), AP_HOSTNAME);
|
||||
}
|
||||
|
||||
// Start the web server
|
||||
startWebServer();
|
||||
} else {
|
||||
// User cancelled - go back to mode selection
|
||||
exitActivity();
|
||||
state = WebServerActivityState::MODE_SELECTION;
|
||||
enterNewActivity(new NetworkModeSelectionActivity(
|
||||
renderer, inputManager, [this](const NetworkMode mode) { onNetworkModeSelected(mode); },
|
||||
[this]() { onGoBack(); }));
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::startAccessPoint() {
|
||||
Serial.printf("[%lu] [WEBACT] Starting Access Point mode...\n", millis());
|
||||
Serial.printf("[%lu] [WEBACT] [MEM] Free heap before AP start: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
// Configure and start the AP
|
||||
WiFi.mode(WIFI_AP);
|
||||
delay(100);
|
||||
|
||||
// Start soft AP
|
||||
bool apStarted;
|
||||
if (AP_PASSWORD && strlen(AP_PASSWORD) >= 8) {
|
||||
apStarted = WiFi.softAP(AP_SSID, AP_PASSWORD, AP_CHANNEL, false, AP_MAX_CONNECTIONS);
|
||||
} else {
|
||||
// Open network (no password)
|
||||
apStarted = WiFi.softAP(AP_SSID, nullptr, AP_CHANNEL, false, AP_MAX_CONNECTIONS);
|
||||
}
|
||||
|
||||
if (!apStarted) {
|
||||
Serial.printf("[%lu] [WEBACT] ERROR: Failed to start Access Point!\n", millis());
|
||||
onGoBack();
|
||||
return;
|
||||
}
|
||||
|
||||
delay(100); // Wait for AP to fully initialize
|
||||
|
||||
// Get AP IP address
|
||||
const IPAddress apIP = WiFi.softAPIP();
|
||||
char ipStr[16];
|
||||
snprintf(ipStr, sizeof(ipStr), "%d.%d.%d.%d", apIP[0], apIP[1], apIP[2], apIP[3]);
|
||||
connectedIP = ipStr;
|
||||
connectedSSID = AP_SSID;
|
||||
|
||||
Serial.printf("[%lu] [WEBACT] Access Point started!\n", millis());
|
||||
Serial.printf("[%lu] [WEBACT] SSID: %s\n", millis(), AP_SSID);
|
||||
Serial.printf("[%lu] [WEBACT] IP: %s\n", millis(), connectedIP.c_str());
|
||||
|
||||
// Start mDNS for hostname resolution
|
||||
if (MDNS.begin(AP_HOSTNAME)) {
|
||||
Serial.printf("[%lu] [WEBACT] mDNS started: http://%s.local/\n", millis(), AP_HOSTNAME);
|
||||
} else {
|
||||
Serial.printf("[%lu] [WEBACT] WARNING: mDNS failed to start\n", millis());
|
||||
}
|
||||
|
||||
// Start DNS server for captive portal behavior
|
||||
// This redirects all DNS queries to our IP, making any domain typed resolve to us
|
||||
dnsServer = new DNSServer();
|
||||
dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
|
||||
dnsServer->start(DNS_PORT, "*", apIP);
|
||||
Serial.printf("[%lu] [WEBACT] DNS server started for captive portal\n", millis());
|
||||
|
||||
Serial.printf("[%lu] [WEBACT] [MEM] Free heap after AP start: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
// Start the web server
|
||||
startWebServer();
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::startWebServer() {
|
||||
Serial.printf("[%lu] [WEBACT] Starting web server...\n", millis());
|
||||
|
||||
// Create the web server instance
|
||||
webServer.reset(new CrossPointWebServer());
|
||||
webServer->begin();
|
||||
|
||||
if (webServer->isRunning()) {
|
||||
state = WebServerActivityState::SERVER_RUNNING;
|
||||
Serial.printf("[%lu] [WEBACT] Web server started successfully\n", millis());
|
||||
|
||||
// Force an immediate render since we're transitioning from a subactivity
|
||||
// that had its own rendering task. We need to make sure our display is shown.
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
Serial.printf("[%lu] [WEBACT] Rendered File Transfer screen\n", millis());
|
||||
} else {
|
||||
Serial.printf("[%lu] [WEBACT] ERROR: Failed to start web server!\n", millis());
|
||||
webServer.reset();
|
||||
// Go back on error
|
||||
onGoBack();
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::stopWebServer() {
|
||||
if (webServer && webServer->isRunning()) {
|
||||
Serial.printf("[%lu] [WEBACT] Stopping web server...\n", millis());
|
||||
webServer->stop();
|
||||
Serial.printf("[%lu] [WEBACT] Web server stopped\n", millis());
|
||||
}
|
||||
webServer.reset();
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::loop() {
|
||||
if (subActivity) {
|
||||
// Forward loop to subactivity
|
||||
subActivity->loop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle different states
|
||||
if (state == WebServerActivityState::SERVER_RUNNING) {
|
||||
// Handle DNS requests for captive portal (AP mode only)
|
||||
if (isApMode && dnsServer) {
|
||||
dnsServer->processNextRequest();
|
||||
}
|
||||
|
||||
// Handle web server requests - call handleClient multiple times per loop
|
||||
// to improve responsiveness and upload throughput
|
||||
if (webServer && webServer->isRunning()) {
|
||||
const unsigned long timeSinceLastHandleClient = millis() - lastHandleClientTime;
|
||||
|
||||
// Log if there's a significant gap between handleClient calls (>100ms)
|
||||
if (lastHandleClientTime > 0 && timeSinceLastHandleClient > 100) {
|
||||
Serial.printf("[%lu] [WEBACT] WARNING: %lu ms gap since last handleClient\n", millis(),
|
||||
timeSinceLastHandleClient);
|
||||
}
|
||||
|
||||
// Call handleClient multiple times to process pending requests faster
|
||||
// This is critical for upload performance - HTTP file uploads send data
|
||||
// in chunks and each handleClient() call processes incoming data
|
||||
constexpr int HANDLE_CLIENT_ITERATIONS = 10;
|
||||
for (int i = 0; i < HANDLE_CLIENT_ITERATIONS && webServer->isRunning(); i++) {
|
||||
webServer->handleClient();
|
||||
}
|
||||
lastHandleClientTime = millis();
|
||||
}
|
||||
|
||||
// Handle exit on Back button
|
||||
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
onGoBack();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
if (updateRequired) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::render() const {
|
||||
// Only render our own UI when server is running
|
||||
// Subactivities handle their own rendering
|
||||
if (state == WebServerActivityState::SERVER_RUNNING) {
|
||||
renderer.clearScreen();
|
||||
renderServerRunning();
|
||||
renderer.displayBuffer();
|
||||
} else if (state == WebServerActivityState::AP_STARTING) {
|
||||
renderer.clearScreen();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
renderer.drawCenteredText(READER_FONT_ID, pageHeight / 2 - 20, "Starting Hotspot...", true, BOLD);
|
||||
renderer.displayBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
void drawQRCode(const GfxRenderer& renderer, const int x, const int y, const std::string& data) {
|
||||
// Implementation of QR code calculation
|
||||
// The structure to manage the QR code
|
||||
QRCode qrcode;
|
||||
uint8_t qrcodeBytes[qrcode_getBufferSize(4)];
|
||||
Serial.printf("[%lu] [WEBACT] QR Code (%lu): %s\n", millis(), data.length(), data.c_str());
|
||||
|
||||
qrcode_initText(&qrcode, qrcodeBytes, 4, ECC_LOW, data.c_str());
|
||||
const uint8_t px = 6; // pixels per module
|
||||
for (uint8_t cy = 0; cy < qrcode.size; cy++) {
|
||||
for (uint8_t cx = 0; cx < qrcode.size; cx++) {
|
||||
if (qrcode_getModule(&qrcode, cx, cy)) {
|
||||
// Serial.print("**");
|
||||
renderer.fillRect(x + px * cx, y + px * cy, px, px, true);
|
||||
} else {
|
||||
// Serial.print(" ");
|
||||
}
|
||||
}
|
||||
// Serial.print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServerActivity::renderServerRunning() const {
|
||||
// Use consistent line spacing
|
||||
constexpr int LINE_SPACING = 28; // Space between lines
|
||||
|
||||
renderer.drawCenteredText(READER_FONT_ID, 15, "File Transfer", true, BOLD);
|
||||
|
||||
if (isApMode) {
|
||||
// AP mode display - center the content block
|
||||
int startY = 55;
|
||||
|
||||
renderer.drawCenteredText(UI_FONT_ID, startY, "Hotspot Mode", true, BOLD);
|
||||
|
||||
std::string ssidInfo = "Network: " + connectedSSID;
|
||||
renderer.drawCenteredText(UI_FONT_ID, startY + LINE_SPACING, ssidInfo.c_str(), true, REGULAR);
|
||||
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, startY + LINE_SPACING * 2, "Connect your device to this WiFi network",
|
||||
true, REGULAR);
|
||||
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, startY + LINE_SPACING * 3,
|
||||
"or scan QR code with your phone to connect to Wifi.", true, REGULAR);
|
||||
// Show QR code for URL
|
||||
std::string wifiConfig = std::string("WIFI:T:WPA;S:") + connectedSSID + ";P:" + "" + ";;";
|
||||
drawQRCode(renderer, (480 - 6 * 33) / 2, startY + LINE_SPACING * 4, wifiConfig);
|
||||
|
||||
startY += 6 * 29 + 3 * LINE_SPACING;
|
||||
// Show primary URL (hostname)
|
||||
std::string hostnameUrl = std::string("http://") + AP_HOSTNAME + ".local/";
|
||||
renderer.drawCenteredText(UI_FONT_ID, startY + LINE_SPACING * 3, hostnameUrl.c_str(), true, BOLD);
|
||||
|
||||
// Show IP address as fallback
|
||||
std::string ipUrl = "or http://" + connectedIP + "/";
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, startY + LINE_SPACING * 4, ipUrl.c_str(), true, REGULAR);
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, startY + LINE_SPACING * 5, "Open this URL in your browser", true, REGULAR);
|
||||
|
||||
// Show QR code for URL
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, startY + LINE_SPACING * 6, "or scan QR code with your phone:", true,
|
||||
REGULAR);
|
||||
drawQRCode(renderer, (480 - 6 * 33) / 2, startY + LINE_SPACING * 7, hostnameUrl);
|
||||
} else {
|
||||
// STA mode display (original behavior)
|
||||
const int startY = 65;
|
||||
|
||||
std::string ssidInfo = "Network: " + connectedSSID;
|
||||
if (ssidInfo.length() > 28) {
|
||||
ssidInfo.replace(25, ssidInfo.length() - 25, "...");
|
||||
}
|
||||
renderer.drawCenteredText(UI_FONT_ID, startY, ssidInfo.c_str(), true, REGULAR);
|
||||
|
||||
std::string ipInfo = "IP Address: " + connectedIP;
|
||||
renderer.drawCenteredText(UI_FONT_ID, startY + LINE_SPACING, ipInfo.c_str(), true, REGULAR);
|
||||
|
||||
// Show web server URL prominently
|
||||
std::string webInfo = "http://" + connectedIP + "/";
|
||||
renderer.drawCenteredText(UI_FONT_ID, startY + LINE_SPACING * 2, webInfo.c_str(), true, BOLD);
|
||||
|
||||
// Also show hostname URL
|
||||
std::string hostnameUrl = std::string("or http://") + AP_HOSTNAME + ".local/";
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, startY + LINE_SPACING * 3, hostnameUrl.c_str(), true, REGULAR);
|
||||
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, startY + LINE_SPACING * 4, "Open this URL in your browser", true, REGULAR);
|
||||
|
||||
// Show QR code for URL
|
||||
drawQRCode(renderer, (480 - 6 * 33) / 2, startY + LINE_SPACING * 6, webInfo);
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, startY + LINE_SPACING * 5, "or scan QR code with your phone:", true,
|
||||
REGULAR);
|
||||
}
|
||||
|
||||
renderer.drawButtonHints(UI_FONT_ID, "« Exit", "", "", "");
|
||||
}
|
||||
73
src/activities/network/CrossPointWebServerActivity.h
Normal file
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "NetworkModeSelectionActivity.h"
|
||||
#include "activities/ActivityWithSubactivity.h"
|
||||
#include "network/CrossPointWebServer.h"
|
||||
|
||||
// Web server activity states
|
||||
enum class WebServerActivityState {
|
||||
MODE_SELECTION, // Choosing between Join Network and Create Hotspot
|
||||
WIFI_SELECTION, // WiFi selection subactivity is active (for Join Network mode)
|
||||
AP_STARTING, // Starting Access Point mode
|
||||
SERVER_RUNNING, // Web server is running and handling requests
|
||||
SHUTTING_DOWN // Shutting down server and WiFi
|
||||
};
|
||||
|
||||
/**
|
||||
* CrossPointWebServerActivity is the entry point for file transfer functionality.
|
||||
* It:
|
||||
* - First presents a choice between "Join a Network" (STA) and "Create Hotspot" (AP)
|
||||
* - For STA mode: Launches WifiSelectionActivity to connect to an existing network
|
||||
* - For AP mode: Creates an Access Point that clients can connect to
|
||||
* - Starts the CrossPointWebServer when connected
|
||||
* - Handles client requests in its loop() function
|
||||
* - Cleans up the server and shuts down WiFi on exit
|
||||
*/
|
||||
class CrossPointWebServerActivity final : public ActivityWithSubactivity {
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
bool updateRequired = false;
|
||||
WebServerActivityState state = WebServerActivityState::MODE_SELECTION;
|
||||
const std::function<void()> onGoBack;
|
||||
|
||||
// Network mode
|
||||
NetworkMode networkMode = NetworkMode::JOIN_NETWORK;
|
||||
bool isApMode = false;
|
||||
|
||||
// Web server - owned by this activity
|
||||
std::unique_ptr<CrossPointWebServer> webServer;
|
||||
|
||||
// Server status
|
||||
std::string connectedIP;
|
||||
std::string connectedSSID; // For STA mode: network name, For AP mode: AP name
|
||||
|
||||
// Performance monitoring
|
||||
unsigned long lastHandleClientTime = 0;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
void renderServerRunning() const;
|
||||
|
||||
void onNetworkModeSelected(NetworkMode mode);
|
||||
void onWifiSelectionComplete(bool connected);
|
||||
void startAccessPoint();
|
||||
void startWebServer();
|
||||
void stopWebServer();
|
||||
|
||||
public:
|
||||
explicit CrossPointWebServerActivity(GfxRenderer& renderer, InputManager& inputManager,
|
||||
const std::function<void()>& onGoBack)
|
||||
: ActivityWithSubactivity("CrossPointWebServer", renderer, inputManager), onGoBack(onGoBack) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
bool skipLoopDelay() override { return webServer && webServer->isRunning(); }
|
||||
};
|
||||
128
src/activities/network/NetworkModeSelectionActivity.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#include "NetworkModeSelectionActivity.h"
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <InputManager.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
namespace {
|
||||
constexpr int MENU_ITEM_COUNT = 2;
|
||||
const char* MENU_ITEMS[MENU_ITEM_COUNT] = {"Join a Network", "Create Hotspot"};
|
||||
const char* MENU_DESCRIPTIONS[MENU_ITEM_COUNT] = {"Connect to an existing WiFi network",
|
||||
"Create a WiFi network others can join"};
|
||||
} // namespace
|
||||
|
||||
void NetworkModeSelectionActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<NetworkModeSelectionActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
void NetworkModeSelectionActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
// Reset selection
|
||||
selectedIndex = 0;
|
||||
|
||||
// Trigger first update
|
||||
updateRequired = true;
|
||||
|
||||
xTaskCreate(&NetworkModeSelectionActivity::taskTrampoline, "NetworkModeTask",
|
||||
2048, // Stack size
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
);
|
||||
}
|
||||
|
||||
void NetworkModeSelectionActivity::onExit() {
|
||||
Activity::onExit();
|
||||
|
||||
// Wait until not rendering to delete task
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
vTaskDelete(displayTaskHandle);
|
||||
displayTaskHandle = nullptr;
|
||||
}
|
||||
vSemaphoreDelete(renderingMutex);
|
||||
renderingMutex = nullptr;
|
||||
}
|
||||
|
||||
void NetworkModeSelectionActivity::loop() {
|
||||
// Handle back button - cancel
|
||||
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
onCancel();
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle confirm button - select current option
|
||||
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
const NetworkMode mode = (selectedIndex == 0) ? NetworkMode::JOIN_NETWORK : NetworkMode::CREATE_HOTSPOT;
|
||||
onModeSelected(mode);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle navigation
|
||||
const bool prevPressed =
|
||||
inputManager.wasPressed(InputManager::BTN_UP) || inputManager.wasPressed(InputManager::BTN_LEFT);
|
||||
const bool nextPressed =
|
||||
inputManager.wasPressed(InputManager::BTN_DOWN) || inputManager.wasPressed(InputManager::BTN_RIGHT);
|
||||
|
||||
if (prevPressed) {
|
||||
selectedIndex = (selectedIndex + MENU_ITEM_COUNT - 1) % MENU_ITEM_COUNT;
|
||||
updateRequired = true;
|
||||
} else if (nextPressed) {
|
||||
selectedIndex = (selectedIndex + 1) % MENU_ITEM_COUNT;
|
||||
updateRequired = true;
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkModeSelectionActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
if (updateRequired) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkModeSelectionActivity::render() const {
|
||||
renderer.clearScreen();
|
||||
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
|
||||
// Draw header
|
||||
renderer.drawCenteredText(READER_FONT_ID, 10, "File Transfer", true, BOLD);
|
||||
|
||||
// Draw subtitle
|
||||
renderer.drawCenteredText(UI_FONT_ID, 50, "How would you like to connect?", true, REGULAR);
|
||||
|
||||
// Draw menu items centered on screen
|
||||
constexpr int itemHeight = 50; // Height for each menu item (including description)
|
||||
const int startY = (pageHeight - (MENU_ITEM_COUNT * itemHeight)) / 2 + 10;
|
||||
|
||||
for (int i = 0; i < MENU_ITEM_COUNT; i++) {
|
||||
const int itemY = startY + i * itemHeight;
|
||||
const bool isSelected = (i == selectedIndex);
|
||||
|
||||
// Draw selection highlight (black fill) for selected item
|
||||
if (isSelected) {
|
||||
renderer.fillRect(20, itemY - 2, pageWidth - 40, itemHeight - 6);
|
||||
}
|
||||
|
||||
// Draw text: black=false (white text) when selected (on black background)
|
||||
// black=true (black text) when not selected (on white background)
|
||||
renderer.drawText(UI_FONT_ID, 30, itemY, MENU_ITEMS[i], /*black=*/!isSelected);
|
||||
renderer.drawText(SMALL_FONT_ID, 30, itemY + 22, MENU_DESCRIPTIONS[i], /*black=*/!isSelected);
|
||||
}
|
||||
|
||||
// Draw help text at bottom
|
||||
renderer.drawButtonHints(UI_FONT_ID, "« Back", "Select", "", "");
|
||||
|
||||
renderer.displayBuffer();
|
||||
}
|
||||
41
src/activities/network/NetworkModeSelectionActivity.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "../Activity.h"
|
||||
|
||||
// Enum for network mode selection
|
||||
enum class NetworkMode { JOIN_NETWORK, CREATE_HOTSPOT };
|
||||
|
||||
/**
|
||||
* NetworkModeSelectionActivity presents the user with a choice:
|
||||
* - "Join a Network" - Connect to an existing WiFi network (STA mode)
|
||||
* - "Create Hotspot" - Create an Access Point that others can connect to (AP mode)
|
||||
*
|
||||
* The onModeSelected callback is called with the user's choice.
|
||||
* The onCancel callback is called if the user presses back.
|
||||
*/
|
||||
class NetworkModeSelectionActivity final : public Activity {
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
int selectedIndex = 0;
|
||||
bool updateRequired = false;
|
||||
const std::function<void(NetworkMode)> onModeSelected;
|
||||
const std::function<void()> onCancel;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
|
||||
public:
|
||||
explicit NetworkModeSelectionActivity(GfxRenderer& renderer, InputManager& inputManager,
|
||||
const std::function<void(NetworkMode)>& onModeSelected,
|
||||
const std::function<void()>& onCancel)
|
||||
: Activity("NetworkModeSelection", renderer, inputManager), onModeSelected(onModeSelected), onCancel(onCancel) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
};
|
||||
688
src/activities/network/WifiSelectionActivity.cpp
Normal file
@@ -0,0 +1,688 @@
|
||||
#include "WifiSelectionActivity.h"
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "WifiCredentialStore.h"
|
||||
#include "activities/util/KeyboardEntryActivity.h"
|
||||
#include "config.h"
|
||||
|
||||
void WifiSelectionActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<WifiSelectionActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
// Load saved WiFi credentials - SD card operations need lock as we use SPI for both
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
WIFI_STORE.loadFromFile();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
|
||||
// Reset state
|
||||
selectedNetworkIndex = 0;
|
||||
networks.clear();
|
||||
state = WifiSelectionState::SCANNING;
|
||||
selectedSSID.clear();
|
||||
connectedIP.clear();
|
||||
connectionError.clear();
|
||||
enteredPassword.clear();
|
||||
usedSavedPassword = false;
|
||||
savePromptSelection = 0;
|
||||
forgetPromptSelection = 0;
|
||||
|
||||
// Trigger first update to show scanning message
|
||||
updateRequired = true;
|
||||
|
||||
xTaskCreate(&WifiSelectionActivity::taskTrampoline, "WifiSelectionTask",
|
||||
4096, // Stack size (larger for WiFi operations)
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
);
|
||||
|
||||
// Start WiFi scan
|
||||
startWifiScan();
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::onExit() {
|
||||
Activity::onExit();
|
||||
|
||||
Serial.printf("[%lu] [WIFI] [MEM] Free heap at onExit start: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
// Stop any ongoing WiFi scan
|
||||
Serial.printf("[%lu] [WIFI] Deleting WiFi scan...\n", millis());
|
||||
WiFi.scanDelete();
|
||||
Serial.printf("[%lu] [WIFI] [MEM] Free heap after scanDelete: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
// Note: We do NOT disconnect WiFi here - the parent activity (CrossPointWebServerActivity)
|
||||
// manages WiFi connection state. We just clean up the scan and task.
|
||||
|
||||
// Acquire mutex before deleting task to ensure task isn't using it
|
||||
// This prevents hangs/crashes if the task holds the mutex when deleted
|
||||
Serial.printf("[%lu] [WIFI] Acquiring rendering mutex before task deletion...\n", millis());
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
|
||||
// Delete the display task (we now hold the mutex, so task is blocked if it needs it)
|
||||
Serial.printf("[%lu] [WIFI] Deleting display task...\n", millis());
|
||||
if (displayTaskHandle) {
|
||||
vTaskDelete(displayTaskHandle);
|
||||
displayTaskHandle = nullptr;
|
||||
Serial.printf("[%lu] [WIFI] Display task deleted\n", millis());
|
||||
}
|
||||
|
||||
// Now safe to delete the mutex since we own it
|
||||
Serial.printf("[%lu] [WIFI] Deleting mutex...\n", millis());
|
||||
vSemaphoreDelete(renderingMutex);
|
||||
renderingMutex = nullptr;
|
||||
Serial.printf("[%lu] [WIFI] Mutex deleted\n", millis());
|
||||
|
||||
Serial.printf("[%lu] [WIFI] [MEM] Free heap at onExit end: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::startWifiScan() {
|
||||
state = WifiSelectionState::SCANNING;
|
||||
networks.clear();
|
||||
updateRequired = true;
|
||||
|
||||
// Set WiFi mode to station
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect();
|
||||
delay(100);
|
||||
|
||||
// Start async scan
|
||||
WiFi.scanNetworks(true); // true = async scan
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::processWifiScanResults() {
|
||||
const int16_t scanResult = WiFi.scanComplete();
|
||||
|
||||
if (scanResult == WIFI_SCAN_RUNNING) {
|
||||
// Scan still in progress
|
||||
return;
|
||||
}
|
||||
|
||||
if (scanResult == WIFI_SCAN_FAILED) {
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Scan complete, process results
|
||||
// Use a map to deduplicate networks by SSID, keeping the strongest signal
|
||||
std::map<std::string, WifiNetworkInfo> uniqueNetworks;
|
||||
|
||||
for (int i = 0; i < scanResult; i++) {
|
||||
std::string ssid = WiFi.SSID(i).c_str();
|
||||
const int32_t rssi = WiFi.RSSI(i);
|
||||
|
||||
// Skip hidden networks (empty SSID)
|
||||
if (ssid.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if we've already seen this SSID
|
||||
auto it = uniqueNetworks.find(ssid);
|
||||
if (it == uniqueNetworks.end() || rssi > it->second.rssi) {
|
||||
// New network or stronger signal than existing entry
|
||||
WifiNetworkInfo network;
|
||||
network.ssid = ssid;
|
||||
network.rssi = rssi;
|
||||
network.isEncrypted = (WiFi.encryptionType(i) != WIFI_AUTH_OPEN);
|
||||
network.hasSavedPassword = WIFI_STORE.hasSavedCredential(network.ssid);
|
||||
uniqueNetworks[ssid] = network;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert map to vector
|
||||
networks.clear();
|
||||
for (const auto& pair : uniqueNetworks) {
|
||||
// cppcheck-suppress useStlAlgorithm
|
||||
networks.push_back(pair.second);
|
||||
}
|
||||
|
||||
// Sort by signal strength (strongest first)
|
||||
std::sort(networks.begin(), networks.end(),
|
||||
[](const WifiNetworkInfo& a, const WifiNetworkInfo& b) { return a.rssi > b.rssi; });
|
||||
|
||||
// Show networks with PW first
|
||||
std::sort(networks.begin(), networks.end(), [](const WifiNetworkInfo& a, const WifiNetworkInfo& b) {
|
||||
return a.hasSavedPassword && !b.hasSavedPassword;
|
||||
});
|
||||
|
||||
WiFi.scanDelete();
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
selectedNetworkIndex = 0;
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::selectNetwork(const int index) {
|
||||
if (index < 0 || index >= static_cast<int>(networks.size())) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& network = networks[index];
|
||||
selectedSSID = network.ssid;
|
||||
selectedRequiresPassword = network.isEncrypted;
|
||||
usedSavedPassword = false;
|
||||
enteredPassword.clear();
|
||||
|
||||
// Check if we have saved credentials for this network
|
||||
const auto* savedCred = WIFI_STORE.findCredential(selectedSSID);
|
||||
if (savedCred && !savedCred->password.empty()) {
|
||||
// Use saved password - connect directly
|
||||
enteredPassword = savedCred->password;
|
||||
usedSavedPassword = true;
|
||||
Serial.printf("[%lu] [WiFi] Using saved password for %s, length: %zu\n", millis(), selectedSSID.c_str(),
|
||||
enteredPassword.size());
|
||||
attemptConnection();
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedRequiresPassword) {
|
||||
// Show password entry
|
||||
state = WifiSelectionState::PASSWORD_ENTRY;
|
||||
// Don't allow screen updates while changing activity
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
enterNewActivity(new KeyboardEntryActivity(
|
||||
renderer, inputManager, "Enter WiFi Password",
|
||||
"", // No initial text
|
||||
50, // Y position
|
||||
64, // Max password length
|
||||
false, // Show password by default (hard keyboard to use)
|
||||
[this](const std::string& text) {
|
||||
enteredPassword = text;
|
||||
exitActivity();
|
||||
},
|
||||
[this] {
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
updateRequired = true;
|
||||
exitActivity();
|
||||
}));
|
||||
updateRequired = true;
|
||||
xSemaphoreGive(renderingMutex);
|
||||
} else {
|
||||
// Connect directly for open networks
|
||||
attemptConnection();
|
||||
}
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::attemptConnection() {
|
||||
state = WifiSelectionState::CONNECTING;
|
||||
connectionStartTime = millis();
|
||||
connectedIP.clear();
|
||||
connectionError.clear();
|
||||
updateRequired = true;
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
if (selectedRequiresPassword && !enteredPassword.empty()) {
|
||||
WiFi.begin(selectedSSID.c_str(), enteredPassword.c_str());
|
||||
} else {
|
||||
WiFi.begin(selectedSSID.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::checkConnectionStatus() {
|
||||
if (state != WifiSelectionState::CONNECTING) {
|
||||
return;
|
||||
}
|
||||
|
||||
const wl_status_t status = WiFi.status();
|
||||
|
||||
if (status == WL_CONNECTED) {
|
||||
// Successfully connected
|
||||
IPAddress ip = WiFi.localIP();
|
||||
char ipStr[16];
|
||||
snprintf(ipStr, sizeof(ipStr), "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
|
||||
connectedIP = ipStr;
|
||||
|
||||
// If we entered a new password, ask if user wants to save it
|
||||
// Otherwise, immediately complete so parent can start web server
|
||||
if (!usedSavedPassword && !enteredPassword.empty()) {
|
||||
state = WifiSelectionState::SAVE_PROMPT;
|
||||
savePromptSelection = 0; // Default to "Yes"
|
||||
updateRequired = true;
|
||||
} else {
|
||||
// Using saved password or open network - complete immediately
|
||||
Serial.printf("[%lu] [WIFI] Connected with saved/open credentials, completing immediately\n", millis());
|
||||
onComplete(true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (status == WL_CONNECT_FAILED || status == WL_NO_SSID_AVAIL) {
|
||||
connectionError = "Connection failed";
|
||||
if (status == WL_NO_SSID_AVAIL) {
|
||||
connectionError = "Network not found";
|
||||
}
|
||||
state = WifiSelectionState::CONNECTION_FAILED;
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for timeout
|
||||
if (millis() - connectionStartTime > CONNECTION_TIMEOUT_MS) {
|
||||
WiFi.disconnect();
|
||||
connectionError = "Connection timeout";
|
||||
state = WifiSelectionState::CONNECTION_FAILED;
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::loop() {
|
||||
if (subActivity) {
|
||||
subActivity->loop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Check scan progress
|
||||
if (state == WifiSelectionState::SCANNING) {
|
||||
processWifiScanResults();
|
||||
return;
|
||||
}
|
||||
|
||||
// Check connection progress
|
||||
if (state == WifiSelectionState::CONNECTING) {
|
||||
checkConnectionStatus();
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == WifiSelectionState::PASSWORD_ENTRY) {
|
||||
// Reach here once password entry finished in subactivity
|
||||
attemptConnection();
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle save prompt state
|
||||
if (state == WifiSelectionState::SAVE_PROMPT) {
|
||||
if (inputManager.wasPressed(InputManager::BTN_LEFT) || inputManager.wasPressed(InputManager::BTN_UP)) {
|
||||
if (savePromptSelection > 0) {
|
||||
savePromptSelection--;
|
||||
updateRequired = true;
|
||||
}
|
||||
} else if (inputManager.wasPressed(InputManager::BTN_RIGHT) || inputManager.wasPressed(InputManager::BTN_DOWN)) {
|
||||
if (savePromptSelection < 1) {
|
||||
savePromptSelection++;
|
||||
updateRequired = true;
|
||||
}
|
||||
} else if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
if (savePromptSelection == 0) {
|
||||
// User chose "Yes" - save the password
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
WIFI_STORE.addCredential(selectedSSID, enteredPassword);
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
// Complete - parent will start web server
|
||||
onComplete(true);
|
||||
} else if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
// Skip saving, complete anyway
|
||||
onComplete(true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle forget prompt state (connection failed with saved credentials)
|
||||
if (state == WifiSelectionState::FORGET_PROMPT) {
|
||||
if (inputManager.wasPressed(InputManager::BTN_LEFT) || inputManager.wasPressed(InputManager::BTN_UP)) {
|
||||
if (forgetPromptSelection > 0) {
|
||||
forgetPromptSelection--;
|
||||
updateRequired = true;
|
||||
}
|
||||
} else if (inputManager.wasPressed(InputManager::BTN_RIGHT) || inputManager.wasPressed(InputManager::BTN_DOWN)) {
|
||||
if (forgetPromptSelection < 1) {
|
||||
forgetPromptSelection++;
|
||||
updateRequired = true;
|
||||
}
|
||||
} else if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
if (forgetPromptSelection == 0) {
|
||||
// User chose "Yes" - forget the network
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
WIFI_STORE.removeCredential(selectedSSID);
|
||||
xSemaphoreGive(renderingMutex);
|
||||
// Update the network list to reflect the change
|
||||
const auto network = find_if(networks.begin(), networks.end(),
|
||||
[this](const WifiNetworkInfo& net) { return net.ssid == selectedSSID; });
|
||||
if (network != networks.end()) {
|
||||
network->hasSavedPassword = false;
|
||||
}
|
||||
}
|
||||
// Go back to network list
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
updateRequired = true;
|
||||
} else if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
// Skip forgetting, go back to network list
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
updateRequired = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle connected state (should not normally be reached - connection completes immediately)
|
||||
if (state == WifiSelectionState::CONNECTED) {
|
||||
// Safety fallback - immediately complete
|
||||
onComplete(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle connection failed state
|
||||
if (state == WifiSelectionState::CONNECTION_FAILED) {
|
||||
if (inputManager.wasPressed(InputManager::BTN_BACK) || inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
// If we used saved credentials, offer to forget the network
|
||||
if (usedSavedPassword) {
|
||||
state = WifiSelectionState::FORGET_PROMPT;
|
||||
forgetPromptSelection = 0; // Default to "Yes"
|
||||
} else {
|
||||
// Go back to network list on failure
|
||||
state = WifiSelectionState::NETWORK_LIST;
|
||||
}
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle network list state
|
||||
if (state == WifiSelectionState::NETWORK_LIST) {
|
||||
// Check for Back button to exit (cancel)
|
||||
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
onComplete(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for Confirm button to select network or rescan
|
||||
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
if (!networks.empty()) {
|
||||
selectNetwork(selectedNetworkIndex);
|
||||
} else {
|
||||
startWifiScan();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle UP/DOWN navigation
|
||||
if (inputManager.wasPressed(InputManager::BTN_UP) || inputManager.wasPressed(InputManager::BTN_LEFT)) {
|
||||
if (selectedNetworkIndex > 0) {
|
||||
selectedNetworkIndex--;
|
||||
updateRequired = true;
|
||||
}
|
||||
} else if (inputManager.wasPressed(InputManager::BTN_DOWN) || inputManager.wasPressed(InputManager::BTN_RIGHT)) {
|
||||
if (!networks.empty() && selectedNetworkIndex < static_cast<int>(networks.size()) - 1) {
|
||||
selectedNetworkIndex++;
|
||||
updateRequired = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string WifiSelectionActivity::getSignalStrengthIndicator(const int32_t rssi) const {
|
||||
// Convert RSSI to signal bars representation
|
||||
if (rssi >= -50) {
|
||||
return "||||"; // Excellent
|
||||
}
|
||||
if (rssi >= -60) {
|
||||
return "||| "; // Good
|
||||
}
|
||||
if (rssi >= -70) {
|
||||
return "|| "; // Fair
|
||||
}
|
||||
if (rssi >= -80) {
|
||||
return "| "; // Weak
|
||||
}
|
||||
return " "; // Very weak
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
if (subActivity) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (updateRequired) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::render() const {
|
||||
renderer.clearScreen();
|
||||
|
||||
switch (state) {
|
||||
case WifiSelectionState::SCANNING:
|
||||
renderConnecting(); // Reuse connecting screen with different message
|
||||
break;
|
||||
case WifiSelectionState::NETWORK_LIST:
|
||||
renderNetworkList();
|
||||
break;
|
||||
case WifiSelectionState::CONNECTING:
|
||||
renderConnecting();
|
||||
break;
|
||||
case WifiSelectionState::CONNECTED:
|
||||
renderConnected();
|
||||
break;
|
||||
case WifiSelectionState::SAVE_PROMPT:
|
||||
renderSavePrompt();
|
||||
break;
|
||||
case WifiSelectionState::CONNECTION_FAILED:
|
||||
renderConnectionFailed();
|
||||
break;
|
||||
case WifiSelectionState::FORGET_PROMPT:
|
||||
renderForgetPrompt();
|
||||
break;
|
||||
}
|
||||
|
||||
renderer.displayBuffer();
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::renderNetworkList() const {
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
|
||||
// Draw header
|
||||
renderer.drawCenteredText(READER_FONT_ID, 10, "WiFi Networks", true, BOLD);
|
||||
|
||||
if (networks.empty()) {
|
||||
// No networks found or scan failed
|
||||
const auto height = renderer.getLineHeight(UI_FONT_ID);
|
||||
const auto top = (pageHeight - height) / 2;
|
||||
renderer.drawCenteredText(UI_FONT_ID, top, "No networks found", true, REGULAR);
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, top + height + 10, "Press OK to scan again", true, REGULAR);
|
||||
} else {
|
||||
// Calculate how many networks we can display
|
||||
constexpr int startY = 60;
|
||||
constexpr int lineHeight = 25;
|
||||
const int maxVisibleNetworks = (pageHeight - startY - 40) / lineHeight;
|
||||
|
||||
// Calculate scroll offset to keep selected item visible
|
||||
int scrollOffset = 0;
|
||||
if (selectedNetworkIndex >= maxVisibleNetworks) {
|
||||
scrollOffset = selectedNetworkIndex - maxVisibleNetworks + 1;
|
||||
}
|
||||
|
||||
// Draw networks
|
||||
int displayIndex = 0;
|
||||
for (size_t i = scrollOffset; i < networks.size() && displayIndex < maxVisibleNetworks; i++, displayIndex++) {
|
||||
const int networkY = startY + displayIndex * lineHeight;
|
||||
const auto& network = networks[i];
|
||||
|
||||
// Draw selection indicator
|
||||
if (static_cast<int>(i) == selectedNetworkIndex) {
|
||||
renderer.drawText(UI_FONT_ID, 5, networkY, ">");
|
||||
}
|
||||
|
||||
// Draw network name (truncate if too long)
|
||||
std::string displayName = network.ssid;
|
||||
if (displayName.length() > 16) {
|
||||
displayName.replace(13, displayName.length() - 13, "...");
|
||||
}
|
||||
renderer.drawText(UI_FONT_ID, 20, networkY, displayName.c_str());
|
||||
|
||||
// Draw signal strength indicator
|
||||
std::string signalStr = getSignalStrengthIndicator(network.rssi);
|
||||
renderer.drawText(UI_FONT_ID, pageWidth - 90, networkY, signalStr.c_str());
|
||||
|
||||
// Draw saved indicator (checkmark) for networks with saved passwords
|
||||
if (network.hasSavedPassword) {
|
||||
renderer.drawText(UI_FONT_ID, pageWidth - 50, networkY, "+");
|
||||
}
|
||||
|
||||
// Draw lock icon for encrypted networks
|
||||
if (network.isEncrypted) {
|
||||
renderer.drawText(UI_FONT_ID, pageWidth - 30, networkY, "*");
|
||||
}
|
||||
}
|
||||
|
||||
// Draw scroll indicators if needed
|
||||
if (scrollOffset > 0) {
|
||||
renderer.drawText(SMALL_FONT_ID, pageWidth - 15, startY - 10, "^");
|
||||
}
|
||||
if (scrollOffset + maxVisibleNetworks < static_cast<int>(networks.size())) {
|
||||
renderer.drawText(SMALL_FONT_ID, pageWidth - 15, startY + maxVisibleNetworks * lineHeight, "v");
|
||||
}
|
||||
|
||||
// Show network count
|
||||
char countStr[32];
|
||||
snprintf(countStr, sizeof(countStr), "%zu networks found", networks.size());
|
||||
renderer.drawText(SMALL_FONT_ID, 20, pageHeight - 90, countStr);
|
||||
}
|
||||
|
||||
// Draw help text
|
||||
renderer.drawText(SMALL_FONT_ID, 20, pageHeight - 75, "* = Encrypted | + = Saved");
|
||||
renderer.drawButtonHints(UI_FONT_ID, "« Back", "Connect", "", "");
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::renderConnecting() const {
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
const auto height = renderer.getLineHeight(UI_FONT_ID);
|
||||
const auto top = (pageHeight - height) / 2;
|
||||
|
||||
if (state == WifiSelectionState::SCANNING) {
|
||||
renderer.drawCenteredText(UI_FONT_ID, top, "Scanning...", true, REGULAR);
|
||||
} else {
|
||||
renderer.drawCenteredText(READER_FONT_ID, top - 40, "Connecting...", true, BOLD);
|
||||
|
||||
std::string ssidInfo = "to " + selectedSSID;
|
||||
if (ssidInfo.length() > 25) {
|
||||
ssidInfo.replace(22, ssidInfo.length() - 22, "...");
|
||||
}
|
||||
renderer.drawCenteredText(UI_FONT_ID, top, ssidInfo.c_str(), true, REGULAR);
|
||||
}
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::renderConnected() const {
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
const auto height = renderer.getLineHeight(UI_FONT_ID);
|
||||
const auto top = (pageHeight - height * 4) / 2;
|
||||
|
||||
renderer.drawCenteredText(READER_FONT_ID, top - 30, "Connected!", true, BOLD);
|
||||
|
||||
std::string ssidInfo = "Network: " + selectedSSID;
|
||||
if (ssidInfo.length() > 28) {
|
||||
ssidInfo.replace(25, ssidInfo.length() - 25, "...");
|
||||
}
|
||||
renderer.drawCenteredText(UI_FONT_ID, top + 10, ssidInfo.c_str(), true, REGULAR);
|
||||
|
||||
const std::string ipInfo = "IP Address: " + connectedIP;
|
||||
renderer.drawCenteredText(UI_FONT_ID, top + 40, ipInfo.c_str(), true, REGULAR);
|
||||
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight - 30, "Press any button to continue", true, REGULAR);
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::renderSavePrompt() const {
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
const auto height = renderer.getLineHeight(UI_FONT_ID);
|
||||
const auto top = (pageHeight - height * 3) / 2;
|
||||
|
||||
renderer.drawCenteredText(READER_FONT_ID, top - 40, "Connected!", true, BOLD);
|
||||
|
||||
std::string ssidInfo = "Network: " + selectedSSID;
|
||||
if (ssidInfo.length() > 28) {
|
||||
ssidInfo.replace(25, ssidInfo.length() - 25, "...");
|
||||
}
|
||||
renderer.drawCenteredText(UI_FONT_ID, top, ssidInfo.c_str(), true, REGULAR);
|
||||
|
||||
renderer.drawCenteredText(UI_FONT_ID, top + 40, "Save password for next time?", true, REGULAR);
|
||||
|
||||
// Draw Yes/No buttons
|
||||
const int buttonY = top + 80;
|
||||
constexpr int buttonWidth = 60;
|
||||
constexpr int buttonSpacing = 30;
|
||||
constexpr int totalWidth = buttonWidth * 2 + buttonSpacing;
|
||||
const int startX = (pageWidth - totalWidth) / 2;
|
||||
|
||||
// Draw "Yes" button
|
||||
if (savePromptSelection == 0) {
|
||||
renderer.drawText(UI_FONT_ID, startX, buttonY, "[Yes]");
|
||||
} else {
|
||||
renderer.drawText(UI_FONT_ID, startX + 4, buttonY, "Yes");
|
||||
}
|
||||
|
||||
// Draw "No" button
|
||||
if (savePromptSelection == 1) {
|
||||
renderer.drawText(UI_FONT_ID, startX + buttonWidth + buttonSpacing, buttonY, "[No]");
|
||||
} else {
|
||||
renderer.drawText(UI_FONT_ID, startX + buttonWidth + buttonSpacing + 4, buttonY, "No");
|
||||
}
|
||||
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight - 30, "LEFT/RIGHT: Select | OK: Confirm", true, REGULAR);
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::renderConnectionFailed() const {
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
const auto height = renderer.getLineHeight(UI_FONT_ID);
|
||||
const auto top = (pageHeight - height * 2) / 2;
|
||||
|
||||
renderer.drawCenteredText(READER_FONT_ID, top - 20, "Connection Failed", true, BOLD);
|
||||
renderer.drawCenteredText(UI_FONT_ID, top + 20, connectionError.c_str(), true, REGULAR);
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight - 30, "Press any button to continue", true, REGULAR);
|
||||
}
|
||||
|
||||
void WifiSelectionActivity::renderForgetPrompt() const {
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
const auto height = renderer.getLineHeight(UI_FONT_ID);
|
||||
const auto top = (pageHeight - height * 3) / 2;
|
||||
|
||||
renderer.drawCenteredText(READER_FONT_ID, top - 40, "Forget Network?", true, BOLD);
|
||||
|
||||
std::string ssidInfo = "Network: " + selectedSSID;
|
||||
if (ssidInfo.length() > 28) {
|
||||
ssidInfo.replace(25, ssidInfo.length() - 25, "...");
|
||||
}
|
||||
renderer.drawCenteredText(UI_FONT_ID, top, ssidInfo.c_str(), true, REGULAR);
|
||||
|
||||
renderer.drawCenteredText(UI_FONT_ID, top + 40, "Remove saved password?", true, REGULAR);
|
||||
|
||||
// Draw Yes/No buttons
|
||||
const int buttonY = top + 80;
|
||||
constexpr int buttonWidth = 60;
|
||||
constexpr int buttonSpacing = 30;
|
||||
constexpr int totalWidth = buttonWidth * 2 + buttonSpacing;
|
||||
const int startX = (pageWidth - totalWidth) / 2;
|
||||
|
||||
// Draw "Yes" button
|
||||
if (forgetPromptSelection == 0) {
|
||||
renderer.drawText(UI_FONT_ID, startX, buttonY, "[Yes]");
|
||||
} else {
|
||||
renderer.drawText(UI_FONT_ID, startX + 4, buttonY, "Yes");
|
||||
}
|
||||
|
||||
// Draw "No" button
|
||||
if (forgetPromptSelection == 1) {
|
||||
renderer.drawText(UI_FONT_ID, startX + buttonWidth + buttonSpacing, buttonY, "[No]");
|
||||
} else {
|
||||
renderer.drawText(UI_FONT_ID, startX + buttonWidth + buttonSpacing + 4, buttonY, "No");
|
||||
}
|
||||
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, pageHeight - 30, "LEFT/RIGHT: Select | OK: Confirm", true, REGULAR);
|
||||
}
|
||||
104
src/activities/network/WifiSelectionActivity.h
Normal file
@@ -0,0 +1,104 @@
|
||||
#pragma once
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "activities/ActivityWithSubactivity.h"
|
||||
|
||||
// Structure to hold WiFi network information
|
||||
struct WifiNetworkInfo {
|
||||
std::string ssid;
|
||||
int32_t rssi;
|
||||
bool isEncrypted;
|
||||
bool hasSavedPassword; // Whether we have saved credentials for this network
|
||||
};
|
||||
|
||||
// WiFi selection states
|
||||
enum class WifiSelectionState {
|
||||
SCANNING, // Scanning for networks
|
||||
NETWORK_LIST, // Displaying available networks
|
||||
PASSWORD_ENTRY, // Entering password for selected network
|
||||
CONNECTING, // Attempting to connect
|
||||
CONNECTED, // Successfully connected
|
||||
SAVE_PROMPT, // Asking user if they want to save the password
|
||||
CONNECTION_FAILED, // Connection failed
|
||||
FORGET_PROMPT // Asking user if they want to forget the network
|
||||
};
|
||||
|
||||
/**
|
||||
* WifiSelectionActivity is responsible for scanning WiFi APs and connecting to them.
|
||||
* It will:
|
||||
* - Enter scanning mode on entry
|
||||
* - List available WiFi networks
|
||||
* - Allow selection and launch KeyboardEntryActivity for password if needed
|
||||
* - Save the password if requested
|
||||
* - Call onComplete callback when connected or cancelled
|
||||
*
|
||||
* The onComplete callback receives true if connected successfully, false if cancelled.
|
||||
*/
|
||||
class WifiSelectionActivity final : public ActivityWithSubactivity {
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
bool updateRequired = false;
|
||||
WifiSelectionState state = WifiSelectionState::SCANNING;
|
||||
int selectedNetworkIndex = 0;
|
||||
std::vector<WifiNetworkInfo> networks;
|
||||
const std::function<void(bool connected)> onComplete;
|
||||
|
||||
// Selected network for connection
|
||||
std::string selectedSSID;
|
||||
bool selectedRequiresPassword = false;
|
||||
|
||||
// Connection result
|
||||
std::string connectedIP;
|
||||
std::string connectionError;
|
||||
|
||||
// Password to potentially save (from keyboard or saved credentials)
|
||||
std::string enteredPassword;
|
||||
|
||||
// Whether network was connected using a saved password (skip save prompt)
|
||||
bool usedSavedPassword = false;
|
||||
|
||||
// Save/forget prompt selection (0 = Yes, 1 = No)
|
||||
int savePromptSelection = 0;
|
||||
int forgetPromptSelection = 0;
|
||||
|
||||
// Connection timeout
|
||||
static constexpr unsigned long CONNECTION_TIMEOUT_MS = 15000;
|
||||
unsigned long connectionStartTime = 0;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
void renderNetworkList() const;
|
||||
void renderPasswordEntry() const;
|
||||
void renderConnecting() const;
|
||||
void renderConnected() const;
|
||||
void renderSavePrompt() const;
|
||||
void renderConnectionFailed() const;
|
||||
void renderForgetPrompt() const;
|
||||
|
||||
void startWifiScan();
|
||||
void processWifiScanResults();
|
||||
void selectNetwork(int index);
|
||||
void attemptConnection();
|
||||
void checkConnectionStatus();
|
||||
std::string getSignalStrengthIndicator(int32_t rssi) const;
|
||||
|
||||
public:
|
||||
explicit WifiSelectionActivity(GfxRenderer& renderer, InputManager& inputManager,
|
||||
const std::function<void(bool connected)>& onComplete)
|
||||
: ActivityWithSubactivity("WifiSelection", renderer, inputManager), onComplete(onComplete) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
|
||||
// Get the IP address after successful connection
|
||||
const std::string& getConnectedIP() const { return connectedIP; }
|
||||
};
|
||||
@@ -1,22 +1,23 @@
|
||||
#include "EpubReaderActivity.h"
|
||||
|
||||
#include <Epub/Page.h>
|
||||
#include <FsHelpers.h>
|
||||
#include <GfxRenderer.h>
|
||||
#include <SD.h>
|
||||
#include <InputManager.h>
|
||||
|
||||
#include "Battery.h"
|
||||
#include "CrossPointSettings.h"
|
||||
#include "CrossPointState.h"
|
||||
#include "EpubReaderChapterSelectionActivity.h"
|
||||
#include "config.h"
|
||||
|
||||
namespace {
|
||||
constexpr int pagesPerRefresh = 15;
|
||||
constexpr unsigned long skipChapterMs = 700;
|
||||
constexpr unsigned long goHomeMs = 1000;
|
||||
constexpr float lineCompression = 0.95f;
|
||||
constexpr int marginTop = 8;
|
||||
constexpr int marginRight = 10;
|
||||
constexpr int marginBottom = 22;
|
||||
constexpr int marginLeft = 10;
|
||||
constexpr int horizontalPadding = 5;
|
||||
constexpr int statusBarMargin = 19;
|
||||
} // namespace
|
||||
|
||||
void EpubReaderActivity::taskTrampoline(void* param) {
|
||||
@@ -25,24 +26,49 @@ void EpubReaderActivity::taskTrampoline(void* param) {
|
||||
}
|
||||
|
||||
void EpubReaderActivity::onEnter() {
|
||||
ActivityWithSubactivity::onEnter();
|
||||
|
||||
if (!epub) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Configure screen orientation based on settings
|
||||
switch (SETTINGS.orientation) {
|
||||
case CrossPointSettings::ORIENTATION::PORTRAIT:
|
||||
renderer.setOrientation(GfxRenderer::Orientation::Portrait);
|
||||
break;
|
||||
case CrossPointSettings::ORIENTATION::LANDSCAPE_CW:
|
||||
renderer.setOrientation(GfxRenderer::Orientation::LandscapeClockwise);
|
||||
break;
|
||||
case CrossPointSettings::ORIENTATION::INVERTED:
|
||||
renderer.setOrientation(GfxRenderer::Orientation::PortraitInverted);
|
||||
break;
|
||||
case CrossPointSettings::ORIENTATION::LANDSCAPE_CCW:
|
||||
renderer.setOrientation(GfxRenderer::Orientation::LandscapeCounterClockwise);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
epub->setupCacheDir();
|
||||
|
||||
if (SD.exists((epub->getCachePath() + "/progress.bin").c_str())) {
|
||||
File f = SD.open((epub->getCachePath() + "/progress.bin").c_str());
|
||||
File f;
|
||||
if (FsHelpers::openFileForRead("ERS", epub->getCachePath() + "/progress.bin", f)) {
|
||||
uint8_t data[4];
|
||||
f.read(data, 4);
|
||||
currentSpineIndex = data[0] + (data[1] << 8);
|
||||
nextPageNumber = data[2] + (data[3] << 8);
|
||||
Serial.printf("[%lu] [ERS] Loaded cache: %d, %d\n", millis(), currentSpineIndex, nextPageNumber);
|
||||
if (f.read(data, 4) == 4) {
|
||||
currentSpineIndex = data[0] + (data[1] << 8);
|
||||
nextPageNumber = data[2] + (data[3] << 8);
|
||||
Serial.printf("[%lu] [ERS] Loaded cache: %d, %d\n", millis(), currentSpineIndex, nextPageNumber);
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
|
||||
// Save current epub as last opened epub
|
||||
APP_STATE.openEpubPath = epub->getPath();
|
||||
APP_STATE.saveToFile();
|
||||
|
||||
// Trigger first update
|
||||
updateRequired = true;
|
||||
|
||||
@@ -55,6 +81,11 @@ void EpubReaderActivity::onEnter() {
|
||||
}
|
||||
|
||||
void EpubReaderActivity::onExit() {
|
||||
ActivityWithSubactivity::onExit();
|
||||
|
||||
// Reset orientation back to portrait for the rest of the UI
|
||||
renderer.setOrientation(GfxRenderer::Orientation::Portrait);
|
||||
|
||||
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
@@ -69,20 +100,20 @@ void EpubReaderActivity::onExit() {
|
||||
|
||||
void EpubReaderActivity::loop() {
|
||||
// Pass input responsibility to sub activity if exists
|
||||
if (subAcitivity) {
|
||||
subAcitivity->loop();
|
||||
if (subActivity) {
|
||||
subActivity->loop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Enter chapter selection activity
|
||||
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
if (inputManager.wasReleased(InputManager::BTN_CONFIRM)) {
|
||||
// Don't start activity transition while rendering
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
subAcitivity.reset(new EpubReaderChapterSelectionActivity(
|
||||
exitActivity();
|
||||
enterNewActivity(new EpubReaderChapterSelectionActivity(
|
||||
this->renderer, this->inputManager, epub, currentSpineIndex,
|
||||
[this] {
|
||||
subAcitivity->onExit();
|
||||
subAcitivity.reset();
|
||||
exitActivity();
|
||||
updateRequired = true;
|
||||
},
|
||||
[this](const int newSpineIndex) {
|
||||
@@ -91,15 +122,20 @@ void EpubReaderActivity::loop() {
|
||||
nextPageNumber = 0;
|
||||
section.reset();
|
||||
}
|
||||
subAcitivity->onExit();
|
||||
subAcitivity.reset();
|
||||
exitActivity();
|
||||
updateRequired = true;
|
||||
}));
|
||||
subAcitivity->onEnter();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
|
||||
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
// Long press BACK (1s+) goes directly to home
|
||||
if (inputManager.isPressed(InputManager::BTN_BACK) && inputManager.getHeldTime() >= goHomeMs) {
|
||||
onGoHome();
|
||||
return;
|
||||
}
|
||||
|
||||
// Short press BACK goes to file selection
|
||||
if (inputManager.wasReleased(InputManager::BTN_BACK) && inputManager.getHeldTime() < goHomeMs) {
|
||||
onGoBack();
|
||||
return;
|
||||
}
|
||||
@@ -202,32 +238,70 @@ void EpubReaderActivity::renderScreen() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Apply screen viewable areas and additional padding
|
||||
int orientedMarginTop, orientedMarginRight, orientedMarginBottom, orientedMarginLeft;
|
||||
renderer.getOrientedViewableTRBL(&orientedMarginTop, &orientedMarginRight, &orientedMarginBottom,
|
||||
&orientedMarginLeft);
|
||||
orientedMarginLeft += horizontalPadding;
|
||||
orientedMarginRight += horizontalPadding;
|
||||
orientedMarginBottom += statusBarMargin;
|
||||
|
||||
if (!section) {
|
||||
const auto filepath = epub->getSpineItem(currentSpineIndex);
|
||||
const auto filepath = epub->getSpineItem(currentSpineIndex).href;
|
||||
Serial.printf("[%lu] [ERS] Loading file: %s, index: %d\n", millis(), filepath.c_str(), currentSpineIndex);
|
||||
section = std::unique_ptr<Section>(new Section(epub, currentSpineIndex, renderer));
|
||||
if (!section->loadCacheMetadata(READER_FONT_ID, lineCompression, marginTop, marginRight, marginBottom, marginLeft,
|
||||
SETTINGS.extraParagraphSpacing)) {
|
||||
|
||||
const auto viewportWidth = renderer.getScreenWidth() - orientedMarginLeft - orientedMarginRight;
|
||||
const auto viewportHeight = renderer.getScreenHeight() - orientedMarginTop - orientedMarginBottom;
|
||||
|
||||
if (!section->loadCacheMetadata(READER_FONT_ID, lineCompression, SETTINGS.extraParagraphSpacing, viewportWidth,
|
||||
viewportHeight)) {
|
||||
Serial.printf("[%lu] [ERS] Cache not found, building...\n", millis());
|
||||
|
||||
// Progress bar dimensions
|
||||
constexpr int barWidth = 200;
|
||||
constexpr int barHeight = 10;
|
||||
constexpr int boxMargin = 20;
|
||||
const int textWidth = renderer.getTextWidth(READER_FONT_ID, "Indexing...");
|
||||
const int boxWidthWithBar = (barWidth > textWidth ? barWidth : textWidth) + boxMargin * 2;
|
||||
const int boxWidthNoBar = textWidth + boxMargin * 2;
|
||||
const int boxHeightWithBar = renderer.getLineHeight(READER_FONT_ID) + barHeight + boxMargin * 3;
|
||||
const int boxHeightNoBar = renderer.getLineHeight(READER_FONT_ID) + boxMargin * 2;
|
||||
const int boxXWithBar = (renderer.getScreenWidth() - boxWidthWithBar) / 2;
|
||||
const int boxXNoBar = (renderer.getScreenWidth() - boxWidthNoBar) / 2;
|
||||
constexpr int boxY = 50;
|
||||
const int barX = boxXWithBar + (boxWidthWithBar - barWidth) / 2;
|
||||
const int barY = boxY + renderer.getLineHeight(READER_FONT_ID) + boxMargin * 2;
|
||||
|
||||
// Always show "Indexing..." text first
|
||||
{
|
||||
const int textWidth = renderer.getTextWidth(READER_FONT_ID, "Indexing...");
|
||||
constexpr int margin = 20;
|
||||
// Round all coordinates to 8 pixel boundaries
|
||||
const int x = ((GfxRenderer::getScreenWidth() - textWidth - margin * 2) / 2 + 7) / 8 * 8;
|
||||
constexpr int y = 56;
|
||||
const int w = (textWidth + margin * 2 + 7) / 8 * 8;
|
||||
const int h = (renderer.getLineHeight(READER_FONT_ID) + margin * 2 + 7) / 8 * 8;
|
||||
renderer.clearScreen();
|
||||
renderer.drawText(READER_FONT_ID, x + margin, y + margin, "Indexing...");
|
||||
renderer.drawRect(x + 5, y + 5, w - 10, h - 10);
|
||||
renderer.fillRect(boxXNoBar, boxY, boxWidthNoBar, boxHeightNoBar, false);
|
||||
renderer.drawText(READER_FONT_ID, boxXNoBar + boxMargin, boxY + boxMargin, "Indexing...");
|
||||
renderer.drawRect(boxXNoBar + 5, boxY + 5, boxWidthNoBar - 10, boxHeightNoBar - 10);
|
||||
renderer.displayBuffer();
|
||||
pagesUntilFullRefresh = 0;
|
||||
}
|
||||
|
||||
section->setupCacheDir();
|
||||
if (!section->persistPageDataToSD(READER_FONT_ID, lineCompression, marginTop, marginRight, marginBottom,
|
||||
marginLeft, SETTINGS.extraParagraphSpacing)) {
|
||||
|
||||
// Setup callback - only called for chapters >= 50KB, redraws with progress bar
|
||||
auto progressSetup = [this, boxXWithBar, boxWidthWithBar, boxHeightWithBar, barX, barY] {
|
||||
renderer.fillRect(boxXWithBar, boxY, boxWidthWithBar, boxHeightWithBar, false);
|
||||
renderer.drawText(READER_FONT_ID, boxXWithBar + boxMargin, boxY + boxMargin, "Indexing...");
|
||||
renderer.drawRect(boxXWithBar + 5, boxY + 5, boxWidthWithBar - 10, boxHeightWithBar - 10);
|
||||
renderer.drawRect(barX, barY, barWidth, barHeight);
|
||||
renderer.displayBuffer();
|
||||
};
|
||||
|
||||
// Progress callback to update progress bar
|
||||
auto progressCallback = [this, barX, barY, barWidth, barHeight](int progress) {
|
||||
const int fillWidth = (barWidth - 2) * progress / 100;
|
||||
renderer.fillRect(barX + 1, barY + 1, fillWidth, barHeight - 2, true);
|
||||
renderer.displayBuffer(EInkDisplay::FAST_REFRESH);
|
||||
};
|
||||
|
||||
if (!section->persistPageDataToSD(READER_FONT_ID, lineCompression, SETTINGS.extraParagraphSpacing, viewportWidth,
|
||||
viewportHeight, progressSetup, progressCallback)) {
|
||||
Serial.printf("[%lu] [ERS] Failed to persist page data to SD\n", millis());
|
||||
section.reset();
|
||||
return;
|
||||
@@ -248,7 +322,7 @@ void EpubReaderActivity::renderScreen() {
|
||||
if (section->pageCount == 0) {
|
||||
Serial.printf("[%lu] [ERS] No pages to render\n", millis());
|
||||
renderer.drawCenteredText(READER_FONT_ID, 300, "Empty chapter", true, BOLD);
|
||||
renderStatusBar();
|
||||
renderStatusBar(orientedMarginRight, orientedMarginBottom, orientedMarginLeft);
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
@@ -256,7 +330,7 @@ void EpubReaderActivity::renderScreen() {
|
||||
if (section->currentPage < 0 || section->currentPage >= section->pageCount) {
|
||||
Serial.printf("[%lu] [ERS] Page out of bounds: %d (max %d)\n", millis(), section->currentPage, section->pageCount);
|
||||
renderer.drawCenteredText(READER_FONT_ID, 300, "Out of bounds", true, BOLD);
|
||||
renderStatusBar();
|
||||
renderStatusBar(orientedMarginRight, orientedMarginBottom, orientedMarginLeft);
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
@@ -270,23 +344,27 @@ void EpubReaderActivity::renderScreen() {
|
||||
return renderScreen();
|
||||
}
|
||||
const auto start = millis();
|
||||
renderContents(std::move(p));
|
||||
renderContents(std::move(p), orientedMarginTop, orientedMarginRight, orientedMarginBottom, orientedMarginLeft);
|
||||
Serial.printf("[%lu] [ERS] Rendered page in %dms\n", millis(), millis() - start);
|
||||
}
|
||||
|
||||
File f = SD.open((epub->getCachePath() + "/progress.bin").c_str(), FILE_WRITE);
|
||||
uint8_t data[4];
|
||||
data[0] = currentSpineIndex & 0xFF;
|
||||
data[1] = (currentSpineIndex >> 8) & 0xFF;
|
||||
data[2] = section->currentPage & 0xFF;
|
||||
data[3] = (section->currentPage >> 8) & 0xFF;
|
||||
f.write(data, 4);
|
||||
f.close();
|
||||
File f;
|
||||
if (FsHelpers::openFileForWrite("ERS", epub->getCachePath() + "/progress.bin", f)) {
|
||||
uint8_t data[4];
|
||||
data[0] = currentSpineIndex & 0xFF;
|
||||
data[1] = (currentSpineIndex >> 8) & 0xFF;
|
||||
data[2] = section->currentPage & 0xFF;
|
||||
data[3] = (section->currentPage >> 8) & 0xFF;
|
||||
f.write(data, 4);
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
|
||||
void EpubReaderActivity::renderContents(std::unique_ptr<Page> page) {
|
||||
page->render(renderer, READER_FONT_ID);
|
||||
renderStatusBar();
|
||||
void EpubReaderActivity::renderContents(std::unique_ptr<Page> page, const int orientedMarginTop,
|
||||
const int orientedMarginRight, const int orientedMarginBottom,
|
||||
const int orientedMarginLeft) {
|
||||
page->render(renderer, READER_FONT_ID, orientedMarginLeft, orientedMarginTop);
|
||||
renderStatusBar(orientedMarginRight, orientedMarginBottom, orientedMarginLeft);
|
||||
if (pagesUntilFullRefresh <= 1) {
|
||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
||||
pagesUntilFullRefresh = pagesPerRefresh;
|
||||
@@ -303,13 +381,13 @@ void EpubReaderActivity::renderContents(std::unique_ptr<Page> page) {
|
||||
{
|
||||
renderer.clearScreen(0x00);
|
||||
renderer.setRenderMode(GfxRenderer::GRAYSCALE_LSB);
|
||||
page->render(renderer, READER_FONT_ID);
|
||||
page->render(renderer, READER_FONT_ID, orientedMarginLeft, orientedMarginTop);
|
||||
renderer.copyGrayscaleLsbBuffers();
|
||||
|
||||
// Render and copy to MSB buffer
|
||||
renderer.clearScreen(0x00);
|
||||
renderer.setRenderMode(GfxRenderer::GRAYSCALE_MSB);
|
||||
page->render(renderer, READER_FONT_ID);
|
||||
page->render(renderer, READER_FONT_ID, orientedMarginLeft, orientedMarginTop);
|
||||
renderer.copyGrayscaleMsbBuffers();
|
||||
|
||||
// display grayscale part
|
||||
@@ -321,72 +399,90 @@ void EpubReaderActivity::renderContents(std::unique_ptr<Page> page) {
|
||||
renderer.restoreBwBuffer();
|
||||
}
|
||||
|
||||
void EpubReaderActivity::renderStatusBar() const {
|
||||
constexpr auto textY = 776;
|
||||
void EpubReaderActivity::renderStatusBar(const int orientedMarginRight, const int orientedMarginBottom,
|
||||
const int orientedMarginLeft) const {
|
||||
// determine visible status bar elements
|
||||
const bool showProgress = SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::FULL;
|
||||
const bool showBattery = SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::NO_PROGRESS ||
|
||||
SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::FULL;
|
||||
const bool showChapterTitle = SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::NO_PROGRESS ||
|
||||
SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::FULL;
|
||||
|
||||
// Calculate progress in book
|
||||
float sectionChapterProg = static_cast<float>(section->currentPage) / section->pageCount;
|
||||
uint8_t bookProgress = epub->calculateProgress(currentSpineIndex, sectionChapterProg);
|
||||
// Position status bar near the bottom of the logical screen, regardless of orientation
|
||||
const auto screenHeight = renderer.getScreenHeight();
|
||||
const auto textY = screenHeight - orientedMarginBottom + 2;
|
||||
int percentageTextWidth = 0;
|
||||
int progressTextWidth = 0;
|
||||
|
||||
// Right aligned text for progress counter
|
||||
const std::string progress = std::to_string(section->currentPage + 1) + "/" + std::to_string(section->pageCount) +
|
||||
" " + std::to_string(bookProgress) + "%";
|
||||
const auto progressTextWidth = renderer.getTextWidth(SMALL_FONT_ID, progress.c_str());
|
||||
renderer.drawText(SMALL_FONT_ID, GfxRenderer::getScreenWidth() - marginRight - progressTextWidth, textY,
|
||||
progress.c_str());
|
||||
if (showProgress) {
|
||||
// Calculate progress in book
|
||||
const float sectionChapterProg = static_cast<float>(section->currentPage) / section->pageCount;
|
||||
const uint8_t bookProgress = epub->calculateProgress(currentSpineIndex, sectionChapterProg);
|
||||
|
||||
// Left aligned battery icon and percentage
|
||||
const uint16_t percentage = battery.readPercentage();
|
||||
const auto percentageText = std::to_string(percentage) + "%";
|
||||
const auto percentageTextWidth = renderer.getTextWidth(SMALL_FONT_ID, percentageText.c_str());
|
||||
renderer.drawText(SMALL_FONT_ID, 20 + marginLeft, textY, percentageText.c_str());
|
||||
|
||||
// 1 column on left, 2 columns on right, 5 columns of battery body
|
||||
constexpr int batteryWidth = 15;
|
||||
constexpr int batteryHeight = 10;
|
||||
constexpr int x = marginLeft;
|
||||
constexpr int y = 783;
|
||||
|
||||
// Top line
|
||||
renderer.drawLine(x, y, x + batteryWidth - 4, y);
|
||||
// Bottom line
|
||||
renderer.drawLine(x, y + batteryHeight - 1, x + batteryWidth - 4, y + batteryHeight - 1);
|
||||
// Left line
|
||||
renderer.drawLine(x, y, x, y + batteryHeight - 1);
|
||||
// Battery end
|
||||
renderer.drawLine(x + batteryWidth - 4, y, x + batteryWidth - 4, y + batteryHeight - 1);
|
||||
renderer.drawLine(x + batteryWidth - 3, y + 2, x + batteryWidth - 1, y + 2);
|
||||
renderer.drawLine(x + batteryWidth - 3, y + batteryHeight - 3, x + batteryWidth - 1, y + batteryHeight - 3);
|
||||
renderer.drawLine(x + batteryWidth - 1, y + 2, x + batteryWidth - 1, y + batteryHeight - 3);
|
||||
|
||||
// The +1 is to round up, so that we always fill at least one pixel
|
||||
int filledWidth = percentage * (batteryWidth - 5) / 100 + 1;
|
||||
if (filledWidth > batteryWidth - 5) {
|
||||
filledWidth = batteryWidth - 5; // Ensure we don't overflow
|
||||
// Right aligned text for progress counter
|
||||
const std::string progress = std::to_string(section->currentPage + 1) + "/" + std::to_string(section->pageCount) +
|
||||
" " + std::to_string(bookProgress) + "%";
|
||||
progressTextWidth = renderer.getTextWidth(SMALL_FONT_ID, progress.c_str());
|
||||
renderer.drawText(SMALL_FONT_ID, renderer.getScreenWidth() - orientedMarginRight - progressTextWidth, textY,
|
||||
progress.c_str());
|
||||
}
|
||||
renderer.fillRect(x + 1, y + 1, filledWidth, batteryHeight - 2);
|
||||
|
||||
// Centered chatper title text
|
||||
// Page width minus existing content with 30px padding on each side
|
||||
const int titleMarginLeft = 20 + percentageTextWidth + 30 + marginLeft;
|
||||
const int titleMarginRight = progressTextWidth + 30 + marginRight;
|
||||
const int availableTextWidth = GfxRenderer::getScreenWidth() - titleMarginLeft - titleMarginRight;
|
||||
const int tocIndex = epub->getTocIndexForSpineIndex(currentSpineIndex);
|
||||
if (showBattery) {
|
||||
// Left aligned battery icon and percentage
|
||||
const uint16_t percentage = battery.readPercentage();
|
||||
const auto percentageText = std::to_string(percentage) + "%";
|
||||
percentageTextWidth = renderer.getTextWidth(SMALL_FONT_ID, percentageText.c_str());
|
||||
renderer.drawText(SMALL_FONT_ID, 20 + orientedMarginLeft, textY, percentageText.c_str());
|
||||
|
||||
std::string title;
|
||||
int titleWidth;
|
||||
if (tocIndex == -1) {
|
||||
title = "Unnamed";
|
||||
titleWidth = renderer.getTextWidth(SMALL_FONT_ID, "Unnamed");
|
||||
} else {
|
||||
const auto tocItem = epub->getTocItem(tocIndex);
|
||||
title = tocItem.title;
|
||||
titleWidth = renderer.getTextWidth(SMALL_FONT_ID, title.c_str());
|
||||
while (titleWidth > availableTextWidth) {
|
||||
title = title.substr(0, title.length() - 8) + "...";
|
||||
titleWidth = renderer.getTextWidth(SMALL_FONT_ID, title.c_str());
|
||||
// 1 column on left, 2 columns on right, 5 columns of battery body
|
||||
constexpr int batteryWidth = 15;
|
||||
constexpr int batteryHeight = 10;
|
||||
const int x = orientedMarginLeft;
|
||||
const int y = screenHeight - orientedMarginBottom + 5;
|
||||
|
||||
// Top line
|
||||
renderer.drawLine(x, y, x + batteryWidth - 4, y);
|
||||
// Bottom line
|
||||
renderer.drawLine(x, y + batteryHeight - 1, x + batteryWidth - 4, y + batteryHeight - 1);
|
||||
// Left line
|
||||
renderer.drawLine(x, y, x, y + batteryHeight - 1);
|
||||
// Battery end
|
||||
renderer.drawLine(x + batteryWidth - 4, y, x + batteryWidth - 4, y + batteryHeight - 1);
|
||||
renderer.drawLine(x + batteryWidth - 3, y + 2, x + batteryWidth - 1, y + 2);
|
||||
renderer.drawLine(x + batteryWidth - 3, y + batteryHeight - 3, x + batteryWidth - 1, y + batteryHeight - 3);
|
||||
renderer.drawLine(x + batteryWidth - 1, y + 2, x + batteryWidth - 1, y + batteryHeight - 3);
|
||||
|
||||
// The +1 is to round up, so that we always fill at least one pixel
|
||||
int filledWidth = percentage * (batteryWidth - 5) / 100 + 1;
|
||||
if (filledWidth > batteryWidth - 5) {
|
||||
filledWidth = batteryWidth - 5; // Ensure we don't overflow
|
||||
}
|
||||
renderer.fillRect(x + 1, y + 1, filledWidth, batteryHeight - 2);
|
||||
}
|
||||
|
||||
renderer.drawText(SMALL_FONT_ID, titleMarginLeft + (availableTextWidth - titleWidth) / 2, textY, title.c_str());
|
||||
if (showChapterTitle) {
|
||||
// Centered chatper title text
|
||||
// Page width minus existing content with 30px padding on each side
|
||||
const int titleMarginLeft = 20 + percentageTextWidth + 30 + orientedMarginLeft;
|
||||
const int titleMarginRight = progressTextWidth + 30 + orientedMarginRight;
|
||||
const int availableTextWidth = renderer.getScreenWidth() - titleMarginLeft - titleMarginRight;
|
||||
const int tocIndex = epub->getTocIndexForSpineIndex(currentSpineIndex);
|
||||
|
||||
std::string title;
|
||||
int titleWidth;
|
||||
if (tocIndex == -1) {
|
||||
title = "Unnamed";
|
||||
titleWidth = renderer.getTextWidth(SMALL_FONT_ID, "Unnamed");
|
||||
} else {
|
||||
const auto tocItem = epub->getTocItem(tocIndex);
|
||||
title = tocItem.title;
|
||||
titleWidth = renderer.getTextWidth(SMALL_FONT_ID, title.c_str());
|
||||
while (titleWidth > availableTextWidth && title.length() > 11) {
|
||||
title.replace(title.length() - 8, 8, "...");
|
||||
titleWidth = renderer.getTextWidth(SMALL_FONT_ID, title.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
renderer.drawText(SMALL_FONT_ID, titleMarginLeft + (availableTextWidth - titleWidth) / 2, textY, title.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,30 +5,34 @@
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include "../Activity.h"
|
||||
#include "activities/ActivityWithSubactivity.h"
|
||||
|
||||
class EpubReaderActivity final : public Activity {
|
||||
class EpubReaderActivity final : public ActivityWithSubactivity {
|
||||
std::shared_ptr<Epub> epub;
|
||||
std::unique_ptr<Section> section = nullptr;
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
std::unique_ptr<Activity> subAcitivity = nullptr;
|
||||
int currentSpineIndex = 0;
|
||||
int nextPageNumber = 0;
|
||||
int pagesUntilFullRefresh = 0;
|
||||
bool updateRequired = false;
|
||||
const std::function<void()> onGoBack;
|
||||
const std::function<void()> onGoHome;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void renderScreen();
|
||||
void renderContents(std::unique_ptr<Page> p);
|
||||
void renderStatusBar() const;
|
||||
void renderContents(std::unique_ptr<Page> page, int orientedMarginTop, int orientedMarginRight,
|
||||
int orientedMarginBottom, int orientedMarginLeft);
|
||||
void renderStatusBar(int orientedMarginRight, int orientedMarginBottom, int orientedMarginLeft) const;
|
||||
|
||||
public:
|
||||
explicit EpubReaderActivity(GfxRenderer& renderer, InputManager& inputManager, std::unique_ptr<Epub> epub,
|
||||
const std::function<void()>& onGoBack)
|
||||
: Activity(renderer, inputManager), epub(std::move(epub)), onGoBack(onGoBack) {}
|
||||
const std::function<void()>& onGoBack, const std::function<void()>& onGoHome)
|
||||
: ActivityWithSubactivity("EpubReader", renderer, inputManager),
|
||||
epub(std::move(epub)),
|
||||
onGoBack(onGoBack),
|
||||
onGoHome(onGoHome) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
|
||||
@@ -1,12 +1,31 @@
|
||||
#include "EpubReaderChapterSelectionActivity.h"
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <InputManager.h>
|
||||
#include <SD.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
constexpr int PAGE_ITEMS = 24;
|
||||
namespace {
|
||||
// Time threshold for treating a long press as a page-up/page-down
|
||||
constexpr int SKIP_PAGE_MS = 700;
|
||||
} // namespace
|
||||
|
||||
int EpubReaderChapterSelectionActivity::getPageItems() const {
|
||||
// Layout constants used in renderScreen
|
||||
constexpr int startY = 60;
|
||||
constexpr int lineHeight = 30;
|
||||
|
||||
const int screenHeight = renderer.getScreenHeight();
|
||||
const int availableHeight = screenHeight - startY;
|
||||
int items = availableHeight / lineHeight;
|
||||
|
||||
// Ensure we always have at least one item per page to avoid division by zero
|
||||
if (items < 1) {
|
||||
items = 1;
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
void EpubReaderChapterSelectionActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<EpubReaderChapterSelectionActivity*>(param);
|
||||
@@ -14,6 +33,8 @@ void EpubReaderChapterSelectionActivity::taskTrampoline(void* param) {
|
||||
}
|
||||
|
||||
void EpubReaderChapterSelectionActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
if (!epub) {
|
||||
return;
|
||||
}
|
||||
@@ -24,7 +45,7 @@ void EpubReaderChapterSelectionActivity::onEnter() {
|
||||
// Trigger first update
|
||||
updateRequired = true;
|
||||
xTaskCreate(&EpubReaderChapterSelectionActivity::taskTrampoline, "EpubReaderChapterSelectionActivityTask",
|
||||
2048, // Stack size
|
||||
4096, // Stack size
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
@@ -32,6 +53,8 @@ void EpubReaderChapterSelectionActivity::onEnter() {
|
||||
}
|
||||
|
||||
void EpubReaderChapterSelectionActivity::onExit() {
|
||||
Activity::onExit();
|
||||
|
||||
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
@@ -49,22 +72,23 @@ void EpubReaderChapterSelectionActivity::loop() {
|
||||
inputManager.wasReleased(InputManager::BTN_DOWN) || inputManager.wasReleased(InputManager::BTN_RIGHT);
|
||||
|
||||
const bool skipPage = inputManager.getHeldTime() > SKIP_PAGE_MS;
|
||||
const int pageItems = getPageItems();
|
||||
|
||||
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
if (inputManager.wasReleased(InputManager::BTN_CONFIRM)) {
|
||||
onSelectSpineIndex(selectorIndex);
|
||||
} else if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
} else if (inputManager.wasReleased(InputManager::BTN_BACK)) {
|
||||
onGoBack();
|
||||
} else if (prevReleased) {
|
||||
if (skipPage) {
|
||||
selectorIndex =
|
||||
((selectorIndex / PAGE_ITEMS - 1) * PAGE_ITEMS + epub->getSpineItemsCount()) % epub->getSpineItemsCount();
|
||||
((selectorIndex / pageItems - 1) * pageItems + epub->getSpineItemsCount()) % epub->getSpineItemsCount();
|
||||
} else {
|
||||
selectorIndex = (selectorIndex + epub->getSpineItemsCount() - 1) % epub->getSpineItemsCount();
|
||||
}
|
||||
updateRequired = true;
|
||||
} else if (nextReleased) {
|
||||
if (skipPage) {
|
||||
selectorIndex = ((selectorIndex / PAGE_ITEMS + 1) * PAGE_ITEMS) % epub->getSpineItemsCount();
|
||||
selectorIndex = ((selectorIndex / pageItems + 1) * pageItems) % epub->getSpineItemsCount();
|
||||
} else {
|
||||
selectorIndex = (selectorIndex + 1) % epub->getSpineItemsCount();
|
||||
}
|
||||
@@ -88,17 +112,18 @@ void EpubReaderChapterSelectionActivity::renderScreen() {
|
||||
renderer.clearScreen();
|
||||
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const int pageItems = getPageItems();
|
||||
renderer.drawCenteredText(READER_FONT_ID, 10, "Select Chapter", true, BOLD);
|
||||
|
||||
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
|
||||
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 + 2, pageWidth - 1, 30);
|
||||
for (int i = pageStartIndex; i < epub->getSpineItemsCount() && i < pageStartIndex + PAGE_ITEMS; i++) {
|
||||
const auto pageStartIndex = selectorIndex / pageItems * pageItems;
|
||||
renderer.fillRect(0, 60 + (selectorIndex % pageItems) * 30 - 2, pageWidth - 1, 30);
|
||||
for (int i = pageStartIndex; i < epub->getSpineItemsCount() && i < pageStartIndex + pageItems; i++) {
|
||||
const int tocIndex = epub->getTocIndexForSpineIndex(i);
|
||||
if (tocIndex == -1) {
|
||||
renderer.drawText(UI_FONT_ID, 20, 60 + (i % PAGE_ITEMS) * 30, "Unnamed", i != selectorIndex);
|
||||
renderer.drawText(UI_FONT_ID, 20, 60 + (i % pageItems) * 30, "Unnamed", i != selectorIndex);
|
||||
} else {
|
||||
auto item = epub->getTocItem(tocIndex);
|
||||
renderer.drawText(UI_FONT_ID, 20 + (item.level - 1) * 15, 60 + (i % PAGE_ITEMS) * 30, item.title.c_str(),
|
||||
renderer.drawText(UI_FONT_ID, 20 + (item.level - 1) * 15, 60 + (i % pageItems) * 30, item.title.c_str(),
|
||||
i != selectorIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,10 @@ class EpubReaderChapterSelectionActivity final : public Activity {
|
||||
const std::function<void()> onGoBack;
|
||||
const std::function<void(int newSpineIndex)> onSelectSpineIndex;
|
||||
|
||||
// Number of items that fit on a page, derived from logical screen height.
|
||||
// This adapts automatically when switching between portrait and landscape.
|
||||
int getPageItems() const;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void renderScreen();
|
||||
@@ -27,7 +31,7 @@ class EpubReaderChapterSelectionActivity final : public Activity {
|
||||
const std::shared_ptr<Epub>& epub, const int currentSpineIndex,
|
||||
const std::function<void()>& onGoBack,
|
||||
const std::function<void(int newSpineIndex)>& onSelectSpineIndex)
|
||||
: Activity(renderer, inputManager),
|
||||
: Activity("EpubReaderChapterSelection", renderer, inputManager),
|
||||
epub(epub),
|
||||
currentSpineIndex(currentSpineIndex),
|
||||
onGoBack(onGoBack),
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
#include "FileSelectionActivity.h"
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <InputManager.h>
|
||||
#include <SD.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
namespace {
|
||||
constexpr int PAGE_ITEMS = 23;
|
||||
constexpr int SKIP_PAGE_MS = 700;
|
||||
constexpr unsigned long GO_HOME_MS = 1000;
|
||||
} // namespace
|
||||
|
||||
void sortFileList(std::vector<std::string>& strs) {
|
||||
std::sort(begin(strs), end(strs), [](const std::string& str1, const std::string& str2) {
|
||||
if (str1.back() == '/' && str2.back() != '/') return true;
|
||||
@@ -33,8 +40,12 @@ void FileSelectionActivity::loadFiles() {
|
||||
|
||||
if (file.isDirectory()) {
|
||||
files.emplace_back(filename + "/");
|
||||
} else if (filename.substr(filename.length() - 5) == ".epub") {
|
||||
files.emplace_back(filename);
|
||||
} else {
|
||||
std::string ext4 = filename.length() >= 4 ? filename.substr(filename.length() - 4) : "";
|
||||
std::string ext5 = filename.length() >= 5 ? filename.substr(filename.length() - 5) : "";
|
||||
if (ext5 == ".epub" || ext5 == ".xtch" || ext4 == ".xtc") {
|
||||
files.emplace_back(filename);
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
@@ -43,9 +54,11 @@ void FileSelectionActivity::loadFiles() {
|
||||
}
|
||||
|
||||
void FileSelectionActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
basepath = "/";
|
||||
// basepath is set via constructor parameter (defaults to "/" if not specified)
|
||||
loadFiles();
|
||||
selectorIndex = 0;
|
||||
|
||||
@@ -61,6 +74,8 @@ void FileSelectionActivity::onEnter() {
|
||||
}
|
||||
|
||||
void FileSelectionActivity::onExit() {
|
||||
Activity::onExit();
|
||||
|
||||
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
@@ -73,12 +88,24 @@ void FileSelectionActivity::onExit() {
|
||||
}
|
||||
|
||||
void FileSelectionActivity::loop() {
|
||||
const bool prevPressed =
|
||||
inputManager.wasPressed(InputManager::BTN_UP) || inputManager.wasPressed(InputManager::BTN_LEFT);
|
||||
const bool nextPressed =
|
||||
inputManager.wasPressed(InputManager::BTN_DOWN) || inputManager.wasPressed(InputManager::BTN_RIGHT);
|
||||
// Long press BACK (1s+) goes to root folder
|
||||
if (inputManager.isPressed(InputManager::BTN_BACK) && inputManager.getHeldTime() >= GO_HOME_MS) {
|
||||
if (basepath != "/") {
|
||||
basepath = "/";
|
||||
loadFiles();
|
||||
updateRequired = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
const bool prevReleased =
|
||||
inputManager.wasReleased(InputManager::BTN_UP) || inputManager.wasReleased(InputManager::BTN_LEFT);
|
||||
const bool nextReleased =
|
||||
inputManager.wasReleased(InputManager::BTN_DOWN) || inputManager.wasReleased(InputManager::BTN_RIGHT);
|
||||
|
||||
const bool skipPage = inputManager.getHeldTime() > SKIP_PAGE_MS;
|
||||
|
||||
if (inputManager.wasReleased(InputManager::BTN_CONFIRM)) {
|
||||
if (files.empty()) {
|
||||
return;
|
||||
}
|
||||
@@ -91,21 +118,31 @@ void FileSelectionActivity::loop() {
|
||||
} else {
|
||||
onSelect(basepath + files[selectorIndex]);
|
||||
}
|
||||
} else if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
if (basepath != "/") {
|
||||
basepath = basepath.substr(0, basepath.rfind('/'));
|
||||
if (basepath.empty()) basepath = "/";
|
||||
loadFiles();
|
||||
updateRequired = true;
|
||||
} else {
|
||||
// At root level, go back home
|
||||
onGoHome();
|
||||
} else if (inputManager.wasReleased(InputManager::BTN_BACK)) {
|
||||
// Short press: go up one directory, or go home if at root
|
||||
if (inputManager.getHeldTime() < GO_HOME_MS) {
|
||||
if (basepath != "/") {
|
||||
basepath.replace(basepath.find_last_of('/'), std::string::npos, "");
|
||||
if (basepath.empty()) basepath = "/";
|
||||
loadFiles();
|
||||
updateRequired = true;
|
||||
} else {
|
||||
onGoHome();
|
||||
}
|
||||
}
|
||||
} else if (prevReleased) {
|
||||
if (skipPage) {
|
||||
selectorIndex = ((selectorIndex / PAGE_ITEMS - 1) * PAGE_ITEMS + files.size()) % files.size();
|
||||
} else {
|
||||
selectorIndex = (selectorIndex + files.size() - 1) % files.size();
|
||||
}
|
||||
} else if (prevPressed) {
|
||||
selectorIndex = (selectorIndex + files.size() - 1) % files.size();
|
||||
updateRequired = true;
|
||||
} else if (nextPressed) {
|
||||
selectorIndex = (selectorIndex + 1) % files.size();
|
||||
} else if (nextReleased) {
|
||||
if (skipPage) {
|
||||
selectorIndex = ((selectorIndex / PAGE_ITEMS + 1) * PAGE_ITEMS) % files.size();
|
||||
} else {
|
||||
selectorIndex = (selectorIndex + 1) % files.size();
|
||||
}
|
||||
updateRequired = true;
|
||||
}
|
||||
}
|
||||
@@ -125,22 +162,28 @@ void FileSelectionActivity::displayTaskLoop() {
|
||||
void FileSelectionActivity::render() const {
|
||||
renderer.clearScreen();
|
||||
|
||||
const auto pageWidth = GfxRenderer::getScreenWidth();
|
||||
renderer.drawCenteredText(READER_FONT_ID, 10, "CrossPoint Reader", true, BOLD);
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
renderer.drawCenteredText(READER_FONT_ID, 10, "Books", true, BOLD);
|
||||
|
||||
// Help text
|
||||
renderer.drawText(SMALL_FONT_ID, 20, GfxRenderer::getScreenHeight() - 30, "Press BACK for Home");
|
||||
renderer.drawButtonHints(UI_FONT_ID, "« Home", "Open", "", "");
|
||||
|
||||
if (files.empty()) {
|
||||
renderer.drawText(UI_FONT_ID, 20, 60, "No EPUBs found");
|
||||
} else {
|
||||
// Draw selection
|
||||
renderer.fillRect(0, 60 + selectorIndex * 30 + 2, pageWidth - 1, 30);
|
||||
renderer.drawText(UI_FONT_ID, 20, 60, "No books found");
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < files.size(); i++) {
|
||||
const auto file = files[i];
|
||||
renderer.drawText(UI_FONT_ID, 20, 60 + i * 30, file.c_str(), i != selectorIndex);
|
||||
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
|
||||
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 - 2, pageWidth - 1, 30);
|
||||
for (int i = pageStartIndex; i < files.size() && i < pageStartIndex + PAGE_ITEMS; i++) {
|
||||
auto item = files[i];
|
||||
int itemWidth = renderer.getTextWidth(UI_FONT_ID, item.c_str());
|
||||
while (itemWidth > renderer.getScreenWidth() - 40 && item.length() > 8) {
|
||||
item.replace(item.length() - 5, 5, "...");
|
||||
itemWidth = renderer.getTextWidth(UI_FONT_ID, item.c_str());
|
||||
}
|
||||
renderer.drawText(UI_FONT_ID, 20, 60 + (i % PAGE_ITEMS) * 30, item.c_str(), i != selectorIndex);
|
||||
}
|
||||
|
||||
renderer.displayBuffer();
|
||||
|
||||
@@ -27,8 +27,11 @@ class FileSelectionActivity final : public Activity {
|
||||
public:
|
||||
explicit FileSelectionActivity(GfxRenderer& renderer, InputManager& inputManager,
|
||||
const std::function<void(const std::string&)>& onSelect,
|
||||
const std::function<void()>& onGoHome)
|
||||
: Activity(renderer, inputManager), onSelect(onSelect), onGoHome(onGoHome) {}
|
||||
const std::function<void()>& onGoHome, std::string initialPath = "/")
|
||||
: Activity("FileSelection", renderer, inputManager),
|
||||
basepath(initialPath.empty() ? "/" : std::move(initialPath)),
|
||||
onSelect(onSelect),
|
||||
onGoHome(onGoHome) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
|
||||
@@ -2,12 +2,32 @@
|
||||
|
||||
#include <SD.h>
|
||||
|
||||
#include "CrossPointState.h"
|
||||
#include "Epub.h"
|
||||
#include "EpubReaderActivity.h"
|
||||
#include "FileSelectionActivity.h"
|
||||
#include "Xtc.h"
|
||||
#include "XtcReaderActivity.h"
|
||||
#include "activities/util/FullScreenMessageActivity.h"
|
||||
|
||||
std::string ReaderActivity::extractFolderPath(const std::string& filePath) {
|
||||
const auto lastSlash = filePath.find_last_of('/');
|
||||
if (lastSlash == std::string::npos || lastSlash == 0) {
|
||||
return "/";
|
||||
}
|
||||
return filePath.substr(0, lastSlash);
|
||||
}
|
||||
|
||||
bool ReaderActivity::isXtcFile(const std::string& path) {
|
||||
if (path.length() < 4) return false;
|
||||
std::string ext4 = path.substr(path.length() - 4);
|
||||
if (ext4 == ".xtc") return true;
|
||||
if (path.length() >= 5) {
|
||||
std::string ext5 = path.substr(path.length() - 5);
|
||||
if (ext5 == ".xtch") return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<Epub> ReaderActivity::loadEpub(const std::string& path) {
|
||||
if (!SD.exists(path.c_str())) {
|
||||
Serial.printf("[%lu] [ ] File does not exist: %s\n", millis(), path.c_str());
|
||||
@@ -23,46 +43,102 @@ std::unique_ptr<Epub> ReaderActivity::loadEpub(const std::string& path) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ReaderActivity::onSelectEpubFile(const std::string& path) {
|
||||
std::unique_ptr<Xtc> ReaderActivity::loadXtc(const std::string& path) {
|
||||
if (!SD.exists(path.c_str())) {
|
||||
Serial.printf("[%lu] [ ] File does not exist: %s\n", millis(), path.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto xtc = std::unique_ptr<Xtc>(new Xtc(path, "/.crosspoint"));
|
||||
if (xtc->load()) {
|
||||
return xtc;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [ ] Failed to load XTC\n", millis());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ReaderActivity::onSelectBookFile(const std::string& path) {
|
||||
currentBookPath = path; // Track current book path
|
||||
exitActivity();
|
||||
enterNewActivity(new FullScreenMessageActivity(renderer, inputManager, "Loading..."));
|
||||
|
||||
auto epub = loadEpub(path);
|
||||
if (epub) {
|
||||
APP_STATE.openEpubPath = path;
|
||||
APP_STATE.saveToFile();
|
||||
onGoToEpubReader(std::move(epub));
|
||||
if (isXtcFile(path)) {
|
||||
// Load XTC file
|
||||
auto xtc = loadXtc(path);
|
||||
if (xtc) {
|
||||
onGoToXtcReader(std::move(xtc));
|
||||
} else {
|
||||
exitActivity();
|
||||
enterNewActivity(new FullScreenMessageActivity(renderer, inputManager, "Failed to load XTC", REGULAR,
|
||||
EInkDisplay::HALF_REFRESH));
|
||||
delay(2000);
|
||||
onGoToFileSelection();
|
||||
}
|
||||
} else {
|
||||
exitActivity();
|
||||
enterNewActivity(new FullScreenMessageActivity(renderer, inputManager, "Failed to load epub", REGULAR,
|
||||
EInkDisplay::HALF_REFRESH));
|
||||
delay(2000);
|
||||
onGoToFileSelection();
|
||||
// Load EPUB file
|
||||
auto epub = loadEpub(path);
|
||||
if (epub) {
|
||||
onGoToEpubReader(std::move(epub));
|
||||
} else {
|
||||
exitActivity();
|
||||
enterNewActivity(new FullScreenMessageActivity(renderer, inputManager, "Failed to load epub", REGULAR,
|
||||
EInkDisplay::HALF_REFRESH));
|
||||
delay(2000);
|
||||
onGoToFileSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ReaderActivity::onGoToFileSelection() {
|
||||
void ReaderActivity::onGoToFileSelection(const std::string& fromBookPath) {
|
||||
exitActivity();
|
||||
// If coming from a book, start in that book's folder; otherwise start from root
|
||||
const auto initialPath = fromBookPath.empty() ? "/" : extractFolderPath(fromBookPath);
|
||||
enterNewActivity(new FileSelectionActivity(
|
||||
renderer, inputManager, [this](const std::string& path) { onSelectEpubFile(path); }, onGoBack));
|
||||
renderer, inputManager, [this](const std::string& path) { onSelectBookFile(path); }, onGoBack, initialPath));
|
||||
}
|
||||
|
||||
void ReaderActivity::onGoToEpubReader(std::unique_ptr<Epub> epub) {
|
||||
const auto epubPath = epub->getPath();
|
||||
currentBookPath = epubPath;
|
||||
exitActivity();
|
||||
enterNewActivity(new EpubReaderActivity(renderer, inputManager, std::move(epub), [this] { onGoToFileSelection(); }));
|
||||
enterNewActivity(new EpubReaderActivity(
|
||||
renderer, inputManager, std::move(epub), [this, epubPath] { onGoToFileSelection(epubPath); },
|
||||
[this] { onGoBack(); }));
|
||||
}
|
||||
|
||||
void ReaderActivity::onGoToXtcReader(std::unique_ptr<Xtc> xtc) {
|
||||
const auto xtcPath = xtc->getPath();
|
||||
currentBookPath = xtcPath;
|
||||
exitActivity();
|
||||
enterNewActivity(new XtcReaderActivity(
|
||||
renderer, inputManager, std::move(xtc), [this, xtcPath] { onGoToFileSelection(xtcPath); },
|
||||
[this] { onGoBack(); }));
|
||||
}
|
||||
|
||||
void ReaderActivity::onEnter() {
|
||||
if (initialEpubPath.empty()) {
|
||||
onGoToFileSelection();
|
||||
ActivityWithSubactivity::onEnter();
|
||||
|
||||
if (initialBookPath.empty()) {
|
||||
onGoToFileSelection(); // Start from root when entering via Browse
|
||||
return;
|
||||
}
|
||||
|
||||
auto epub = loadEpub(initialEpubPath);
|
||||
if (!epub) {
|
||||
onGoBack();
|
||||
return;
|
||||
}
|
||||
currentBookPath = initialBookPath;
|
||||
|
||||
onGoToEpubReader(std::move(epub));
|
||||
if (isXtcFile(initialBookPath)) {
|
||||
auto xtc = loadXtc(initialBookPath);
|
||||
if (!xtc) {
|
||||
onGoBack();
|
||||
return;
|
||||
}
|
||||
onGoToXtcReader(std::move(xtc));
|
||||
} else {
|
||||
auto epub = loadEpub(initialBookPath);
|
||||
if (!epub) {
|
||||
onGoBack();
|
||||
return;
|
||||
}
|
||||
onGoToEpubReader(std::move(epub));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,21 +4,27 @@
|
||||
#include "../ActivityWithSubactivity.h"
|
||||
|
||||
class Epub;
|
||||
class Xtc;
|
||||
|
||||
class ReaderActivity final : public ActivityWithSubactivity {
|
||||
std::string initialEpubPath;
|
||||
std::string initialBookPath;
|
||||
std::string currentBookPath; // Track current book path for navigation
|
||||
const std::function<void()> onGoBack;
|
||||
static std::unique_ptr<Epub> loadEpub(const std::string& path);
|
||||
static std::unique_ptr<Xtc> loadXtc(const std::string& path);
|
||||
static bool isXtcFile(const std::string& path);
|
||||
|
||||
void onSelectEpubFile(const std::string& path);
|
||||
void onGoToFileSelection();
|
||||
static std::string extractFolderPath(const std::string& filePath);
|
||||
void onSelectBookFile(const std::string& path);
|
||||
void onGoToFileSelection(const std::string& fromBookPath = "");
|
||||
void onGoToEpubReader(std::unique_ptr<Epub> epub);
|
||||
void onGoToXtcReader(std::unique_ptr<Xtc> xtc);
|
||||
|
||||
public:
|
||||
explicit ReaderActivity(GfxRenderer& renderer, InputManager& inputManager, std::string initialEpubPath,
|
||||
explicit ReaderActivity(GfxRenderer& renderer, InputManager& inputManager, std::string initialBookPath,
|
||||
const std::function<void()>& onGoBack)
|
||||
: ActivityWithSubactivity(renderer, inputManager),
|
||||
initialEpubPath(std::move(initialEpubPath)),
|
||||
: ActivityWithSubactivity("Reader", renderer, inputManager),
|
||||
initialBookPath(std::move(initialBookPath)),
|
||||
onGoBack(onGoBack) {}
|
||||
void onEnter() override;
|
||||
};
|
||||
|
||||
360
src/activities/reader/XtcReaderActivity.cpp
Normal file
@@ -0,0 +1,360 @@
|
||||
/**
|
||||
* XtcReaderActivity.cpp
|
||||
*
|
||||
* XTC ebook reader activity implementation
|
||||
* Displays pre-rendered XTC pages on e-ink display
|
||||
*/
|
||||
|
||||
#include "XtcReaderActivity.h"
|
||||
|
||||
#include <FsHelpers.h>
|
||||
#include <GfxRenderer.h>
|
||||
#include <InputManager.h>
|
||||
|
||||
#include "CrossPointSettings.h"
|
||||
#include "CrossPointState.h"
|
||||
#include "config.h"
|
||||
|
||||
namespace {
|
||||
constexpr int pagesPerRefresh = 15;
|
||||
constexpr unsigned long skipPageMs = 700;
|
||||
constexpr unsigned long goHomeMs = 1000;
|
||||
} // namespace
|
||||
|
||||
void XtcReaderActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<XtcReaderActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
void XtcReaderActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
if (!xtc) {
|
||||
return;
|
||||
}
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
xtc->setupCacheDir();
|
||||
|
||||
// Load saved progress
|
||||
loadProgress();
|
||||
|
||||
// Save current XTC as last opened book
|
||||
APP_STATE.openEpubPath = xtc->getPath();
|
||||
APP_STATE.saveToFile();
|
||||
|
||||
// Trigger first update
|
||||
updateRequired = true;
|
||||
|
||||
xTaskCreate(&XtcReaderActivity::taskTrampoline, "XtcReaderActivityTask",
|
||||
4096, // Stack size (smaller than EPUB since no parsing needed)
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
);
|
||||
}
|
||||
|
||||
void XtcReaderActivity::onExit() {
|
||||
Activity::onExit();
|
||||
|
||||
// Wait until not rendering to delete task
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
vTaskDelete(displayTaskHandle);
|
||||
displayTaskHandle = nullptr;
|
||||
}
|
||||
vSemaphoreDelete(renderingMutex);
|
||||
renderingMutex = nullptr;
|
||||
xtc.reset();
|
||||
}
|
||||
|
||||
void XtcReaderActivity::loop() {
|
||||
// Long press BACK (1s+) goes directly to home
|
||||
if (inputManager.isPressed(InputManager::BTN_BACK) && inputManager.getHeldTime() >= goHomeMs) {
|
||||
onGoHome();
|
||||
return;
|
||||
}
|
||||
|
||||
// Short press BACK goes to file selection
|
||||
if (inputManager.wasReleased(InputManager::BTN_BACK) && inputManager.getHeldTime() < goHomeMs) {
|
||||
onGoBack();
|
||||
return;
|
||||
}
|
||||
|
||||
const bool prevReleased =
|
||||
inputManager.wasReleased(InputManager::BTN_UP) || inputManager.wasReleased(InputManager::BTN_LEFT);
|
||||
const bool nextReleased =
|
||||
inputManager.wasReleased(InputManager::BTN_DOWN) || inputManager.wasReleased(InputManager::BTN_RIGHT);
|
||||
|
||||
if (!prevReleased && !nextReleased) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle end of book
|
||||
if (currentPage >= xtc->getPageCount()) {
|
||||
currentPage = xtc->getPageCount() - 1;
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const bool skipPages = inputManager.getHeldTime() > skipPageMs;
|
||||
const int skipAmount = skipPages ? 10 : 1;
|
||||
|
||||
if (prevReleased) {
|
||||
if (currentPage >= static_cast<uint32_t>(skipAmount)) {
|
||||
currentPage -= skipAmount;
|
||||
} else {
|
||||
currentPage = 0;
|
||||
}
|
||||
updateRequired = true;
|
||||
} else if (nextReleased) {
|
||||
currentPage += skipAmount;
|
||||
if (currentPage >= xtc->getPageCount()) {
|
||||
currentPage = xtc->getPageCount(); // Allow showing "End of book"
|
||||
}
|
||||
updateRequired = true;
|
||||
}
|
||||
}
|
||||
|
||||
void XtcReaderActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
if (updateRequired) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
renderScreen();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void XtcReaderActivity::renderScreen() {
|
||||
if (!xtc) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bounds check
|
||||
if (currentPage >= xtc->getPageCount()) {
|
||||
// Show end of book screen
|
||||
renderer.clearScreen();
|
||||
renderer.drawCenteredText(UI_FONT_ID, 300, "End of book", true, BOLD);
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
renderPage();
|
||||
saveProgress();
|
||||
}
|
||||
|
||||
void XtcReaderActivity::renderPage() {
|
||||
const uint16_t pageWidth = xtc->getPageWidth();
|
||||
const uint16_t pageHeight = xtc->getPageHeight();
|
||||
const uint8_t bitDepth = xtc->getBitDepth();
|
||||
|
||||
// Calculate buffer size for one page
|
||||
// XTG (1-bit): Row-major, ((width+7)/8) * height bytes
|
||||
// XTH (2-bit): Two bit planes, column-major, ((width * height + 7) / 8) * 2 bytes
|
||||
size_t pageBufferSize;
|
||||
if (bitDepth == 2) {
|
||||
pageBufferSize = ((static_cast<size_t>(pageWidth) * pageHeight + 7) / 8) * 2;
|
||||
} else {
|
||||
pageBufferSize = ((pageWidth + 7) / 8) * pageHeight;
|
||||
}
|
||||
|
||||
// Allocate page buffer
|
||||
uint8_t* pageBuffer = static_cast<uint8_t*>(malloc(pageBufferSize));
|
||||
if (!pageBuffer) {
|
||||
Serial.printf("[%lu] [XTR] Failed to allocate page buffer (%lu bytes)\n", millis(), pageBufferSize);
|
||||
renderer.clearScreen();
|
||||
renderer.drawCenteredText(UI_FONT_ID, 300, "Memory error", true, BOLD);
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
// Load page data
|
||||
size_t bytesRead = xtc->loadPage(currentPage, pageBuffer, pageBufferSize);
|
||||
if (bytesRead == 0) {
|
||||
Serial.printf("[%lu] [XTR] Failed to load page %lu\n", millis(), currentPage);
|
||||
free(pageBuffer);
|
||||
renderer.clearScreen();
|
||||
renderer.drawCenteredText(UI_FONT_ID, 300, "Page load error", true, BOLD);
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
// Clear screen first
|
||||
renderer.clearScreen();
|
||||
|
||||
// Copy page bitmap using GfxRenderer's drawPixel
|
||||
// XTC/XTCH pages are pre-rendered with status bar included, so render full page
|
||||
const uint16_t maxSrcY = pageHeight;
|
||||
|
||||
if (bitDepth == 2) {
|
||||
// XTH 2-bit mode: Two bit planes, column-major order
|
||||
// - Columns scanned right to left (x = width-1 down to 0)
|
||||
// - 8 vertical pixels per byte (MSB = topmost pixel in group)
|
||||
// - First plane: Bit1, Second plane: Bit2
|
||||
// - Pixel value = (bit1 << 1) | bit2
|
||||
// - Grayscale: 0=White, 1=Dark Grey, 2=Light Grey, 3=Black
|
||||
|
||||
const size_t planeSize = (static_cast<size_t>(pageWidth) * pageHeight + 7) / 8;
|
||||
const uint8_t* plane1 = pageBuffer; // Bit1 plane
|
||||
const uint8_t* plane2 = pageBuffer + planeSize; // Bit2 plane
|
||||
const size_t colBytes = (pageHeight + 7) / 8; // Bytes per column (100 for 800 height)
|
||||
|
||||
// Lambda to get pixel value at (x, y)
|
||||
auto getPixelValue = [&](uint16_t x, uint16_t y) -> uint8_t {
|
||||
const size_t colIndex = pageWidth - 1 - x;
|
||||
const size_t byteInCol = y / 8;
|
||||
const size_t bitInByte = 7 - (y % 8);
|
||||
const size_t byteOffset = colIndex * colBytes + byteInCol;
|
||||
const uint8_t bit1 = (plane1[byteOffset] >> bitInByte) & 1;
|
||||
const uint8_t bit2 = (plane2[byteOffset] >> bitInByte) & 1;
|
||||
return (bit1 << 1) | bit2;
|
||||
};
|
||||
|
||||
// Optimized grayscale rendering without storeBwBuffer (saves 48KB peak memory)
|
||||
// Flow: BW display → LSB/MSB passes → grayscale display → re-render BW for next frame
|
||||
|
||||
// Count pixel distribution for debugging
|
||||
uint32_t pixelCounts[4] = {0, 0, 0, 0};
|
||||
for (uint16_t y = 0; y < pageHeight; y++) {
|
||||
for (uint16_t x = 0; x < pageWidth; x++) {
|
||||
pixelCounts[getPixelValue(x, y)]++;
|
||||
}
|
||||
}
|
||||
Serial.printf("[%lu] [XTR] Pixel distribution: White=%lu, DarkGrey=%lu, LightGrey=%lu, Black=%lu\n", millis(),
|
||||
pixelCounts[0], pixelCounts[1], pixelCounts[2], pixelCounts[3]);
|
||||
|
||||
// Pass 1: BW buffer - draw all non-white pixels as black
|
||||
for (uint16_t y = 0; y < pageHeight; y++) {
|
||||
for (uint16_t x = 0; x < pageWidth; x++) {
|
||||
if (getPixelValue(x, y) >= 1) {
|
||||
renderer.drawPixel(x, y, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Display BW with conditional refresh based on pagesUntilFullRefresh
|
||||
if (pagesUntilFullRefresh <= 1) {
|
||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
||||
pagesUntilFullRefresh = pagesPerRefresh;
|
||||
} else {
|
||||
renderer.displayBuffer();
|
||||
pagesUntilFullRefresh--;
|
||||
}
|
||||
|
||||
// Pass 2: LSB buffer - mark DARK gray only (XTH value 1)
|
||||
// In LUT: 0 bit = apply gray effect, 1 bit = untouched
|
||||
renderer.clearScreen(0x00);
|
||||
for (uint16_t y = 0; y < pageHeight; y++) {
|
||||
for (uint16_t x = 0; x < pageWidth; x++) {
|
||||
if (getPixelValue(x, y) == 1) { // Dark grey only
|
||||
renderer.drawPixel(x, y, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
renderer.copyGrayscaleLsbBuffers();
|
||||
|
||||
// Pass 3: MSB buffer - mark LIGHT AND DARK gray (XTH value 1 or 2)
|
||||
// In LUT: 0 bit = apply gray effect, 1 bit = untouched
|
||||
renderer.clearScreen(0x00);
|
||||
for (uint16_t y = 0; y < pageHeight; y++) {
|
||||
for (uint16_t x = 0; x < pageWidth; x++) {
|
||||
const uint8_t pv = getPixelValue(x, y);
|
||||
if (pv == 1 || pv == 2) { // Dark grey or Light grey
|
||||
renderer.drawPixel(x, y, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
renderer.copyGrayscaleMsbBuffers();
|
||||
|
||||
// Display grayscale overlay
|
||||
renderer.displayGrayBuffer();
|
||||
|
||||
// Pass 4: Re-render BW to framebuffer (restore for next frame, instead of restoreBwBuffer)
|
||||
renderer.clearScreen();
|
||||
for (uint16_t y = 0; y < pageHeight; y++) {
|
||||
for (uint16_t x = 0; x < pageWidth; x++) {
|
||||
if (getPixelValue(x, y) >= 1) {
|
||||
renderer.drawPixel(x, y, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup grayscale buffers with current frame buffer
|
||||
renderer.cleanupGrayscaleWithFrameBuffer();
|
||||
|
||||
free(pageBuffer);
|
||||
|
||||
Serial.printf("[%lu] [XTR] Rendered page %lu/%lu (2-bit grayscale)\n", millis(), currentPage + 1,
|
||||
xtc->getPageCount());
|
||||
return;
|
||||
} else {
|
||||
// 1-bit mode: 8 pixels per byte, MSB first
|
||||
const size_t srcRowBytes = (pageWidth + 7) / 8; // 60 bytes for 480 width
|
||||
|
||||
for (uint16_t srcY = 0; srcY < maxSrcY; srcY++) {
|
||||
const size_t srcRowStart = srcY * srcRowBytes;
|
||||
|
||||
for (uint16_t srcX = 0; srcX < pageWidth; srcX++) {
|
||||
// Read source pixel (MSB first, bit 7 = leftmost pixel)
|
||||
const size_t srcByte = srcRowStart + srcX / 8;
|
||||
const size_t srcBit = 7 - (srcX % 8);
|
||||
const bool isBlack = !((pageBuffer[srcByte] >> srcBit) & 1); // XTC: 0 = black, 1 = white
|
||||
|
||||
if (isBlack) {
|
||||
renderer.drawPixel(srcX, srcY, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// White pixels are already cleared by clearScreen()
|
||||
|
||||
free(pageBuffer);
|
||||
|
||||
// XTC pages already have status bar pre-rendered, no need to add our own
|
||||
|
||||
// Display with appropriate refresh
|
||||
if (pagesUntilFullRefresh <= 1) {
|
||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
||||
pagesUntilFullRefresh = pagesPerRefresh;
|
||||
} else {
|
||||
renderer.displayBuffer();
|
||||
pagesUntilFullRefresh--;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [XTR] Rendered page %lu/%lu (%u-bit)\n", millis(), currentPage + 1, xtc->getPageCount(),
|
||||
bitDepth);
|
||||
}
|
||||
|
||||
void XtcReaderActivity::saveProgress() const {
|
||||
File f;
|
||||
if (FsHelpers::openFileForWrite("XTR", xtc->getCachePath() + "/progress.bin", f)) {
|
||||
uint8_t data[4];
|
||||
data[0] = currentPage & 0xFF;
|
||||
data[1] = (currentPage >> 8) & 0xFF;
|
||||
data[2] = (currentPage >> 16) & 0xFF;
|
||||
data[3] = (currentPage >> 24) & 0xFF;
|
||||
f.write(data, 4);
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
|
||||
void XtcReaderActivity::loadProgress() {
|
||||
File f;
|
||||
if (FsHelpers::openFileForRead("XTR", xtc->getCachePath() + "/progress.bin", f)) {
|
||||
uint8_t data[4];
|
||||
if (f.read(data, 4) == 4) {
|
||||
currentPage = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
|
||||
Serial.printf("[%lu] [XTR] Loaded progress: page %lu\n", millis(), currentPage);
|
||||
|
||||
// Validate page number
|
||||
if (currentPage >= xtc->getPageCount()) {
|
||||
currentPage = 0;
|
||||
}
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
41
src/activities/reader/XtcReaderActivity.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* XtcReaderActivity.h
|
||||
*
|
||||
* XTC ebook reader activity for CrossPoint Reader
|
||||
* Displays pre-rendered XTC pages on e-ink display
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Xtc.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include "activities/Activity.h"
|
||||
|
||||
class XtcReaderActivity final : public Activity {
|
||||
std::shared_ptr<Xtc> xtc;
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
uint32_t currentPage = 0;
|
||||
int pagesUntilFullRefresh = 0;
|
||||
bool updateRequired = false;
|
||||
const std::function<void()> onGoBack;
|
||||
const std::function<void()> onGoHome;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void renderScreen();
|
||||
void renderPage();
|
||||
void saveProgress() const;
|
||||
void loadProgress();
|
||||
|
||||
public:
|
||||
explicit XtcReaderActivity(GfxRenderer& renderer, InputManager& inputManager, std::unique_ptr<Xtc> xtc,
|
||||
const std::function<void()>& onGoBack, const std::function<void()>& onGoHome)
|
||||
: Activity("XtcReader", renderer, inputManager), xtc(std::move(xtc)), onGoBack(onGoBack), onGoHome(onGoHome) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
};
|
||||
242
src/activities/settings/OtaUpdateActivity.cpp
Normal file
@@ -0,0 +1,242 @@
|
||||
#include "OtaUpdateActivity.h"
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <InputManager.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include "activities/network/WifiSelectionActivity.h"
|
||||
#include "config.h"
|
||||
#include "network/OtaUpdater.h"
|
||||
|
||||
void OtaUpdateActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<OtaUpdateActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
void OtaUpdateActivity::onWifiSelectionComplete(const bool success) {
|
||||
exitActivity();
|
||||
|
||||
if (!success) {
|
||||
Serial.printf("[%lu] [OTA] WiFi connection failed, exiting\n", millis());
|
||||
goBack();
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [OTA] WiFi connected, checking for update\n", millis());
|
||||
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
state = CHECKING_FOR_UPDATE;
|
||||
xSemaphoreGive(renderingMutex);
|
||||
updateRequired = true;
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
const auto res = updater.checkForUpdate();
|
||||
if (res != OtaUpdater::OK) {
|
||||
Serial.printf("[%lu] [OTA] Update check failed: %d\n", millis(), res);
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
state = FAILED;
|
||||
xSemaphoreGive(renderingMutex);
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!updater.isUpdateNewer()) {
|
||||
Serial.printf("[%lu] [OTA] No new update available\n", millis());
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
state = NO_UPDATE;
|
||||
xSemaphoreGive(renderingMutex);
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
state = WAITING_CONFIRMATION;
|
||||
xSemaphoreGive(renderingMutex);
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
void OtaUpdateActivity::onEnter() {
|
||||
ActivityWithSubactivity::onEnter();
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
xTaskCreate(&OtaUpdateActivity::taskTrampoline, "OtaUpdateActivityTask",
|
||||
2048, // Stack size
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
);
|
||||
|
||||
// Turn on WiFi immediately
|
||||
Serial.printf("[%lu] [OTA] Turning on WiFi...\n", millis());
|
||||
WiFi.mode(WIFI_STA);
|
||||
|
||||
// Launch WiFi selection subactivity
|
||||
Serial.printf("[%lu] [OTA] Launching WifiSelectionActivity...\n", millis());
|
||||
enterNewActivity(new WifiSelectionActivity(renderer, inputManager,
|
||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
||||
}
|
||||
|
||||
void OtaUpdateActivity::onExit() {
|
||||
ActivityWithSubactivity::onExit();
|
||||
|
||||
// Turn off wifi
|
||||
WiFi.disconnect(false); // false = don't erase credentials, send disconnect frame
|
||||
delay(100); // Allow disconnect frame to be sent
|
||||
WiFi.mode(WIFI_OFF);
|
||||
delay(100); // Allow WiFi hardware to fully power down
|
||||
|
||||
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
vTaskDelete(displayTaskHandle);
|
||||
displayTaskHandle = nullptr;
|
||||
}
|
||||
vSemaphoreDelete(renderingMutex);
|
||||
renderingMutex = nullptr;
|
||||
}
|
||||
|
||||
void OtaUpdateActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
if (updateRequired) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void OtaUpdateActivity::render() {
|
||||
if (subActivity) {
|
||||
// Subactivity handles its own rendering
|
||||
return;
|
||||
}
|
||||
|
||||
float updaterProgress = 0;
|
||||
if (state == UPDATE_IN_PROGRESS) {
|
||||
Serial.printf("[%lu] [OTA] Update progress: %d / %d\n", millis(), updater.processedSize, updater.totalSize);
|
||||
updaterProgress = static_cast<float>(updater.processedSize) / static_cast<float>(updater.totalSize);
|
||||
// Only update every 2% at the most
|
||||
if (static_cast<int>(updaterProgress * 50) == lastUpdaterPercentage / 2) {
|
||||
return;
|
||||
}
|
||||
lastUpdaterPercentage = static_cast<int>(updaterProgress * 100);
|
||||
}
|
||||
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
|
||||
renderer.clearScreen();
|
||||
renderer.drawCenteredText(READER_FONT_ID, 10, "Update", true, BOLD);
|
||||
|
||||
if (state == CHECKING_FOR_UPDATE) {
|
||||
renderer.drawCenteredText(UI_FONT_ID, 300, "Checking for update...", true, BOLD);
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == WAITING_CONFIRMATION) {
|
||||
renderer.drawCenteredText(UI_FONT_ID, 200, "New update available!", true, BOLD);
|
||||
renderer.drawText(UI_FONT_ID, 20, 250, "Current Version: " CROSSPOINT_VERSION);
|
||||
renderer.drawText(UI_FONT_ID, 20, 270, ("New Version: " + updater.getLatestVersion()).c_str());
|
||||
|
||||
renderer.drawRect(25, pageHeight - 40, 106, 40);
|
||||
renderer.drawText(UI_FONT_ID, 25 + (105 - renderer.getTextWidth(UI_FONT_ID, "Cancel")) / 2, pageHeight - 35,
|
||||
"Cancel");
|
||||
|
||||
renderer.drawRect(130, pageHeight - 40, 106, 40);
|
||||
renderer.drawText(UI_FONT_ID, 130 + (105 - renderer.getTextWidth(UI_FONT_ID, "Update")) / 2, pageHeight - 35,
|
||||
"Update");
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == UPDATE_IN_PROGRESS) {
|
||||
renderer.drawCenteredText(UI_FONT_ID, 310, "Updating...", true, BOLD);
|
||||
renderer.drawRect(20, 350, pageWidth - 40, 50);
|
||||
renderer.fillRect(24, 354, static_cast<int>(updaterProgress * static_cast<float>(pageWidth - 44)), 42);
|
||||
renderer.drawCenteredText(UI_FONT_ID, 420, (std::to_string(static_cast<int>(updaterProgress * 100)) + "%").c_str());
|
||||
renderer.drawCenteredText(
|
||||
UI_FONT_ID, 440, (std::to_string(updater.processedSize) + " / " + std::to_string(updater.totalSize)).c_str());
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == NO_UPDATE) {
|
||||
renderer.drawCenteredText(UI_FONT_ID, 300, "No update available", true, BOLD);
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == FAILED) {
|
||||
renderer.drawCenteredText(UI_FONT_ID, 300, "Update failed", true, BOLD);
|
||||
renderer.displayBuffer();
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == FINISHED) {
|
||||
renderer.drawCenteredText(UI_FONT_ID, 300, "Update complete", true, BOLD);
|
||||
renderer.drawCenteredText(UI_FONT_ID, 350, "Press and hold power button to turn back on");
|
||||
renderer.displayBuffer();
|
||||
state = SHUTTING_DOWN;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void OtaUpdateActivity::loop() {
|
||||
if (subActivity) {
|
||||
subActivity->loop();
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == WAITING_CONFIRMATION) {
|
||||
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
Serial.printf("[%lu] [OTA] New update available, starting download...\n", millis());
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
state = UPDATE_IN_PROGRESS;
|
||||
xSemaphoreGive(renderingMutex);
|
||||
updateRequired = true;
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
const auto res = updater.installUpdate([this](const size_t, const size_t) { updateRequired = true; });
|
||||
|
||||
if (res != OtaUpdater::OK) {
|
||||
Serial.printf("[%lu] [OTA] Update failed: %d\n", millis(), res);
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
state = FAILED;
|
||||
xSemaphoreGive(renderingMutex);
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
state = FINISHED;
|
||||
xSemaphoreGive(renderingMutex);
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
goBack();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == FAILED) {
|
||||
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
goBack();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == NO_UPDATE) {
|
||||
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
goBack();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (state == SHUTTING_DOWN) {
|
||||
ESP.restart();
|
||||
}
|
||||
}
|
||||
43
src/activities/settings/OtaUpdateActivity.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include "activities/ActivityWithSubactivity.h"
|
||||
#include "network/OtaUpdater.h"
|
||||
|
||||
class OtaUpdateActivity : public ActivityWithSubactivity {
|
||||
enum State {
|
||||
WIFI_SELECTION,
|
||||
CHECKING_FOR_UPDATE,
|
||||
WAITING_CONFIRMATION,
|
||||
UPDATE_IN_PROGRESS,
|
||||
NO_UPDATE,
|
||||
FAILED,
|
||||
FINISHED,
|
||||
SHUTTING_DOWN
|
||||
};
|
||||
|
||||
// Can't initialize this to 0 or the first render doesn't happen
|
||||
static constexpr unsigned int UNINITIALIZED_PERCENTAGE = 111;
|
||||
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
bool updateRequired = false;
|
||||
const std::function<void()> goBack;
|
||||
State state = WIFI_SELECTION;
|
||||
unsigned int lastUpdaterPercentage = UNINITIALIZED_PERCENTAGE;
|
||||
OtaUpdater updater;
|
||||
|
||||
void onWifiSelectionComplete(bool success);
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render();
|
||||
|
||||
public:
|
||||
explicit OtaUpdateActivity(GfxRenderer& renderer, InputManager& inputManager, const std::function<void()>& goBack)
|
||||
: ActivityWithSubactivity("OtaUpdate", renderer, inputManager), goBack(goBack), updater() {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
};
|
||||
@@ -1,15 +1,28 @@
|
||||
#include "SettingsActivity.h"
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <InputManager.h>
|
||||
|
||||
#include "CrossPointSettings.h"
|
||||
#include "OtaUpdateActivity.h"
|
||||
#include "config.h"
|
||||
|
||||
// Define the static settings list
|
||||
|
||||
const SettingInfo SettingsActivity::settingsList[settingsCount] = {
|
||||
{"White Sleep Screen", &CrossPointSettings::whiteSleepScreen},
|
||||
{"Extra Paragraph Spacing", &CrossPointSettings::extraParagraphSpacing}};
|
||||
namespace {
|
||||
constexpr int settingsCount = 6;
|
||||
const SettingInfo settingsList[settingsCount] = {
|
||||
// Should match with SLEEP_SCREEN_MODE
|
||||
{"Sleep Screen", SettingType::ENUM, &CrossPointSettings::sleepScreen, {"Dark", "Light", "Custom", "Cover"}},
|
||||
{"Status Bar", SettingType::ENUM, &CrossPointSettings::statusBar, {"None", "No Progress", "Full"}},
|
||||
{"Extra Paragraph Spacing", SettingType::TOGGLE, &CrossPointSettings::extraParagraphSpacing, {}},
|
||||
{"Short Power Button Click", SettingType::TOGGLE, &CrossPointSettings::shortPwrBtn, {}},
|
||||
{"Reading Orientation",
|
||||
SettingType::ENUM,
|
||||
&CrossPointSettings::orientation,
|
||||
{"Portrait", "Landscape CW", "Inverted", "Landscape CCW"}},
|
||||
{"Check for updates", SettingType::ACTION, nullptr, {}},
|
||||
};
|
||||
} // namespace
|
||||
|
||||
void SettingsActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<SettingsActivity*>(param);
|
||||
@@ -17,6 +30,8 @@ void SettingsActivity::taskTrampoline(void* param) {
|
||||
}
|
||||
|
||||
void SettingsActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
// Reset selection to first item
|
||||
@@ -34,6 +49,8 @@ void SettingsActivity::onEnter() {
|
||||
}
|
||||
|
||||
void SettingsActivity::onExit() {
|
||||
ActivityWithSubactivity::onExit();
|
||||
|
||||
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
@@ -45,6 +62,11 @@ void SettingsActivity::onExit() {
|
||||
}
|
||||
|
||||
void SettingsActivity::loop() {
|
||||
if (subActivity) {
|
||||
subActivity->loop();
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle actions with early return
|
||||
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
toggleCurrentSetting();
|
||||
@@ -64,9 +86,11 @@ void SettingsActivity::loop() {
|
||||
selectedSettingIndex = (selectedSettingIndex > 0) ? (selectedSettingIndex - 1) : (settingsCount - 1);
|
||||
updateRequired = true;
|
||||
} else if (inputManager.wasPressed(InputManager::BTN_DOWN) || inputManager.wasPressed(InputManager::BTN_RIGHT)) {
|
||||
// Move selection down (with wrap-around)
|
||||
selectedSettingIndex = (selectedSettingIndex < settingsCount - 1) ? (selectedSettingIndex + 1) : 0;
|
||||
updateRequired = true;
|
||||
// Move selection down
|
||||
if (selectedSettingIndex < settingsCount - 1) {
|
||||
selectedSettingIndex++;
|
||||
updateRequired = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,9 +100,29 @@ void SettingsActivity::toggleCurrentSetting() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Toggle the boolean value using the member pointer
|
||||
bool currentValue = SETTINGS.*(settingsList[selectedSettingIndex].valuePtr);
|
||||
SETTINGS.*(settingsList[selectedSettingIndex].valuePtr) = !currentValue;
|
||||
const auto& setting = settingsList[selectedSettingIndex];
|
||||
|
||||
if (setting.type == SettingType::TOGGLE && setting.valuePtr != nullptr) {
|
||||
// Toggle the boolean value using the member pointer
|
||||
const bool currentValue = SETTINGS.*(setting.valuePtr);
|
||||
SETTINGS.*(setting.valuePtr) = !currentValue;
|
||||
} else if (setting.type == SettingType::ENUM && setting.valuePtr != nullptr) {
|
||||
const uint8_t currentValue = SETTINGS.*(setting.valuePtr);
|
||||
SETTINGS.*(setting.valuePtr) = (currentValue + 1) % static_cast<uint8_t>(setting.enumValues.size());
|
||||
} else if (setting.type == SettingType::ACTION) {
|
||||
if (std::string(setting.name) == "Check for updates") {
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
exitActivity();
|
||||
enterNewActivity(new OtaUpdateActivity(renderer, inputManager, [this] {
|
||||
exitActivity();
|
||||
updateRequired = true;
|
||||
}));
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
} else {
|
||||
// Only toggle if it's a toggle type and has a value pointer
|
||||
return;
|
||||
}
|
||||
|
||||
// Save settings when they change
|
||||
SETTINGS.saveToFile();
|
||||
@@ -86,7 +130,7 @@ void SettingsActivity::toggleCurrentSetting() {
|
||||
|
||||
void SettingsActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
if (updateRequired) {
|
||||
if (updateRequired && !subActivity) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
@@ -99,14 +143,12 @@ void SettingsActivity::displayTaskLoop() {
|
||||
void SettingsActivity::render() const {
|
||||
renderer.clearScreen();
|
||||
|
||||
const auto pageWidth = GfxRenderer::getScreenWidth();
|
||||
const auto pageHeight = GfxRenderer::getScreenHeight();
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
|
||||
// Draw header
|
||||
renderer.drawCenteredText(READER_FONT_ID, 10, "Settings", true, BOLD);
|
||||
|
||||
// We always have at least one setting
|
||||
|
||||
// Draw all settings
|
||||
for (int i = 0; i < settingsCount; i++) {
|
||||
const int settingY = 60 + i * 30; // 30 pixels between settings
|
||||
@@ -116,14 +158,25 @@ void SettingsActivity::render() const {
|
||||
renderer.drawText(UI_FONT_ID, 5, settingY, ">");
|
||||
}
|
||||
|
||||
// Draw setting name and value
|
||||
// Draw setting name
|
||||
renderer.drawText(UI_FONT_ID, 20, settingY, settingsList[i].name);
|
||||
bool value = SETTINGS.*(settingsList[i].valuePtr);
|
||||
renderer.drawText(UI_FONT_ID, pageWidth - 80, settingY, value ? "ON" : "OFF");
|
||||
|
||||
// Draw value based on setting type
|
||||
if (settingsList[i].type == SettingType::TOGGLE && settingsList[i].valuePtr != nullptr) {
|
||||
const bool value = SETTINGS.*(settingsList[i].valuePtr);
|
||||
renderer.drawText(UI_FONT_ID, pageWidth - 80, settingY, value ? "ON" : "OFF");
|
||||
} else if (settingsList[i].type == SettingType::ENUM && settingsList[i].valuePtr != nullptr) {
|
||||
const uint8_t value = SETTINGS.*(settingsList[i].valuePtr);
|
||||
auto valueText = settingsList[i].enumValues[value];
|
||||
const auto width = renderer.getTextWidth(UI_FONT_ID, valueText.c_str());
|
||||
renderer.drawText(UI_FONT_ID, pageWidth - 50 - width, settingY, valueText.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// Draw help text
|
||||
renderer.drawText(SMALL_FONT_ID, 20, pageHeight - 30, "Press OK to toggle, BACK to save & exit");
|
||||
renderer.drawButtonHints(UI_FONT_ID, "« Save", "Toggle", "", "");
|
||||
renderer.drawText(SMALL_FONT_ID, pageWidth - 20 - renderer.getTextWidth(SMALL_FONT_ID, CROSSPOINT_VERSION),
|
||||
pageHeight - 30, CROSSPOINT_VERSION);
|
||||
|
||||
// Always use standard refresh for settings screen
|
||||
renderer.displayBuffer();
|
||||
|
||||
@@ -3,31 +3,31 @@
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../Activity.h"
|
||||
#include "activities/ActivityWithSubactivity.h"
|
||||
|
||||
class CrossPointSettings;
|
||||
|
||||
enum class SettingType { TOGGLE, ENUM, ACTION };
|
||||
|
||||
// Structure to hold setting information
|
||||
struct SettingInfo {
|
||||
const char* name; // Display name of the setting
|
||||
uint8_t CrossPointSettings::* valuePtr; // Pointer to member in CrossPointSettings
|
||||
SettingType type; // Type of setting
|
||||
uint8_t CrossPointSettings::* valuePtr; // Pointer to member in CrossPointSettings (for TOGGLE/ENUM)
|
||||
std::vector<std::string> enumValues;
|
||||
};
|
||||
|
||||
class SettingsActivity final : public Activity {
|
||||
class SettingsActivity final : public ActivityWithSubactivity {
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
bool updateRequired = false;
|
||||
int selectedSettingIndex = 0; // Currently selected setting
|
||||
const std::function<void()> onGoHome;
|
||||
|
||||
// Static settings list
|
||||
static constexpr int settingsCount = 2; // Number of settings
|
||||
static const SettingInfo settingsList[settingsCount];
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
void render() const;
|
||||
@@ -35,7 +35,7 @@ class SettingsActivity final : public Activity {
|
||||
|
||||
public:
|
||||
explicit SettingsActivity(GfxRenderer& renderer, InputManager& inputManager, const std::function<void()>& onGoHome)
|
||||
: Activity(renderer, inputManager), onGoHome(onGoHome) {}
|
||||
: ActivityWithSubactivity("Settings", renderer, inputManager), onGoHome(onGoHome) {}
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
|
||||
@@ -5,8 +5,10 @@
|
||||
#include "config.h"
|
||||
|
||||
void FullScreenMessageActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
const auto height = renderer.getLineHeight(UI_FONT_ID);
|
||||
const auto top = (GfxRenderer::getScreenHeight() - height) / 2;
|
||||
const auto top = (renderer.getScreenHeight() - height) / 2;
|
||||
|
||||
renderer.clearScreen();
|
||||
renderer.drawCenteredText(UI_FONT_ID, top, text.c_str(), true, style);
|
||||
|
||||
@@ -16,6 +16,9 @@ class FullScreenMessageActivity final : public Activity {
|
||||
explicit FullScreenMessageActivity(GfxRenderer& renderer, InputManager& inputManager, std::string text,
|
||||
const EpdFontStyle style = REGULAR,
|
||||
const EInkDisplay::RefreshMode refreshMode = EInkDisplay::FAST_REFRESH)
|
||||
: Activity(renderer, inputManager), text(std::move(text)), style(style), refreshMode(refreshMode) {}
|
||||
: Activity("FullScreenMessage", renderer, inputManager),
|
||||
text(std::move(text)),
|
||||
style(style),
|
||||
refreshMode(refreshMode) {}
|
||||
void onEnter() override;
|
||||
};
|
||||
|
||||
345
src/activities/util/KeyboardEntryActivity.cpp
Normal file
@@ -0,0 +1,345 @@
|
||||
#include "KeyboardEntryActivity.h"
|
||||
|
||||
#include "../../config.h"
|
||||
|
||||
// Keyboard layouts - lowercase
|
||||
const char* const KeyboardEntryActivity::keyboard[NUM_ROWS] = {
|
||||
"`1234567890-=", "qwertyuiop[]\\", "asdfghjkl;'", "zxcvbnm,./",
|
||||
"^ _____<OK" // ^ = shift, _ = space, < = backspace, OK = done
|
||||
};
|
||||
|
||||
// Keyboard layouts - uppercase/symbols
|
||||
const char* const KeyboardEntryActivity::keyboardShift[NUM_ROWS] = {"~!@#$%^&*()_+", "QWERTYUIOP{}|", "ASDFGHJKL:\"",
|
||||
"ZXCVBNM<>?", "SPECIAL ROW"};
|
||||
|
||||
void KeyboardEntryActivity::taskTrampoline(void* param) {
|
||||
auto* self = static_cast<KeyboardEntryActivity*>(param);
|
||||
self->displayTaskLoop();
|
||||
}
|
||||
|
||||
void KeyboardEntryActivity::displayTaskLoop() {
|
||||
while (true) {
|
||||
if (updateRequired) {
|
||||
updateRequired = false;
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
render();
|
||||
xSemaphoreGive(renderingMutex);
|
||||
}
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardEntryActivity::onEnter() {
|
||||
Activity::onEnter();
|
||||
|
||||
renderingMutex = xSemaphoreCreateMutex();
|
||||
|
||||
// Trigger first update
|
||||
updateRequired = true;
|
||||
|
||||
xTaskCreate(&KeyboardEntryActivity::taskTrampoline, "KeyboardEntryActivity",
|
||||
2048, // Stack size
|
||||
this, // Parameters
|
||||
1, // Priority
|
||||
&displayTaskHandle // Task handle
|
||||
);
|
||||
}
|
||||
|
||||
void KeyboardEntryActivity::onExit() {
|
||||
Activity::onExit();
|
||||
|
||||
// Wait until not rendering to delete task to avoid killing mid-instruction to EPD
|
||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||
if (displayTaskHandle) {
|
||||
vTaskDelete(displayTaskHandle);
|
||||
displayTaskHandle = nullptr;
|
||||
}
|
||||
vSemaphoreDelete(renderingMutex);
|
||||
renderingMutex = nullptr;
|
||||
}
|
||||
|
||||
int KeyboardEntryActivity::getRowLength(const int row) const {
|
||||
if (row < 0 || row >= NUM_ROWS) return 0;
|
||||
|
||||
// Return actual length of each row based on keyboard layout
|
||||
switch (row) {
|
||||
case 0:
|
||||
return 13; // `1234567890-=
|
||||
case 1:
|
||||
return 13; // qwertyuiop[]backslash
|
||||
case 2:
|
||||
return 11; // asdfghjkl;'
|
||||
case 3:
|
||||
return 10; // zxcvbnm,./
|
||||
case 4:
|
||||
return 10; // caps (2 wide), space (5 wide), backspace (2 wide), OK
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
char KeyboardEntryActivity::getSelectedChar() const {
|
||||
const char* const* layout = shiftActive ? keyboardShift : keyboard;
|
||||
|
||||
if (selectedRow < 0 || selectedRow >= NUM_ROWS) return '\0';
|
||||
if (selectedCol < 0 || selectedCol >= getRowLength(selectedRow)) return '\0';
|
||||
|
||||
return layout[selectedRow][selectedCol];
|
||||
}
|
||||
|
||||
void KeyboardEntryActivity::handleKeyPress() {
|
||||
// Handle special row (bottom row with shift, space, backspace, done)
|
||||
if (selectedRow == SPECIAL_ROW) {
|
||||
if (selectedCol >= SHIFT_COL && selectedCol < SPACE_COL) {
|
||||
// Shift toggle
|
||||
shiftActive = !shiftActive;
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedCol >= SPACE_COL && selectedCol < BACKSPACE_COL) {
|
||||
// Space bar
|
||||
if (maxLength == 0 || text.length() < maxLength) {
|
||||
text += ' ';
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedCol >= BACKSPACE_COL && selectedCol < DONE_COL) {
|
||||
// Backspace
|
||||
if (!text.empty()) {
|
||||
text.pop_back();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedCol >= DONE_COL) {
|
||||
// Done button
|
||||
if (onComplete) {
|
||||
onComplete(text);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Regular character
|
||||
const char c = getSelectedChar();
|
||||
if (c == '\0') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (maxLength == 0 || text.length() < maxLength) {
|
||||
text += c;
|
||||
// Auto-disable shift after typing a letter
|
||||
if (shiftActive && ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))) {
|
||||
shiftActive = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardEntryActivity::loop() {
|
||||
// Navigation
|
||||
if (inputManager.wasPressed(InputManager::BTN_UP)) {
|
||||
if (selectedRow > 0) {
|
||||
selectedRow--;
|
||||
// Clamp column to valid range for new row
|
||||
const int maxCol = getRowLength(selectedRow) - 1;
|
||||
if (selectedCol > maxCol) selectedCol = maxCol;
|
||||
}
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
if (inputManager.wasPressed(InputManager::BTN_DOWN)) {
|
||||
if (selectedRow < NUM_ROWS - 1) {
|
||||
selectedRow++;
|
||||
const int maxCol = getRowLength(selectedRow) - 1;
|
||||
if (selectedCol > maxCol) selectedCol = maxCol;
|
||||
}
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
if (inputManager.wasPressed(InputManager::BTN_LEFT)) {
|
||||
// Special bottom row case
|
||||
if (selectedRow == SPECIAL_ROW) {
|
||||
// Bottom row has special key widths
|
||||
if (selectedCol >= SHIFT_COL && selectedCol < SPACE_COL) {
|
||||
// In shift key, do nothing
|
||||
} else if (selectedCol >= SPACE_COL && selectedCol < BACKSPACE_COL) {
|
||||
// In space bar, move to shift
|
||||
selectedCol = SHIFT_COL;
|
||||
} else if (selectedCol >= BACKSPACE_COL && selectedCol < DONE_COL) {
|
||||
// In backspace, move to space
|
||||
selectedCol = SPACE_COL;
|
||||
} else if (selectedCol >= DONE_COL) {
|
||||
// At done button, move to backspace
|
||||
selectedCol = BACKSPACE_COL;
|
||||
}
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedCol > 0) {
|
||||
selectedCol--;
|
||||
} else if (selectedRow > 0) {
|
||||
// Wrap to previous row
|
||||
selectedRow--;
|
||||
selectedCol = getRowLength(selectedRow) - 1;
|
||||
}
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
if (inputManager.wasPressed(InputManager::BTN_RIGHT)) {
|
||||
const int maxCol = getRowLength(selectedRow) - 1;
|
||||
|
||||
// Special bottom row case
|
||||
if (selectedRow == SPECIAL_ROW) {
|
||||
// Bottom row has special key widths
|
||||
if (selectedCol >= SHIFT_COL && selectedCol < SPACE_COL) {
|
||||
// In shift key, move to space
|
||||
selectedCol = SPACE_COL;
|
||||
} else if (selectedCol >= SPACE_COL && selectedCol < BACKSPACE_COL) {
|
||||
// In space bar, move to backspace
|
||||
selectedCol = BACKSPACE_COL;
|
||||
} else if (selectedCol >= BACKSPACE_COL && selectedCol < DONE_COL) {
|
||||
// In backspace, move to done
|
||||
selectedCol = DONE_COL;
|
||||
} else if (selectedCol >= DONE_COL) {
|
||||
// At done button, do nothing
|
||||
}
|
||||
updateRequired = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (selectedCol < maxCol) {
|
||||
selectedCol++;
|
||||
} else if (selectedRow < NUM_ROWS - 1) {
|
||||
// Wrap to next row
|
||||
selectedRow++;
|
||||
selectedCol = 0;
|
||||
}
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
// Selection
|
||||
if (inputManager.wasPressed(InputManager::BTN_CONFIRM)) {
|
||||
handleKeyPress();
|
||||
updateRequired = true;
|
||||
}
|
||||
|
||||
// Cancel
|
||||
if (inputManager.wasPressed(InputManager::BTN_BACK)) {
|
||||
if (onCancel) {
|
||||
onCancel();
|
||||
}
|
||||
updateRequired = true;
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardEntryActivity::render() const {
|
||||
const auto pageWidth = renderer.getScreenWidth();
|
||||
|
||||
renderer.clearScreen();
|
||||
|
||||
// Draw title
|
||||
renderer.drawCenteredText(UI_FONT_ID, startY, title.c_str(), true, REGULAR);
|
||||
|
||||
// Draw input field
|
||||
const int inputY = startY + 22;
|
||||
renderer.drawText(UI_FONT_ID, 10, inputY, "[");
|
||||
|
||||
std::string displayText;
|
||||
if (isPassword) {
|
||||
displayText = std::string(text.length(), '*');
|
||||
} else {
|
||||
displayText = text;
|
||||
}
|
||||
|
||||
// Show cursor at end
|
||||
displayText += "_";
|
||||
|
||||
// Truncate if too long for display - use actual character width from font
|
||||
int approxCharWidth = renderer.getSpaceWidth(UI_FONT_ID);
|
||||
if (approxCharWidth < 1) approxCharWidth = 8; // Fallback to approximate width
|
||||
const int maxDisplayLen = (pageWidth - 40) / approxCharWidth;
|
||||
if (displayText.length() > static_cast<size_t>(maxDisplayLen)) {
|
||||
displayText = "..." + displayText.substr(displayText.length() - maxDisplayLen + 3);
|
||||
}
|
||||
|
||||
renderer.drawText(UI_FONT_ID, 20, inputY, displayText.c_str());
|
||||
renderer.drawText(UI_FONT_ID, pageWidth - 15, inputY, "]");
|
||||
|
||||
// Draw keyboard - use compact spacing to fit 5 rows on screen
|
||||
const int keyboardStartY = inputY + 25;
|
||||
constexpr int keyWidth = 18;
|
||||
constexpr int keyHeight = 18;
|
||||
constexpr int keySpacing = 3;
|
||||
|
||||
const char* const* layout = shiftActive ? keyboardShift : keyboard;
|
||||
|
||||
// Calculate left margin to center the longest row (13 keys)
|
||||
constexpr int maxRowWidth = KEYS_PER_ROW * (keyWidth + keySpacing);
|
||||
const int leftMargin = (pageWidth - maxRowWidth) / 2;
|
||||
|
||||
for (int row = 0; row < NUM_ROWS; row++) {
|
||||
const int rowY = keyboardStartY + row * (keyHeight + keySpacing);
|
||||
|
||||
// Left-align all rows for consistent navigation
|
||||
const int startX = leftMargin;
|
||||
|
||||
// Handle bottom row (row 4) specially with proper multi-column keys
|
||||
if (row == 4) {
|
||||
// Bottom row layout: CAPS (2 cols) | SPACE (5 cols) | <- (2 cols) | OK (2 cols)
|
||||
// Total: 11 visual columns, but we use logical positions for selection
|
||||
|
||||
int currentX = startX;
|
||||
|
||||
// CAPS key (logical col 0, spans 2 key widths)
|
||||
const bool capsSelected = (selectedRow == 4 && selectedCol >= SHIFT_COL && selectedCol < SPACE_COL);
|
||||
renderItemWithSelector(currentX + 2, rowY, shiftActive ? "CAPS" : "caps", capsSelected);
|
||||
currentX += 2 * (keyWidth + keySpacing);
|
||||
|
||||
// Space bar (logical cols 2-6, spans 5 key widths)
|
||||
const bool spaceSelected = (selectedRow == 4 && selectedCol >= SPACE_COL && selectedCol < BACKSPACE_COL);
|
||||
const int spaceTextWidth = renderer.getTextWidth(UI_FONT_ID, "_____");
|
||||
const int spaceXWidth = 5 * (keyWidth + keySpacing);
|
||||
const int spaceXPos = currentX + (spaceXWidth - spaceTextWidth) / 2;
|
||||
renderItemWithSelector(spaceXPos, rowY, "_____", spaceSelected);
|
||||
currentX += spaceXWidth;
|
||||
|
||||
// Backspace key (logical col 7, spans 2 key widths)
|
||||
const bool bsSelected = (selectedRow == 4 && selectedCol >= BACKSPACE_COL && selectedCol < DONE_COL);
|
||||
renderItemWithSelector(currentX + 2, rowY, "<-", bsSelected);
|
||||
currentX += 2 * (keyWidth + keySpacing);
|
||||
|
||||
// OK button (logical col 9, spans 2 key widths)
|
||||
const bool okSelected = (selectedRow == 4 && selectedCol >= DONE_COL);
|
||||
renderItemWithSelector(currentX + 2, rowY, "OK", okSelected);
|
||||
} else {
|
||||
// Regular rows: render each key individually
|
||||
for (int col = 0; col < getRowLength(row); col++) {
|
||||
// Get the character to display
|
||||
const char c = layout[row][col];
|
||||
std::string keyLabel(1, c);
|
||||
const int charWidth = renderer.getTextWidth(UI_FONT_ID, keyLabel.c_str());
|
||||
|
||||
const int keyX = startX + col * (keyWidth + keySpacing) + (keyWidth - charWidth) / 2;
|
||||
const bool isSelected = row == selectedRow && col == selectedCol;
|
||||
renderItemWithSelector(keyX, rowY, keyLabel.c_str(), isSelected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw help text at absolute bottom of screen (consistent with other screens)
|
||||
const auto pageHeight = renderer.getScreenHeight();
|
||||
renderer.drawText(SMALL_FONT_ID, 10, pageHeight - 30, "Navigate: D-pad | Select: OK | Cancel: BACK");
|
||||
renderer.displayBuffer();
|
||||
}
|
||||
|
||||
void KeyboardEntryActivity::renderItemWithSelector(const int x, const int y, const char* item,
|
||||
const bool isSelected) const {
|
||||
if (isSelected) {
|
||||
const int itemWidth = renderer.getTextWidth(UI_FONT_ID, item);
|
||||
renderer.drawText(UI_FONT_ID, x - 6, y, "[");
|
||||
renderer.drawText(UI_FONT_ID, x + itemWidth, y, "]");
|
||||
}
|
||||
renderer.drawText(UI_FONT_ID, x, y, item);
|
||||
}
|
||||
100
src/activities/util/KeyboardEntryActivity.h
Normal file
@@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
#include <GfxRenderer.h>
|
||||
#include <InputManager.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../Activity.h"
|
||||
|
||||
/**
|
||||
* Reusable keyboard entry activity for text input.
|
||||
* Can be started from any activity that needs text entry.
|
||||
*
|
||||
* Usage:
|
||||
* 1. Create a KeyboardEntryActivity instance
|
||||
* 2. Set callbacks with setOnComplete() and setOnCancel()
|
||||
* 3. Call onEnter() to start the activity
|
||||
* 4. Call loop() in your main loop
|
||||
* 5. When complete or cancelled, callbacks will be invoked
|
||||
*/
|
||||
class KeyboardEntryActivity : public Activity {
|
||||
public:
|
||||
// Callback types
|
||||
using OnCompleteCallback = std::function<void(const std::string&)>;
|
||||
using OnCancelCallback = std::function<void()>;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param renderer Reference to the GfxRenderer for drawing
|
||||
* @param inputManager Reference to InputManager for handling input
|
||||
* @param title Title to display above the keyboard
|
||||
* @param initialText Initial text to show in the input field
|
||||
* @param startY Y position to start rendering the keyboard
|
||||
* @param maxLength Maximum length of input text (0 for unlimited)
|
||||
* @param isPassword If true, display asterisks instead of actual characters
|
||||
* @param onComplete Callback invoked when input is complete
|
||||
* @param onCancel Callback invoked when input is cancelled
|
||||
*/
|
||||
explicit KeyboardEntryActivity(GfxRenderer& renderer, InputManager& inputManager, std::string title = "Enter Text",
|
||||
std::string initialText = "", const int startY = 10, const size_t maxLength = 0,
|
||||
const bool isPassword = false, OnCompleteCallback onComplete = nullptr,
|
||||
OnCancelCallback onCancel = nullptr)
|
||||
: Activity("KeyboardEntry", renderer, inputManager),
|
||||
title(std::move(title)),
|
||||
text(std::move(initialText)),
|
||||
startY(startY),
|
||||
maxLength(maxLength),
|
||||
isPassword(isPassword),
|
||||
onComplete(std::move(onComplete)),
|
||||
onCancel(std::move(onCancel)) {}
|
||||
|
||||
// Activity overrides
|
||||
void onEnter() override;
|
||||
void onExit() override;
|
||||
void loop() override;
|
||||
|
||||
private:
|
||||
std::string title;
|
||||
int startY;
|
||||
std::string text;
|
||||
size_t maxLength;
|
||||
bool isPassword;
|
||||
TaskHandle_t displayTaskHandle = nullptr;
|
||||
SemaphoreHandle_t renderingMutex = nullptr;
|
||||
bool updateRequired = false;
|
||||
|
||||
// Keyboard state
|
||||
int selectedRow = 0;
|
||||
int selectedCol = 0;
|
||||
bool shiftActive = false;
|
||||
|
||||
// Callbacks
|
||||
OnCompleteCallback onComplete;
|
||||
OnCancelCallback onCancel;
|
||||
|
||||
// Keyboard layout
|
||||
static constexpr int NUM_ROWS = 5;
|
||||
static constexpr int KEYS_PER_ROW = 13; // Max keys per row (rows 0 and 1 have 13 keys)
|
||||
static const char* const keyboard[NUM_ROWS];
|
||||
static const char* const keyboardShift[NUM_ROWS];
|
||||
|
||||
// Special key positions (bottom row)
|
||||
static constexpr int SPECIAL_ROW = 4;
|
||||
static constexpr int SHIFT_COL = 0;
|
||||
static constexpr int SPACE_COL = 2;
|
||||
static constexpr int BACKSPACE_COL = 7;
|
||||
static constexpr int DONE_COL = 9;
|
||||
|
||||
static void taskTrampoline(void* param);
|
||||
[[noreturn]] void displayTaskLoop();
|
||||
char getSelectedChar() const;
|
||||
void handleKeyPress();
|
||||
int getRowLength(int row) const;
|
||||
void render() const;
|
||||
void renderItemWithSelector(int x, int y, const char* item, bool isSelected) const;
|
||||
};
|
||||
@@ -26,4 +26,4 @@
|
||||
* "./lib/EpdFont/builtinFonts/pixelarial14.h",
|
||||
* ].map{|f| Digest::SHA256.hexdigest(File.read(f)).to_i(16) }.sum % (2 ** 32) - (2 ** 31)'
|
||||
*/
|
||||
#define SMALL_FONT_ID (-139796914)
|
||||
#define SMALL_FONT_ID 1482513144
|
||||
|
||||
131
src/main.cpp
@@ -19,6 +19,7 @@
|
||||
#include "activities/boot_sleep/BootActivity.h"
|
||||
#include "activities/boot_sleep/SleepActivity.h"
|
||||
#include "activities/home/HomeActivity.h"
|
||||
#include "activities/network/CrossPointWebServerActivity.h"
|
||||
#include "activities/reader/ReaderActivity.h"
|
||||
#include "activities/settings/SettingsActivity.h"
|
||||
#include "activities/util/FullScreenMessageActivity.h"
|
||||
@@ -57,18 +58,17 @@ EpdFont ubuntu10Font(&ubuntu_10);
|
||||
EpdFont ubuntuBold10Font(&ubuntu_bold_10);
|
||||
EpdFontFamily ubuntuFontFamily(&ubuntu10Font, &ubuntuBold10Font);
|
||||
|
||||
// Power button timing
|
||||
// Time required to confirm boot from sleep
|
||||
constexpr unsigned long POWER_BUTTON_WAKEUP_MS = 500;
|
||||
// Time required to enter sleep mode
|
||||
constexpr unsigned long POWER_BUTTON_SLEEP_MS = 500;
|
||||
// Auto-sleep timeout (10 minutes of inactivity)
|
||||
constexpr unsigned long AUTO_SLEEP_TIMEOUT_MS = 10 * 60 * 1000;
|
||||
// measurement of power button press duration calibration value
|
||||
unsigned long t1 = 0;
|
||||
unsigned long t2 = 0;
|
||||
|
||||
void exitActivity() {
|
||||
if (currentActivity) {
|
||||
currentActivity->onExit();
|
||||
delete currentActivity;
|
||||
currentActivity = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,23 +79,28 @@ void enterNewActivity(Activity* activity) {
|
||||
|
||||
// Verify long press on wake-up from deep sleep
|
||||
void verifyWakeupLongPress() {
|
||||
// Give the user up to 1000ms to start holding the power button, and must hold for POWER_BUTTON_WAKEUP_MS
|
||||
// Give the user up to 1000ms to start holding the power button, and must hold for SETTINGS.getPowerButtonDuration()
|
||||
const auto start = millis();
|
||||
bool abort = false;
|
||||
// It takes us some time to wake up from deep sleep, so we need to subtract that from the duration
|
||||
uint16_t calibration = 25;
|
||||
uint16_t calibratedPressDuration =
|
||||
(calibration < SETTINGS.getPowerButtonDuration()) ? SETTINGS.getPowerButtonDuration() - calibration : 1;
|
||||
|
||||
Serial.printf("[%lu] [ ] Verifying power button press\n", millis());
|
||||
inputManager.update();
|
||||
// Verify the user has actually pressed
|
||||
while (!inputManager.isPressed(InputManager::BTN_POWER) && millis() - start < 1000) {
|
||||
delay(50);
|
||||
delay(10); // only wait 10ms each iteration to not delay too much in case of short configured duration.
|
||||
inputManager.update();
|
||||
}
|
||||
|
||||
t2 = millis();
|
||||
if (inputManager.isPressed(InputManager::BTN_POWER)) {
|
||||
do {
|
||||
delay(50);
|
||||
delay(10);
|
||||
inputManager.update();
|
||||
} while (inputManager.isPressed(InputManager::BTN_POWER) && inputManager.getHeldTime() < POWER_BUTTON_WAKEUP_MS);
|
||||
abort = inputManager.getHeldTime() < POWER_BUTTON_WAKEUP_MS;
|
||||
} while (inputManager.isPressed(InputManager::BTN_POWER) && inputManager.getHeldTime() < calibratedPressDuration);
|
||||
abort = inputManager.getHeldTime() < calibratedPressDuration;
|
||||
} else {
|
||||
abort = true;
|
||||
}
|
||||
@@ -121,14 +126,12 @@ void enterDeepSleep() {
|
||||
exitActivity();
|
||||
enterNewActivity(new SleepActivity(renderer, inputManager));
|
||||
|
||||
Serial.printf("[%lu] [ ] Power button released after a long press. Entering deep sleep.\n", millis());
|
||||
delay(1000); // Allow Serial buffer to empty and display to update
|
||||
|
||||
// Enable Wakeup on LOW (button press)
|
||||
esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
|
||||
|
||||
einkDisplay.deepSleep();
|
||||
|
||||
Serial.printf("[%lu] [ ] Power button press calibration value: %lu ms\n", millis(), t2 - t1);
|
||||
Serial.printf("[%lu] [ ] Entering deep sleep.\n", millis());
|
||||
esp_deep_sleep_enable_gpio_wakeup(1ULL << InputManager::POWER_BUTTON_PIN, ESP_GPIO_WAKEUP_GPIO_LOW);
|
||||
// Ensure that the power button has been released to avoid immediately turning back on if you're holding it
|
||||
waitForPowerRelease();
|
||||
// Enter Deep Sleep
|
||||
esp_deep_sleep_start();
|
||||
}
|
||||
@@ -139,6 +142,12 @@ void onGoToReader(const std::string& initialEpubPath) {
|
||||
enterNewActivity(new ReaderActivity(renderer, inputManager, initialEpubPath, onGoHome));
|
||||
}
|
||||
void onGoToReaderHome() { onGoToReader(std::string()); }
|
||||
void onContinueReading() { onGoToReader(APP_STATE.openEpubPath); }
|
||||
|
||||
void onGoToFileTransfer() {
|
||||
exitActivity();
|
||||
enterNewActivity(new CrossPointWebServerActivity(renderer, inputManager, onGoHome));
|
||||
}
|
||||
|
||||
void onGoToSettings() {
|
||||
exitActivity();
|
||||
@@ -147,44 +156,66 @@ void onGoToSettings() {
|
||||
|
||||
void onGoHome() {
|
||||
exitActivity();
|
||||
enterNewActivity(new HomeActivity(renderer, inputManager, onGoToReaderHome, onGoToSettings));
|
||||
enterNewActivity(new HomeActivity(renderer, inputManager, onContinueReading, onGoToReaderHome, onGoToSettings,
|
||||
onGoToFileTransfer));
|
||||
}
|
||||
|
||||
void setupDisplayAndFonts() {
|
||||
einkDisplay.begin();
|
||||
Serial.printf("[%lu] [ ] Display initialized\n", millis());
|
||||
renderer.insertFont(READER_FONT_ID, bookerlyFontFamily);
|
||||
renderer.insertFont(UI_FONT_ID, ubuntuFontFamily);
|
||||
renderer.insertFont(SMALL_FONT_ID, smallFontFamily);
|
||||
Serial.printf("[%lu] [ ] Fonts setup\n", millis());
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
t1 = millis();
|
||||
|
||||
// Only start serial if USB connected
|
||||
pinMode(UART0_RXD, INPUT);
|
||||
if (digitalRead(UART0_RXD) == HIGH) {
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [ ] Starting CrossPoint version " CROSSPOINT_VERSION "\n", millis());
|
||||
|
||||
inputManager.begin();
|
||||
verifyWakeupLongPress();
|
||||
|
||||
// Initialize pins
|
||||
pinMode(BAT_GPIO0, INPUT);
|
||||
|
||||
// Initialize SPI with custom pins
|
||||
SPI.begin(EPD_SCLK, SD_SPI_MISO, EPD_MOSI, EPD_CS);
|
||||
|
||||
// Initialize display
|
||||
einkDisplay.begin();
|
||||
Serial.printf("[%lu] [ ] Display initialized\n", millis());
|
||||
// SD Card Initialization
|
||||
// We need 6 open files concurrently when parsing a new chapter
|
||||
if (!SD.begin(SD_SPI_CS, SPI, SPI_FQ, "/sd", 6)) {
|
||||
Serial.printf("[%lu] [ ] SD card initialization failed\n", millis());
|
||||
setupDisplayAndFonts();
|
||||
exitActivity();
|
||||
enterNewActivity(new FullScreenMessageActivity(renderer, inputManager, "SD card error", BOLD));
|
||||
return;
|
||||
}
|
||||
|
||||
renderer.insertFont(READER_FONT_ID, bookerlyFontFamily);
|
||||
renderer.insertFont(UI_FONT_ID, ubuntuFontFamily);
|
||||
renderer.insertFont(SMALL_FONT_ID, smallFontFamily);
|
||||
Serial.printf("[%lu] [ ] Fonts setup\n", millis());
|
||||
SETTINGS.loadFromFile();
|
||||
|
||||
// verify power button press duration after we've read settings.
|
||||
verifyWakeupLongPress();
|
||||
|
||||
setupDisplayAndFonts();
|
||||
|
||||
exitActivity();
|
||||
enterNewActivity(new BootActivity(renderer, inputManager));
|
||||
|
||||
// SD Card Initialization
|
||||
SD.begin(SD_SPI_CS, SPI, SPI_FQ);
|
||||
|
||||
SETTINGS.loadFromFile();
|
||||
APP_STATE.loadFromFile();
|
||||
if (APP_STATE.openEpubPath.empty()) {
|
||||
onGoHome();
|
||||
} else {
|
||||
onGoToReader(APP_STATE.openEpubPath);
|
||||
// Clear app state to avoid getting into a boot loop if the epub doesn't load
|
||||
const auto path = APP_STATE.openEpubPath;
|
||||
APP_STATE.openEpubPath = "";
|
||||
APP_STATE.saveToFile();
|
||||
onGoToReader(path);
|
||||
}
|
||||
|
||||
// Ensure we're not still holding the power button before leaving setup
|
||||
@@ -192,17 +223,18 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(10);
|
||||
|
||||
static unsigned long maxLoopDuration = 0;
|
||||
const unsigned long loopStartTime = millis();
|
||||
static unsigned long lastMemPrint = 0;
|
||||
|
||||
inputManager.update();
|
||||
|
||||
if (Serial && millis() - lastMemPrint >= 10000) {
|
||||
Serial.printf("[%lu] [MEM] Free: %d bytes, Total: %d bytes, Min Free: %d bytes\n", millis(), ESP.getFreeHeap(),
|
||||
ESP.getHeapSize(), ESP.getMinFreeHeap());
|
||||
lastMemPrint = millis();
|
||||
}
|
||||
|
||||
inputManager.update();
|
||||
|
||||
// Check for any user activity (button press or release)
|
||||
static unsigned long lastActivityTime = millis();
|
||||
if (inputManager.wasAnyPressed() || inputManager.wasAnyReleased()) {
|
||||
@@ -216,13 +248,34 @@ void loop() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (inputManager.wasReleased(InputManager::BTN_POWER) && inputManager.getHeldTime() > POWER_BUTTON_SLEEP_MS) {
|
||||
if (inputManager.isPressed(InputManager::BTN_POWER) &&
|
||||
inputManager.getHeldTime() > SETTINGS.getPowerButtonDuration()) {
|
||||
enterDeepSleep();
|
||||
// This should never be hit as `enterDeepSleep` calls esp_deep_sleep_start
|
||||
return;
|
||||
}
|
||||
|
||||
const unsigned long activityStartTime = millis();
|
||||
if (currentActivity) {
|
||||
currentActivity->loop();
|
||||
}
|
||||
const unsigned long activityDuration = millis() - activityStartTime;
|
||||
|
||||
const unsigned long loopDuration = millis() - loopStartTime;
|
||||
if (loopDuration > maxLoopDuration) {
|
||||
maxLoopDuration = loopDuration;
|
||||
if (maxLoopDuration > 50) {
|
||||
Serial.printf("[%lu] [LOOP] New max loop duration: %lu ms (activity: %lu ms)\n", millis(), maxLoopDuration,
|
||||
activityDuration);
|
||||
}
|
||||
}
|
||||
|
||||
// Add delay at the end of the loop to prevent tight spinning
|
||||
// When an activity requests skip loop delay (e.g., webserver running), use yield() for faster response
|
||||
// Otherwise, use longer delay to save power
|
||||
if (currentActivity && currentActivity->skipLoopDelay()) {
|
||||
yield(); // Give FreeRTOS a chance to run tasks, but return immediately
|
||||
} else {
|
||||
delay(10); // Normal delay when no activity requires fast response
|
||||
}
|
||||
}
|
||||
|
||||
542
src/network/CrossPointWebServer.cpp
Normal file
@@ -0,0 +1,542 @@
|
||||
#include "CrossPointWebServer.h"
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <FsHelpers.h>
|
||||
#include <SD.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "html/FilesPageHtml.generated.h"
|
||||
#include "html/HomePageHtml.generated.h"
|
||||
|
||||
namespace {
|
||||
// Folders/files to hide from the web interface file browser
|
||||
// Note: Items starting with "." are automatically hidden
|
||||
const char* HIDDEN_ITEMS[] = {"System Volume Information", "XTCache"};
|
||||
constexpr size_t HIDDEN_ITEMS_COUNT = sizeof(HIDDEN_ITEMS) / sizeof(HIDDEN_ITEMS[0]);
|
||||
} // namespace
|
||||
|
||||
// File listing page template - now using generated headers:
|
||||
// - HomePageHtml (from html/HomePage.html)
|
||||
// - FilesPageHeaderHtml (from html/FilesPageHeader.html)
|
||||
// - FilesPageFooterHtml (from html/FilesPageFooter.html)
|
||||
CrossPointWebServer::CrossPointWebServer() {}
|
||||
|
||||
CrossPointWebServer::~CrossPointWebServer() { stop(); }
|
||||
|
||||
void CrossPointWebServer::begin() {
|
||||
if (running) {
|
||||
Serial.printf("[%lu] [WEB] Web server already running\n", millis());
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if we have a valid network connection (either STA connected or AP mode)
|
||||
const wifi_mode_t wifiMode = WiFi.getMode();
|
||||
const bool isStaConnected = (wifiMode & WIFI_MODE_STA) && (WiFi.status() == WL_CONNECTED);
|
||||
const bool isInApMode = (wifiMode & WIFI_MODE_AP) && (WiFi.softAPgetStationNum() >= 0); // AP is running
|
||||
|
||||
if (!isStaConnected && !isInApMode) {
|
||||
Serial.printf("[%lu] [WEB] Cannot start webserver - no valid network (mode=%d, status=%d)\n", millis(), wifiMode,
|
||||
WiFi.status());
|
||||
return;
|
||||
}
|
||||
|
||||
// Store AP mode flag for later use (e.g., in handleStatus)
|
||||
apMode = isInApMode;
|
||||
|
||||
Serial.printf("[%lu] [WEB] [MEM] Free heap before begin: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
Serial.printf("[%lu] [WEB] Network mode: %s\n", millis(), apMode ? "AP" : "STA");
|
||||
|
||||
Serial.printf("[%lu] [WEB] Creating web server on port %d...\n", millis(), port);
|
||||
server.reset(new WebServer(port));
|
||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after WebServer allocation: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
if (!server) {
|
||||
Serial.printf("[%lu] [WEB] Failed to create WebServer!\n", millis());
|
||||
return;
|
||||
}
|
||||
|
||||
// Setup routes
|
||||
Serial.printf("[%lu] [WEB] Setting up routes...\n", millis());
|
||||
server->on("/", HTTP_GET, [this] { handleRoot(); });
|
||||
server->on("/files", HTTP_GET, [this] { handleFileList(); });
|
||||
|
||||
server->on("/api/status", HTTP_GET, [this] { handleStatus(); });
|
||||
server->on("/api/files", HTTP_GET, [this] { handleFileListData(); });
|
||||
|
||||
// Upload endpoint with special handling for multipart form data
|
||||
server->on("/upload", HTTP_POST, [this] { handleUploadPost(); }, [this] { handleUpload(); });
|
||||
|
||||
// Create folder endpoint
|
||||
server->on("/mkdir", HTTP_POST, [this] { handleCreateFolder(); });
|
||||
|
||||
// Delete file/folder endpoint
|
||||
server->on("/delete", HTTP_POST, [this] { handleDelete(); });
|
||||
|
||||
server->onNotFound([this] { handleNotFound(); });
|
||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after route setup: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
server->begin();
|
||||
running = true;
|
||||
|
||||
Serial.printf("[%lu] [WEB] Web server started on port %d\n", millis(), port);
|
||||
// Show the correct IP based on network mode
|
||||
const String ipAddr = apMode ? WiFi.softAPIP().toString() : WiFi.localIP().toString();
|
||||
Serial.printf("[%lu] [WEB] Access at http://%s/\n", millis(), ipAddr.c_str());
|
||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after server.begin(): %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
}
|
||||
|
||||
void CrossPointWebServer::stop() {
|
||||
if (!running || !server) {
|
||||
Serial.printf("[%lu] [WEB] stop() called but already stopped (running=%d, server=%p)\n", millis(), running,
|
||||
server.get());
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [WEB] STOP INITIATED - setting running=false first\n", millis());
|
||||
running = false; // Set this FIRST to prevent handleClient from using server
|
||||
|
||||
Serial.printf("[%lu] [WEB] [MEM] Free heap before stop: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
// Add delay to allow any in-flight handleClient() calls to complete
|
||||
delay(100);
|
||||
Serial.printf("[%lu] [WEB] Waited 100ms for handleClient to finish\n", millis());
|
||||
|
||||
server->stop();
|
||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after server->stop(): %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
// Add another delay before deletion to ensure server->stop() completes
|
||||
delay(50);
|
||||
Serial.printf("[%lu] [WEB] Waited 50ms before deleting server\n", millis());
|
||||
|
||||
server.reset();
|
||||
Serial.printf("[%lu] [WEB] Web server stopped and deleted\n", millis());
|
||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after delete server: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
// Note: Static upload variables (uploadFileName, uploadPath, uploadError) are declared
|
||||
// later in the file and will be cleared when they go out of scope or on next upload
|
||||
Serial.printf("[%lu] [WEB] [MEM] Free heap final: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
}
|
||||
|
||||
void CrossPointWebServer::handleClient() const {
|
||||
static unsigned long lastDebugPrint = 0;
|
||||
|
||||
// Check running flag FIRST before accessing server
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Double-check server pointer is valid
|
||||
if (!server) {
|
||||
Serial.printf("[%lu] [WEB] WARNING: handleClient called with null server!\n", millis());
|
||||
return;
|
||||
}
|
||||
|
||||
// Print debug every 10 seconds to confirm handleClient is being called
|
||||
if (millis() - lastDebugPrint > 10000) {
|
||||
Serial.printf("[%lu] [WEB] handleClient active, server running on port %d\n", millis(), port);
|
||||
lastDebugPrint = millis();
|
||||
}
|
||||
|
||||
server->handleClient();
|
||||
}
|
||||
|
||||
void CrossPointWebServer::handleRoot() const {
|
||||
server->send(200, "text/html", HomePageHtml);
|
||||
Serial.printf("[%lu] [WEB] Served root page\n", millis());
|
||||
}
|
||||
|
||||
void CrossPointWebServer::handleNotFound() const {
|
||||
String message = "404 Not Found\n\n";
|
||||
message += "URI: " + server->uri() + "\n";
|
||||
server->send(404, "text/plain", message);
|
||||
}
|
||||
|
||||
void CrossPointWebServer::handleStatus() const {
|
||||
// Get correct IP based on AP vs STA mode
|
||||
const String ipAddr = apMode ? WiFi.softAPIP().toString() : WiFi.localIP().toString();
|
||||
|
||||
String json = "{";
|
||||
json += "\"version\":\"" + String(CROSSPOINT_VERSION) + "\",";
|
||||
json += "\"ip\":\"" + ipAddr + "\",";
|
||||
json += "\"mode\":\"" + String(apMode ? "AP" : "STA") + "\",";
|
||||
json += "\"rssi\":" + String(apMode ? 0 : WiFi.RSSI()) + ","; // RSSI not applicable in AP mode
|
||||
json += "\"freeHeap\":" + String(ESP.getFreeHeap()) + ",";
|
||||
json += "\"uptime\":" + String(millis() / 1000);
|
||||
json += "}";
|
||||
|
||||
server->send(200, "application/json", json);
|
||||
}
|
||||
|
||||
void CrossPointWebServer::scanFiles(const char* path, const std::function<void(FileInfo)>& callback) const {
|
||||
File root = SD.open(path);
|
||||
if (!root) {
|
||||
Serial.printf("[%lu] [WEB] Failed to open directory: %s\n", millis(), path);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!root.isDirectory()) {
|
||||
Serial.printf("[%lu] [WEB] Not a directory: %s\n", millis(), path);
|
||||
root.close();
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [WEB] Scanning files in: %s\n", millis(), path);
|
||||
|
||||
File file = root.openNextFile();
|
||||
while (file) {
|
||||
auto fileName = String(file.name());
|
||||
|
||||
// Skip hidden items (starting with ".")
|
||||
bool shouldHide = fileName.startsWith(".");
|
||||
|
||||
// Check against explicitly hidden items list
|
||||
if (!shouldHide) {
|
||||
for (size_t i = 0; i < HIDDEN_ITEMS_COUNT; i++) {
|
||||
if (fileName.equals(HIDDEN_ITEMS[i])) {
|
||||
shouldHide = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!shouldHide) {
|
||||
FileInfo info;
|
||||
info.name = fileName;
|
||||
info.isDirectory = file.isDirectory();
|
||||
|
||||
if (info.isDirectory) {
|
||||
info.size = 0;
|
||||
info.isEpub = false;
|
||||
} else {
|
||||
info.size = file.size();
|
||||
info.isEpub = isEpubFile(info.name);
|
||||
}
|
||||
|
||||
callback(info);
|
||||
}
|
||||
|
||||
file.close();
|
||||
file = root.openNextFile();
|
||||
}
|
||||
root.close();
|
||||
}
|
||||
|
||||
bool CrossPointWebServer::isEpubFile(const String& filename) const {
|
||||
String lower = filename;
|
||||
lower.toLowerCase();
|
||||
return lower.endsWith(".epub");
|
||||
}
|
||||
|
||||
void CrossPointWebServer::handleFileList() const { server->send(200, "text/html", FilesPageHtml); }
|
||||
|
||||
void CrossPointWebServer::handleFileListData() const {
|
||||
// Get current path from query string (default to root)
|
||||
String currentPath = "/";
|
||||
if (server->hasArg("path")) {
|
||||
currentPath = server->arg("path");
|
||||
// Ensure path starts with /
|
||||
if (!currentPath.startsWith("/")) {
|
||||
currentPath = "/" + currentPath;
|
||||
}
|
||||
// Remove trailing slash unless it's root
|
||||
if (currentPath.length() > 1 && currentPath.endsWith("/")) {
|
||||
currentPath = currentPath.substring(0, currentPath.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
server->setContentLength(CONTENT_LENGTH_UNKNOWN);
|
||||
server->send(200, "application/json", "");
|
||||
server->sendContent("[");
|
||||
char output[512];
|
||||
constexpr size_t outputSize = sizeof(output);
|
||||
bool seenFirst = false;
|
||||
scanFiles(currentPath.c_str(), [this, &output, seenFirst](const FileInfo& info) mutable {
|
||||
JsonDocument doc;
|
||||
doc["name"] = info.name;
|
||||
doc["size"] = info.size;
|
||||
doc["isDirectory"] = info.isDirectory;
|
||||
doc["isEpub"] = info.isEpub;
|
||||
const size_t written = serializeJson(doc, output, outputSize);
|
||||
if (written >= outputSize) {
|
||||
// JSON output truncated; skip this entry to avoid sending malformed JSON
|
||||
Serial.printf("[%lu] [WEB] Skipping file entry with oversized JSON for name: %s\n", millis(), info.name.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
if (seenFirst) {
|
||||
server->sendContent(",");
|
||||
} else {
|
||||
seenFirst = true;
|
||||
}
|
||||
server->sendContent(output);
|
||||
});
|
||||
server->sendContent("]");
|
||||
// End of streamed response, empty chunk to signal client
|
||||
server->sendContent("");
|
||||
Serial.printf("[%lu] [WEB] Served file listing page for path: %s\n", millis(), currentPath.c_str());
|
||||
}
|
||||
|
||||
// Static variables for upload handling
|
||||
static File uploadFile;
|
||||
static String uploadFileName;
|
||||
static String uploadPath = "/";
|
||||
static size_t uploadSize = 0;
|
||||
static bool uploadSuccess = false;
|
||||
static String uploadError = "";
|
||||
|
||||
void CrossPointWebServer::handleUpload() const {
|
||||
static unsigned long lastWriteTime = 0;
|
||||
static unsigned long uploadStartTime = 0;
|
||||
static size_t lastLoggedSize = 0;
|
||||
|
||||
// Safety check: ensure server is still valid
|
||||
if (!running || !server) {
|
||||
Serial.printf("[%lu] [WEB] [UPLOAD] ERROR: handleUpload called but server not running!\n", millis());
|
||||
return;
|
||||
}
|
||||
|
||||
const HTTPUpload& upload = server->upload();
|
||||
|
||||
if (upload.status == UPLOAD_FILE_START) {
|
||||
uploadFileName = upload.filename;
|
||||
uploadSize = 0;
|
||||
uploadSuccess = false;
|
||||
uploadError = "";
|
||||
uploadStartTime = millis();
|
||||
lastWriteTime = millis();
|
||||
lastLoggedSize = 0;
|
||||
|
||||
// Get upload path from query parameter (defaults to root if not specified)
|
||||
// Note: We use query parameter instead of form data because multipart form
|
||||
// fields aren't available until after file upload completes
|
||||
if (server->hasArg("path")) {
|
||||
uploadPath = server->arg("path");
|
||||
// Ensure path starts with /
|
||||
if (!uploadPath.startsWith("/")) {
|
||||
uploadPath = "/" + uploadPath;
|
||||
}
|
||||
// Remove trailing slash unless it's root
|
||||
if (uploadPath.length() > 1 && uploadPath.endsWith("/")) {
|
||||
uploadPath = uploadPath.substring(0, uploadPath.length() - 1);
|
||||
}
|
||||
} else {
|
||||
uploadPath = "/";
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [WEB] [UPLOAD] START: %s to path: %s\n", millis(), uploadFileName.c_str(), uploadPath.c_str());
|
||||
Serial.printf("[%lu] [WEB] [UPLOAD] Free heap: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||
|
||||
// Create file path
|
||||
String filePath = uploadPath;
|
||||
if (!filePath.endsWith("/")) filePath += "/";
|
||||
filePath += uploadFileName;
|
||||
|
||||
// Check if file already exists
|
||||
if (SD.exists(filePath.c_str())) {
|
||||
Serial.printf("[%lu] [WEB] [UPLOAD] Overwriting existing file: %s\n", millis(), filePath.c_str());
|
||||
SD.remove(filePath.c_str());
|
||||
}
|
||||
|
||||
// Open file for writing
|
||||
if (!FsHelpers::openFileForWrite("WEB", filePath, uploadFile)) {
|
||||
uploadError = "Failed to create file on SD card";
|
||||
Serial.printf("[%lu] [WEB] [UPLOAD] FAILED to create file: %s\n", millis(), filePath.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [WEB] [UPLOAD] File created successfully: %s\n", millis(), filePath.c_str());
|
||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||
if (uploadFile && uploadError.isEmpty()) {
|
||||
const unsigned long writeStartTime = millis();
|
||||
const size_t written = uploadFile.write(upload.buf, upload.currentSize);
|
||||
const unsigned long writeEndTime = millis();
|
||||
const unsigned long writeDuration = writeEndTime - writeStartTime;
|
||||
|
||||
if (written != upload.currentSize) {
|
||||
uploadError = "Failed to write to SD card - disk may be full";
|
||||
uploadFile.close();
|
||||
Serial.printf("[%lu] [WEB] [UPLOAD] WRITE ERROR - expected %d, wrote %d\n", millis(), upload.currentSize,
|
||||
written);
|
||||
} else {
|
||||
uploadSize += written;
|
||||
|
||||
// Log progress every 50KB or if write took >100ms
|
||||
if (uploadSize - lastLoggedSize >= 51200 || writeDuration > 100) {
|
||||
const unsigned long timeSinceStart = millis() - uploadStartTime;
|
||||
const unsigned long timeSinceLastWrite = millis() - lastWriteTime;
|
||||
const float kbps = (uploadSize / 1024.0) / (timeSinceStart / 1000.0);
|
||||
|
||||
Serial.printf(
|
||||
"[%lu] [WEB] [UPLOAD] Progress: %d bytes (%.1f KB), %.1f KB/s, write took %lu ms, gap since last: %lu "
|
||||
"ms\n",
|
||||
millis(), uploadSize, uploadSize / 1024.0, kbps, writeDuration, timeSinceLastWrite);
|
||||
lastLoggedSize = uploadSize;
|
||||
}
|
||||
lastWriteTime = millis();
|
||||
}
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_END) {
|
||||
if (uploadFile) {
|
||||
uploadFile.close();
|
||||
|
||||
if (uploadError.isEmpty()) {
|
||||
uploadSuccess = true;
|
||||
Serial.printf("[%lu] [WEB] Upload complete: %s (%d bytes)\n", millis(), uploadFileName.c_str(), uploadSize);
|
||||
}
|
||||
}
|
||||
} else if (upload.status == UPLOAD_FILE_ABORTED) {
|
||||
if (uploadFile) {
|
||||
uploadFile.close();
|
||||
// Try to delete the incomplete file
|
||||
String filePath = uploadPath;
|
||||
if (!filePath.endsWith("/")) filePath += "/";
|
||||
filePath += uploadFileName;
|
||||
SD.remove(filePath.c_str());
|
||||
}
|
||||
uploadError = "Upload aborted";
|
||||
Serial.printf("[%lu] [WEB] Upload aborted\n", millis());
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServer::handleUploadPost() const {
|
||||
if (uploadSuccess) {
|
||||
server->send(200, "text/plain", "File uploaded successfully: " + uploadFileName);
|
||||
} else {
|
||||
const String error = uploadError.isEmpty() ? "Unknown error during upload" : uploadError;
|
||||
server->send(400, "text/plain", error);
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServer::handleCreateFolder() const {
|
||||
// Get folder name from form data
|
||||
if (!server->hasArg("name")) {
|
||||
server->send(400, "text/plain", "Missing folder name");
|
||||
return;
|
||||
}
|
||||
|
||||
const String folderName = server->arg("name");
|
||||
|
||||
// Validate folder name
|
||||
if (folderName.isEmpty()) {
|
||||
server->send(400, "text/plain", "Folder name cannot be empty");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get parent path
|
||||
String parentPath = "/";
|
||||
if (server->hasArg("path")) {
|
||||
parentPath = server->arg("path");
|
||||
if (!parentPath.startsWith("/")) {
|
||||
parentPath = "/" + parentPath;
|
||||
}
|
||||
if (parentPath.length() > 1 && parentPath.endsWith("/")) {
|
||||
parentPath = parentPath.substring(0, parentPath.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Build full folder path
|
||||
String folderPath = parentPath;
|
||||
if (!folderPath.endsWith("/")) folderPath += "/";
|
||||
folderPath += folderName;
|
||||
|
||||
Serial.printf("[%lu] [WEB] Creating folder: %s\n", millis(), folderPath.c_str());
|
||||
|
||||
// Check if already exists
|
||||
if (SD.exists(folderPath.c_str())) {
|
||||
server->send(400, "text/plain", "Folder already exists");
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the folder
|
||||
if (SD.mkdir(folderPath.c_str())) {
|
||||
Serial.printf("[%lu] [WEB] Folder created successfully: %s\n", millis(), folderPath.c_str());
|
||||
server->send(200, "text/plain", "Folder created: " + folderName);
|
||||
} else {
|
||||
Serial.printf("[%lu] [WEB] Failed to create folder: %s\n", millis(), folderPath.c_str());
|
||||
server->send(500, "text/plain", "Failed to create folder");
|
||||
}
|
||||
}
|
||||
|
||||
void CrossPointWebServer::handleDelete() const {
|
||||
// Get path from form data
|
||||
if (!server->hasArg("path")) {
|
||||
server->send(400, "text/plain", "Missing path");
|
||||
return;
|
||||
}
|
||||
|
||||
String itemPath = server->arg("path");
|
||||
const String itemType = server->hasArg("type") ? server->arg("type") : "file";
|
||||
|
||||
// Validate path
|
||||
if (itemPath.isEmpty() || itemPath == "/") {
|
||||
server->send(400, "text/plain", "Cannot delete root directory");
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure path starts with /
|
||||
if (!itemPath.startsWith("/")) {
|
||||
itemPath = "/" + itemPath;
|
||||
}
|
||||
|
||||
// Security check: prevent deletion of protected items
|
||||
const String itemName = itemPath.substring(itemPath.lastIndexOf('/') + 1);
|
||||
|
||||
// Check if item starts with a dot (hidden/system file)
|
||||
if (itemName.startsWith(".")) {
|
||||
Serial.printf("[%lu] [WEB] Delete rejected - hidden/system item: %s\n", millis(), itemPath.c_str());
|
||||
server->send(403, "text/plain", "Cannot delete system files");
|
||||
return;
|
||||
}
|
||||
|
||||
// Check against explicitly protected items
|
||||
for (size_t i = 0; i < HIDDEN_ITEMS_COUNT; i++) {
|
||||
if (itemName.equals(HIDDEN_ITEMS[i])) {
|
||||
Serial.printf("[%lu] [WEB] Delete rejected - protected item: %s\n", millis(), itemPath.c_str());
|
||||
server->send(403, "text/plain", "Cannot delete protected items");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if item exists
|
||||
if (!SD.exists(itemPath.c_str())) {
|
||||
Serial.printf("[%lu] [WEB] Delete failed - item not found: %s\n", millis(), itemPath.c_str());
|
||||
server->send(404, "text/plain", "Item not found");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.printf("[%lu] [WEB] Attempting to delete %s: %s\n", millis(), itemType.c_str(), itemPath.c_str());
|
||||
|
||||
bool success = false;
|
||||
|
||||
if (itemType == "folder") {
|
||||
// For folders, try to remove (will fail if not empty)
|
||||
File dir = SD.open(itemPath.c_str());
|
||||
if (dir && dir.isDirectory()) {
|
||||
// Check if folder is empty
|
||||
File entry = dir.openNextFile();
|
||||
if (entry) {
|
||||
// Folder is not empty
|
||||
entry.close();
|
||||
dir.close();
|
||||
Serial.printf("[%lu] [WEB] Delete failed - folder not empty: %s\n", millis(), itemPath.c_str());
|
||||
server->send(400, "text/plain", "Folder is not empty. Delete contents first.");
|
||||
return;
|
||||
}
|
||||
dir.close();
|
||||
}
|
||||
success = SD.rmdir(itemPath.c_str());
|
||||
} else {
|
||||
// For files, use remove
|
||||
success = SD.remove(itemPath.c_str());
|
||||
}
|
||||
|
||||
if (success) {
|
||||
Serial.printf("[%lu] [WEB] Successfully deleted: %s\n", millis(), itemPath.c_str());
|
||||
server->send(200, "text/plain", "Deleted successfully");
|
||||
} else {
|
||||
Serial.printf("[%lu] [WEB] Failed to delete: %s\n", millis(), itemPath.c_str());
|
||||
server->send(500, "text/plain", "Failed to delete item");
|
||||
}
|
||||
}
|
||||
56
src/network/CrossPointWebServer.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <WebServer.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
// Structure to hold file information
|
||||
struct FileInfo {
|
||||
String name;
|
||||
size_t size;
|
||||
bool isEpub;
|
||||
bool isDirectory;
|
||||
};
|
||||
|
||||
class CrossPointWebServer {
|
||||
public:
|
||||
CrossPointWebServer();
|
||||
~CrossPointWebServer();
|
||||
|
||||
// Start the web server (call after WiFi is connected)
|
||||
void begin();
|
||||
|
||||
// Stop the web server
|
||||
void stop();
|
||||
|
||||
// Call this periodically to handle client requests
|
||||
void handleClient() const;
|
||||
|
||||
// Check if server is running
|
||||
bool isRunning() const { return running; }
|
||||
|
||||
// Get the port number
|
||||
uint16_t getPort() const { return port; }
|
||||
|
||||
private:
|
||||
std::unique_ptr<WebServer> server = nullptr;
|
||||
bool running = false;
|
||||
bool apMode = false; // true when running in AP mode, false for STA mode
|
||||
uint16_t port = 80;
|
||||
|
||||
// File scanning
|
||||
void scanFiles(const char* path, const std::function<void(FileInfo)>& callback) const;
|
||||
String formatFileSize(size_t bytes) const;
|
||||
bool isEpubFile(const String& filename) const;
|
||||
|
||||
// Request handlers
|
||||
void handleRoot() const;
|
||||
void handleNotFound() const;
|
||||
void handleStatus() const;
|
||||
void handleFileList() const;
|
||||
void handleFileListData() const;
|
||||
void handleUpload() const;
|
||||
void handleUploadPost() const;
|
||||
void handleCreateFolder() const;
|
||||
void handleDelete() const;
|
||||
};
|
||||