mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-08 08:37:38 +03:00
Compare commits
No commits in common. "fd3fe3021912dc8bcf65d02716b3ced276d08df1" and "702f05e3f7416bed30cf29365ff2816fadef9965" have entirely different histories.
fd3fe30219
...
702f05e3f7
15
.github/PULL_REQUEST_TEMPLATE.md
vendored
15
.github/PULL_REQUEST_TEMPLATE.md
vendored
@ -1,18 +1,9 @@
|
|||||||
## Summary
|
## Summary
|
||||||
|
|
||||||
* **What is the goal of this PR?** (e.g., Implements the new feature for file uploading.)
|
* **What is the goal of this PR?** (e.g., Fixes a bug in the user authentication module, Implements the new feature for
|
||||||
|
file uploading.)
|
||||||
* **What changes are included?**
|
* **What changes are included?**
|
||||||
|
|
||||||
## Additional Context
|
## Additional Context
|
||||||
|
|
||||||
* Add any other information that might be helpful for the reviewer (e.g., performance implications, potential risks,
|
* Add any other information that might be helpful for the reviewer (e.g., performance implications, potential risks, specific areas to focus on).
|
||||||
specific areas to focus on).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### AI Usage
|
|
||||||
|
|
||||||
While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it
|
|
||||||
helps set the right context for reviewers.
|
|
||||||
|
|
||||||
Did you use AI tools to help write this code? _**< YES | PARTIALLY | NO >**_
|
|
||||||
|
|||||||
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@ -7,11 +7,11 @@ name: CI
|
|||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
- uses: actions/setup-python@v6
|
- uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
python-version: '3.14'
|
python-version: '3.14'
|
||||||
|
|||||||
26
.github/workflows/pr-formatting-check.yml
vendored
26
.github/workflows/pr-formatting-check.yml
vendored
@ -1,26 +0,0 @@
|
|||||||
name: "PR Formatting"
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request_target:
|
|
||||||
types:
|
|
||||||
- opened
|
|
||||||
- reopened
|
|
||||||
- edited
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
statuses: write
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
title-check:
|
|
||||||
name: Title Check
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Harden Runner
|
|
||||||
uses: step-security/harden-runner@ec9f2d5744a09debf3a187a3f4f675c53b671911 # v2.13.0
|
|
||||||
with:
|
|
||||||
egress-policy: audit
|
|
||||||
|
|
||||||
- name: Check PR Title
|
|
||||||
uses: amannn/action-semantic-pull-request@v6
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
3
.github/workflows/release.yml
vendored
3
.github/workflows/release.yml
vendored
@ -7,18 +7,17 @@ on:
|
|||||||
jobs:
|
jobs:
|
||||||
build-release:
|
build-release:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v6
|
- uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
- uses: actions/cache@v5
|
- uses: actions/cache@v5
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
~/.cache/pip
|
~/.cache/pip
|
||||||
~/.platformio/.cache
|
~/.platformio/.cache
|
||||||
key: ${{ runner.os }}-pio
|
key: ${{ runner.os }}-pio
|
||||||
|
|
||||||
- uses: actions/setup-python@v6
|
- uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
python-version: '3.14'
|
python-version: '3.14'
|
||||||
|
|||||||
@ -25,7 +25,7 @@ This project is **not affiliated with Xteink**; it's built as a community projec
|
|||||||
|
|
||||||
## Features & Usage
|
## Features & Usage
|
||||||
|
|
||||||
- [x] EPUB parsing and rendering (EPUB 2 and EPUB 3)
|
- [x] EPUB parsing and rendering
|
||||||
- [ ] Image support within EPUB
|
- [ ] Image support within EPUB
|
||||||
- [x] Saved reading position
|
- [x] Saved reading position
|
||||||
- [x] File explorer with file picker
|
- [x] File explorer with file picker
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
# CrossPoint User Guide
|
# CrossPoint User Guide
|
||||||
|
|
||||||
Welcome to the **CrossPoint** firmware. This guide outlines the hardware controls, navigation, and reading features of the device.
|
Welcome to the **CrossPoint** firmware. This guide outlines the hardware controls, navigation, and reading features of
|
||||||
|
the device.
|
||||||
|
|
||||||
## 1. Hardware Overview
|
## 1. Hardware Overview
|
||||||
|
|
||||||
@ -8,9 +9,9 @@ The device utilises the standard buttons on the Xtink X4 (in the same layout as
|
|||||||
|
|
||||||
### Button Layout
|
### Button Layout
|
||||||
| Location | Buttons |
|
| Location | Buttons |
|
||||||
| --------------- | ---------------------------------------------------- |
|
|-----------------|--------------------------------------------|
|
||||||
| **Bottom Edge** | **Back**, **Confirm**, **Left**, **Right** |
|
| **Bottom Edge** | **Back**, **Confirm**, **Left**, **Right** |
|
||||||
| **Right Side** | **Power**, **Volume Up**, **Volume Down**, **Reset** |
|
| **Right Side** | **Power**, **Volume Up**, **Volume Down** |
|
||||||
|
|
||||||
Button layout can be customized in **[Settings](#35-settings)**.
|
Button layout can be customized in **[Settings](#35-settings)**.
|
||||||
|
|
||||||
@ -20,10 +21,8 @@ Button layout can be customized in **[Settings](#35-settings)**.
|
|||||||
|
|
||||||
### Power On / Off
|
### Power On / Off
|
||||||
|
|
||||||
To turn the device on or off, **press and hold the Power button for approximately half a second**.
|
To turn the device on or off, **press and hold the Power button for half a second**. In **[Settings](#35-settings)** you can configure
|
||||||
In **[Settings](#35-settings)** you can configure the power button to turn the device off with a short press instead of a long one.
|
the power button to trigger on a short press instead of a long one.
|
||||||
|
|
||||||
To reboot the device (for example if it's frozen, or after a firmware update), press and release the Reset button, and then quickly press and hold the Power button for a few seconds.
|
|
||||||
|
|
||||||
### First Launch
|
### First Launch
|
||||||
|
|
||||||
@ -38,13 +37,15 @@ Upon turning the device on for the first time, you will be placed on the **[Home
|
|||||||
|
|
||||||
### 3.1 Home Screen
|
### 3.1 Home Screen
|
||||||
|
|
||||||
The Home Screen is the main entry point to the firmware. From here you can navigate to **[Reading Mode](#4-reading-mode)** with the most recently read book, **[Book Selection](#32-book-selection)**, **[Settings](#35-settings)**, or the **[File Upload](#34-file-upload-screen)** screen.
|
The Home Screen is the main entry point to the firmware. From here you can navigate to **[Reading Mode](#4-reading-mode)** with the most recently read book, **[Book Selection](#32-book-selection)**,
|
||||||
|
**[Settings](#35-settings)**, or the **[File Upload](#34-file-upload-screen)** screen.
|
||||||
|
|
||||||
### 3.2 Book Selection
|
### 3.2 Book Selection
|
||||||
|
|
||||||
The Book Selection acts as a folder and file browser.
|
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. You can also long-press these buttons to scroll a full page up or down.
|
* **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.
|
* **Open Selection:** Press **Confirm** to open a folder or read a selected book.
|
||||||
|
|
||||||
### 3.3 Reading Mode
|
### 3.3 Reading Mode
|
||||||
@ -53,64 +54,42 @@ See [Reading Mode](#4-reading-mode) below for more information.
|
|||||||
|
|
||||||
### 3.4 File Upload Screen
|
### 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.
|
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.
|
See the [webserver docs](./docs/webserver.md) for more information on how to connect to the web server and upload files.
|
||||||
|
|
||||||
> [!TIP]
|
|
||||||
> Advanced users can also manage files programmatically or via the command line using `curl`. See the [webserver docs](./docs/webserver.md) for details.
|
|
||||||
|
|
||||||
### 3.5 Settings
|
### 3.5 Settings
|
||||||
|
|
||||||
The Settings screen allows you to configure the device's behavior. There are a few settings you can adjust:
|
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:
|
- **Sleep Screen**: Which sleep screen to display when the device sleeps, options are:
|
||||||
- "Dark" (default) - The default dark Crosspoint logo sleep screen
|
- "Dark" (default) - The default dark sleep screen
|
||||||
- "Light" - The same default sleep screen, on a white background
|
- "Light" - The same default sleep screen, on a white background
|
||||||
- "Custom" - Custom images from the SD card; see [Sleep Screen](#36-sleep-screen) below for more information
|
- "Custom" - Custom images from the SD card, see [Sleep Screen](#36-sleep-screen) below for more information
|
||||||
- "Cover" - The book cover image (Note: this is experimental and may not work as expected)
|
- "Cover" - The book cover image (Note: this is experimental and may not work as expected)
|
||||||
- "None" - A blank screen
|
- **Status Bar**: Configure the status bar displayed while reading, options are:
|
||||||
- **Sleep Screen Cover Mode**: How to display the book cover when "Cover" sleep screen is selected:
|
|
||||||
- "Fit" (default) - Scale the image down to fit centered on the screen, padding with white borders as necessary
|
|
||||||
- "Crop" - Scale the image down and crop as necessary to try to to fill the screen (Note: this is experimental and may not work as expected)
|
|
||||||
- **Status Bar**: Configure the status bar displayed while reading:
|
|
||||||
- "None" - No status bar
|
- "None" - No status bar
|
||||||
- "No Progress" - Show status bar without reading progress
|
- "No Progress" - Show status bar without reading progress
|
||||||
- "Full" - Show status bar with reading progress
|
- "Full" - Show status bar with reading progress
|
||||||
- **Hide Battery %**: Configure where to suppress the battery pecentage display in the status bar; the battery icon will still be shown:
|
- **Extra Paragraph Spacing**: If enabled, vertical space will be added between paragraphs in the book, if disabled,
|
||||||
- "Never" - Always show battery percentage (default)
|
paragraphs will not have vertical space between them, but will have first word indentation.
|
||||||
- "In Reader" - Show battery percentage everywhere except in reading mode
|
- **Short Power Button Click**: Whether to trigger the power button on a short press or a long press.
|
||||||
- "Always" - Always hide battery percentage
|
- **Reading Orientation**: Set the screen orientation for reading, options are:
|
||||||
- **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-line indentation.
|
|
||||||
- **Text Anti-Aliasing**: Whether to show smooth grey edges (anti-aliasing) on text in reading mode. Note this slows down page turns slightly.
|
|
||||||
- **Short Power Button Click**: Controls the effect of a short click of the power button:
|
|
||||||
- "Ignore" - Require a long press to turn off the device
|
|
||||||
- "Sleep" - A short press powers the device off
|
|
||||||
- "Page Turn" - A short press in reading mode turns to the next page; a long press turns the device off
|
|
||||||
- **Reading Orientation**: Set the screen orientation for reading EPUB files:
|
|
||||||
- "Portrait" (default) - Standard portrait orientation
|
- "Portrait" (default) - Standard portrait orientation
|
||||||
- "Landscape CW" - Landscape, rotated clockwise
|
- "Landscape CW" - Landscape, rotated clockwise
|
||||||
- "Inverted" - Portrait, upside down
|
- "Inverted" - Portrait, upside down
|
||||||
- "Landscape CCW" - Landscape, rotated counter-clockwise
|
- "Landscape CCW" - Landscape, rotated counter-clockwise
|
||||||
- **Front Button Layout**: Configure the order of the bottom edge buttons:
|
- **Front Button Layout**: Configure the order of the bottom edge buttons, options are:
|
||||||
- Back, Confirm, Left, Right (default)
|
- "Bck, Cnfrm, Lft, Rght" (default) - Back, Confirm, Left, Right
|
||||||
- Left, Right, Back, Confirm
|
- "Lft, Rght, Bck, Cnfrm" - Left, Right, Back, Confirm
|
||||||
- Left, Back, Confirm, Right
|
- "Lft, Bck, Cnfrm, Rght" - Left, Back, Confirm, Right
|
||||||
- **Side Button Layout (reader)**: Swap the order of the up and down volume buttons from Previous/Next to Next/Previous. This change is only in effect when reading.
|
- **Side Button Layout**: Swap the order of the volume buttons from Previous/Next to Next/Previous. This change is only in effect when reading.
|
||||||
- **Long-press Chapter Skip**: Set whether long-pressing page turn buttons skip to the next/previous chapter.
|
- **Reader Font Family**: Choose the font used for reading, options are:
|
||||||
- "Chapter Skip" (default) - Long-pressing skips to next/previous chapter
|
|
||||||
- "Page Scroll" - Long-pressing scrolls a page up/down
|
|
||||||
- Swap the order of the up and down volume buttons from Previous/Next to Next/Previous. This change is only in effect when reading.
|
|
||||||
- **Reader Font Family**: Choose the font used for reading:
|
|
||||||
- "Bookerly" (default) - Amazon's reading font
|
- "Bookerly" (default) - Amazon's reading font
|
||||||
- "Noto Sans" - Google's sans-serif font
|
- "Noto Sans" - Google's sans-serif font
|
||||||
- "Open Dyslexic" - Font designed for readers with dyslexia
|
- "Open Dyslexic" - Font designed for readers with dyslexia
|
||||||
- **Reader Font Size**: Adjust the text size for reading; options are "Small", "Medium", "Large", or "X Large".
|
- **Reader Font Size**: Adjust the text size for reading, options are "Small", "Medium", "Large", or "X Large".
|
||||||
- **Reader Line Spacing**: Adjust the spacing between lines; options are "Tight", "Normal", or "Wide".
|
- **Reader Line Spacing**: Adjust the spacing between lines, options are "Tight", "Normal", or "Wide".
|
||||||
- **Reader Screen Margin**: Controls the screen margins in reader mode between 5 and 40 pixels in 5 pixel increments.
|
|
||||||
- **Reader Paragraph Alignment**: Set the alignment of paragraphs; options are "Justified" (default), "Left", "Center", or "Right".
|
|
||||||
- **Time to Sleep**: Set the duration of inactivity before the device automatically goes to sleep.
|
|
||||||
- **Refresh Frequency**: Set how often the screen does a full refresh while reading to reduce ghosting.
|
|
||||||
- **Calibre Settings**: Set up integration for accessing a Calibre web library or connecting to Calibre as a wireless device.
|
|
||||||
- **Check for updates**: Check for firmware updates over WiFi.
|
- **Check for updates**: Check for firmware updates over WiFi.
|
||||||
|
|
||||||
### 3.6 Sleep Screen
|
### 3.6 Sleep Screen
|
||||||
@ -118,7 +97,9 @@ The Settings screen allows you to configure the device's behavior. There are a f
|
|||||||
You can customize the sleep screen by placing custom images in specific locations on the SD card:
|
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.
|
- **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.bmp` file, and one will be randomly selected each time the device sleeps.
|
- **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.bmp` file, and one will be
|
||||||
|
randomly selected each time the device sleeps.
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> You'll need to set the **Sleep Screen** setting to **Custom** in order to use these images.
|
> You'll need to set the **Sleep Screen** setting to **Custom** in order to use these images.
|
||||||
@ -136,24 +117,17 @@ Once you have opened a book, the button layout changes to facilitate reading.
|
|||||||
|
|
||||||
### Page Turning
|
### Page Turning
|
||||||
| Action | Buttons |
|
| Action | Buttons |
|
||||||
| ----------------- | ------------------------------------ |
|
|-------------------|--------------------------------------|
|
||||||
| **Previous Page** | Press **Left** _or_ **Volume Up** |
|
| **Previous Page** | Press **Left** _or_ **Volume Up** |
|
||||||
| **Next Page** | Press **Right** _or_ **Volume Down** |
|
| **Next Page** | Press **Right** _or_ **Volume Down** |
|
||||||
|
|
||||||
The role of the volume (side) buttons can be swapped in **[Settings](#35-settings)**.
|
|
||||||
|
|
||||||
If the **Short Power Button Click** setting is set to "Page Turn", you can also turn to the next page by briefly pressing the Power button.
|
|
||||||
|
|
||||||
### Chapter Navigation
|
### Chapter Navigation
|
||||||
* **Next Chapter:** Press and **hold** the **Right** (or **Volume Down**) button briefly, then release.
|
* **Next Chapter:** Press and **hold** the **Right** (or **Volume Down**) button briefly, then release.
|
||||||
* **Previous Chapter:** Press and **hold** the **Left** (or **Volume Up**) button briefly, then release.
|
* **Previous Chapter:** Press and **hold** the **Left** (or **Volume Up**) button briefly, then release.
|
||||||
|
|
||||||
This feature can be disabled in **[Settings](#35-settings)** to help avoid changing chapters by mistake.
|
|
||||||
|
|
||||||
|
|
||||||
### System Navigation
|
### System Navigation
|
||||||
* **Return to Book Selection:** Press **Back** to close the book and return to the **[Book Selection](#32-book-selection)** screen.
|
* **Return to Book Selection:** Press **Back** to close the book and return to the **[Book Selection](#32-book-selection)** screen.
|
||||||
* **Return to Home:** Press and **hold** the **Back** button to close the book and return to the **[Home](#31-home-screen)** screen.
|
* **Return to Home:** Press and hold **Back** to close the book and return to the **[Home](#31-home-screen)** screen.
|
||||||
* **Chapter Menu:** Press **Confirm** to open the **[Table of Contents/Chapter Selection](#5-chapter-selection-screen)**.
|
* **Chapter Menu:** Press **Confirm** to open the **[Table of Contents/Chapter Selection](#5-chapter-selection-screen)**.
|
||||||
|
|
||||||
---
|
---
|
||||||
@ -170,6 +144,7 @@ Accessible by pressing **Confirm** while inside a book.
|
|||||||
|
|
||||||
## 6. Current Limitations & Roadmap
|
## 6. Current Limitations & Roadmap
|
||||||
|
|
||||||
Please note that this firmware is currently in active development. The following features are **not yet supported** but are planned for future updates:
|
Please note that this firmware is currently in active development. The following features are **not yet supported** but
|
||||||
|
are planned for future updates:
|
||||||
|
|
||||||
* **Images:** Embedded images in e-books will not render.
|
* **Images:** Embedded images in e-books will not render.
|
||||||
|
|||||||
@ -170,40 +170,6 @@ This is useful for organizing your ebooks by genre, author, or series.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Command Line File Management
|
|
||||||
|
|
||||||
For power users, you can manage files directly from your terminal using `curl` while the device is in File Upload mode.
|
|
||||||
|
|
||||||
### Uploading a File
|
|
||||||
To upload a file to the root directory, use the following command:
|
|
||||||
```bash
|
|
||||||
curl -F "file=@book.epub" "http://crosspoint.local/upload?path=/"
|
|
||||||
```
|
|
||||||
|
|
||||||
* **`-F "file=@filename"`**: Points to the local file on your computer.
|
|
||||||
* **`path=/`**: The destination folder on the device SD card.
|
|
||||||
|
|
||||||
### Deleting a File
|
|
||||||
|
|
||||||
To delete a specific file, provide the full path on the SD card:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
curl -F "path=/folder/file.epub" "http://crosspoint.local/delete"
|
|
||||||
```
|
|
||||||
|
|
||||||
### Advanced Flags
|
|
||||||
|
|
||||||
For more reliable transfers of large EPUB files, consider adding these flags:
|
|
||||||
|
|
||||||
* `-#`: Shows a simple progress bar.
|
|
||||||
* `--connect-timeout 30`: Limits how long curl waits to establish a connection (in seconds).
|
|
||||||
* `--max-time 300`: Sets a maximum duration for the entire transfer (5 minutes).
|
|
||||||
|
|
||||||
> [!NOTE]
|
|
||||||
> These examples use `crosspoint.local`. If your network does not support mDNS or the address does not resolve, replace it with the specific **IP Address** displayed on your device screen (e.g., `http://192.168.1.102/`).
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
|
|
||||||
### Cannot See the Device on the Network
|
### Cannot See the Device on the Network
|
||||||
|
|||||||
BIN
firmware.bin
BIN
firmware.bin
Binary file not shown.
File diff suppressed because one or more lines are too long
@ -14,7 +14,7 @@ for size in ${BOOKERLY_FONT_SIZES[@]}; do
|
|||||||
font_name="bookerly_${size}_$(echo $style | tr '[:upper:]' '[:lower:]')"
|
font_name="bookerly_${size}_$(echo $style | tr '[:upper:]' '[:lower:]')"
|
||||||
font_path="../builtinFonts/source/Bookerly/Bookerly-${style}.ttf"
|
font_path="../builtinFonts/source/Bookerly/Bookerly-${style}.ttf"
|
||||||
output_path="../builtinFonts/${font_name}.h"
|
output_path="../builtinFonts/${font_name}.h"
|
||||||
python3 fontconvert.py $font_name $size $font_path --2bit > $output_path
|
python fontconvert.py $font_name $size $font_path --2bit > $output_path
|
||||||
echo "Generated $output_path"
|
echo "Generated $output_path"
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
@ -24,7 +24,7 @@ for size in ${NOTOSANS_FONT_SIZES[@]}; do
|
|||||||
font_name="notosans_${size}_$(echo $style | tr '[:upper:]' '[:lower:]')"
|
font_name="notosans_${size}_$(echo $style | tr '[:upper:]' '[:lower:]')"
|
||||||
font_path="../builtinFonts/source/NotoSans/NotoSans-${style}.ttf"
|
font_path="../builtinFonts/source/NotoSans/NotoSans-${style}.ttf"
|
||||||
output_path="../builtinFonts/${font_name}.h"
|
output_path="../builtinFonts/${font_name}.h"
|
||||||
python3 fontconvert.py $font_name $size $font_path --2bit > $output_path
|
python fontconvert.py $font_name $size $font_path --2bit > $output_path
|
||||||
echo "Generated $output_path"
|
echo "Generated $output_path"
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
@ -34,7 +34,7 @@ for size in ${OPENDYSLEXIC_FONT_SIZES[@]}; do
|
|||||||
font_name="opendyslexic_${size}_$(echo $style | tr '[:upper:]' '[:lower:]')"
|
font_name="opendyslexic_${size}_$(echo $style | tr '[:upper:]' '[:lower:]')"
|
||||||
font_path="../builtinFonts/source/OpenDyslexic/OpenDyslexic-${style}.otf"
|
font_path="../builtinFonts/source/OpenDyslexic/OpenDyslexic-${style}.otf"
|
||||||
output_path="../builtinFonts/${font_name}.h"
|
output_path="../builtinFonts/${font_name}.h"
|
||||||
python3 fontconvert.py $font_name $size $font_path --2bit > $output_path
|
python fontconvert.py $font_name $size $font_path --2bit > $output_path
|
||||||
echo "Generated $output_path"
|
echo "Generated $output_path"
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
@ -47,9 +47,9 @@ for size in ${UI_FONT_SIZES[@]}; do
|
|||||||
font_name="ubuntu_${size}_$(echo $style | tr '[:upper:]' '[:lower:]')"
|
font_name="ubuntu_${size}_$(echo $style | tr '[:upper:]' '[:lower:]')"
|
||||||
font_path="../builtinFonts/source/Ubuntu/Ubuntu-${style}.ttf"
|
font_path="../builtinFonts/source/Ubuntu/Ubuntu-${style}.ttf"
|
||||||
output_path="../builtinFonts/${font_name}.h"
|
output_path="../builtinFonts/${font_name}.h"
|
||||||
python3 fontconvert.py $font_name $size $font_path > $output_path
|
python fontconvert.py $font_name $size $font_path > $output_path
|
||||||
echo "Generated $output_path"
|
echo "Generated $output_path"
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
python3 fontconvert.py notosans_8_regular 8 ../builtinFonts/source/NotoSans/NotoSans-Regular.ttf > ../builtinFonts/notosans_8_regular.h
|
python fontconvert.py notosans_8_regular 8 ../builtinFonts/source/NotoSans/NotoSans-Regular.ttf > ../builtinFonts/notosans_8_regular.h
|
||||||
|
|||||||
@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
#include "Epub/parsers/ContainerParser.h"
|
#include "Epub/parsers/ContainerParser.h"
|
||||||
#include "Epub/parsers/ContentOpfParser.h"
|
#include "Epub/parsers/ContentOpfParser.h"
|
||||||
#include "Epub/parsers/TocNavParser.h"
|
|
||||||
#include "Epub/parsers/TocNcxParser.h"
|
#include "Epub/parsers/TocNcxParser.h"
|
||||||
|
|
||||||
bool Epub::findContentOpfFile(std::string* contentOpfFile) const {
|
bool Epub::findContentOpfFile(std::string* contentOpfFile) const {
|
||||||
@ -81,10 +80,6 @@ bool Epub::parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata) {
|
|||||||
tocNcxItem = opfParser.tocNcxPath;
|
tocNcxItem = opfParser.tocNcxPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!opfParser.tocNavPath.empty()) {
|
|
||||||
tocNavItem = opfParser.tocNavPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [EBP] Successfully parsed content.opf\n", millis());
|
Serial.printf("[%lu] [EBP] Successfully parsed content.opf\n", millis());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -146,63 +141,6 @@ bool Epub::parseTocNcxFile() const {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Epub::parseTocNavFile() const {
|
|
||||||
// the nav file should have been specified in the content.opf file (EPUB 3)
|
|
||||||
if (tocNavItem.empty()) {
|
|
||||||
Serial.printf("[%lu] [EBP] No nav file specified\n", millis());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [EBP] Parsing toc nav file: %s\n", millis(), tocNavItem.c_str());
|
|
||||||
|
|
||||||
const auto tmpNavPath = getCachePath() + "/toc.nav";
|
|
||||||
FsFile tempNavFile;
|
|
||||||
if (!SdMan.openFileForWrite("EBP", tmpNavPath, tempNavFile)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
readItemContentsToStream(tocNavItem, tempNavFile, 1024);
|
|
||||||
tempNavFile.close();
|
|
||||||
if (!SdMan.openFileForRead("EBP", tmpNavPath, tempNavFile)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const auto navSize = tempNavFile.size();
|
|
||||||
|
|
||||||
// Note: We can't use `contentBasePath` here as the nav file may be in a different folder to the content.opf
|
|
||||||
// and the HTMLX nav file will have hrefs relative to itself
|
|
||||||
const std::string navContentBasePath = tocNavItem.substr(0, tocNavItem.find_last_of('/') + 1);
|
|
||||||
TocNavParser navParser(navContentBasePath, navSize, bookMetadataCache.get());
|
|
||||||
|
|
||||||
if (!navParser.setup()) {
|
|
||||||
Serial.printf("[%lu] [EBP] Could not setup toc nav parser\n", millis());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto navBuffer = static_cast<uint8_t*>(malloc(1024));
|
|
||||||
if (!navBuffer) {
|
|
||||||
Serial.printf("[%lu] [EBP] Could not allocate memory for toc nav parser\n", millis());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (tempNavFile.available()) {
|
|
||||||
const auto readSize = tempNavFile.read(navBuffer, 1024);
|
|
||||||
const auto processedSize = navParser.write(navBuffer, readSize);
|
|
||||||
|
|
||||||
if (processedSize != readSize) {
|
|
||||||
Serial.printf("[%lu] [EBP] Could not process all toc nav data\n", millis());
|
|
||||||
free(navBuffer);
|
|
||||||
tempNavFile.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(navBuffer);
|
|
||||||
tempNavFile.close();
|
|
||||||
SdMan.remove(tmpNavPath.c_str());
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [EBP] Parsed TOC nav items\n", millis());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// load in the meta data for the epub file
|
// load in the meta data for the epub file
|
||||||
bool Epub::load(const bool buildIfMissing) {
|
bool Epub::load(const bool buildIfMissing) {
|
||||||
Serial.printf("[%lu] [EBP] Loading ePub: %s\n", millis(), filepath.c_str());
|
Serial.printf("[%lu] [EBP] Loading ePub: %s\n", millis(), filepath.c_str());
|
||||||
@ -246,31 +184,15 @@ bool Epub::load(const bool buildIfMissing) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TOC Pass - try EPUB 3 nav first, fall back to NCX
|
// TOC Pass
|
||||||
if (!bookMetadataCache->beginTocPass()) {
|
if (!bookMetadataCache->beginTocPass()) {
|
||||||
Serial.printf("[%lu] [EBP] Could not begin writing toc pass\n", millis());
|
Serial.printf("[%lu] [EBP] Could not begin writing toc pass\n", millis());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (!parseTocNcxFile()) {
|
||||||
bool tocParsed = false;
|
Serial.printf("[%lu] [EBP] Could not parse toc\n", millis());
|
||||||
|
return false;
|
||||||
// Try EPUB 3 nav document first (preferred)
|
|
||||||
if (!tocNavItem.empty()) {
|
|
||||||
Serial.printf("[%lu] [EBP] Attempting to parse EPUB 3 nav document\n", millis());
|
|
||||||
tocParsed = parseTocNavFile();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fall back to NCX if nav parsing failed or wasn't available
|
|
||||||
if (!tocParsed && !tocNcxItem.empty()) {
|
|
||||||
Serial.printf("[%lu] [EBP] Falling back to NCX TOC\n", millis());
|
|
||||||
tocParsed = parseTocNcxFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tocParsed) {
|
|
||||||
Serial.printf("[%lu] [EBP] Warning: Could not parse any TOC format\n", millis());
|
|
||||||
// Continue anyway - book will work without TOC
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bookMetadataCache->endTocPass()) {
|
if (!bookMetadataCache->endTocPass()) {
|
||||||
Serial.printf("[%lu] [EBP] Could not end writing toc pass\n", millis());
|
Serial.printf("[%lu] [EBP] Could not end writing toc pass\n", millis());
|
||||||
return false;
|
return false;
|
||||||
@ -348,14 +270,11 @@ const std::string& Epub::getAuthor() const {
|
|||||||
return bookMetadataCache->coreMetadata.author;
|
return bookMetadataCache->coreMetadata.author;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Epub::getCoverBmpPath(bool cropped) const {
|
std::string Epub::getCoverBmpPath() const { return cachePath + "/cover.bmp"; }
|
||||||
const auto coverFileName = "cover" + cropped ? "_crop" : "";
|
|
||||||
return cachePath + "/" + coverFileName + ".bmp";
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Epub::generateCoverBmp(bool cropped) const {
|
bool Epub::generateCoverBmp() const {
|
||||||
// Already generated, return true
|
// Already generated, return true
|
||||||
if (SdMan.exists(getCoverBmpPath(cropped).c_str())) {
|
if (SdMan.exists(getCoverBmpPath().c_str())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -387,7 +306,7 @@ bool Epub::generateCoverBmp(bool cropped) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FsFile coverBmp;
|
FsFile coverBmp;
|
||||||
if (!SdMan.openFileForWrite("EBP", getCoverBmpPath(cropped), coverBmp)) {
|
if (!SdMan.openFileForWrite("EBP", getCoverBmpPath(), coverBmp)) {
|
||||||
coverJpg.close();
|
coverJpg.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -398,7 +317,7 @@ bool Epub::generateCoverBmp(bool cropped) const {
|
|||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
Serial.printf("[%lu] [EBP] Failed to generate BMP from JPG cover image\n", millis());
|
Serial.printf("[%lu] [EBP] Failed to generate BMP from JPG cover image\n", millis());
|
||||||
SdMan.remove(getCoverBmpPath(cropped).c_str());
|
SdMan.remove(getCoverBmpPath().c_str());
|
||||||
}
|
}
|
||||||
Serial.printf("[%lu] [EBP] Generated BMP from JPG cover image, success: %s\n", millis(), success ? "yes" : "no");
|
Serial.printf("[%lu] [EBP] Generated BMP from JPG cover image, success: %s\n", millis(), success ? "yes" : "no");
|
||||||
return success;
|
return success;
|
||||||
@ -409,70 +328,6 @@ bool Epub::generateCoverBmp(bool cropped) const {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Epub::getThumbBmpPath() const { return cachePath + "/thumb.bmp"; }
|
|
||||||
|
|
||||||
bool Epub::generateThumbBmp() const {
|
|
||||||
// Already generated, return true
|
|
||||||
if (SdMan.exists(getThumbBmpPath().c_str())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
|
|
||||||
Serial.printf("[%lu] [EBP] Cannot generate thumb 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 for thumbnail\n", millis());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (coverImageHref.substr(coverImageHref.length() - 4) == ".jpg" ||
|
|
||||||
coverImageHref.substr(coverImageHref.length() - 5) == ".jpeg") {
|
|
||||||
Serial.printf("[%lu] [EBP] Generating thumb BMP from JPG cover image\n", millis());
|
|
||||||
const auto coverJpgTempPath = getCachePath() + "/.cover.jpg";
|
|
||||||
|
|
||||||
FsFile coverJpg;
|
|
||||||
if (!SdMan.openFileForWrite("EBP", coverJpgTempPath, coverJpg)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
readItemContentsToStream(coverImageHref, coverJpg, 1024);
|
|
||||||
coverJpg.close();
|
|
||||||
|
|
||||||
if (!SdMan.openFileForRead("EBP", coverJpgTempPath, coverJpg)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
FsFile thumbBmp;
|
|
||||||
if (!SdMan.openFileForWrite("EBP", getThumbBmpPath(), thumbBmp)) {
|
|
||||||
coverJpg.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Use smaller target size for Continue Reading card (half of screen: 240x400)
|
|
||||||
// Generate 1-bit BMP for fast home screen rendering (no gray passes needed)
|
|
||||||
constexpr int THUMB_TARGET_WIDTH = 240;
|
|
||||||
constexpr int THUMB_TARGET_HEIGHT = 400;
|
|
||||||
const bool success = JpegToBmpConverter::jpegFileTo1BitBmpStreamWithSize(coverJpg, thumbBmp, THUMB_TARGET_WIDTH,
|
|
||||||
THUMB_TARGET_HEIGHT);
|
|
||||||
coverJpg.close();
|
|
||||||
thumbBmp.close();
|
|
||||||
SdMan.remove(coverJpgTempPath.c_str());
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
Serial.printf("[%lu] [EBP] Failed to generate thumb BMP from JPG cover image\n", millis());
|
|
||||||
SdMan.remove(getThumbBmpPath().c_str());
|
|
||||||
}
|
|
||||||
Serial.printf("[%lu] [EBP] Generated thumb 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 thumbnail\n", millis());
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t* Epub::readItemContentsToBytes(const std::string& itemHref, size_t* size, const bool trailingNullByte) const {
|
uint8_t* Epub::readItemContentsToBytes(const std::string& itemHref, size_t* size, const bool trailingNullByte) const {
|
||||||
if (itemHref.empty()) {
|
if (itemHref.empty()) {
|
||||||
Serial.printf("[%lu] [EBP] Failed to read item, empty href\n", millis());
|
Serial.printf("[%lu] [EBP] Failed to read item, empty href\n", millis());
|
||||||
|
|||||||
@ -12,10 +12,8 @@
|
|||||||
class ZipFile;
|
class ZipFile;
|
||||||
|
|
||||||
class Epub {
|
class Epub {
|
||||||
// the ncx file (EPUB 2)
|
// the ncx file
|
||||||
std::string tocNcxItem;
|
std::string tocNcxItem;
|
||||||
// the nav file (EPUB 3)
|
|
||||||
std::string tocNavItem;
|
|
||||||
// where is the EPUBfile?
|
// where is the EPUBfile?
|
||||||
std::string filepath;
|
std::string filepath;
|
||||||
// the base path for items in the EPUB file
|
// the base path for items in the EPUB file
|
||||||
@ -28,7 +26,6 @@ class Epub {
|
|||||||
bool findContentOpfFile(std::string* contentOpfFile) const;
|
bool findContentOpfFile(std::string* contentOpfFile) const;
|
||||||
bool parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata);
|
bool parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata);
|
||||||
bool parseTocNcxFile() const;
|
bool parseTocNcxFile() const;
|
||||||
bool parseTocNavFile() const;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Epub(std::string filepath, const std::string& cacheDir) : filepath(std::move(filepath)) {
|
explicit Epub(std::string filepath, const std::string& cacheDir) : filepath(std::move(filepath)) {
|
||||||
@ -44,10 +41,8 @@ class Epub {
|
|||||||
const std::string& getPath() const;
|
const std::string& getPath() const;
|
||||||
const std::string& getTitle() const;
|
const std::string& getTitle() const;
|
||||||
const std::string& getAuthor() const;
|
const std::string& getAuthor() const;
|
||||||
std::string getCoverBmpPath(bool cropped = false) const;
|
std::string getCoverBmpPath() const;
|
||||||
bool generateCoverBmp(bool cropped = false) const;
|
bool generateCoverBmp() const;
|
||||||
std::string getThumbBmpPath() const;
|
|
||||||
bool generateThumbBmp() const;
|
|
||||||
uint8_t* readItemContentsToBytes(const std::string& itemHref, size_t* size = nullptr,
|
uint8_t* readItemContentsToBytes(const std::string& itemHref, size_t* size = nullptr,
|
||||||
bool trailingNullByte = false) const;
|
bool trailingNullByte = false) const;
|
||||||
bool readItemContentsToStream(const std::string& itemHref, Print& out, size_t chunkSize) const;
|
bool readItemContentsToStream(const std::string& itemHref, Print& out, size_t chunkSize) const;
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
#include "FsHelpers.h"
|
#include "FsHelpers.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
constexpr uint8_t BOOK_CACHE_VERSION = 4;
|
constexpr uint8_t BOOK_CACHE_VERSION = 3;
|
||||||
constexpr char bookBinFile[] = "/book.bin";
|
constexpr char bookBinFile[] = "/book.bin";
|
||||||
constexpr char tmpSpineBinFile[] = "/spine.bin.tmp";
|
constexpr char tmpSpineBinFile[] = "/spine.bin.tmp";
|
||||||
constexpr char tmpTocBinFile[] = "/toc.bin.tmp";
|
constexpr char tmpTocBinFile[] = "/toc.bin.tmp";
|
||||||
|
|||||||
@ -43,7 +43,7 @@ std::vector<uint16_t> ParsedText::calculateWordWidths(const GfxRenderer& rendere
|
|||||||
wordWidths.reserve(totalWordCount);
|
wordWidths.reserve(totalWordCount);
|
||||||
|
|
||||||
// add em-space at the beginning of first word in paragraph to indent
|
// add em-space at the beginning of first word in paragraph to indent
|
||||||
if ((style == TextBlock::JUSTIFIED || style == TextBlock::LEFT_ALIGN) && !extraParagraphSpacing) {
|
if (!extraParagraphSpacing) {
|
||||||
std::string& first_word = words.front();
|
std::string& first_word = words.front();
|
||||||
first_word.insert(0, "\xe2\x80\x83");
|
first_word.insert(0, "\xe2\x80\x83");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,9 +7,9 @@
|
|||||||
#include "parsers/ChapterHtmlSlimParser.h"
|
#include "parsers/ChapterHtmlSlimParser.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
constexpr uint8_t SECTION_FILE_VERSION = 9;
|
constexpr uint8_t SECTION_FILE_VERSION = 8;
|
||||||
constexpr uint32_t HEADER_SIZE = sizeof(uint8_t) + sizeof(int) + sizeof(float) + sizeof(bool) + sizeof(uint8_t) +
|
constexpr uint32_t HEADER_SIZE = sizeof(uint8_t) + sizeof(int) + sizeof(float) + sizeof(bool) + sizeof(uint16_t) +
|
||||||
sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t);
|
sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t);
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
uint32_t Section::onPageComplete(std::unique_ptr<Page> page) {
|
uint32_t Section::onPageComplete(std::unique_ptr<Page> page) {
|
||||||
@ -30,21 +30,19 @@ uint32_t Section::onPageComplete(std::unique_ptr<Page> page) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Section::writeSectionFileHeader(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
|
void Section::writeSectionFileHeader(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
|
||||||
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
|
const uint16_t viewportWidth, const uint16_t viewportHeight) {
|
||||||
const uint16_t viewportHeight) {
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
Serial.printf("[%lu] [SCT] File not open for writing header\n", millis());
|
Serial.printf("[%lu] [SCT] File not open for writing header\n", millis());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
static_assert(HEADER_SIZE == sizeof(SECTION_FILE_VERSION) + sizeof(fontId) + sizeof(lineCompression) +
|
static_assert(HEADER_SIZE == sizeof(SECTION_FILE_VERSION) + sizeof(fontId) + sizeof(lineCompression) +
|
||||||
sizeof(extraParagraphSpacing) + sizeof(paragraphAlignment) + sizeof(viewportWidth) +
|
sizeof(extraParagraphSpacing) + sizeof(viewportWidth) + sizeof(viewportHeight) +
|
||||||
sizeof(viewportHeight) + sizeof(pageCount) + sizeof(uint32_t),
|
sizeof(pageCount) + sizeof(uint32_t),
|
||||||
"Header size mismatch");
|
"Header size mismatch");
|
||||||
serialization::writePod(file, SECTION_FILE_VERSION);
|
serialization::writePod(file, SECTION_FILE_VERSION);
|
||||||
serialization::writePod(file, fontId);
|
serialization::writePod(file, fontId);
|
||||||
serialization::writePod(file, lineCompression);
|
serialization::writePod(file, lineCompression);
|
||||||
serialization::writePod(file, extraParagraphSpacing);
|
serialization::writePod(file, extraParagraphSpacing);
|
||||||
serialization::writePod(file, paragraphAlignment);
|
|
||||||
serialization::writePod(file, viewportWidth);
|
serialization::writePod(file, viewportWidth);
|
||||||
serialization::writePod(file, viewportHeight);
|
serialization::writePod(file, viewportHeight);
|
||||||
serialization::writePod(file, pageCount); // Placeholder for page count (will be initially 0 when written)
|
serialization::writePod(file, pageCount); // Placeholder for page count (will be initially 0 when written)
|
||||||
@ -52,8 +50,7 @@ void Section::writeSectionFileHeader(const int fontId, const float lineCompressi
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Section::loadSectionFile(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
|
bool Section::loadSectionFile(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
|
||||||
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
|
const uint16_t viewportWidth, const uint16_t viewportHeight) {
|
||||||
const uint16_t viewportHeight) {
|
|
||||||
if (!SdMan.openFileForRead("SCT", filePath, file)) {
|
if (!SdMan.openFileForRead("SCT", filePath, file)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -73,17 +70,15 @@ bool Section::loadSectionFile(const int fontId, const float lineCompression, con
|
|||||||
uint16_t fileViewportWidth, fileViewportHeight;
|
uint16_t fileViewportWidth, fileViewportHeight;
|
||||||
float fileLineCompression;
|
float fileLineCompression;
|
||||||
bool fileExtraParagraphSpacing;
|
bool fileExtraParagraphSpacing;
|
||||||
uint8_t fileParagraphAlignment;
|
|
||||||
serialization::readPod(file, fileFontId);
|
serialization::readPod(file, fileFontId);
|
||||||
serialization::readPod(file, fileLineCompression);
|
serialization::readPod(file, fileLineCompression);
|
||||||
serialization::readPod(file, fileExtraParagraphSpacing);
|
serialization::readPod(file, fileExtraParagraphSpacing);
|
||||||
serialization::readPod(file, fileParagraphAlignment);
|
|
||||||
serialization::readPod(file, fileViewportWidth);
|
serialization::readPod(file, fileViewportWidth);
|
||||||
serialization::readPod(file, fileViewportHeight);
|
serialization::readPod(file, fileViewportHeight);
|
||||||
|
|
||||||
if (fontId != fileFontId || lineCompression != fileLineCompression ||
|
if (fontId != fileFontId || lineCompression != fileLineCompression ||
|
||||||
extraParagraphSpacing != fileExtraParagraphSpacing || paragraphAlignment != fileParagraphAlignment ||
|
extraParagraphSpacing != fileExtraParagraphSpacing || viewportWidth != fileViewportWidth ||
|
||||||
viewportWidth != fileViewportWidth || viewportHeight != fileViewportHeight) {
|
viewportHeight != fileViewportHeight) {
|
||||||
file.close();
|
file.close();
|
||||||
Serial.printf("[%lu] [SCT] Deserialization failed: Parameters do not match\n", millis());
|
Serial.printf("[%lu] [SCT] Deserialization failed: Parameters do not match\n", millis());
|
||||||
clearCache();
|
clearCache();
|
||||||
@ -114,8 +109,8 @@ bool Section::clearCache() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Section::createSectionFile(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
|
bool Section::createSectionFile(const int fontId, const float lineCompression, const bool extraParagraphSpacing,
|
||||||
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
|
const uint16_t viewportWidth, const uint16_t viewportHeight,
|
||||||
const uint16_t viewportHeight, const std::function<void()>& progressSetupFn,
|
const std::function<void()>& progressSetupFn,
|
||||||
const std::function<void(int)>& progressFn) {
|
const std::function<void(int)>& progressFn) {
|
||||||
constexpr uint32_t MIN_SIZE_FOR_PROGRESS = 50 * 1024; // 50KB
|
constexpr uint32_t MIN_SIZE_FOR_PROGRESS = 50 * 1024; // 50KB
|
||||||
const auto localPath = epub->getSpineItem(spineIndex).href;
|
const auto localPath = epub->getSpineItem(spineIndex).href;
|
||||||
@ -171,13 +166,11 @@ bool Section::createSectionFile(const int fontId, const float lineCompression, c
|
|||||||
if (!SdMan.openFileForWrite("SCT", filePath, file)) {
|
if (!SdMan.openFileForWrite("SCT", filePath, file)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
writeSectionFileHeader(fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
|
writeSectionFileHeader(fontId, lineCompression, extraParagraphSpacing, viewportWidth, viewportHeight);
|
||||||
viewportHeight);
|
|
||||||
std::vector<uint32_t> lut = {};
|
std::vector<uint32_t> lut = {};
|
||||||
|
|
||||||
ChapterHtmlSlimParser visitor(
|
ChapterHtmlSlimParser visitor(
|
||||||
tmpHtmlPath, renderer, fontId, lineCompression, extraParagraphSpacing, paragraphAlignment, viewportWidth,
|
tmpHtmlPath, renderer, fontId, lineCompression, extraParagraphSpacing, viewportWidth, viewportHeight,
|
||||||
viewportHeight,
|
|
||||||
[this, &lut](std::unique_ptr<Page> page) { lut.emplace_back(this->onPageComplete(std::move(page))); },
|
[this, &lut](std::unique_ptr<Page> page) { lut.emplace_back(this->onPageComplete(std::move(page))); },
|
||||||
progressFn);
|
progressFn);
|
||||||
success = visitor.parseAndBuildPages();
|
success = visitor.parseAndBuildPages();
|
||||||
|
|||||||
@ -14,8 +14,8 @@ class Section {
|
|||||||
std::string filePath;
|
std::string filePath;
|
||||||
FsFile file;
|
FsFile file;
|
||||||
|
|
||||||
void writeSectionFileHeader(int fontId, float lineCompression, bool extraParagraphSpacing, uint8_t paragraphAlignment,
|
void writeSectionFileHeader(int fontId, float lineCompression, bool extraParagraphSpacing, uint16_t viewportWidth,
|
||||||
uint16_t viewportWidth, uint16_t viewportHeight);
|
uint16_t viewportHeight);
|
||||||
uint32_t onPageComplete(std::unique_ptr<Page> page);
|
uint32_t onPageComplete(std::unique_ptr<Page> page);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -28,12 +28,11 @@ class Section {
|
|||||||
renderer(renderer),
|
renderer(renderer),
|
||||||
filePath(epub->getCachePath() + "/sections/" + std::to_string(spineIndex) + ".bin") {}
|
filePath(epub->getCachePath() + "/sections/" + std::to_string(spineIndex) + ".bin") {}
|
||||||
~Section() = default;
|
~Section() = default;
|
||||||
bool loadSectionFile(int fontId, float lineCompression, bool extraParagraphSpacing, uint8_t paragraphAlignment,
|
bool loadSectionFile(int fontId, float lineCompression, bool extraParagraphSpacing, uint16_t viewportWidth,
|
||||||
uint16_t viewportWidth, uint16_t viewportHeight);
|
uint16_t viewportHeight);
|
||||||
bool clearCache() const;
|
bool clearCache() const;
|
||||||
bool createSectionFile(int fontId, float lineCompression, bool extraParagraphSpacing, uint8_t paragraphAlignment,
|
bool createSectionFile(int fontId, float lineCompression, bool extraParagraphSpacing, uint16_t viewportWidth,
|
||||||
uint16_t viewportWidth, uint16_t viewportHeight,
|
uint16_t viewportHeight, const std::function<void()>& progressSetupFn = nullptr,
|
||||||
const std::function<void()>& progressSetupFn = nullptr,
|
|
||||||
const std::function<void(int)>& progressFn = nullptr);
|
const std::function<void(int)>& progressFn = nullptr);
|
||||||
std::unique_ptr<Page> loadPageFromSectionFile();
|
std::unique_ptr<Page> loadPageFromSectionFile();
|
||||||
};
|
};
|
||||||
|
|||||||
163
lib/Epub/Epub/htmlEntities.cpp
Normal file
163
lib/Epub/Epub/htmlEntities.cpp
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
// from
|
||||||
|
// https://github.com/atomic14/diy-esp32-epub-reader/blob/2c2f57fdd7e2a788d14a0bcb26b9e845a47aac42/lib/Epub/RubbishHtmlParser/htmlEntities.cpp
|
||||||
|
|
||||||
|
#include "htmlEntities.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
const int MAX_ENTITY_LENGTH = 10;
|
||||||
|
|
||||||
|
// Use book: entities_ww2.epub to test this (Page 7: Entities parser test)
|
||||||
|
// Note the supported keys are only in lowercase
|
||||||
|
// Store the mappings in a unordered hash map
|
||||||
|
static std::unordered_map<std::string, std::string> entity_lookup(
|
||||||
|
{{""", "\""}, {"⁄", "⁄"}, {"&", "&"}, {"<", "<"}, {">", ">"},
|
||||||
|
{"À", "À"}, {"Á", "Á"}, {"Â", "Â"}, {"Ã", "Ã"}, {"Ä", "Ä"},
|
||||||
|
{"Å", "Å"}, {"Æ", "Æ"}, {"Ç", "Ç"}, {"È", "È"}, {"É", "É"},
|
||||||
|
{"Ê", "Ê"}, {"Ë", "Ë"}, {"Ì", "Ì"}, {"Í", "Í"}, {"Î", "Î"},
|
||||||
|
{"Ï", "Ï"}, {"Ð", "Ð"}, {"Ñ", "Ñ"}, {"Ò", "Ò"}, {"Ó", "Ó"},
|
||||||
|
{"Ô", "Ô"}, {"Õ", "Õ"}, {"Ö", "Ö"}, {"Ø", "Ø"}, {"Ù", "Ù"},
|
||||||
|
{"Ú", "Ú"}, {"Û", "Û"}, {"Ü", "Ü"}, {"Ý", "Ý"}, {"Þ", "Þ"},
|
||||||
|
{"ß", "ß"}, {"à", "à"}, {"á", "á"}, {"â", "â"}, {"ã", "ã"},
|
||||||
|
{"ä", "ä"}, {"å", "å"}, {"æ", "æ"}, {"ç", "ç"}, {"è", "è"},
|
||||||
|
{"é", "é"}, {"ê", "ê"}, {"ë", "ë"}, {"ì", "ì"}, {"í", "í"},
|
||||||
|
{"î", "î"}, {"ï", "ï"}, {"ð", "ð"}, {"ñ", "ñ"}, {"ò", "ò"},
|
||||||
|
{"ó", "ó"}, {"ô", "ô"}, {"õ", "õ"}, {"ö", "ö"}, {"ø", "ø"},
|
||||||
|
{"ù", "ù"}, {"ú", "ú"}, {"û", "û"}, {"ü", "ü"}, {"ý", "ý"},
|
||||||
|
{"þ", "þ"}, {"ÿ", "ÿ"}, {" ", " "}, {"¡", "¡"}, {"¢", "¢"},
|
||||||
|
{"£", "£"}, {"¤", "¤"}, {"¥", "¥"}, {"¦", "¦"}, {"§", "§"},
|
||||||
|
{"¨", "¨"}, {"©", "©"}, {"ª", "ª"}, {"«", "«"}, {"¬", "¬"},
|
||||||
|
{"­", ""}, {"®", "®"}, {"¯", "¯"}, {"°", "°"}, {"±", "±"},
|
||||||
|
{"²", "²"}, {"³", "³"}, {"´", "´"}, {"µ", "µ"}, {"¶", "¶"},
|
||||||
|
{"¸", "¸"}, {"¹", "¹"}, {"º", "º"}, {"»", "»"}, {"¼", "¼"},
|
||||||
|
{"½", "½"}, {"¾", "¾"}, {"¿", "¿"}, {"×", "×"}, {"÷", "÷"},
|
||||||
|
{"∀", "∀"}, {"∂", "∂"}, {"∃", "∃"}, {"∅", "∅"}, {"∇", "∇"},
|
||||||
|
{"∈", "∈"}, {"∉", "∉"}, {"∋", "∋"}, {"∏", "∏"}, {"∑", "∑"},
|
||||||
|
{"−", "−"}, {"∗", "∗"}, {"√", "√"}, {"∝", "∝"}, {"∞", "∞"},
|
||||||
|
{"∠", "∠"}, {"∧", "∧"}, {"∨", "∨"}, {"∩", "∩"}, {"∪", "∪"},
|
||||||
|
{"∫", "∫"}, {"∴", "∴"}, {"∼", "∼"}, {"≅", "≅"}, {"≈", "≈"},
|
||||||
|
{"≠", "≠"}, {"≡", "≡"}, {"≤", "≤"}, {"≥", "≥"}, {"⊂", "⊂"},
|
||||||
|
{"⊃", "⊃"}, {"⊄", "⊄"}, {"⊆", "⊆"}, {"⊇", "⊇"}, {"⊕", "⊕"},
|
||||||
|
{"⊗", "⊗"}, {"⊥", "⊥"}, {"⋅", "⋅"}, {"Α", "Α"}, {"Β", "Β"},
|
||||||
|
{"Γ", "Γ"}, {"Δ", "Δ"}, {"Ε", "Ε"}, {"Ζ", "Ζ"}, {"Η", "Η"},
|
||||||
|
{"Θ", "Θ"}, {"Ι", "Ι"}, {"Κ", "Κ"}, {"Λ", "Λ"}, {"Μ", "Μ"},
|
||||||
|
{"Ν", "Ν"}, {"Ξ", "Ξ"}, {"Ο", "Ο"}, {"Π", "Π"}, {"Ρ", "Ρ"},
|
||||||
|
{"Σ", "Σ"}, {"Τ", "Τ"}, {"Υ", "Υ"}, {"Φ", "Φ"}, {"Χ", "Χ"},
|
||||||
|
{"Ψ", "Ψ"}, {"Ω", "Ω"}, {"α", "α"}, {"β", "β"}, {"γ", "γ"},
|
||||||
|
{"δ", "δ"}, {"ε", "ε"}, {"ζ", "ζ"}, {"η", "η"}, {"θ", "θ"},
|
||||||
|
{"ι", "ι"}, {"κ", "κ"}, {"λ", "λ"}, {"μ", "μ"}, {"ν", "ν"},
|
||||||
|
{"ξ", "ξ"}, {"ο", "ο"}, {"π", "π"}, {"ρ", "ρ"}, {"ς", "ς"},
|
||||||
|
{"σ", "σ"}, {"τ", "τ"}, {"υ", "υ"}, {"φ", "φ"}, {"χ", "χ"},
|
||||||
|
{"ψ", "ψ"}, {"ω", "ω"}, {"ϑ", "ϑ"}, {"ϒ", "ϒ"}, {"ϖ", "ϖ"},
|
||||||
|
{"Œ", "Œ"}, {"œ", "œ"}, {"Š", "Š"}, {"š", "š"}, {"Ÿ", "Ÿ"},
|
||||||
|
{"ƒ", "ƒ"}, {"ˆ", "ˆ"}, {"˜", "˜"}, {" ", ""}, {" ", ""},
|
||||||
|
{" ", ""}, {"‌", ""}, {"‍", ""}, {"‎", ""}, {"‏", ""},
|
||||||
|
{"–", "–"}, {"—", "—"}, {"‘", "‘"}, {"’", "’"}, {"‚", "‚"},
|
||||||
|
{"“", "“"}, {"”", "”"}, {"„", "„"}, {"†", "†"}, {"‡", "‡"},
|
||||||
|
{"•", "•"}, {"…", "…"}, {"‰", "‰"}, {"′", "′"}, {"″", "″"},
|
||||||
|
{"‹", "‹"}, {"›", "›"}, {"‾", "‾"}, {"€", "€"}, {"™", "™"},
|
||||||
|
{"←", "←"}, {"↑", "↑"}, {"→", "→"}, {"↓", "↓"}, {"↔", "↔"},
|
||||||
|
{"↵", "↵"}, {"⌈", "⌈"}, {"⌉", "⌉"}, {"⌊", "⌊"}, {"⌋", "⌋"},
|
||||||
|
{"◊", "◊"}, {"♠", "♠"}, {"♣", "♣"}, {"♥", "♥"}, {"♦", "♦"}});
|
||||||
|
|
||||||
|
// converts from a unicode code point to the utf8 equivalent
|
||||||
|
void convert_to_utf8(const int code, std::string& res) {
|
||||||
|
// convert to a utf8 sequence
|
||||||
|
if (code < 0x80) {
|
||||||
|
res += static_cast<char>(code);
|
||||||
|
} else if (code < 0x800) {
|
||||||
|
res += static_cast<char>(0xc0 | (code >> 6));
|
||||||
|
res += static_cast<char>(0x80 | (code & 0x3f));
|
||||||
|
} else if (code < 0x10000) {
|
||||||
|
res += static_cast<char>(0xe0 | (code >> 12));
|
||||||
|
res += static_cast<char>(0x80 | ((code >> 6) & 0x3f));
|
||||||
|
res += static_cast<char>(0x80 | (code & 0x3f));
|
||||||
|
} else if (code < 0x200000) {
|
||||||
|
res += static_cast<char>(0xf0 | (code >> 18));
|
||||||
|
res += static_cast<char>(0x80 | ((code >> 12) & 0x3f));
|
||||||
|
res += static_cast<char>(0x80 | ((code >> 6) & 0x3f));
|
||||||
|
res += static_cast<char>(0x80 | (code & 0x3f));
|
||||||
|
} else if (code < 0x4000000) {
|
||||||
|
res += static_cast<char>(0xf8 | (code >> 24));
|
||||||
|
res += static_cast<char>(0x80 | ((code >> 18) & 0x3f));
|
||||||
|
res += static_cast<char>(0x80 | ((code >> 12) & 0x3f));
|
||||||
|
res += static_cast<char>(0x80 | ((code >> 6) & 0x3f));
|
||||||
|
res += static_cast<char>(0x80 | (code & 0x3f));
|
||||||
|
} else if (code < 0x80000000) {
|
||||||
|
res += static_cast<char>(0xfc | (code >> 30));
|
||||||
|
res += static_cast<char>(0x80 | ((code >> 24) & 0x3f));
|
||||||
|
res += static_cast<char>(0x80 | ((code >> 18) & 0x3f));
|
||||||
|
res += static_cast<char>(0x80 | ((code >> 12) & 0x3f));
|
||||||
|
res += static_cast<char>(0x80 | ((code >> 6) & 0x3f));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handles numeric entities - e.g. Ӓ or ሴ
|
||||||
|
bool process_numeric_entity(const std::string& entity, std::string& res) {
|
||||||
|
int code = 0;
|
||||||
|
// is it hex?
|
||||||
|
if (entity[2] == 'x' || entity[2] == 'X') {
|
||||||
|
// parse the hex code
|
||||||
|
code = strtol(entity.substr(3, entity.size() - 3).c_str(), nullptr, 16);
|
||||||
|
} else {
|
||||||
|
code = strtol(entity.substr(2, entity.size() - 3).c_str(), nullptr, 10);
|
||||||
|
}
|
||||||
|
if (code != 0) {
|
||||||
|
// special handling for nbsp
|
||||||
|
if (code == 0xA0) {
|
||||||
|
res += " ";
|
||||||
|
} else {
|
||||||
|
convert_to_utf8(code, res);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// handles named entities - e.g. &
|
||||||
|
bool process_string_entity(const std::string& entity, std::string& res) {
|
||||||
|
// it's a named entity - find it in the lookup table
|
||||||
|
// find it in the map
|
||||||
|
const auto it = entity_lookup.find(entity);
|
||||||
|
if (it != entity_lookup.end()) {
|
||||||
|
res += it->second;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// replace all the entities in the string
|
||||||
|
std::string replaceHtmlEntities(const char* text) {
|
||||||
|
std::string res;
|
||||||
|
res.reserve(strlen(text));
|
||||||
|
for (int i = 0; i < strlen(text); ++i) {
|
||||||
|
bool flag = false;
|
||||||
|
// do we have a potential entity?
|
||||||
|
if (text[i] == '&') {
|
||||||
|
// find the end of the entity
|
||||||
|
int j = i + 1;
|
||||||
|
while (j < strlen(text) && text[j] != ';' && j - i < MAX_ENTITY_LENGTH) {
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
if (j - i > 2) {
|
||||||
|
char entity[j - i + 1];
|
||||||
|
strncpy(entity, text + i, j - i);
|
||||||
|
// is it a numeric code?
|
||||||
|
if (entity[1] == '#') {
|
||||||
|
flag = process_numeric_entity(entity, res);
|
||||||
|
} else {
|
||||||
|
flag = process_string_entity(entity, res);
|
||||||
|
}
|
||||||
|
// skip past the entity if we successfully decoded it
|
||||||
|
if (flag) {
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!flag) {
|
||||||
|
res += text[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
7
lib/Epub/Epub/htmlEntities.h
Normal file
7
lib/Epub/Epub/htmlEntities.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
// from
|
||||||
|
// https://github.com/atomic14/diy-esp32-epub-reader/blob/2c2f57fdd7e2a788d14a0bcb26b9e845a47aac42/lib/Epub/RubbishHtmlParser/htmlEntities.cpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string replaceHtmlEntities(const char* text);
|
||||||
@ -6,6 +6,7 @@
|
|||||||
#include <expat.h>
|
#include <expat.h>
|
||||||
|
|
||||||
#include "../Page.h"
|
#include "../Page.h"
|
||||||
|
#include "../htmlEntities.h"
|
||||||
|
|
||||||
const char* HEADER_TAGS[] = {"h1", "h2", "h3", "h4", "h5", "h6"};
|
const char* HEADER_TAGS[] = {"h1", "h2", "h3", "h4", "h5", "h6"};
|
||||||
constexpr int NUM_HEADER_TAGS = sizeof(HEADER_TAGS) / sizeof(HEADER_TAGS[0]);
|
constexpr int NUM_HEADER_TAGS = sizeof(HEADER_TAGS) / sizeof(HEADER_TAGS[0]);
|
||||||
@ -25,7 +26,7 @@ constexpr int NUM_ITALIC_TAGS = sizeof(ITALIC_TAGS) / sizeof(ITALIC_TAGS[0]);
|
|||||||
const char* IMAGE_TAGS[] = {"img"};
|
const char* IMAGE_TAGS[] = {"img"};
|
||||||
constexpr int NUM_IMAGE_TAGS = sizeof(IMAGE_TAGS) / sizeof(IMAGE_TAGS[0]);
|
constexpr int NUM_IMAGE_TAGS = sizeof(IMAGE_TAGS) / sizeof(IMAGE_TAGS[0]);
|
||||||
|
|
||||||
const char* SKIP_TAGS[] = {"head"};
|
const char* SKIP_TAGS[] = {"head", "table"};
|
||||||
constexpr int NUM_SKIP_TAGS = sizeof(SKIP_TAGS) / sizeof(SKIP_TAGS[0]);
|
constexpr int NUM_SKIP_TAGS = sizeof(SKIP_TAGS) / sizeof(SKIP_TAGS[0]);
|
||||||
|
|
||||||
bool isWhitespace(const char c) { return c == ' ' || c == '\r' || c == '\n' || c == '\t'; }
|
bool isWhitespace(const char c) { return c == ' ' || c == '\r' || c == '\n' || c == '\t'; }
|
||||||
@ -63,43 +64,12 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special handling for tables - show placeholder text instead of dropping silently
|
|
||||||
if (strcmp(name, "table") == 0) {
|
|
||||||
// Add placeholder text
|
|
||||||
self->startNewTextBlock(TextBlock::CENTER_ALIGN);
|
|
||||||
if (self->currentTextBlock) {
|
|
||||||
self->currentTextBlock->addWord("[Table omitted]", EpdFontFamily::ITALIC);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip table contents
|
|
||||||
self->skipUntilDepth = self->depth;
|
|
||||||
self->depth += 1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matches(name, IMAGE_TAGS, NUM_IMAGE_TAGS)) {
|
if (matches(name, IMAGE_TAGS, NUM_IMAGE_TAGS)) {
|
||||||
// TODO: Start processing image tags
|
// TODO: Start processing image tags
|
||||||
std::string alt;
|
|
||||||
if (atts != nullptr) {
|
|
||||||
for (int i = 0; atts[i]; i += 2) {
|
|
||||||
if (strcmp(atts[i], "alt") == 0) {
|
|
||||||
alt = "[Image: " + std::string(atts[i + 1]) + "]";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Serial.printf("[%lu] [EHP] Image alt: %s\n", millis(), alt.c_str());
|
|
||||||
|
|
||||||
self->startNewTextBlock(TextBlock::CENTER_ALIGN);
|
|
||||||
self->italicUntilDepth = min(self->italicUntilDepth, self->depth);
|
|
||||||
self->depth += 1;
|
|
||||||
self->characterData(userData, alt.c_str(), alt.length());
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// Skip for now
|
|
||||||
self->skipUntilDepth = self->depth;
|
self->skipUntilDepth = self->depth;
|
||||||
self->depth += 1;
|
self->depth += 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (matches(name, SKIP_TAGS, NUM_SKIP_TAGS)) {
|
if (matches(name, SKIP_TAGS, NUM_SKIP_TAGS)) {
|
||||||
// start skip
|
// start skip
|
||||||
@ -127,10 +97,7 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
|
|||||||
if (strcmp(name, "br") == 0) {
|
if (strcmp(name, "br") == 0) {
|
||||||
self->startNewTextBlock(self->currentTextBlock->getStyle());
|
self->startNewTextBlock(self->currentTextBlock->getStyle());
|
||||||
} else {
|
} else {
|
||||||
self->startNewTextBlock((TextBlock::Style)self->paragraphAlignment);
|
self->startNewTextBlock(TextBlock::JUSTIFIED);
|
||||||
if (strcmp(name, "li") == 0) {
|
|
||||||
self->currentTextBlock->addWord("\xe2\x80\xa2", EpdFontFamily::REGULAR);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (matches(name, BOLD_TAGS, NUM_BOLD_TAGS)) {
|
} else if (matches(name, BOLD_TAGS, NUM_BOLD_TAGS)) {
|
||||||
self->boldUntilDepth = std::min(self->boldUntilDepth, self->depth);
|
self->boldUntilDepth = std::min(self->boldUntilDepth, self->depth);
|
||||||
@ -163,46 +130,17 @@ void XMLCALL ChapterHtmlSlimParser::characterData(void* userData, const XML_Char
|
|||||||
// Currently looking at whitespace, if there's anything in the partWordBuffer, flush it
|
// Currently looking at whitespace, if there's anything in the partWordBuffer, flush it
|
||||||
if (self->partWordBufferIndex > 0) {
|
if (self->partWordBufferIndex > 0) {
|
||||||
self->partWordBuffer[self->partWordBufferIndex] = '\0';
|
self->partWordBuffer[self->partWordBufferIndex] = '\0';
|
||||||
self->currentTextBlock->addWord(self->partWordBuffer, fontStyle);
|
self->currentTextBlock->addWord(std::move(replaceHtmlEntities(self->partWordBuffer)), fontStyle);
|
||||||
self->partWordBufferIndex = 0;
|
self->partWordBufferIndex = 0;
|
||||||
}
|
}
|
||||||
// Skip the whitespace char
|
// Skip the whitespace char
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip soft-hyphen with UTF-8 representation (U+00AD) = 0xC2 0xAD
|
|
||||||
const XML_Char SHY_BYTE_1 = static_cast<XML_Char>(0xC2);
|
|
||||||
const XML_Char SHY_BYTE_2 = static_cast<XML_Char>(0xAD);
|
|
||||||
// 1. Check for the start of the 2-byte Soft Hyphen sequence
|
|
||||||
if (s[i] == SHY_BYTE_1) {
|
|
||||||
// 2. Check if the next byte exists AND if it completes the sequence
|
|
||||||
// We must check i + 1 < len to prevent reading past the end of the buffer.
|
|
||||||
if ((i + 1 < len) && (s[i + 1] == SHY_BYTE_2)) {
|
|
||||||
// Sequence 0xC2 0xAD found!
|
|
||||||
// Skip the current byte (0xC2) and the next byte (0xAD)
|
|
||||||
i++; // Increment 'i' one more time to skip the 0xAD byte
|
|
||||||
continue; // Skip the rest of the loop and move to the next iteration
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Skip Zero Width No-Break Space / BOM (U+FEFF) = 0xEF 0xBB 0xBF
|
|
||||||
const XML_Char FEFF_BYTE_1 = static_cast<XML_Char>(0xEF);
|
|
||||||
const XML_Char FEFF_BYTE_2 = static_cast<XML_Char>(0xBB);
|
|
||||||
const XML_Char FEFF_BYTE_3 = static_cast<XML_Char>(0xBF);
|
|
||||||
|
|
||||||
if (s[i] == FEFF_BYTE_1) {
|
|
||||||
// Check if the next two bytes complete the 3-byte sequence
|
|
||||||
if ((i + 2 < len) && (s[i + 1] == FEFF_BYTE_2) && (s[i + 2] == FEFF_BYTE_3)) {
|
|
||||||
// Sequence 0xEF 0xBB 0xBF found!
|
|
||||||
i += 2; // Skip the next two bytes
|
|
||||||
continue; // Move to the next iteration
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we're about to run out of space, then cut the word off and start a new one
|
// If we're about to run out of space, then cut the word off and start a new one
|
||||||
if (self->partWordBufferIndex >= MAX_WORD_SIZE) {
|
if (self->partWordBufferIndex >= MAX_WORD_SIZE) {
|
||||||
self->partWordBuffer[self->partWordBufferIndex] = '\0';
|
self->partWordBuffer[self->partWordBufferIndex] = '\0';
|
||||||
self->currentTextBlock->addWord(self->partWordBuffer, fontStyle);
|
self->currentTextBlock->addWord(std::move(replaceHtmlEntities(self->partWordBuffer)), fontStyle);
|
||||||
self->partWordBufferIndex = 0;
|
self->partWordBufferIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -244,7 +182,7 @@ void XMLCALL ChapterHtmlSlimParser::endElement(void* userData, const XML_Char* n
|
|||||||
}
|
}
|
||||||
|
|
||||||
self->partWordBuffer[self->partWordBufferIndex] = '\0';
|
self->partWordBuffer[self->partWordBufferIndex] = '\0';
|
||||||
self->currentTextBlock->addWord(self->partWordBuffer, fontStyle);
|
self->currentTextBlock->addWord(std::move(replaceHtmlEntities(self->partWordBuffer)), fontStyle);
|
||||||
self->partWordBufferIndex = 0;
|
self->partWordBufferIndex = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -268,7 +206,7 @@ void XMLCALL ChapterHtmlSlimParser::endElement(void* userData, const XML_Char* n
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ChapterHtmlSlimParser::parseAndBuildPages() {
|
bool ChapterHtmlSlimParser::parseAndBuildPages() {
|
||||||
startNewTextBlock((TextBlock::Style)this->paragraphAlignment);
|
startNewTextBlock(TextBlock::JUSTIFIED);
|
||||||
|
|
||||||
const XML_Parser parser = XML_ParserCreate(nullptr);
|
const XML_Parser parser = XML_ParserCreate(nullptr);
|
||||||
int done;
|
int done;
|
||||||
|
|||||||
@ -33,7 +33,6 @@ class ChapterHtmlSlimParser {
|
|||||||
int fontId;
|
int fontId;
|
||||||
float lineCompression;
|
float lineCompression;
|
||||||
bool extraParagraphSpacing;
|
bool extraParagraphSpacing;
|
||||||
uint8_t paragraphAlignment;
|
|
||||||
uint16_t viewportWidth;
|
uint16_t viewportWidth;
|
||||||
uint16_t viewportHeight;
|
uint16_t viewportHeight;
|
||||||
|
|
||||||
@ -47,8 +46,7 @@ class ChapterHtmlSlimParser {
|
|||||||
public:
|
public:
|
||||||
explicit ChapterHtmlSlimParser(const std::string& filepath, GfxRenderer& renderer, const int fontId,
|
explicit ChapterHtmlSlimParser(const std::string& filepath, GfxRenderer& renderer, const int fontId,
|
||||||
const float lineCompression, const bool extraParagraphSpacing,
|
const float lineCompression, const bool extraParagraphSpacing,
|
||||||
const uint8_t paragraphAlignment, const uint16_t viewportWidth,
|
const uint16_t viewportWidth, const uint16_t viewportHeight,
|
||||||
const uint16_t viewportHeight,
|
|
||||||
const std::function<void(std::unique_ptr<Page>)>& completePageFn,
|
const std::function<void(std::unique_ptr<Page>)>& completePageFn,
|
||||||
const std::function<void(int)>& progressFn = nullptr)
|
const std::function<void(int)>& progressFn = nullptr)
|
||||||
: filepath(filepath),
|
: filepath(filepath),
|
||||||
@ -56,7 +54,6 @@ class ChapterHtmlSlimParser {
|
|||||||
fontId(fontId),
|
fontId(fontId),
|
||||||
lineCompression(lineCompression),
|
lineCompression(lineCompression),
|
||||||
extraParagraphSpacing(extraParagraphSpacing),
|
extraParagraphSpacing(extraParagraphSpacing),
|
||||||
paragraphAlignment(paragraphAlignment),
|
|
||||||
viewportWidth(viewportWidth),
|
viewportWidth(viewportWidth),
|
||||||
viewportHeight(viewportHeight),
|
viewportHeight(viewportHeight),
|
||||||
completePageFn(completePageFn),
|
completePageFn(completePageFn),
|
||||||
|
|||||||
@ -161,17 +161,14 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
|
|||||||
std::string itemId;
|
std::string itemId;
|
||||||
std::string href;
|
std::string href;
|
||||||
std::string mediaType;
|
std::string mediaType;
|
||||||
std::string properties;
|
|
||||||
|
|
||||||
for (int i = 0; atts[i]; i += 2) {
|
for (int i = 0; atts[i]; i += 2) {
|
||||||
if (strcmp(atts[i], "id") == 0) {
|
if (strcmp(atts[i], "id") == 0) {
|
||||||
itemId = atts[i + 1];
|
itemId = atts[i + 1];
|
||||||
} else if (strcmp(atts[i], "href") == 0) {
|
} else if (strcmp(atts[i], "href") == 0) {
|
||||||
href = FsHelpers::normalisePath(self->baseContentPath + atts[i + 1]);
|
href = self->baseContentPath + atts[i + 1];
|
||||||
} else if (strcmp(atts[i], "media-type") == 0) {
|
} else if (strcmp(atts[i], "media-type") == 0) {
|
||||||
mediaType = atts[i + 1];
|
mediaType = atts[i + 1];
|
||||||
} else if (strcmp(atts[i], "properties") == 0) {
|
|
||||||
properties = atts[i + 1];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,15 +188,6 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
|
|||||||
href.c_str());
|
href.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EPUB 3: Check for nav document (properties contains "nav")
|
|
||||||
if (!properties.empty() && self->tocNavPath.empty()) {
|
|
||||||
// Properties is space-separated, check if "nav" is present as a word
|
|
||||||
if (properties == "nav" || properties.find("nav ") == 0 || properties.find(" nav") != std::string::npos) {
|
|
||||||
self->tocNavPath = href;
|
|
||||||
Serial.printf("[%lu] [COF] Found EPUB 3 nav document: %s\n", millis(), href.c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,7 +231,7 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (strcmp(atts[i], "href") == 0) {
|
} else if (strcmp(atts[i], "href") == 0) {
|
||||||
textHref = FsHelpers::normalisePath(self->baseContentPath + atts[i + 1]);
|
textHref = self->baseContentPath + atts[i + 1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((type == "text" || (type == "start" && !self->textReferenceHref.empty())) && (textHref.length() > 0)) {
|
if ((type == "text" || (type == "start" && !self->textReferenceHref.empty())) && (textHref.length() > 0)) {
|
||||||
|
|||||||
@ -35,7 +35,6 @@ class ContentOpfParser final : public Print {
|
|||||||
std::string title;
|
std::string title;
|
||||||
std::string author;
|
std::string author;
|
||||||
std::string tocNcxPath;
|
std::string tocNcxPath;
|
||||||
std::string tocNavPath; // EPUB 3 nav document path
|
|
||||||
std::string coverItemHref;
|
std::string coverItemHref;
|
||||||
std::string textReferenceHref;
|
std::string textReferenceHref;
|
||||||
|
|
||||||
|
|||||||
@ -1,185 +0,0 @@
|
|||||||
#include "TocNavParser.h"
|
|
||||||
|
|
||||||
#include <FsHelpers.h>
|
|
||||||
#include <HardwareSerial.h>
|
|
||||||
|
|
||||||
#include "../BookMetadataCache.h"
|
|
||||||
|
|
||||||
bool TocNavParser::setup() {
|
|
||||||
parser = XML_ParserCreate(nullptr);
|
|
||||||
if (!parser) {
|
|
||||||
Serial.printf("[%lu] [NAV] Couldn't allocate memory for parser\n", millis());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
XML_SetUserData(parser, this);
|
|
||||||
XML_SetElementHandler(parser, startElement, endElement);
|
|
||||||
XML_SetCharacterDataHandler(parser, characterData);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
TocNavParser::~TocNavParser() {
|
|
||||||
if (parser) {
|
|
||||||
XML_StopParser(parser, XML_FALSE);
|
|
||||||
XML_SetElementHandler(parser, nullptr, nullptr);
|
|
||||||
XML_SetCharacterDataHandler(parser, nullptr);
|
|
||||||
XML_ParserFree(parser);
|
|
||||||
parser = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t TocNavParser::write(const uint8_t data) { return write(&data, 1); }
|
|
||||||
|
|
||||||
size_t TocNavParser::write(const uint8_t* buffer, const size_t size) {
|
|
||||||
if (!parser) return 0;
|
|
||||||
|
|
||||||
const uint8_t* currentBufferPos = buffer;
|
|
||||||
auto remainingInBuffer = size;
|
|
||||||
|
|
||||||
while (remainingInBuffer > 0) {
|
|
||||||
void* const buf = XML_GetBuffer(parser, 1024);
|
|
||||||
if (!buf) {
|
|
||||||
Serial.printf("[%lu] [NAV] Couldn't allocate memory for buffer\n", millis());
|
|
||||||
XML_StopParser(parser, XML_FALSE);
|
|
||||||
XML_SetElementHandler(parser, nullptr, nullptr);
|
|
||||||
XML_SetCharacterDataHandler(parser, nullptr);
|
|
||||||
XML_ParserFree(parser);
|
|
||||||
parser = nullptr;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto toRead = remainingInBuffer < 1024 ? remainingInBuffer : 1024;
|
|
||||||
memcpy(buf, currentBufferPos, toRead);
|
|
||||||
|
|
||||||
if (XML_ParseBuffer(parser, static_cast<int>(toRead), remainingSize == toRead) == XML_STATUS_ERROR) {
|
|
||||||
Serial.printf("[%lu] [NAV] Parse error at line %lu: %s\n", millis(), XML_GetCurrentLineNumber(parser),
|
|
||||||
XML_ErrorString(XML_GetErrorCode(parser)));
|
|
||||||
XML_StopParser(parser, XML_FALSE);
|
|
||||||
XML_SetElementHandler(parser, nullptr, nullptr);
|
|
||||||
XML_SetCharacterDataHandler(parser, nullptr);
|
|
||||||
XML_ParserFree(parser);
|
|
||||||
parser = nullptr;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentBufferPos += toRead;
|
|
||||||
remainingInBuffer -= toRead;
|
|
||||||
remainingSize -= toRead;
|
|
||||||
}
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void XMLCALL TocNavParser::startElement(void* userData, const XML_Char* name, const XML_Char** atts) {
|
|
||||||
auto* self = static_cast<TocNavParser*>(userData);
|
|
||||||
|
|
||||||
// Track HTML structure loosely - we mainly care about finding <nav epub:type="toc">
|
|
||||||
if (strcmp(name, "html") == 0) {
|
|
||||||
self->state = IN_HTML;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self->state == IN_HTML && strcmp(name, "body") == 0) {
|
|
||||||
self->state = IN_BODY;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look for <nav epub:type="toc"> anywhere in body (or nested elements)
|
|
||||||
if (self->state >= IN_BODY && strcmp(name, "nav") == 0) {
|
|
||||||
for (int i = 0; atts[i]; i += 2) {
|
|
||||||
if ((strcmp(atts[i], "epub:type") == 0 || strcmp(atts[i], "type") == 0) && strcmp(atts[i + 1], "toc") == 0) {
|
|
||||||
self->state = IN_NAV_TOC;
|
|
||||||
Serial.printf("[%lu] [NAV] Found nav toc element\n", millis());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only process ol/li/a if we're inside the toc nav
|
|
||||||
if (self->state < IN_NAV_TOC) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(name, "ol") == 0) {
|
|
||||||
self->olDepth++;
|
|
||||||
self->state = IN_OL;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self->state == IN_OL && strcmp(name, "li") == 0) {
|
|
||||||
self->state = IN_LI;
|
|
||||||
self->currentLabel.clear();
|
|
||||||
self->currentHref.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self->state == IN_LI && strcmp(name, "a") == 0) {
|
|
||||||
self->state = IN_ANCHOR;
|
|
||||||
// Get href attribute
|
|
||||||
for (int i = 0; atts[i]; i += 2) {
|
|
||||||
if (strcmp(atts[i], "href") == 0) {
|
|
||||||
self->currentHref = atts[i + 1];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void XMLCALL TocNavParser::characterData(void* userData, const XML_Char* s, const int len) {
|
|
||||||
auto* self = static_cast<TocNavParser*>(userData);
|
|
||||||
|
|
||||||
// Only collect text when inside an anchor within the TOC nav
|
|
||||||
if (self->state == IN_ANCHOR) {
|
|
||||||
self->currentLabel.append(s, len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void XMLCALL TocNavParser::endElement(void* userData, const XML_Char* name) {
|
|
||||||
auto* self = static_cast<TocNavParser*>(userData);
|
|
||||||
|
|
||||||
if (strcmp(name, "a") == 0 && self->state == IN_ANCHOR) {
|
|
||||||
// Create TOC entry when closing anchor tag (we have all data now)
|
|
||||||
if (!self->currentLabel.empty() && !self->currentHref.empty()) {
|
|
||||||
std::string href = FsHelpers::normalisePath(self->baseContentPath + self->currentHref);
|
|
||||||
std::string anchor;
|
|
||||||
|
|
||||||
const size_t pos = href.find('#');
|
|
||||||
if (pos != std::string::npos) {
|
|
||||||
anchor = href.substr(pos + 1);
|
|
||||||
href = href.substr(0, pos);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (self->cache) {
|
|
||||||
// olDepth gives us the nesting level (1-based from the outer ol)
|
|
||||||
self->cache->createTocEntry(self->currentLabel, href, anchor, self->olDepth);
|
|
||||||
}
|
|
||||||
|
|
||||||
self->currentLabel.clear();
|
|
||||||
self->currentHref.clear();
|
|
||||||
}
|
|
||||||
self->state = IN_LI;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(name, "li") == 0 && (self->state == IN_LI || self->state == IN_OL)) {
|
|
||||||
self->state = IN_OL;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(name, "ol") == 0 && self->state >= IN_NAV_TOC) {
|
|
||||||
self->olDepth--;
|
|
||||||
if (self->olDepth == 0) {
|
|
||||||
self->state = IN_NAV_TOC;
|
|
||||||
} else {
|
|
||||||
self->state = IN_LI; // Back to parent li
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(name, "nav") == 0 && self->state >= IN_NAV_TOC) {
|
|
||||||
self->state = IN_BODY;
|
|
||||||
Serial.printf("[%lu] [NAV] Finished parsing nav toc\n", millis());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,47 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <Print.h>
|
|
||||||
#include <expat.h>
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class BookMetadataCache;
|
|
||||||
|
|
||||||
// Parser for EPUB 3 nav.xhtml navigation documents
|
|
||||||
// Parses HTML5 nav elements with epub:type="toc" to extract table of contents
|
|
||||||
class TocNavParser final : public Print {
|
|
||||||
enum ParserState {
|
|
||||||
START,
|
|
||||||
IN_HTML,
|
|
||||||
IN_BODY,
|
|
||||||
IN_NAV_TOC, // Inside <nav epub:type="toc">
|
|
||||||
IN_OL, // Inside <ol>
|
|
||||||
IN_LI, // Inside <li>
|
|
||||||
IN_ANCHOR, // Inside <a>
|
|
||||||
};
|
|
||||||
|
|
||||||
const std::string& baseContentPath;
|
|
||||||
size_t remainingSize;
|
|
||||||
XML_Parser parser = nullptr;
|
|
||||||
ParserState state = START;
|
|
||||||
BookMetadataCache* cache;
|
|
||||||
|
|
||||||
// Track nesting depth for <ol> elements to determine TOC depth
|
|
||||||
uint8_t olDepth = 0;
|
|
||||||
// Current entry data being collected
|
|
||||||
std::string currentLabel;
|
|
||||||
std::string currentHref;
|
|
||||||
|
|
||||||
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:
|
|
||||||
explicit TocNavParser(const std::string& baseContentPath, const size_t xmlSize, BookMetadataCache* cache)
|
|
||||||
: baseContentPath(baseContentPath), remainingSize(xmlSize), cache(cache) {}
|
|
||||||
~TocNavParser() override;
|
|
||||||
|
|
||||||
bool setup();
|
|
||||||
|
|
||||||
size_t write(uint8_t) override;
|
|
||||||
size_t write(const uint8_t* buffer, size_t size) override;
|
|
||||||
};
|
|
||||||
@ -1,6 +1,5 @@
|
|||||||
#include "TocNcxParser.h"
|
#include "TocNcxParser.h"
|
||||||
|
|
||||||
#include <FsHelpers.h>
|
|
||||||
#include <HardwareSerial.h>
|
#include <HardwareSerial.h>
|
||||||
|
|
||||||
#include "../BookMetadataCache.h"
|
#include "../BookMetadataCache.h"
|
||||||
@ -160,7 +159,7 @@ void XMLCALL TocNcxParser::endElement(void* userData, const XML_Char* name) {
|
|||||||
// This is the safest place to push the data, assuming <navLabel> always comes before <content>.
|
// This is the safest place to push the data, assuming <navLabel> always comes before <content>.
|
||||||
// NCX spec says navLabel comes before content.
|
// NCX spec says navLabel comes before content.
|
||||||
if (!self->currentLabel.empty() && !self->currentSrc.empty()) {
|
if (!self->currentLabel.empty() && !self->currentSrc.empty()) {
|
||||||
std::string href = FsHelpers::normalisePath(self->baseContentPath + self->currentSrc);
|
std::string href = self->baseContentPath + self->currentSrc;
|
||||||
std::string anchor;
|
std::string anchor;
|
||||||
|
|
||||||
const size_t pos = href.find('#');
|
const size_t pos = href.find('#');
|
||||||
|
|||||||
@ -8,15 +8,119 @@
|
|||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Note: For cover images, dithering is done in JpegToBmpConverter.cpp
|
// Note: For cover images, dithering is done in JpegToBmpConverter.cpp
|
||||||
// This file handles BMP reading - use simple quantization to avoid double-dithering
|
// This file handles BMP reading - use simple quantization to avoid double-dithering
|
||||||
constexpr bool USE_ATKINSON = true; // Use Atkinson dithering instead of Floyd-Steinberg
|
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() {
|
Bitmap::~Bitmap() {
|
||||||
delete[] errorCurRow;
|
delete[] errorCurRow;
|
||||||
delete[] errorNextRow;
|
delete[] errorNextRow;
|
||||||
|
|
||||||
delete atkinsonDitherer;
|
|
||||||
delete fsDitherer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Bitmap::readLE16(FsFile& f) {
|
uint16_t Bitmap::readLE16(FsFile& f) {
|
||||||
@ -140,25 +244,40 @@ BmpReaderError Bitmap::parseHeaders() {
|
|||||||
return BmpReaderError::SeekPixelDataFailed;
|
return BmpReaderError::SeekPixelDataFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create ditherer if enabled (only for 2-bit output)
|
// Allocate Floyd-Steinberg error buffers if enabled
|
||||||
// Use OUTPUT dimensions for dithering (after prescaling)
|
if (USE_FLOYD_STEINBERG) {
|
||||||
if (bpp > 2 && dithering) {
|
delete[] errorCurRow;
|
||||||
if (USE_ATKINSON) {
|
delete[] errorNextRow;
|
||||||
atkinsonDitherer = new AtkinsonDitherer(width);
|
errorCurRow = new int16_t[width + 2](); // +2 for boundary handling
|
||||||
} else {
|
errorNextRow = new int16_t[width + 2]();
|
||||||
fsDitherer = new FloydSteinbergDitherer(width);
|
lastRowY = -1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return BmpReaderError::Ok;
|
return BmpReaderError::Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
// packed 2bpp output, 0 = black, 1 = dark gray, 2 = light gray, 3 = white
|
// packed 2bpp output, 0 = black, 1 = dark gray, 2 = light gray, 3 = white
|
||||||
BmpReaderError Bitmap::readNextRow(uint8_t* data, uint8_t* rowBuffer) const {
|
BmpReaderError Bitmap::readRow(uint8_t* data, uint8_t* rowBuffer, int rowY) const {
|
||||||
// Note: rowBuffer should be pre-allocated by the caller to size 'rowBytes'
|
// Note: rowBuffer should be pre-allocated by the caller to size 'rowBytes'
|
||||||
if (file.read(rowBuffer, rowBytes) != rowBytes) return BmpReaderError::ShortReadRow;
|
if (file.read(rowBuffer, rowBytes) != rowBytes) return BmpReaderError::ShortReadRow;
|
||||||
|
|
||||||
prevRowY += 1;
|
// 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* outPtr = data;
|
||||||
uint8_t currentOutByte = 0;
|
uint8_t currentOutByte = 0;
|
||||||
@ -168,18 +287,12 @@ BmpReaderError Bitmap::readNextRow(uint8_t* data, uint8_t* rowBuffer) const {
|
|||||||
// Helper lambda to pack 2bpp color into the output stream
|
// Helper lambda to pack 2bpp color into the output stream
|
||||||
auto packPixel = [&](const uint8_t lum) {
|
auto packPixel = [&](const uint8_t lum) {
|
||||||
uint8_t color;
|
uint8_t color;
|
||||||
if (atkinsonDitherer) {
|
if (useFS) {
|
||||||
color = atkinsonDitherer->processPixel(adjustPixel(lum), currentX);
|
// Floyd-Steinberg error diffusion
|
||||||
} else if (fsDitherer) {
|
color = quantizeFloydSteinberg(lum, currentX, width, errorCurRow, errorNextRow, false);
|
||||||
color = fsDitherer->processPixel(adjustPixel(lum), currentX);
|
|
||||||
} else {
|
} else {
|
||||||
if (bpp > 2) {
|
|
||||||
// Simple quantization or noise dithering
|
// Simple quantization or noise dithering
|
||||||
color = quantize(adjustPixel(lum), currentX, prevRowY);
|
color = quantize(lum, currentX, rowY);
|
||||||
} else {
|
|
||||||
// do not quantize 2bpp image
|
|
||||||
color = static_cast<uint8_t>(lum >> 6);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
currentOutByte |= (color << bitShift);
|
currentOutByte |= (color << bitShift);
|
||||||
if (bitShift == 0) {
|
if (bitShift == 0) {
|
||||||
@ -228,10 +341,7 @@ BmpReaderError Bitmap::readNextRow(uint8_t* data, uint8_t* rowBuffer) const {
|
|||||||
}
|
}
|
||||||
case 1: {
|
case 1: {
|
||||||
for (int x = 0; x < width; x++) {
|
for (int x = 0; x < width; x++) {
|
||||||
// Get palette index (0 or 1) from bit at position x
|
lum = (rowBuffer[x >> 3] & (0x80 >> (x & 7))) ? 0xFF : 0x00;
|
||||||
const uint8_t palIndex = (rowBuffer[x >> 3] & (0x80 >> (x & 7))) ? 1 : 0;
|
|
||||||
// Use palette lookup for proper black/white mapping
|
|
||||||
lum = paletteLum[palIndex];
|
|
||||||
packPixel(lum);
|
packPixel(lum);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -240,11 +350,6 @@ BmpReaderError Bitmap::readNextRow(uint8_t* data, uint8_t* rowBuffer) const {
|
|||||||
return BmpReaderError::UnsupportedBpp;
|
return BmpReaderError::UnsupportedBpp;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (atkinsonDitherer)
|
|
||||||
atkinsonDitherer->nextRow();
|
|
||||||
else if (fsDitherer)
|
|
||||||
fsDitherer->nextRow();
|
|
||||||
|
|
||||||
// Flush remaining bits if width is not a multiple of 4
|
// Flush remaining bits if width is not a multiple of 4
|
||||||
if (bitShift != 6) *outPtr = currentOutByte;
|
if (bitShift != 6) *outPtr = currentOutByte;
|
||||||
|
|
||||||
@ -256,9 +361,12 @@ BmpReaderError Bitmap::rewindToData() const {
|
|||||||
return BmpReaderError::SeekPixelDataFailed;
|
return BmpReaderError::SeekPixelDataFailed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset dithering when rewinding
|
// Reset Floyd-Steinberg error buffers when rewinding
|
||||||
if (fsDitherer) fsDitherer->reset();
|
if (USE_FLOYD_STEINBERG && errorCurRow && errorNextRow) {
|
||||||
if (atkinsonDitherer) atkinsonDitherer->reset();
|
memset(errorCurRow, 0, (width + 2) * sizeof(int16_t));
|
||||||
|
memset(errorNextRow, 0, (width + 2) * sizeof(int16_t));
|
||||||
|
lastRowY = -1;
|
||||||
|
}
|
||||||
|
|
||||||
return BmpReaderError::Ok;
|
return BmpReaderError::Ok;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,10 +2,6 @@
|
|||||||
|
|
||||||
#include <SdFat.h>
|
#include <SdFat.h>
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
#include "BitmapHelpers.h"
|
|
||||||
|
|
||||||
enum class BmpReaderError : uint8_t {
|
enum class BmpReaderError : uint8_t {
|
||||||
Ok = 0,
|
Ok = 0,
|
||||||
FileInvalid,
|
FileInvalid,
|
||||||
@ -32,25 +28,22 @@ class Bitmap {
|
|||||||
public:
|
public:
|
||||||
static const char* errorToString(BmpReaderError err);
|
static const char* errorToString(BmpReaderError err);
|
||||||
|
|
||||||
explicit Bitmap(FsFile& file, bool dithering = false) : file(file), dithering(dithering) {}
|
explicit Bitmap(FsFile& file) : file(file) {}
|
||||||
~Bitmap();
|
~Bitmap();
|
||||||
BmpReaderError parseHeaders();
|
BmpReaderError parseHeaders();
|
||||||
BmpReaderError readNextRow(uint8_t* data, uint8_t* rowBuffer) const;
|
BmpReaderError readRow(uint8_t* data, uint8_t* rowBuffer, int rowY) const;
|
||||||
BmpReaderError rewindToData() const;
|
BmpReaderError rewindToData() const;
|
||||||
int getWidth() const { return width; }
|
int getWidth() const { return width; }
|
||||||
int getHeight() const { return height; }
|
int getHeight() const { return height; }
|
||||||
bool isTopDown() const { return topDown; }
|
bool isTopDown() const { return topDown; }
|
||||||
bool hasGreyscale() const { return bpp > 1; }
|
bool hasGreyscale() const { return bpp > 1; }
|
||||||
int getRowBytes() const { return rowBytes; }
|
int getRowBytes() const { return rowBytes; }
|
||||||
bool is1Bit() const { return bpp == 1; }
|
|
||||||
uint16_t getBpp() const { return bpp; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static uint16_t readLE16(FsFile& f);
|
static uint16_t readLE16(FsFile& f);
|
||||||
static uint32_t readLE32(FsFile& f);
|
static uint32_t readLE32(FsFile& f);
|
||||||
|
|
||||||
FsFile& file;
|
FsFile& file;
|
||||||
bool dithering = false;
|
|
||||||
int width = 0;
|
int width = 0;
|
||||||
int height = 0;
|
int height = 0;
|
||||||
bool topDown = false;
|
bool topDown = false;
|
||||||
@ -62,8 +55,5 @@ class Bitmap {
|
|||||||
// Floyd-Steinberg dithering state (mutable for const methods)
|
// Floyd-Steinberg dithering state (mutable for const methods)
|
||||||
mutable int16_t* errorCurRow = nullptr;
|
mutable int16_t* errorCurRow = nullptr;
|
||||||
mutable int16_t* errorNextRow = nullptr;
|
mutable int16_t* errorNextRow = nullptr;
|
||||||
mutable int prevRowY = -1; // Track row progression for error propagation
|
mutable int lastRowY = -1; // Track row progression for error propagation
|
||||||
|
|
||||||
mutable AtkinsonDitherer* atkinsonDitherer = nullptr;
|
|
||||||
mutable FloydSteinbergDitherer* fsDitherer = nullptr;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,106 +0,0 @@
|
|||||||
#include "BitmapHelpers.h"
|
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
// Brightness/Contrast adjustments:
|
|
||||||
constexpr bool USE_BRIGHTNESS = false; // true: apply brightness/gamma adjustments
|
|
||||||
constexpr int BRIGHTNESS_BOOST = 10; // Brightness offset (0-50)
|
|
||||||
constexpr bool GAMMA_CORRECTION = false; // Gamma curve (brightens midtones)
|
|
||||||
constexpr float CONTRAST_FACTOR = 1.15f; // Contrast multiplier (1.0 = no change, >1 = more contrast)
|
|
||||||
constexpr bool USE_NOISE_DITHERING = false; // Hash-based noise dithering
|
|
||||||
|
|
||||||
// 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
|
|
||||||
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 - divide into 4 levels
|
|
||||||
// The thresholds are fine-tuned to the X4 display
|
|
||||||
uint8_t quantizeSimple(int gray) {
|
|
||||||
if (gray < 45) {
|
|
||||||
return 0;
|
|
||||||
} else if (gray < 70) {
|
|
||||||
return 1;
|
|
||||||
} else if (gray < 140) {
|
|
||||||
return 2;
|
|
||||||
} else {
|
|
||||||
return 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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) {
|
|
||||||
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 - selects between methods based on config
|
|
||||||
uint8_t quantize(int gray, int x, int y) {
|
|
||||||
if (USE_NOISE_DITHERING) {
|
|
||||||
return quantizeNoise(gray, x, y);
|
|
||||||
} else {
|
|
||||||
return quantizeSimple(gray);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1-bit noise dithering for fast home screen rendering
|
|
||||||
// Uses hash-based noise for consistent dithering that works well at small sizes
|
|
||||||
uint8_t quantize1bit(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
|
|
||||||
|
|
||||||
// Simple threshold with noise: gray >= (128 + noise offset) -> white
|
|
||||||
// The noise adds variation around the 128 midpoint
|
|
||||||
const int adjustedThreshold = 128 + ((threshold - 128) / 2); // Range: 64-192
|
|
||||||
return (gray >= adjustedThreshold) ? 1 : 0;
|
|
||||||
}
|
|
||||||
@ -1,314 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
// Helper functions
|
|
||||||
uint8_t quantize(int gray, int x, int y);
|
|
||||||
uint8_t quantizeSimple(int gray);
|
|
||||||
uint8_t quantize1bit(int gray, int x, int y);
|
|
||||||
int adjustPixel(int gray);
|
|
||||||
|
|
||||||
// 1-bit Atkinson dithering - better quality than noise dithering for thumbnails
|
|
||||||
// Error distribution pattern (same as 2-bit but quantizes to 2 levels):
|
|
||||||
// X 1/8 1/8
|
|
||||||
// 1/8 1/8 1/8
|
|
||||||
// 1/8
|
|
||||||
class Atkinson1BitDitherer {
|
|
||||||
public:
|
|
||||||
explicit Atkinson1BitDitherer(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
|
|
||||||
}
|
|
||||||
|
|
||||||
~Atkinson1BitDitherer() {
|
|
||||||
delete[] errorRow0;
|
|
||||||
delete[] errorRow1;
|
|
||||||
delete[] errorRow2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// EXPLICITLY DELETE THE COPY CONSTRUCTOR
|
|
||||||
Atkinson1BitDitherer(const Atkinson1BitDitherer& other) = delete;
|
|
||||||
|
|
||||||
// EXPLICITLY DELETE THE COPY ASSIGNMENT OPERATOR
|
|
||||||
Atkinson1BitDitherer& operator=(const Atkinson1BitDitherer& other) = delete;
|
|
||||||
|
|
||||||
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 2 levels (1-bit): 0 = black, 1 = white
|
|
||||||
uint8_t quantized;
|
|
||||||
int quantizedValue;
|
|
||||||
if (adjusted < 128) {
|
|
||||||
quantized = 0;
|
|
||||||
quantizedValue = 0;
|
|
||||||
} else {
|
|
||||||
quantized = 1;
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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:
|
|
||||||
explicit 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;
|
|
||||||
}
|
|
||||||
// **1. EXPLICITLY DELETE THE COPY CONSTRUCTOR**
|
|
||||||
AtkinsonDitherer(const AtkinsonDitherer& other) = delete;
|
|
||||||
|
|
||||||
// **2. EXPLICITLY DELETE THE COPY ASSIGNMENT OPERATOR**
|
|
||||||
AtkinsonDitherer& operator=(const AtkinsonDitherer& other) = delete;
|
|
||||||
|
|
||||||
uint8_t processPixel(int gray, int x) {
|
|
||||||
// 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 (false) { // original thresholds
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
} else { // fine-tuned to X4 eink display
|
|
||||||
if (adjusted < 30) {
|
|
||||||
quantized = 0;
|
|
||||||
quantizedValue = 15;
|
|
||||||
} else if (adjusted < 50) {
|
|
||||||
quantized = 1;
|
|
||||||
quantizedValue = 30;
|
|
||||||
} else if (adjusted < 140) {
|
|
||||||
quantized = 2;
|
|
||||||
quantizedValue = 80;
|
|
||||||
} else {
|
|
||||||
quantized = 3;
|
|
||||||
quantizedValue = 210;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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:
|
|
||||||
explicit 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// **1. EXPLICITLY DELETE THE COPY CONSTRUCTOR**
|
|
||||||
FloydSteinbergDitherer(const FloydSteinbergDitherer& other) = delete;
|
|
||||||
|
|
||||||
// **2. EXPLICITLY DELETE THE COPY ASSIGNMENT OPERATOR**
|
|
||||||
FloydSteinbergDitherer& operator=(const FloydSteinbergDitherer& other) = delete;
|
|
||||||
|
|
||||||
// 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) {
|
|
||||||
// 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 (false) { // original thresholds
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
} else { // fine-tuned to X4 eink display
|
|
||||||
if (adjusted < 30) {
|
|
||||||
quantized = 0;
|
|
||||||
quantizedValue = 15;
|
|
||||||
} else if (adjusted < 50) {
|
|
||||||
quantized = 1;
|
|
||||||
quantizedValue = 30;
|
|
||||||
} else if (adjusted < 140) {
|
|
||||||
quantized = 2;
|
|
||||||
quantizedValue = 80;
|
|
||||||
} else {
|
|
||||||
quantized = 3;
|
|
||||||
quantizedValue = 210;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate error
|
|
||||||
int error = adjusted - quantizedValue;
|
|
||||||
|
|
||||||
// Distribute error to neighbors (serpentine: direction-aware)
|
|
||||||
if (!isReverseRow()) {
|
|
||||||
// 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;
|
|
||||||
};
|
|
||||||
@ -152,30 +152,18 @@ void GfxRenderer::drawImage(const uint8_t bitmap[], const int x, const int y, co
|
|||||||
einkDisplay.drawImage(bitmap, rotatedX, rotatedY, width, height);
|
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,
|
void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, const int maxWidth,
|
||||||
const float cropX, const float cropY) const {
|
const int maxHeight) const {
|
||||||
// For 1-bit bitmaps, use optimized 1-bit rendering path (no crop support for 1-bit)
|
|
||||||
if (bitmap.is1Bit() && cropX == 0.0f && cropY == 0.0f) {
|
|
||||||
drawBitmap1Bit(bitmap, x, y, maxWidth, maxHeight);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
float scale = 1.0f;
|
float scale = 1.0f;
|
||||||
bool isScaled = false;
|
bool isScaled = false;
|
||||||
int cropPixX = std::floor(bitmap.getWidth() * cropX / 2.0f);
|
if (maxWidth > 0 && bitmap.getWidth() > maxWidth) {
|
||||||
int cropPixY = std::floor(bitmap.getHeight() * cropY / 2.0f);
|
scale = static_cast<float>(maxWidth) / static_cast<float>(bitmap.getWidth());
|
||||||
Serial.printf("[%lu] [GFX] Cropping %dx%d by %dx%d pix, is %s\n", millis(), bitmap.getWidth(), bitmap.getHeight(),
|
|
||||||
cropPixX, cropPixY, bitmap.isTopDown() ? "top-down" : "bottom-up");
|
|
||||||
|
|
||||||
if (maxWidth > 0 && (1.0f - cropX) * bitmap.getWidth() > maxWidth) {
|
|
||||||
scale = static_cast<float>(maxWidth) / static_cast<float>((1.0f - cropX) * bitmap.getWidth());
|
|
||||||
isScaled = true;
|
isScaled = true;
|
||||||
}
|
}
|
||||||
if (maxHeight > 0 && (1.0f - cropY) * bitmap.getHeight() > maxHeight) {
|
if (maxHeight > 0 && bitmap.getHeight() > maxHeight) {
|
||||||
scale = std::min(scale, static_cast<float>(maxHeight) / static_cast<float>((1.0f - cropY) * bitmap.getHeight()));
|
scale = std::min(scale, static_cast<float>(maxHeight) / static_cast<float>(bitmap.getHeight()));
|
||||||
isScaled = true;
|
isScaled = true;
|
||||||
}
|
}
|
||||||
Serial.printf("[%lu] [GFX] Scaling by %f - %s\n", millis(), scale, isScaled ? "scaled" : "not scaled");
|
|
||||||
|
|
||||||
// Calculate output row size (2 bits per pixel, packed into bytes)
|
// 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
|
// IMPORTANT: Use int, not uint8_t, to avoid overflow for images > 1020 pixels wide
|
||||||
@ -190,45 +178,32 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int bmpY = 0; bmpY < (bitmap.getHeight() - cropPixY); bmpY++) {
|
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).
|
// 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.
|
// Screen's (0, 0) is the top-left corner.
|
||||||
int screenY = -cropPixY + (bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY);
|
int screenY = y + (bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY);
|
||||||
if (isScaled) {
|
if (isScaled) {
|
||||||
screenY = std::floor(screenY * scale);
|
screenY = std::floor(screenY * scale);
|
||||||
}
|
}
|
||||||
screenY += y; // the offset should not be scaled
|
|
||||||
if (screenY >= getScreenHeight()) {
|
if (screenY >= getScreenHeight()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (screenY < 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) {
|
if (bitmap.readRow(outputRow, rowBytes, bmpY) != BmpReaderError::Ok) {
|
||||||
Serial.printf("[%lu] [GFX] Failed to read row %d from bitmap\n", millis(), bmpY);
|
Serial.printf("[%lu] [GFX] Failed to read row %d from bitmap\n", millis(), bmpY);
|
||||||
free(outputRow);
|
free(outputRow);
|
||||||
free(rowBytes);
|
free(rowBytes);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bmpY < cropPixY) {
|
for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) {
|
||||||
// Skip the row if it's outside the crop area
|
int screenX = x + bmpX;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int bmpX = cropPixX; bmpX < bitmap.getWidth() - cropPixX; bmpX++) {
|
|
||||||
int screenX = bmpX - cropPixX;
|
|
||||||
if (isScaled) {
|
if (isScaled) {
|
||||||
screenX = std::floor(screenX * scale);
|
screenX = std::floor(screenX * scale);
|
||||||
}
|
}
|
||||||
screenX += x; // the offset should not be scaled
|
|
||||||
if (screenX >= getScreenWidth()) {
|
if (screenX >= getScreenWidth()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (screenX < 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
||||||
|
|
||||||
@ -246,143 +221,6 @@ void GfxRenderer::drawBitmap(const Bitmap& bitmap, const int x, const int y, con
|
|||||||
free(rowBytes);
|
free(rowBytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GfxRenderer::drawBitmap1Bit(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// For 1-bit BMP, output is still 2-bit packed (for consistency with readNextRow)
|
|
||||||
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 1-bit BMP row buffers\n", millis());
|
|
||||||
free(outputRow);
|
|
||||||
free(rowBytes);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int bmpY = 0; bmpY < bitmap.getHeight(); bmpY++) {
|
|
||||||
// Read rows sequentially using readNextRow
|
|
||||||
if (bitmap.readNextRow(outputRow, rowBytes) != BmpReaderError::Ok) {
|
|
||||||
Serial.printf("[%lu] [GFX] Failed to read row %d from 1-bit bitmap\n", millis(), bmpY);
|
|
||||||
free(outputRow);
|
|
||||||
free(rowBytes);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate screen Y based on whether BMP is top-down or bottom-up
|
|
||||||
const int bmpYOffset = bitmap.isTopDown() ? bmpY : bitmap.getHeight() - 1 - bmpY;
|
|
||||||
int screenY = y + (isScaled ? static_cast<int>(std::floor(bmpYOffset * scale)) : bmpYOffset);
|
|
||||||
if (screenY >= getScreenHeight()) {
|
|
||||||
continue; // Continue reading to keep row counter in sync
|
|
||||||
}
|
|
||||||
if (screenY < 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int bmpX = 0; bmpX < bitmap.getWidth(); bmpX++) {
|
|
||||||
int screenX = x + (isScaled ? static_cast<int>(std::floor(bmpX * scale)) : bmpX);
|
|
||||||
if (screenX >= getScreenWidth()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (screenX < 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get 2-bit value (result of readNextRow quantization)
|
|
||||||
const uint8_t val = outputRow[bmpX / 4] >> (6 - ((bmpX * 2) % 8)) & 0x3;
|
|
||||||
|
|
||||||
// For 1-bit source: 0 or 1 -> map to black (0,1,2) or white (3)
|
|
||||||
// val < 3 means black pixel (draw it)
|
|
||||||
if (val < 3) {
|
|
||||||
drawPixel(screenX, screenY, true);
|
|
||||||
}
|
|
||||||
// White pixels (val == 3) are not drawn (leave background)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(outputRow);
|
|
||||||
free(rowBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GfxRenderer::fillPolygon(const int* xPoints, const int* yPoints, int numPoints, bool state) const {
|
|
||||||
if (numPoints < 3) return;
|
|
||||||
|
|
||||||
// Find bounding box
|
|
||||||
int minY = yPoints[0], maxY = yPoints[0];
|
|
||||||
for (int i = 1; i < numPoints; i++) {
|
|
||||||
if (yPoints[i] < minY) minY = yPoints[i];
|
|
||||||
if (yPoints[i] > maxY) maxY = yPoints[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clip to screen
|
|
||||||
if (minY < 0) minY = 0;
|
|
||||||
if (maxY >= getScreenHeight()) maxY = getScreenHeight() - 1;
|
|
||||||
|
|
||||||
// Allocate node buffer for scanline algorithm
|
|
||||||
auto* nodeX = static_cast<int*>(malloc(numPoints * sizeof(int)));
|
|
||||||
if (!nodeX) {
|
|
||||||
Serial.printf("[%lu] [GFX] !! Failed to allocate polygon node buffer\n", millis());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scanline fill algorithm
|
|
||||||
for (int scanY = minY; scanY <= maxY; scanY++) {
|
|
||||||
int nodes = 0;
|
|
||||||
|
|
||||||
// Find all intersection points with edges
|
|
||||||
int j = numPoints - 1;
|
|
||||||
for (int i = 0; i < numPoints; i++) {
|
|
||||||
if ((yPoints[i] < scanY && yPoints[j] >= scanY) || (yPoints[j] < scanY && yPoints[i] >= scanY)) {
|
|
||||||
// Calculate X intersection using fixed-point to avoid float
|
|
||||||
int dy = yPoints[j] - yPoints[i];
|
|
||||||
if (dy != 0) {
|
|
||||||
nodeX[nodes++] = xPoints[i] + (scanY - yPoints[i]) * (xPoints[j] - xPoints[i]) / dy;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
j = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort nodes by X (simple bubble sort, numPoints is small)
|
|
||||||
for (int i = 0; i < nodes - 1; i++) {
|
|
||||||
for (int k = i + 1; k < nodes; k++) {
|
|
||||||
if (nodeX[i] > nodeX[k]) {
|
|
||||||
int temp = nodeX[i];
|
|
||||||
nodeX[i] = nodeX[k];
|
|
||||||
nodeX[k] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fill between pairs of nodes
|
|
||||||
for (int i = 0; i < nodes - 1; i += 2) {
|
|
||||||
int startX = nodeX[i];
|
|
||||||
int endX = nodeX[i + 1];
|
|
||||||
|
|
||||||
// Clip to screen
|
|
||||||
if (startX < 0) startX = 0;
|
|
||||||
if (endX >= getScreenWidth()) endX = getScreenWidth() - 1;
|
|
||||||
|
|
||||||
// Draw horizontal line
|
|
||||||
for (int x = startX; x <= endX; x++) {
|
|
||||||
drawPixel(x, scanY, state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(nodeX);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GfxRenderer::clearScreen(const uint8_t color) const { einkDisplay.clearScreen(color); }
|
void GfxRenderer::clearScreen(const uint8_t color) const { einkDisplay.clearScreen(color); }
|
||||||
|
|
||||||
void GfxRenderer::invertScreen() const {
|
void GfxRenderer::invertScreen() const {
|
||||||
@ -468,10 +306,7 @@ int GfxRenderer::getLineHeight(const int fontId) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GfxRenderer::drawButtonHints(const int fontId, const char* btn1, const char* btn2, const char* btn3,
|
void GfxRenderer::drawButtonHints(const int fontId, const char* btn1, const char* btn2, const char* btn3,
|
||||||
const char* btn4) {
|
const char* btn4) const {
|
||||||
const Orientation orig_orientation = getOrientation();
|
|
||||||
setOrientation(Orientation::Portrait);
|
|
||||||
|
|
||||||
const int pageHeight = getScreenHeight();
|
const int pageHeight = getScreenHeight();
|
||||||
constexpr int buttonWidth = 106;
|
constexpr int buttonWidth = 106;
|
||||||
constexpr int buttonHeight = 40;
|
constexpr int buttonHeight = 40;
|
||||||
@ -484,157 +319,12 @@ void GfxRenderer::drawButtonHints(const int fontId, const char* btn1, const char
|
|||||||
// Only draw if the label is non-empty
|
// Only draw if the label is non-empty
|
||||||
if (labels[i] != nullptr && labels[i][0] != '\0') {
|
if (labels[i] != nullptr && labels[i][0] != '\0') {
|
||||||
const int x = buttonPositions[i];
|
const int x = buttonPositions[i];
|
||||||
fillRect(x, pageHeight - buttonY, buttonWidth, buttonHeight, false);
|
|
||||||
drawRect(x, pageHeight - buttonY, buttonWidth, buttonHeight);
|
drawRect(x, pageHeight - buttonY, buttonWidth, buttonHeight);
|
||||||
const int textWidth = getTextWidth(fontId, labels[i]);
|
const int textWidth = getTextWidth(fontId, labels[i]);
|
||||||
const int textX = x + (buttonWidth - 1 - textWidth) / 2;
|
const int textX = x + (buttonWidth - 1 - textWidth) / 2;
|
||||||
drawText(fontId, textX, pageHeight - buttonY + textYOffset, labels[i]);
|
drawText(fontId, textX, pageHeight - buttonY + textYOffset, labels[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setOrientation(orig_orientation);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GfxRenderer::drawSideButtonHints(const int fontId, const char* topBtn, const char* bottomBtn) const {
|
|
||||||
const int screenWidth = getScreenWidth();
|
|
||||||
constexpr int buttonWidth = 40; // Width on screen (height when rotated)
|
|
||||||
constexpr int buttonHeight = 80; // Height on screen (width when rotated)
|
|
||||||
constexpr int buttonX = 5; // Distance from right edge
|
|
||||||
// Position for the button group - buttons share a border so they're adjacent
|
|
||||||
constexpr int topButtonY = 345; // Top button position
|
|
||||||
|
|
||||||
const char* labels[] = {topBtn, bottomBtn};
|
|
||||||
|
|
||||||
// Draw the shared border for both buttons as one unit
|
|
||||||
const int x = screenWidth - buttonX - buttonWidth;
|
|
||||||
|
|
||||||
// Draw top button outline (3 sides, bottom open)
|
|
||||||
if (topBtn != nullptr && topBtn[0] != '\0') {
|
|
||||||
drawLine(x, topButtonY, x + buttonWidth - 1, topButtonY); // Top
|
|
||||||
drawLine(x, topButtonY, x, topButtonY + buttonHeight - 1); // Left
|
|
||||||
drawLine(x + buttonWidth - 1, topButtonY, x + buttonWidth - 1, topButtonY + buttonHeight - 1); // Right
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw shared middle border
|
|
||||||
if ((topBtn != nullptr && topBtn[0] != '\0') || (bottomBtn != nullptr && bottomBtn[0] != '\0')) {
|
|
||||||
drawLine(x, topButtonY + buttonHeight, x + buttonWidth - 1, topButtonY + buttonHeight); // Shared border
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw bottom button outline (3 sides, top is shared)
|
|
||||||
if (bottomBtn != nullptr && bottomBtn[0] != '\0') {
|
|
||||||
drawLine(x, topButtonY + buttonHeight, x, topButtonY + 2 * buttonHeight - 1); // Left
|
|
||||||
drawLine(x + buttonWidth - 1, topButtonY + buttonHeight, x + buttonWidth - 1,
|
|
||||||
topButtonY + 2 * buttonHeight - 1); // Right
|
|
||||||
drawLine(x, topButtonY + 2 * buttonHeight - 1, x + buttonWidth - 1, topButtonY + 2 * buttonHeight - 1); // Bottom
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw text for each button
|
|
||||||
for (int i = 0; i < 2; i++) {
|
|
||||||
if (labels[i] != nullptr && labels[i][0] != '\0') {
|
|
||||||
const int y = topButtonY + i * buttonHeight;
|
|
||||||
|
|
||||||
// Draw rotated text centered in the button
|
|
||||||
const int textWidth = getTextWidth(fontId, labels[i]);
|
|
||||||
const int textHeight = getTextHeight(fontId);
|
|
||||||
|
|
||||||
// Center the rotated text in the button
|
|
||||||
const int textX = x + (buttonWidth - textHeight) / 2;
|
|
||||||
const int textY = y + (buttonHeight + textWidth) / 2;
|
|
||||||
|
|
||||||
drawTextRotated90CW(fontId, textX, textY, labels[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int GfxRenderer::getTextHeight(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(EpdFontFamily::REGULAR)->ascender;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GfxRenderer::drawTextRotated90CW(const int fontId, const int x, const int y, const char* text, const bool black,
|
|
||||||
const EpdFontFamily::Style style) const {
|
|
||||||
// Cannot draw a NULL / empty string
|
|
||||||
if (text == nullptr || *text == '\0') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fontMap.count(fontId) == 0) {
|
|
||||||
Serial.printf("[%lu] [GFX] Font %d not found\n", millis(), fontId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const auto font = fontMap.at(fontId);
|
|
||||||
|
|
||||||
// No printable characters
|
|
||||||
if (!font.hasPrintableChars(text, style)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// For 90° clockwise rotation:
|
|
||||||
// Original (glyphX, glyphY) -> Rotated (glyphY, -glyphX)
|
|
||||||
// Text reads from bottom to top
|
|
||||||
|
|
||||||
int yPos = y; // Current Y position (decreases as we draw characters)
|
|
||||||
|
|
||||||
uint32_t cp;
|
|
||||||
while ((cp = utf8NextCodepoint(reinterpret_cast<const uint8_t**>(&text)))) {
|
|
||||||
const EpdGlyph* glyph = font.getGlyph(cp, style);
|
|
||||||
if (!glyph) {
|
|
||||||
glyph = font.getGlyph('?', style);
|
|
||||||
}
|
|
||||||
if (!glyph) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int is2Bit = font.getData(style)->is2Bit;
|
|
||||||
const uint32_t offset = glyph->dataOffset;
|
|
||||||
const uint8_t width = glyph->width;
|
|
||||||
const uint8_t height = glyph->height;
|
|
||||||
const int left = glyph->left;
|
|
||||||
const int top = glyph->top;
|
|
||||||
|
|
||||||
const uint8_t* bitmap = &font.getData(style)->bitmap[offset];
|
|
||||||
|
|
||||||
if (bitmap != nullptr) {
|
|
||||||
for (int glyphY = 0; glyphY < height; glyphY++) {
|
|
||||||
for (int glyphX = 0; glyphX < width; glyphX++) {
|
|
||||||
const int pixelPosition = glyphY * width + glyphX;
|
|
||||||
|
|
||||||
// 90° clockwise rotation transformation:
|
|
||||||
// screenX = x + (ascender - top + glyphY)
|
|
||||||
// screenY = yPos - (left + glyphX)
|
|
||||||
const int screenX = x + (font.getData(style)->ascender - top + glyphY);
|
|
||||||
const int screenY = yPos - left - glyphX;
|
|
||||||
|
|
||||||
if (is2Bit) {
|
|
||||||
const uint8_t byte = bitmap[pixelPosition / 4];
|
|
||||||
const uint8_t bit_index = (3 - pixelPosition % 4) * 2;
|
|
||||||
const uint8_t bmpVal = 3 - (byte >> bit_index) & 0x3;
|
|
||||||
|
|
||||||
if (renderMode == BW && bmpVal < 3) {
|
|
||||||
drawPixel(screenX, screenY, black);
|
|
||||||
} else if (renderMode == GRAYSCALE_MSB && (bmpVal == 1 || bmpVal == 2)) {
|
|
||||||
drawPixel(screenX, screenY, false);
|
|
||||||
} else if (renderMode == GRAYSCALE_LSB && bmpVal == 1) {
|
|
||||||
drawPixel(screenX, screenY, false);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
const uint8_t byte = bitmap[pixelPosition / 8];
|
|
||||||
const uint8_t bit_index = 7 - (pixelPosition % 8);
|
|
||||||
|
|
||||||
if ((byte >> bit_index) & 1) {
|
|
||||||
drawPixel(screenX, screenY, black);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Move to next character position (going up, so decrease Y)
|
|
||||||
yPos -= glyph->advanceX;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t* GfxRenderer::getFrameBuffer() const { return einkDisplay.getFrameBuffer(); }
|
uint8_t* GfxRenderer::getFrameBuffer() const { return einkDisplay.getFrameBuffer(); }
|
||||||
|
|||||||
@ -66,10 +66,7 @@ class GfxRenderer {
|
|||||||
void drawRect(int x, int y, int width, int height, bool state = true) const;
|
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 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 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, float cropX = 0,
|
void drawBitmap(const Bitmap& bitmap, int x, int y, int maxWidth, int maxHeight) const;
|
||||||
float cropY = 0) const;
|
|
||||||
void drawBitmap1Bit(const Bitmap& bitmap, int x, int y, int maxWidth, int maxHeight) const;
|
|
||||||
void fillPolygon(const int* xPoints, const int* yPoints, int numPoints, bool state = true) const;
|
|
||||||
|
|
||||||
// Text
|
// Text
|
||||||
int getTextWidth(int fontId, const char* text, EpdFontFamily::Style style = EpdFontFamily::REGULAR) const;
|
int getTextWidth(int fontId, const char* text, EpdFontFamily::Style style = EpdFontFamily::REGULAR) const;
|
||||||
@ -84,23 +81,15 @@ class GfxRenderer {
|
|||||||
EpdFontFamily::Style style = EpdFontFamily::REGULAR) const;
|
EpdFontFamily::Style style = EpdFontFamily::REGULAR) const;
|
||||||
|
|
||||||
// UI Components
|
// UI Components
|
||||||
void drawButtonHints(int fontId, const char* btn1, const char* btn2, const char* btn3, const char* btn4);
|
void drawButtonHints(int fontId, const char* btn1, const char* btn2, const char* btn3, const char* btn4) const;
|
||||||
void drawSideButtonHints(int fontId, const char* topBtn, const char* bottomBtn) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
// Helper for drawing rotated text (90 degrees clockwise, for side buttons)
|
|
||||||
void drawTextRotated90CW(int fontId, int x, int y, const char* text, bool black = true,
|
|
||||||
EpdFontFamily::Style style = EpdFontFamily::REGULAR) const;
|
|
||||||
int getTextHeight(int fontId) const;
|
|
||||||
|
|
||||||
public:
|
|
||||||
// Grayscale functions
|
// Grayscale functions
|
||||||
void setRenderMode(const RenderMode mode) { this->renderMode = mode; }
|
void setRenderMode(const RenderMode mode) { this->renderMode = mode; }
|
||||||
void copyGrayscaleLsbBuffers() const;
|
void copyGrayscaleLsbBuffers() const;
|
||||||
void copyGrayscaleMsbBuffers() const;
|
void copyGrayscaleMsbBuffers() const;
|
||||||
void displayGrayBuffer() const;
|
void displayGrayBuffer() const;
|
||||||
bool storeBwBuffer(); // Returns true if buffer was stored successfully
|
bool storeBwBuffer(); // Returns true if buffer was stored successfully
|
||||||
void restoreBwBuffer(); // Restore and free the stored buffer
|
void restoreBwBuffer();
|
||||||
void cleanupGrayscaleWithFrameBuffer() const;
|
void cleanupGrayscaleWithFrameBuffer() const;
|
||||||
|
|
||||||
// Low level functions
|
// Low level functions
|
||||||
|
|||||||
@ -7,8 +7,6 @@
|
|||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#include "BitmapHelpers.h"
|
|
||||||
|
|
||||||
// Context structure for picojpeg callback
|
// Context structure for picojpeg callback
|
||||||
struct JpegReadContext {
|
struct JpegReadContext {
|
||||||
FsFile& file;
|
FsFile& file;
|
||||||
@ -25,12 +23,282 @@ constexpr bool USE_8BIT_OUTPUT = false; // true: 8-bit grayscale (no quantizati
|
|||||||
constexpr bool USE_ATKINSON = true; // Atkinson dithering (cleaner than F-S, less error diffusion)
|
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_FLOYD_STEINBERG = false; // Floyd-Steinberg error diffusion (can cause "worm" artifacts)
|
||||||
constexpr bool USE_NOISE_DITHERING = false; // Hash-based noise dithering (good for downsampling)
|
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)
|
// 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 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_WIDTH = 480; // Max width for cover images (portrait display width)
|
||||||
constexpr int TARGET_MAX_HEIGHT = 800; // Max height for cover images (portrait display height)
|
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) {
|
inline void write16(Print& out, const uint16_t value) {
|
||||||
out.write(value & 0xFF);
|
out.write(value & 0xFF);
|
||||||
out.write((value >> 8) & 0xFF);
|
out.write((value >> 8) & 0xFF);
|
||||||
@ -87,47 +355,8 @@ void writeBmpHeader8bit(Print& bmpOut, const int width, const int height) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function: Write BMP header with 1-bit color depth (black and white)
|
|
||||||
static void writeBmpHeader1bit(Print& bmpOut, const int width, const int height) {
|
|
||||||
// Calculate row padding (each row must be multiple of 4 bytes)
|
|
||||||
const int bytesPerRow = (width + 31) / 32 * 4; // 1 bit per pixel, round up to 4-byte boundary
|
|
||||||
const int imageSize = bytesPerRow * height;
|
|
||||||
const uint32_t fileSize = 62 + imageSize; // 14 (file header) + 40 (DIB header) + 8 (palette) + image
|
|
||||||
|
|
||||||
// BMP File Header (14 bytes)
|
|
||||||
bmpOut.write('B');
|
|
||||||
bmpOut.write('M');
|
|
||||||
write32(bmpOut, fileSize); // File size
|
|
||||||
write32(bmpOut, 0); // Reserved
|
|
||||||
write32(bmpOut, 62); // Offset to pixel data (14 + 40 + 8)
|
|
||||||
|
|
||||||
// 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, 1); // Bits per pixel (1 bit)
|
|
||||||
write32(bmpOut, 0); // BI_RGB (no compression)
|
|
||||||
write32(bmpOut, imageSize);
|
|
||||||
write32(bmpOut, 2835); // xPixelsPerMeter (72 DPI)
|
|
||||||
write32(bmpOut, 2835); // yPixelsPerMeter (72 DPI)
|
|
||||||
write32(bmpOut, 2); // colorsUsed
|
|
||||||
write32(bmpOut, 2); // colorsImportant
|
|
||||||
|
|
||||||
// Color Palette (2 colors x 4 bytes = 8 bytes)
|
|
||||||
// Format: Blue, Green, Red, Reserved (BGRA)
|
|
||||||
// Note: In 1-bit BMP, palette index 0 = black, 1 = white
|
|
||||||
uint8_t palette[8] = {
|
|
||||||
0x00, 0x00, 0x00, 0x00, // Color 0: Black
|
|
||||||
0xFF, 0xFF, 0xFF, 0x00 // Color 1: White
|
|
||||||
};
|
|
||||||
for (const uint8_t i : palette) {
|
|
||||||
bmpOut.write(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper function: Write BMP header with 2-bit color depth
|
// Helper function: Write BMP header with 2-bit color depth
|
||||||
static void writeBmpHeader2bit(Print& bmpOut, const int width, const int height) {
|
void JpegToBmpConverter::writeBmpHeader(Print& bmpOut, const int width, const int height) {
|
||||||
// Calculate row padding (each row must be multiple of 4 bytes)
|
// 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 bytesPerRow = (width * 2 + 31) / 32 * 4; // 2 bits per pixel, round up
|
||||||
const int imageSize = bytesPerRow * height;
|
const int imageSize = bytesPerRow * height;
|
||||||
@ -198,11 +427,9 @@ unsigned char JpegToBmpConverter::jpegReadCallback(unsigned char* pBuf, const un
|
|||||||
return 0; // Success
|
return 0; // Success
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal implementation with configurable target size and bit depth
|
// Core function: Convert JPEG file to 2-bit BMP
|
||||||
bool JpegToBmpConverter::jpegFileToBmpStreamInternal(FsFile& jpegFile, Print& bmpOut, int targetWidth, int targetHeight,
|
bool JpegToBmpConverter::jpegFileToBmpStream(FsFile& jpegFile, Print& bmpOut) {
|
||||||
bool oneBit) {
|
Serial.printf("[%lu] [JPG] Converting JPEG to BMP\n", millis());
|
||||||
Serial.printf("[%lu] [JPG] Converting JPEG to %s BMP (target: %dx%d)\n", millis(), oneBit ? "1-bit" : "2-bit",
|
|
||||||
targetWidth, targetHeight);
|
|
||||||
|
|
||||||
// Setup context for picojpeg callback
|
// Setup context for picojpeg callback
|
||||||
JpegReadContext context = {.file = jpegFile, .bufferPos = 0, .bufferFilled = 0};
|
JpegReadContext context = {.file = jpegFile, .bufferPos = 0, .bufferFilled = 0};
|
||||||
@ -237,13 +464,11 @@ bool JpegToBmpConverter::jpegFileToBmpStreamInternal(FsFile& jpegFile, Print& bm
|
|||||||
uint32_t scaleY_fp = 65536;
|
uint32_t scaleY_fp = 65536;
|
||||||
bool needsScaling = false;
|
bool needsScaling = false;
|
||||||
|
|
||||||
if (targetWidth > 0 && targetHeight > 0 && (imageInfo.m_width > targetWidth || imageInfo.m_height > targetHeight)) {
|
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
|
// Calculate scale to fit within target dimensions while maintaining aspect ratio
|
||||||
const float scaleToFitWidth = static_cast<float>(targetWidth) / imageInfo.m_width;
|
const float scaleToFitWidth = static_cast<float>(TARGET_MAX_WIDTH) / imageInfo.m_width;
|
||||||
const float scaleToFitHeight = static_cast<float>(targetHeight) / imageInfo.m_height;
|
const float scaleToFitHeight = static_cast<float>(TARGET_MAX_HEIGHT) / imageInfo.m_height;
|
||||||
// We scale to the smaller dimension, so we can potentially crop later.
|
const float scale = (scaleToFitWidth < scaleToFitHeight) ? scaleToFitWidth : scaleToFitHeight;
|
||||||
// TODO: ideally, we already crop here.
|
|
||||||
const float scale = (scaleToFitWidth > scaleToFitHeight) ? scaleToFitWidth : scaleToFitHeight;
|
|
||||||
|
|
||||||
outWidth = static_cast<int>(imageInfo.m_width * scale);
|
outWidth = static_cast<int>(imageInfo.m_width * scale);
|
||||||
outHeight = static_cast<int>(imageInfo.m_height * scale);
|
outHeight = static_cast<int>(imageInfo.m_height * scale);
|
||||||
@ -259,19 +484,16 @@ bool JpegToBmpConverter::jpegFileToBmpStreamInternal(FsFile& jpegFile, Print& bm
|
|||||||
needsScaling = true;
|
needsScaling = true;
|
||||||
|
|
||||||
Serial.printf("[%lu] [JPG] Pre-scaling %dx%d -> %dx%d (fit to %dx%d)\n", millis(), imageInfo.m_width,
|
Serial.printf("[%lu] [JPG] Pre-scaling %dx%d -> %dx%d (fit to %dx%d)\n", millis(), imageInfo.m_width,
|
||||||
imageInfo.m_height, outWidth, outHeight, targetWidth, targetHeight);
|
imageInfo.m_height, outWidth, outHeight, TARGET_MAX_WIDTH, TARGET_MAX_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write BMP header with output dimensions
|
// Write BMP header with output dimensions
|
||||||
int bytesPerRow;
|
int bytesPerRow;
|
||||||
if (USE_8BIT_OUTPUT && !oneBit) {
|
if (USE_8BIT_OUTPUT) {
|
||||||
writeBmpHeader8bit(bmpOut, outWidth, outHeight);
|
writeBmpHeader8bit(bmpOut, outWidth, outHeight);
|
||||||
bytesPerRow = (outWidth + 3) / 4 * 4;
|
bytesPerRow = (outWidth + 3) / 4 * 4;
|
||||||
} else if (oneBit) {
|
|
||||||
writeBmpHeader1bit(bmpOut, outWidth, outHeight);
|
|
||||||
bytesPerRow = (outWidth + 31) / 32 * 4; // 1 bit per pixel
|
|
||||||
} else {
|
} else {
|
||||||
writeBmpHeader2bit(bmpOut, outWidth, outHeight);
|
writeBmpHeader(bmpOut, outWidth, outHeight);
|
||||||
bytesPerRow = (outWidth * 2 + 31) / 32 * 4;
|
bytesPerRow = (outWidth * 2 + 31) / 32 * 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,16 +524,11 @@ bool JpegToBmpConverter::jpegFileToBmpStreamInternal(FsFile& jpegFile, Print& bm
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create ditherer if enabled
|
// Create ditherer if enabled (only for 2-bit output)
|
||||||
// Use OUTPUT dimensions for dithering (after prescaling)
|
// Use OUTPUT dimensions for dithering (after prescaling)
|
||||||
AtkinsonDitherer* atkinsonDitherer = nullptr;
|
AtkinsonDitherer* atkinsonDitherer = nullptr;
|
||||||
FloydSteinbergDitherer* fsDitherer = nullptr;
|
FloydSteinbergDitherer* fsDitherer = nullptr;
|
||||||
Atkinson1BitDitherer* atkinson1BitDitherer = nullptr;
|
if (!USE_8BIT_OUTPUT) {
|
||||||
|
|
||||||
if (oneBit) {
|
|
||||||
// For 1-bit output, use Atkinson dithering for better quality
|
|
||||||
atkinson1BitDitherer = new Atkinson1BitDitherer(outWidth);
|
|
||||||
} else if (!USE_8BIT_OUTPUT) {
|
|
||||||
if (USE_ATKINSON) {
|
if (USE_ATKINSON) {
|
||||||
atkinsonDitherer = new AtkinsonDitherer(outWidth);
|
atkinsonDitherer = new AtkinsonDitherer(outWidth);
|
||||||
} else if (USE_FLOYD_STEINBERG) {
|
} else if (USE_FLOYD_STEINBERG) {
|
||||||
@ -397,32 +614,19 @@ bool JpegToBmpConverter::jpegFileToBmpStreamInternal(FsFile& jpegFile, Print& bm
|
|||||||
// No scaling - direct output (1:1 mapping)
|
// No scaling - direct output (1:1 mapping)
|
||||||
memset(rowBuffer, 0, bytesPerRow);
|
memset(rowBuffer, 0, bytesPerRow);
|
||||||
|
|
||||||
if (USE_8BIT_OUTPUT && !oneBit) {
|
if (USE_8BIT_OUTPUT) {
|
||||||
for (int x = 0; x < outWidth; x++) {
|
for (int x = 0; x < outWidth; x++) {
|
||||||
const uint8_t gray = mcuRowBuffer[bufferY * imageInfo.m_width + x];
|
const uint8_t gray = mcuRowBuffer[bufferY * imageInfo.m_width + x];
|
||||||
rowBuffer[x] = adjustPixel(gray);
|
rowBuffer[x] = adjustPixel(gray);
|
||||||
}
|
}
|
||||||
} else if (oneBit) {
|
} else {
|
||||||
// 1-bit output with Atkinson dithering for better quality
|
|
||||||
for (int x = 0; x < outWidth; x++) {
|
for (int x = 0; x < outWidth; x++) {
|
||||||
const uint8_t gray = mcuRowBuffer[bufferY * imageInfo.m_width + x];
|
const uint8_t gray = mcuRowBuffer[bufferY * imageInfo.m_width + x];
|
||||||
const uint8_t bit =
|
|
||||||
atkinson1BitDitherer ? atkinson1BitDitherer->processPixel(gray, x) : quantize1bit(gray, x, y);
|
|
||||||
// Pack 1-bit value: MSB first, 8 pixels per byte
|
|
||||||
const int byteIndex = x / 8;
|
|
||||||
const int bitOffset = 7 - (x % 8);
|
|
||||||
rowBuffer[byteIndex] |= (bit << bitOffset);
|
|
||||||
}
|
|
||||||
if (atkinson1BitDitherer) atkinson1BitDitherer->nextRow();
|
|
||||||
} else {
|
|
||||||
// 2-bit output
|
|
||||||
for (int x = 0; x < outWidth; x++) {
|
|
||||||
const uint8_t gray = adjustPixel(mcuRowBuffer[bufferY * imageInfo.m_width + x]);
|
|
||||||
uint8_t twoBit;
|
uint8_t twoBit;
|
||||||
if (atkinsonDitherer) {
|
if (atkinsonDitherer) {
|
||||||
twoBit = atkinsonDitherer->processPixel(gray, x);
|
twoBit = atkinsonDitherer->processPixel(gray, x);
|
||||||
} else if (fsDitherer) {
|
} else if (fsDitherer) {
|
||||||
twoBit = fsDitherer->processPixel(gray, x);
|
twoBit = fsDitherer->processPixel(gray, x, fsDitherer->isReverseRow());
|
||||||
} else {
|
} else {
|
||||||
twoBit = quantize(gray, x, y);
|
twoBit = quantize(gray, x, y);
|
||||||
}
|
}
|
||||||
@ -473,32 +677,19 @@ bool JpegToBmpConverter::jpegFileToBmpStreamInternal(FsFile& jpegFile, Print& bm
|
|||||||
if (srcY_fp >= nextOutY_srcStart && currentOutY < outHeight) {
|
if (srcY_fp >= nextOutY_srcStart && currentOutY < outHeight) {
|
||||||
memset(rowBuffer, 0, bytesPerRow);
|
memset(rowBuffer, 0, bytesPerRow);
|
||||||
|
|
||||||
if (USE_8BIT_OUTPUT && !oneBit) {
|
if (USE_8BIT_OUTPUT) {
|
||||||
for (int x = 0; x < outWidth; x++) {
|
for (int x = 0; x < outWidth; x++) {
|
||||||
const uint8_t gray = (rowCount[x] > 0) ? (rowAccum[x] / rowCount[x]) : 0;
|
const uint8_t gray = (rowCount[x] > 0) ? (rowAccum[x] / rowCount[x]) : 0;
|
||||||
rowBuffer[x] = adjustPixel(gray);
|
rowBuffer[x] = adjustPixel(gray);
|
||||||
}
|
}
|
||||||
} else if (oneBit) {
|
} else {
|
||||||
// 1-bit output with Atkinson dithering for better quality
|
|
||||||
for (int x = 0; x < outWidth; x++) {
|
for (int x = 0; x < outWidth; x++) {
|
||||||
const uint8_t gray = (rowCount[x] > 0) ? (rowAccum[x] / rowCount[x]) : 0;
|
const uint8_t gray = (rowCount[x] > 0) ? (rowAccum[x] / rowCount[x]) : 0;
|
||||||
const uint8_t bit = atkinson1BitDitherer ? atkinson1BitDitherer->processPixel(gray, x)
|
|
||||||
: quantize1bit(gray, x, currentOutY);
|
|
||||||
// Pack 1-bit value: MSB first, 8 pixels per byte
|
|
||||||
const int byteIndex = x / 8;
|
|
||||||
const int bitOffset = 7 - (x % 8);
|
|
||||||
rowBuffer[byteIndex] |= (bit << bitOffset);
|
|
||||||
}
|
|
||||||
if (atkinson1BitDitherer) atkinson1BitDitherer->nextRow();
|
|
||||||
} else {
|
|
||||||
// 2-bit output
|
|
||||||
for (int x = 0; x < outWidth; x++) {
|
|
||||||
const uint8_t gray = adjustPixel((rowCount[x] > 0) ? (rowAccum[x] / rowCount[x]) : 0);
|
|
||||||
uint8_t twoBit;
|
uint8_t twoBit;
|
||||||
if (atkinsonDitherer) {
|
if (atkinsonDitherer) {
|
||||||
twoBit = atkinsonDitherer->processPixel(gray, x);
|
twoBit = atkinsonDitherer->processPixel(gray, x);
|
||||||
} else if (fsDitherer) {
|
} else if (fsDitherer) {
|
||||||
twoBit = fsDitherer->processPixel(gray, x);
|
twoBit = fsDitherer->processPixel(gray, x, fsDitherer->isReverseRow());
|
||||||
} else {
|
} else {
|
||||||
twoBit = quantize(gray, x, currentOutY);
|
twoBit = quantize(gray, x, currentOutY);
|
||||||
}
|
}
|
||||||
@ -539,29 +730,9 @@ bool JpegToBmpConverter::jpegFileToBmpStreamInternal(FsFile& jpegFile, Print& bm
|
|||||||
if (fsDitherer) {
|
if (fsDitherer) {
|
||||||
delete fsDitherer;
|
delete fsDitherer;
|
||||||
}
|
}
|
||||||
if (atkinson1BitDitherer) {
|
|
||||||
delete atkinson1BitDitherer;
|
|
||||||
}
|
|
||||||
free(mcuRowBuffer);
|
free(mcuRowBuffer);
|
||||||
free(rowBuffer);
|
free(rowBuffer);
|
||||||
|
|
||||||
Serial.printf("[%lu] [JPG] Successfully converted JPEG to BMP\n", millis());
|
Serial.printf("[%lu] [JPG] Successfully converted JPEG to BMP\n", millis());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Core function: Convert JPEG file to 2-bit BMP (uses default target size)
|
|
||||||
bool JpegToBmpConverter::jpegFileToBmpStream(FsFile& jpegFile, Print& bmpOut) {
|
|
||||||
return jpegFileToBmpStreamInternal(jpegFile, bmpOut, TARGET_MAX_WIDTH, TARGET_MAX_HEIGHT, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert with custom target size (for thumbnails, 2-bit)
|
|
||||||
bool JpegToBmpConverter::jpegFileToBmpStreamWithSize(FsFile& jpegFile, Print& bmpOut, int targetMaxWidth,
|
|
||||||
int targetMaxHeight) {
|
|
||||||
return jpegFileToBmpStreamInternal(jpegFile, bmpOut, targetMaxWidth, targetMaxHeight, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert to 1-bit BMP (black and white only, no grays) for fast home screen rendering
|
|
||||||
bool JpegToBmpConverter::jpegFileTo1BitBmpStreamWithSize(FsFile& jpegFile, Print& bmpOut, int targetMaxWidth,
|
|
||||||
int targetMaxHeight) {
|
|
||||||
return jpegFileToBmpStreamInternal(jpegFile, bmpOut, targetMaxWidth, targetMaxHeight, true);
|
|
||||||
}
|
|
||||||
|
|||||||
@ -5,15 +5,11 @@ class Print;
|
|||||||
class ZipFile;
|
class ZipFile;
|
||||||
|
|
||||||
class JpegToBmpConverter {
|
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,
|
static unsigned char jpegReadCallback(unsigned char* pBuf, unsigned char buf_size,
|
||||||
unsigned char* pBytes_actually_read, void* pCallback_data);
|
unsigned char* pBytes_actually_read, void* pCallback_data);
|
||||||
static bool jpegFileToBmpStreamInternal(class FsFile& jpegFile, Print& bmpOut, int targetWidth, int targetHeight,
|
|
||||||
bool oneBit);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static bool jpegFileToBmpStream(FsFile& jpegFile, Print& bmpOut);
|
static bool jpegFileToBmpStream(FsFile& jpegFile, Print& bmpOut);
|
||||||
// Convert with custom target size (for thumbnails)
|
|
||||||
static bool jpegFileToBmpStreamWithSize(FsFile& jpegFile, Print& bmpOut, int targetMaxWidth, int targetMaxHeight);
|
|
||||||
// Convert to 1-bit BMP (black and white only, no grays) for fast home screen rendering
|
|
||||||
static bool jpegFileTo1BitBmpStreamWithSize(FsFile& jpegFile, Print& bmpOut, int targetMaxWidth, int targetMaxHeight);
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,219 +0,0 @@
|
|||||||
#include "OpdsParser.h"
|
|
||||||
|
|
||||||
#include <HardwareSerial.h>
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
OpdsParser::~OpdsParser() {
|
|
||||||
if (parser) {
|
|
||||||
XML_StopParser(parser, XML_FALSE);
|
|
||||||
XML_SetElementHandler(parser, nullptr, nullptr);
|
|
||||||
XML_SetCharacterDataHandler(parser, nullptr);
|
|
||||||
XML_ParserFree(parser);
|
|
||||||
parser = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OpdsParser::parse(const char* xmlData, const size_t length) {
|
|
||||||
clear();
|
|
||||||
|
|
||||||
parser = XML_ParserCreate(nullptr);
|
|
||||||
if (!parser) {
|
|
||||||
Serial.printf("[%lu] [OPDS] Couldn't allocate memory for parser\n", millis());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
XML_SetUserData(parser, this);
|
|
||||||
XML_SetElementHandler(parser, startElement, endElement);
|
|
||||||
XML_SetCharacterDataHandler(parser, characterData);
|
|
||||||
|
|
||||||
// Parse in chunks to avoid large buffer allocations
|
|
||||||
const char* currentPos = xmlData;
|
|
||||||
size_t remaining = length;
|
|
||||||
constexpr size_t chunkSize = 1024;
|
|
||||||
|
|
||||||
while (remaining > 0) {
|
|
||||||
void* const buf = XML_GetBuffer(parser, chunkSize);
|
|
||||||
if (!buf) {
|
|
||||||
Serial.printf("[%lu] [OPDS] Couldn't allocate memory for buffer\n", millis());
|
|
||||||
XML_ParserFree(parser);
|
|
||||||
parser = nullptr;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t toRead = remaining < chunkSize ? remaining : chunkSize;
|
|
||||||
memcpy(buf, currentPos, toRead);
|
|
||||||
|
|
||||||
const bool isFinal = (remaining == toRead);
|
|
||||||
if (XML_ParseBuffer(parser, static_cast<int>(toRead), isFinal) == XML_STATUS_ERROR) {
|
|
||||||
Serial.printf("[%lu] [OPDS] Parse error at line %lu: %s\n", millis(), XML_GetCurrentLineNumber(parser),
|
|
||||||
XML_ErrorString(XML_GetErrorCode(parser)));
|
|
||||||
XML_ParserFree(parser);
|
|
||||||
parser = nullptr;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentPos += toRead;
|
|
||||||
remaining -= toRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean up parser
|
|
||||||
XML_ParserFree(parser);
|
|
||||||
parser = nullptr;
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [OPDS] Parsed %zu entries\n", millis(), entries.size());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsParser::clear() {
|
|
||||||
entries.clear();
|
|
||||||
currentEntry = OpdsEntry{};
|
|
||||||
currentText.clear();
|
|
||||||
inEntry = false;
|
|
||||||
inTitle = false;
|
|
||||||
inAuthor = false;
|
|
||||||
inAuthorName = false;
|
|
||||||
inId = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<OpdsEntry> OpdsParser::getBooks() const {
|
|
||||||
std::vector<OpdsEntry> books;
|
|
||||||
for (const auto& entry : entries) {
|
|
||||||
if (entry.type == OpdsEntryType::BOOK) {
|
|
||||||
books.push_back(entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return books;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* OpdsParser::findAttribute(const XML_Char** atts, const char* name) {
|
|
||||||
for (int i = 0; atts[i]; i += 2) {
|
|
||||||
if (strcmp(atts[i], name) == 0) {
|
|
||||||
return atts[i + 1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void XMLCALL OpdsParser::startElement(void* userData, const XML_Char* name, const XML_Char** atts) {
|
|
||||||
auto* self = static_cast<OpdsParser*>(userData);
|
|
||||||
|
|
||||||
// Check for entry element (with or without namespace prefix)
|
|
||||||
if (strcmp(name, "entry") == 0 || strstr(name, ":entry") != nullptr) {
|
|
||||||
self->inEntry = true;
|
|
||||||
self->currentEntry = OpdsEntry{};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!self->inEntry) return;
|
|
||||||
|
|
||||||
// Check for title element
|
|
||||||
if (strcmp(name, "title") == 0 || strstr(name, ":title") != nullptr) {
|
|
||||||
self->inTitle = true;
|
|
||||||
self->currentText.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for author element
|
|
||||||
if (strcmp(name, "author") == 0 || strstr(name, ":author") != nullptr) {
|
|
||||||
self->inAuthor = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for author name element
|
|
||||||
if (self->inAuthor && (strcmp(name, "name") == 0 || strstr(name, ":name") != nullptr)) {
|
|
||||||
self->inAuthorName = true;
|
|
||||||
self->currentText.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for id element
|
|
||||||
if (strcmp(name, "id") == 0 || strstr(name, ":id") != nullptr) {
|
|
||||||
self->inId = true;
|
|
||||||
self->currentText.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for link element
|
|
||||||
if (strcmp(name, "link") == 0 || strstr(name, ":link") != nullptr) {
|
|
||||||
const char* rel = findAttribute(atts, "rel");
|
|
||||||
const char* type = findAttribute(atts, "type");
|
|
||||||
const char* href = findAttribute(atts, "href");
|
|
||||||
|
|
||||||
if (href) {
|
|
||||||
// Check for acquisition link with epub type (this is a downloadable book)
|
|
||||||
if (rel && type && strstr(rel, "opds-spec.org/acquisition") != nullptr &&
|
|
||||||
strcmp(type, "application/epub+zip") == 0) {
|
|
||||||
self->currentEntry.type = OpdsEntryType::BOOK;
|
|
||||||
self->currentEntry.href = href;
|
|
||||||
}
|
|
||||||
// Check for navigation link (subsection or no rel specified with atom+xml type)
|
|
||||||
else if (type && strstr(type, "application/atom+xml") != nullptr) {
|
|
||||||
// Only set navigation link if we don't already have an epub link
|
|
||||||
if (self->currentEntry.type != OpdsEntryType::BOOK) {
|
|
||||||
self->currentEntry.type = OpdsEntryType::NAVIGATION;
|
|
||||||
self->currentEntry.href = href;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void XMLCALL OpdsParser::endElement(void* userData, const XML_Char* name) {
|
|
||||||
auto* self = static_cast<OpdsParser*>(userData);
|
|
||||||
|
|
||||||
// Check for entry end
|
|
||||||
if (strcmp(name, "entry") == 0 || strstr(name, ":entry") != nullptr) {
|
|
||||||
// Only add entry if it has required fields (title and href)
|
|
||||||
if (!self->currentEntry.title.empty() && !self->currentEntry.href.empty()) {
|
|
||||||
self->entries.push_back(self->currentEntry);
|
|
||||||
}
|
|
||||||
self->inEntry = false;
|
|
||||||
self->currentEntry = OpdsEntry{};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!self->inEntry) return;
|
|
||||||
|
|
||||||
// Check for title end
|
|
||||||
if (strcmp(name, "title") == 0 || strstr(name, ":title") != nullptr) {
|
|
||||||
if (self->inTitle) {
|
|
||||||
self->currentEntry.title = self->currentText;
|
|
||||||
}
|
|
||||||
self->inTitle = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for author end
|
|
||||||
if (strcmp(name, "author") == 0 || strstr(name, ":author") != nullptr) {
|
|
||||||
self->inAuthor = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for author name end
|
|
||||||
if (self->inAuthor && (strcmp(name, "name") == 0 || strstr(name, ":name") != nullptr)) {
|
|
||||||
if (self->inAuthorName) {
|
|
||||||
self->currentEntry.author = self->currentText;
|
|
||||||
}
|
|
||||||
self->inAuthorName = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for id end
|
|
||||||
if (strcmp(name, "id") == 0 || strstr(name, ":id") != nullptr) {
|
|
||||||
if (self->inId) {
|
|
||||||
self->currentEntry.id = self->currentText;
|
|
||||||
}
|
|
||||||
self->inId = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void XMLCALL OpdsParser::characterData(void* userData, const XML_Char* s, const int len) {
|
|
||||||
auto* self = static_cast<OpdsParser*>(userData);
|
|
||||||
|
|
||||||
// Only accumulate text when in a text element
|
|
||||||
if (self->inTitle || self->inAuthorName || self->inId) {
|
|
||||||
self->currentText.append(s, len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,99 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <expat.h>
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Type of OPDS entry.
|
|
||||||
*/
|
|
||||||
enum class OpdsEntryType {
|
|
||||||
NAVIGATION, // Link to another catalog
|
|
||||||
BOOK // Downloadable book
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents an entry from an OPDS feed (either a navigation link or a book).
|
|
||||||
*/
|
|
||||||
struct OpdsEntry {
|
|
||||||
OpdsEntryType type = OpdsEntryType::NAVIGATION;
|
|
||||||
std::string title;
|
|
||||||
std::string author; // Only for books
|
|
||||||
std::string href; // Navigation URL or epub download URL
|
|
||||||
std::string id;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Legacy alias for backward compatibility
|
|
||||||
using OpdsBook = OpdsEntry;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parser for OPDS (Open Publication Distribution System) Atom feeds.
|
|
||||||
* Uses the Expat XML parser to parse OPDS catalog entries.
|
|
||||||
*
|
|
||||||
* Usage:
|
|
||||||
* OpdsParser parser;
|
|
||||||
* if (parser.parse(xmlData, xmlLength)) {
|
|
||||||
* for (const auto& entry : parser.getEntries()) {
|
|
||||||
* if (entry.type == OpdsEntryType::BOOK) {
|
|
||||||
* // Downloadable book
|
|
||||||
* } else {
|
|
||||||
* // Navigation link to another catalog
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
class OpdsParser {
|
|
||||||
public:
|
|
||||||
OpdsParser() = default;
|
|
||||||
~OpdsParser();
|
|
||||||
|
|
||||||
// Disable copy
|
|
||||||
OpdsParser(const OpdsParser&) = delete;
|
|
||||||
OpdsParser& operator=(const OpdsParser&) = delete;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse an OPDS XML feed.
|
|
||||||
* @param xmlData Pointer to the XML data
|
|
||||||
* @param length Length of the XML data
|
|
||||||
* @return true if parsing succeeded, false on error
|
|
||||||
*/
|
|
||||||
bool parse(const char* xmlData, size_t length);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the parsed entries (both navigation and book entries).
|
|
||||||
* @return Vector of OpdsEntry entries
|
|
||||||
*/
|
|
||||||
const std::vector<OpdsEntry>& getEntries() const { return entries; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get only book entries (legacy compatibility).
|
|
||||||
* @return Vector of book entries
|
|
||||||
*/
|
|
||||||
std::vector<OpdsEntry> getBooks() const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clear all parsed entries.
|
|
||||||
*/
|
|
||||||
void clear();
|
|
||||||
|
|
||||||
private:
|
|
||||||
// Expat callbacks
|
|
||||||
static void XMLCALL startElement(void* userData, const XML_Char* name, const XML_Char** atts);
|
|
||||||
static void XMLCALL endElement(void* userData, const XML_Char* name);
|
|
||||||
static void XMLCALL characterData(void* userData, const XML_Char* s, int len);
|
|
||||||
|
|
||||||
// Helper to find attribute value
|
|
||||||
static const char* findAttribute(const XML_Char** atts, const char* name);
|
|
||||||
|
|
||||||
XML_Parser parser = nullptr;
|
|
||||||
std::vector<OpdsEntry> entries;
|
|
||||||
OpdsEntry currentEntry;
|
|
||||||
std::string currentText;
|
|
||||||
|
|
||||||
// Parser state
|
|
||||||
bool inEntry = false;
|
|
||||||
bool inTitle = false;
|
|
||||||
bool inAuthor = false;
|
|
||||||
bool inAuthorName = false;
|
|
||||||
bool inId = false;
|
|
||||||
};
|
|
||||||
191
lib/Txt/Txt.cpp
191
lib/Txt/Txt.cpp
@ -1,191 +0,0 @@
|
|||||||
#include "Txt.h"
|
|
||||||
|
|
||||||
#include <FsHelpers.h>
|
|
||||||
#include <JpegToBmpConverter.h>
|
|
||||||
|
|
||||||
Txt::Txt(std::string path, std::string cacheBasePath)
|
|
||||||
: filepath(std::move(path)), cacheBasePath(std::move(cacheBasePath)) {
|
|
||||||
// Generate cache path from file path hash
|
|
||||||
const size_t hash = std::hash<std::string>{}(filepath);
|
|
||||||
cachePath = this->cacheBasePath + "/txt_" + std::to_string(hash);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Txt::load() {
|
|
||||||
if (loaded) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!SdMan.exists(filepath.c_str())) {
|
|
||||||
Serial.printf("[%lu] [TXT] File does not exist: %s\n", millis(), filepath.c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
FsFile file;
|
|
||||||
if (!SdMan.openFileForRead("TXT", filepath, file)) {
|
|
||||||
Serial.printf("[%lu] [TXT] Failed to open file: %s\n", millis(), filepath.c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
fileSize = file.size();
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
loaded = true;
|
|
||||||
Serial.printf("[%lu] [TXT] Loaded TXT file: %s (%zu bytes)\n", millis(), filepath.c_str(), fileSize);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string Txt::getTitle() const {
|
|
||||||
// Extract filename without path and extension
|
|
||||||
size_t lastSlash = filepath.find_last_of('/');
|
|
||||||
std::string filename = (lastSlash != std::string::npos) ? filepath.substr(lastSlash + 1) : filepath;
|
|
||||||
|
|
||||||
// Remove .txt extension
|
|
||||||
if (filename.length() >= 4 && filename.substr(filename.length() - 4) == ".txt") {
|
|
||||||
filename = filename.substr(0, filename.length() - 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
return filename;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Txt::setupCacheDir() const {
|
|
||||||
if (!SdMan.exists(cacheBasePath.c_str())) {
|
|
||||||
SdMan.mkdir(cacheBasePath.c_str());
|
|
||||||
}
|
|
||||||
if (!SdMan.exists(cachePath.c_str())) {
|
|
||||||
SdMan.mkdir(cachePath.c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string Txt::findCoverImage() const {
|
|
||||||
// Get the folder containing the txt file
|
|
||||||
size_t lastSlash = filepath.find_last_of('/');
|
|
||||||
std::string folder = (lastSlash != std::string::npos) ? filepath.substr(0, lastSlash) : "";
|
|
||||||
if (folder.empty()) {
|
|
||||||
folder = "/";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the base filename without extension (e.g., "mybook" from "/books/mybook.txt")
|
|
||||||
std::string baseName = getTitle();
|
|
||||||
|
|
||||||
// Image extensions to try
|
|
||||||
const char* extensions[] = {".bmp", ".jpg", ".jpeg", ".png", ".BMP", ".JPG", ".JPEG", ".PNG"};
|
|
||||||
|
|
||||||
// First priority: look for image with same name as txt file (e.g., mybook.jpg)
|
|
||||||
for (const auto& ext : extensions) {
|
|
||||||
std::string coverPath = folder + "/" + baseName + ext;
|
|
||||||
if (SdMan.exists(coverPath.c_str())) {
|
|
||||||
Serial.printf("[%lu] [TXT] Found matching cover image: %s\n", millis(), coverPath.c_str());
|
|
||||||
return coverPath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback: look for cover image files
|
|
||||||
const char* coverNames[] = {"cover", "Cover", "COVER"};
|
|
||||||
for (const auto& name : coverNames) {
|
|
||||||
for (const auto& ext : extensions) {
|
|
||||||
std::string coverPath = folder + "/" + std::string(name) + ext;
|
|
||||||
if (SdMan.exists(coverPath.c_str())) {
|
|
||||||
Serial.printf("[%lu] [TXT] Found fallback cover image: %s\n", millis(), coverPath.c_str());
|
|
||||||
return coverPath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string Txt::getCoverBmpPath() const { return cachePath + "/cover.bmp"; }
|
|
||||||
|
|
||||||
bool Txt::generateCoverBmp() const {
|
|
||||||
// Already generated, return true
|
|
||||||
if (SdMan.exists(getCoverBmpPath().c_str())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string coverImagePath = findCoverImage();
|
|
||||||
if (coverImagePath.empty()) {
|
|
||||||
Serial.printf("[%lu] [TXT] No cover image found for TXT file\n", millis());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup cache directory
|
|
||||||
setupCacheDir();
|
|
||||||
|
|
||||||
// Get file extension
|
|
||||||
const size_t len = coverImagePath.length();
|
|
||||||
const bool isJpg =
|
|
||||||
(len >= 4 && (coverImagePath.substr(len - 4) == ".jpg" || coverImagePath.substr(len - 4) == ".JPG")) ||
|
|
||||||
(len >= 5 && (coverImagePath.substr(len - 5) == ".jpeg" || coverImagePath.substr(len - 5) == ".JPEG"));
|
|
||||||
const bool isBmp = len >= 4 && (coverImagePath.substr(len - 4) == ".bmp" || coverImagePath.substr(len - 4) == ".BMP");
|
|
||||||
|
|
||||||
if (isBmp) {
|
|
||||||
// Copy BMP file to cache
|
|
||||||
Serial.printf("[%lu] [TXT] Copying BMP cover image to cache\n", millis());
|
|
||||||
FsFile src, dst;
|
|
||||||
if (!SdMan.openFileForRead("TXT", coverImagePath, src)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!SdMan.openFileForWrite("TXT", getCoverBmpPath(), dst)) {
|
|
||||||
src.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
uint8_t buffer[1024];
|
|
||||||
while (src.available()) {
|
|
||||||
size_t bytesRead = src.read(buffer, sizeof(buffer));
|
|
||||||
dst.write(buffer, bytesRead);
|
|
||||||
}
|
|
||||||
src.close();
|
|
||||||
dst.close();
|
|
||||||
Serial.printf("[%lu] [TXT] Copied BMP cover to cache\n", millis());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isJpg) {
|
|
||||||
// Convert JPG/JPEG to BMP (same approach as Epub)
|
|
||||||
Serial.printf("[%lu] [TXT] Generating BMP from JPG cover image\n", millis());
|
|
||||||
FsFile coverJpg, coverBmp;
|
|
||||||
if (!SdMan.openFileForRead("TXT", coverImagePath, coverJpg)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!SdMan.openFileForWrite("TXT", getCoverBmpPath(), coverBmp)) {
|
|
||||||
coverJpg.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const bool success = JpegToBmpConverter::jpegFileToBmpStream(coverJpg, coverBmp);
|
|
||||||
coverJpg.close();
|
|
||||||
coverBmp.close();
|
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
Serial.printf("[%lu] [TXT] Failed to generate BMP from JPG cover image\n", millis());
|
|
||||||
SdMan.remove(getCoverBmpPath().c_str());
|
|
||||||
} else {
|
|
||||||
Serial.printf("[%lu] [TXT] Generated BMP from JPG cover image\n", millis());
|
|
||||||
}
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
|
|
||||||
// PNG files are not supported (would need a PNG decoder)
|
|
||||||
Serial.printf("[%lu] [TXT] Cover image format not supported (only BMP/JPG/JPEG)\n", millis());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Txt::readContent(uint8_t* buffer, size_t offset, size_t length) const {
|
|
||||||
if (!loaded) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
FsFile file;
|
|
||||||
if (!SdMan.openFileForRead("TXT", filepath, file)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!file.seek(offset)) {
|
|
||||||
file.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t bytesRead = file.read(buffer, length);
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
return bytesRead > 0;
|
|
||||||
}
|
|
||||||
@ -1,33 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <SDCardManager.h>
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class Txt {
|
|
||||||
std::string filepath;
|
|
||||||
std::string cacheBasePath;
|
|
||||||
std::string cachePath;
|
|
||||||
bool loaded = false;
|
|
||||||
size_t fileSize = 0;
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit Txt(std::string path, std::string cacheBasePath);
|
|
||||||
|
|
||||||
bool load();
|
|
||||||
[[nodiscard]] const std::string& getPath() const { return filepath; }
|
|
||||||
[[nodiscard]] const std::string& getCachePath() const { return cachePath; }
|
|
||||||
[[nodiscard]] std::string getTitle() const;
|
|
||||||
[[nodiscard]] size_t getFileSize() const { return fileSize; }
|
|
||||||
|
|
||||||
void setupCacheDir() const;
|
|
||||||
|
|
||||||
// Cover image support - looks for cover.bmp/jpg/jpeg/png in same folder as txt file
|
|
||||||
[[nodiscard]] std::string getCoverBmpPath() const;
|
|
||||||
[[nodiscard]] bool generateCoverBmp() const;
|
|
||||||
[[nodiscard]] std::string findCoverImage() const;
|
|
||||||
|
|
||||||
// Read content from file
|
|
||||||
[[nodiscard]] bool readContent(uint8_t* buffer, size_t offset, size_t length) const;
|
|
||||||
};
|
|
||||||
261
lib/Xtc/Xtc.cpp
261
lib/Xtc/Xtc.cpp
@ -293,267 +293,6 @@ bool Xtc::generateCoverBmp() const {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Xtc::getThumbBmpPath() const { return cachePath + "/thumb.bmp"; }
|
|
||||||
|
|
||||||
bool Xtc::generateThumbBmp() const {
|
|
||||||
// Already generated
|
|
||||||
if (SdMan.exists(getThumbBmpPath().c_str())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!loaded || !parser) {
|
|
||||||
Serial.printf("[%lu] [XTC] Cannot generate thumb 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();
|
|
||||||
|
|
||||||
// Calculate target dimensions for thumbnail (fit within 240x400 Continue Reading card)
|
|
||||||
constexpr int THUMB_TARGET_WIDTH = 240;
|
|
||||||
constexpr int THUMB_TARGET_HEIGHT = 400;
|
|
||||||
|
|
||||||
// Calculate scale factor
|
|
||||||
float scaleX = static_cast<float>(THUMB_TARGET_WIDTH) / pageInfo.width;
|
|
||||||
float scaleY = static_cast<float>(THUMB_TARGET_HEIGHT) / pageInfo.height;
|
|
||||||
float scale = (scaleX < scaleY) ? scaleX : scaleY;
|
|
||||||
|
|
||||||
// Only scale down, never up
|
|
||||||
if (scale >= 1.0f) {
|
|
||||||
// Page is already small enough, just use cover.bmp
|
|
||||||
// Copy cover.bmp to thumb.bmp
|
|
||||||
if (generateCoverBmp()) {
|
|
||||||
FsFile src, dst;
|
|
||||||
if (SdMan.openFileForRead("XTC", getCoverBmpPath(), src)) {
|
|
||||||
if (SdMan.openFileForWrite("XTC", getThumbBmpPath(), dst)) {
|
|
||||||
uint8_t buffer[512];
|
|
||||||
while (src.available()) {
|
|
||||||
size_t bytesRead = src.read(buffer, sizeof(buffer));
|
|
||||||
dst.write(buffer, bytesRead);
|
|
||||||
}
|
|
||||||
dst.close();
|
|
||||||
}
|
|
||||||
src.close();
|
|
||||||
}
|
|
||||||
Serial.printf("[%lu] [XTC] Copied cover to thumb (no scaling needed)\n", millis());
|
|
||||||
return SdMan.exists(getThumbBmpPath().c_str());
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t thumbWidth = static_cast<uint16_t>(pageInfo.width * scale);
|
|
||||||
uint16_t thumbHeight = static_cast<uint16_t>(pageInfo.height * scale);
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [XTC] Generating thumb BMP: %dx%d -> %dx%d (scale: %.3f)\n", millis(), pageInfo.width,
|
|
||||||
pageInfo.height, thumbWidth, thumbHeight, scale);
|
|
||||||
|
|
||||||
// Allocate buffer for page data
|
|
||||||
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 for thumb\n", millis());
|
|
||||||
free(pageBuffer);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create thumbnail BMP file - use 1-bit format for fast home screen rendering (no gray passes)
|
|
||||||
FsFile thumbBmp;
|
|
||||||
if (!SdMan.openFileForWrite("XTC", getThumbBmpPath(), thumbBmp)) {
|
|
||||||
Serial.printf("[%lu] [XTC] Failed to create thumb BMP file\n", millis());
|
|
||||||
free(pageBuffer);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write 1-bit BMP header for fast home screen rendering
|
|
||||||
const uint32_t rowSize = (thumbWidth + 31) / 32 * 4; // 1 bit per pixel, aligned to 4 bytes
|
|
||||||
const uint32_t imageSize = rowSize * thumbHeight;
|
|
||||||
const uint32_t fileSize = 14 + 40 + 8 + imageSize; // 8 bytes for 2-color palette
|
|
||||||
|
|
||||||
// File header
|
|
||||||
thumbBmp.write('B');
|
|
||||||
thumbBmp.write('M');
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&fileSize), 4);
|
|
||||||
uint32_t reserved = 0;
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&reserved), 4);
|
|
||||||
uint32_t dataOffset = 14 + 40 + 8; // 1-bit palette has 2 colors (8 bytes)
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&dataOffset), 4);
|
|
||||||
|
|
||||||
// DIB header
|
|
||||||
uint32_t dibHeaderSize = 40;
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&dibHeaderSize), 4);
|
|
||||||
int32_t widthVal = thumbWidth;
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&widthVal), 4);
|
|
||||||
int32_t heightVal = -static_cast<int32_t>(thumbHeight); // Negative for top-down
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&heightVal), 4);
|
|
||||||
uint16_t planes = 1;
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&planes), 2);
|
|
||||||
uint16_t bitsPerPixel = 1; // 1-bit for black and white
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&bitsPerPixel), 2);
|
|
||||||
uint32_t compression = 0;
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&compression), 4);
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&imageSize), 4);
|
|
||||||
int32_t ppmX = 2835;
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&ppmX), 4);
|
|
||||||
int32_t ppmY = 2835;
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&ppmY), 4);
|
|
||||||
uint32_t colorsUsed = 2;
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&colorsUsed), 4);
|
|
||||||
uint32_t colorsImportant = 2;
|
|
||||||
thumbBmp.write(reinterpret_cast<const uint8_t*>(&colorsImportant), 4);
|
|
||||||
|
|
||||||
// Color palette (2 colors for 1-bit: black and white)
|
|
||||||
uint8_t palette[8] = {
|
|
||||||
0x00, 0x00, 0x00, 0x00, // Color 0: Black
|
|
||||||
0xFF, 0xFF, 0xFF, 0x00 // Color 1: White
|
|
||||||
};
|
|
||||||
thumbBmp.write(palette, 8);
|
|
||||||
|
|
||||||
// Allocate row buffer for 1-bit output
|
|
||||||
uint8_t* rowBuffer = static_cast<uint8_t*>(malloc(rowSize));
|
|
||||||
if (!rowBuffer) {
|
|
||||||
free(pageBuffer);
|
|
||||||
thumbBmp.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fixed-point scale factor (16.16)
|
|
||||||
uint32_t scaleInv_fp = static_cast<uint32_t>(65536.0f / scale);
|
|
||||||
|
|
||||||
// Pre-calculate plane info for 2-bit mode
|
|
||||||
const size_t planeSize = (bitDepth == 2) ? ((static_cast<size_t>(pageInfo.width) * pageInfo.height + 7) / 8) : 0;
|
|
||||||
const uint8_t* plane1 = (bitDepth == 2) ? pageBuffer : nullptr;
|
|
||||||
const uint8_t* plane2 = (bitDepth == 2) ? pageBuffer + planeSize : nullptr;
|
|
||||||
const size_t colBytes = (bitDepth == 2) ? ((pageInfo.height + 7) / 8) : 0;
|
|
||||||
const size_t srcRowBytes = (bitDepth == 1) ? ((pageInfo.width + 7) / 8) : 0;
|
|
||||||
|
|
||||||
for (uint16_t dstY = 0; dstY < thumbHeight; dstY++) {
|
|
||||||
memset(rowBuffer, 0xFF, rowSize); // Start with all white (bit 1)
|
|
||||||
|
|
||||||
// Calculate source Y range with bounds checking
|
|
||||||
uint32_t srcYStart = (static_cast<uint32_t>(dstY) * scaleInv_fp) >> 16;
|
|
||||||
uint32_t srcYEnd = (static_cast<uint32_t>(dstY + 1) * scaleInv_fp) >> 16;
|
|
||||||
if (srcYStart >= pageInfo.height) srcYStart = pageInfo.height - 1;
|
|
||||||
if (srcYEnd > pageInfo.height) srcYEnd = pageInfo.height;
|
|
||||||
if (srcYEnd <= srcYStart) srcYEnd = srcYStart + 1;
|
|
||||||
if (srcYEnd > pageInfo.height) srcYEnd = pageInfo.height;
|
|
||||||
|
|
||||||
for (uint16_t dstX = 0; dstX < thumbWidth; dstX++) {
|
|
||||||
// Calculate source X range with bounds checking
|
|
||||||
uint32_t srcXStart = (static_cast<uint32_t>(dstX) * scaleInv_fp) >> 16;
|
|
||||||
uint32_t srcXEnd = (static_cast<uint32_t>(dstX + 1) * scaleInv_fp) >> 16;
|
|
||||||
if (srcXStart >= pageInfo.width) srcXStart = pageInfo.width - 1;
|
|
||||||
if (srcXEnd > pageInfo.width) srcXEnd = pageInfo.width;
|
|
||||||
if (srcXEnd <= srcXStart) srcXEnd = srcXStart + 1;
|
|
||||||
if (srcXEnd > pageInfo.width) srcXEnd = pageInfo.width;
|
|
||||||
|
|
||||||
// Area averaging: sum grayscale values (0-255 range)
|
|
||||||
uint32_t graySum = 0;
|
|
||||||
uint32_t totalCount = 0;
|
|
||||||
|
|
||||||
for (uint32_t srcY = srcYStart; srcY < srcYEnd && srcY < pageInfo.height; srcY++) {
|
|
||||||
for (uint32_t srcX = srcXStart; srcX < srcXEnd && srcX < pageInfo.width; srcX++) {
|
|
||||||
uint8_t grayValue = 255; // Default: white
|
|
||||||
|
|
||||||
if (bitDepth == 2) {
|
|
||||||
// XTH 2-bit mode: pixel value 0-3
|
|
||||||
// Bounds check for column index
|
|
||||||
if (srcX < pageInfo.width) {
|
|
||||||
const size_t colIndex = pageInfo.width - 1 - srcX;
|
|
||||||
const size_t byteInCol = srcY / 8;
|
|
||||||
const size_t bitInByte = 7 - (srcY % 8);
|
|
||||||
const size_t byteOffset = colIndex * colBytes + byteInCol;
|
|
||||||
// Bounds check for buffer access
|
|
||||||
if (byteOffset < planeSize) {
|
|
||||||
const uint8_t bit1 = (plane1[byteOffset] >> bitInByte) & 1;
|
|
||||||
const uint8_t bit2 = (plane2[byteOffset] >> bitInByte) & 1;
|
|
||||||
const uint8_t pixelValue = (bit1 << 1) | bit2;
|
|
||||||
// Convert 2-bit (0-3) to grayscale: 0=black, 3=white
|
|
||||||
// pixelValue: 0=white, 1=light gray, 2=dark gray, 3=black (XTC polarity)
|
|
||||||
grayValue = (3 - pixelValue) * 85; // 0->255, 1->170, 2->85, 3->0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 1-bit mode
|
|
||||||
const size_t byteIdx = srcY * srcRowBytes + srcX / 8;
|
|
||||||
const size_t bitIdx = 7 - (srcX % 8);
|
|
||||||
// Bounds check for buffer access
|
|
||||||
if (byteIdx < bitmapSize) {
|
|
||||||
const uint8_t pixelBit = (pageBuffer[byteIdx] >> bitIdx) & 1;
|
|
||||||
// XTC polarity: 1=black, 0=white
|
|
||||||
grayValue = pixelBit ? 0 : 255;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
graySum += grayValue;
|
|
||||||
totalCount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate average grayscale and quantize to 1-bit with noise dithering
|
|
||||||
uint8_t avgGray = (totalCount > 0) ? static_cast<uint8_t>(graySum / totalCount) : 255;
|
|
||||||
|
|
||||||
// Hash-based noise dithering for 1-bit output
|
|
||||||
uint32_t hash = static_cast<uint32_t>(dstX) * 374761393u + static_cast<uint32_t>(dstY) * 668265263u;
|
|
||||||
hash = (hash ^ (hash >> 13)) * 1274126177u;
|
|
||||||
const int threshold = static_cast<int>(hash >> 24); // 0-255
|
|
||||||
const int adjustedThreshold = 128 + ((threshold - 128) / 2); // Range: 64-192
|
|
||||||
|
|
||||||
// Quantize to 1-bit: 0=black, 1=white
|
|
||||||
uint8_t oneBit = (avgGray >= adjustedThreshold) ? 1 : 0;
|
|
||||||
|
|
||||||
// Pack 1-bit value into row buffer (MSB first, 8 pixels per byte)
|
|
||||||
const size_t byteIndex = dstX / 8;
|
|
||||||
const size_t bitOffset = 7 - (dstX % 8);
|
|
||||||
// Bounds check for row buffer access
|
|
||||||
if (byteIndex < rowSize) {
|
|
||||||
if (oneBit) {
|
|
||||||
rowBuffer[byteIndex] |= (1 << bitOffset); // Set bit for white
|
|
||||||
} else {
|
|
||||||
rowBuffer[byteIndex] &= ~(1 << bitOffset); // Clear bit for black
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write row (already padded to 4-byte boundary by rowSize)
|
|
||||||
thumbBmp.write(rowBuffer, rowSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(rowBuffer);
|
|
||||||
thumbBmp.close();
|
|
||||||
free(pageBuffer);
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [XTC] Generated thumb BMP (%dx%d): %s\n", millis(), thumbWidth, thumbHeight,
|
|
||||||
getThumbBmpPath().c_str());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t Xtc::getPageCount() const {
|
uint32_t Xtc::getPageCount() const {
|
||||||
if (!loaded || !parser) {
|
if (!loaded || !parser) {
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@ -62,9 +62,6 @@ class Xtc {
|
|||||||
// Cover image support (for sleep screen)
|
// Cover image support (for sleep screen)
|
||||||
std::string getCoverBmpPath() const;
|
std::string getCoverBmpPath() const;
|
||||||
bool generateCoverBmp() const;
|
bool generateCoverBmp() const;
|
||||||
// Thumbnail support (for Continue Reading card)
|
|
||||||
std::string getThumbBmpPath() const;
|
|
||||||
bool generateThumbBmp() const;
|
|
||||||
|
|
||||||
// Page access
|
// Page access
|
||||||
uint32_t getPageCount() const;
|
uint32_t getPageCount() const;
|
||||||
|
|||||||
@ -1,9 +1,7 @@
|
|||||||
[platformio]
|
[platformio]
|
||||||
|
crosspoint_version = 0.11.2
|
||||||
default_envs = default
|
default_envs = default
|
||||||
|
|
||||||
[crosspoint]
|
|
||||||
version = 0.14.0
|
|
||||||
|
|
||||||
[base]
|
[base]
|
||||||
platform = espressif32 @ 6.12.0
|
platform = espressif32 @ 6.12.0
|
||||||
board = esp32-c3-devkitm-1
|
board = esp32-c3-devkitm-1
|
||||||
@ -45,18 +43,17 @@ lib_deps =
|
|||||||
InputManager=symlink://open-x4-sdk/libs/hardware/InputManager
|
InputManager=symlink://open-x4-sdk/libs/hardware/InputManager
|
||||||
EInkDisplay=symlink://open-x4-sdk/libs/display/EInkDisplay
|
EInkDisplay=symlink://open-x4-sdk/libs/display/EInkDisplay
|
||||||
SDCardManager=symlink://open-x4-sdk/libs/hardware/SDCardManager
|
SDCardManager=symlink://open-x4-sdk/libs/hardware/SDCardManager
|
||||||
bblanchon/ArduinoJson @ 7.4.2
|
ArduinoJson @ 7.4.2
|
||||||
ricmoo/QRCode @ 0.0.1
|
QRCode @ 0.0.1
|
||||||
links2004/WebSockets @ 2.7.3
|
|
||||||
|
|
||||||
[env:default]
|
[env:default]
|
||||||
extends = base
|
extends = base
|
||||||
build_flags =
|
build_flags =
|
||||||
${base.build_flags}
|
${base.build_flags}
|
||||||
-DCROSSPOINT_VERSION=\"${crosspoint.version}-dev\"
|
-DCROSSPOINT_VERSION=\"${platformio.crosspoint_version}-dev\"
|
||||||
|
|
||||||
[env:gh_release]
|
[env:gh_release]
|
||||||
extends = base
|
extends = base
|
||||||
build_flags =
|
build_flags =
|
||||||
${base.build_flags}
|
${base.build_flags}
|
||||||
-DCROSSPOINT_VERSION=\"${crosspoint.version}\"
|
-DCROSSPOINT_VERSION=\"${platformio.crosspoint_version}\"
|
||||||
|
|||||||
@ -4,8 +4,6 @@
|
|||||||
#include <SDCardManager.h>
|
#include <SDCardManager.h>
|
||||||
#include <Serialization.h>
|
#include <Serialization.h>
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
|
|
||||||
// Initialize the static instance
|
// Initialize the static instance
|
||||||
@ -14,7 +12,7 @@ CrossPointSettings CrossPointSettings::instance;
|
|||||||
namespace {
|
namespace {
|
||||||
constexpr uint8_t SETTINGS_FILE_VERSION = 1;
|
constexpr uint8_t SETTINGS_FILE_VERSION = 1;
|
||||||
// Increment this when adding new persisted settings fields
|
// Increment this when adding new persisted settings fields
|
||||||
constexpr uint8_t SETTINGS_COUNT = 18;
|
constexpr uint8_t SETTINGS_COUNT = 10;
|
||||||
constexpr char SETTINGS_FILE[] = "/.crosspoint/settings.bin";
|
constexpr char SETTINGS_FILE[] = "/.crosspoint/settings.bin";
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
@ -39,15 +37,6 @@ bool CrossPointSettings::saveToFile() const {
|
|||||||
serialization::writePod(outputFile, fontFamily);
|
serialization::writePod(outputFile, fontFamily);
|
||||||
serialization::writePod(outputFile, fontSize);
|
serialization::writePod(outputFile, fontSize);
|
||||||
serialization::writePod(outputFile, lineSpacing);
|
serialization::writePod(outputFile, lineSpacing);
|
||||||
serialization::writePod(outputFile, paragraphAlignment);
|
|
||||||
serialization::writePod(outputFile, sleepTimeout);
|
|
||||||
serialization::writePod(outputFile, refreshFrequency);
|
|
||||||
serialization::writePod(outputFile, screenMargin);
|
|
||||||
serialization::writePod(outputFile, sleepScreenCoverMode);
|
|
||||||
serialization::writeString(outputFile, std::string(opdsServerUrl));
|
|
||||||
serialization::writePod(outputFile, textAntiAliasing);
|
|
||||||
serialization::writePod(outputFile, hideBatteryPercentage);
|
|
||||||
serialization::writePod(outputFile, longPressChapterSkip);
|
|
||||||
outputFile.close();
|
outputFile.close();
|
||||||
|
|
||||||
Serial.printf("[%lu] [CPS] Settings saved to file\n", millis());
|
Serial.printf("[%lu] [CPS] Settings saved to file\n", millis());
|
||||||
@ -94,28 +83,6 @@ bool CrossPointSettings::loadFromFile() {
|
|||||||
if (++settingsRead >= fileSettingsCount) break;
|
if (++settingsRead >= fileSettingsCount) break;
|
||||||
serialization::readPod(inputFile, lineSpacing);
|
serialization::readPod(inputFile, lineSpacing);
|
||||||
if (++settingsRead >= fileSettingsCount) break;
|
if (++settingsRead >= fileSettingsCount) break;
|
||||||
serialization::readPod(inputFile, paragraphAlignment);
|
|
||||||
if (++settingsRead >= fileSettingsCount) break;
|
|
||||||
serialization::readPod(inputFile, sleepTimeout);
|
|
||||||
if (++settingsRead >= fileSettingsCount) break;
|
|
||||||
serialization::readPod(inputFile, refreshFrequency);
|
|
||||||
if (++settingsRead >= fileSettingsCount) break;
|
|
||||||
serialization::readPod(inputFile, screenMargin);
|
|
||||||
if (++settingsRead >= fileSettingsCount) break;
|
|
||||||
serialization::readPod(inputFile, sleepScreenCoverMode);
|
|
||||||
if (++settingsRead >= fileSettingsCount) break;
|
|
||||||
{
|
|
||||||
std::string urlStr;
|
|
||||||
serialization::readString(inputFile, urlStr);
|
|
||||||
strncpy(opdsServerUrl, urlStr.c_str(), sizeof(opdsServerUrl) - 1);
|
|
||||||
opdsServerUrl[sizeof(opdsServerUrl) - 1] = '\0';
|
|
||||||
}
|
|
||||||
serialization::readPod(inputFile, textAntiAliasing);
|
|
||||||
if (++settingsRead >= fileSettingsCount) break;
|
|
||||||
serialization::readPod(inputFile, hideBatteryPercentage);
|
|
||||||
if (++settingsRead >= fileSettingsCount) break;
|
|
||||||
serialization::readPod(inputFile, longPressChapterSkip);
|
|
||||||
if (++settingsRead >= fileSettingsCount) break;
|
|
||||||
} while (false);
|
} while (false);
|
||||||
|
|
||||||
inputFile.close();
|
inputFile.close();
|
||||||
@ -159,38 +126,6 @@ float CrossPointSettings::getReaderLineCompression() const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long CrossPointSettings::getSleepTimeoutMs() const {
|
|
||||||
switch (sleepTimeout) {
|
|
||||||
case SLEEP_1_MIN:
|
|
||||||
return 1UL * 60 * 1000;
|
|
||||||
case SLEEP_5_MIN:
|
|
||||||
return 5UL * 60 * 1000;
|
|
||||||
case SLEEP_10_MIN:
|
|
||||||
default:
|
|
||||||
return 10UL * 60 * 1000;
|
|
||||||
case SLEEP_15_MIN:
|
|
||||||
return 15UL * 60 * 1000;
|
|
||||||
case SLEEP_30_MIN:
|
|
||||||
return 30UL * 60 * 1000;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int CrossPointSettings::getRefreshFrequency() const {
|
|
||||||
switch (refreshFrequency) {
|
|
||||||
case REFRESH_1:
|
|
||||||
return 1;
|
|
||||||
case REFRESH_5:
|
|
||||||
return 5;
|
|
||||||
case REFRESH_10:
|
|
||||||
return 10;
|
|
||||||
case REFRESH_15:
|
|
||||||
default:
|
|
||||||
return 15;
|
|
||||||
case REFRESH_30:
|
|
||||||
return 30;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int CrossPointSettings::getReaderFontId() const {
|
int CrossPointSettings::getReaderFontId() const {
|
||||||
switch (fontFamily) {
|
switch (fontFamily) {
|
||||||
case BOOKERLY:
|
case BOOKERLY:
|
||||||
|
|||||||
@ -16,8 +16,7 @@ class CrossPointSettings {
|
|||||||
CrossPointSettings& operator=(const CrossPointSettings&) = delete;
|
CrossPointSettings& operator=(const CrossPointSettings&) = delete;
|
||||||
|
|
||||||
// Should match with SettingsActivity text
|
// Should match with SettingsActivity text
|
||||||
enum SLEEP_SCREEN_MODE { DARK = 0, LIGHT = 1, CUSTOM = 2, COVER = 3, BLANK = 4 };
|
enum SLEEP_SCREEN_MODE { DARK = 0, LIGHT = 1, CUSTOM = 2, COVER = 3 };
|
||||||
enum SLEEP_SCREEN_COVER_MODE { FIT = 0, CROP = 1 };
|
|
||||||
|
|
||||||
// Status bar display type enum
|
// Status bar display type enum
|
||||||
enum STATUS_BAR_MODE { NONE = 0, NO_PROGRESS = 1, FULL = 2 };
|
enum STATUS_BAR_MODE { NONE = 0, NO_PROGRESS = 1, FULL = 2 };
|
||||||
@ -44,31 +43,15 @@ class CrossPointSettings {
|
|||||||
// Font size options
|
// Font size options
|
||||||
enum FONT_SIZE { SMALL = 0, MEDIUM = 1, LARGE = 2, EXTRA_LARGE = 3, EXTRA_SMALL = 4 };
|
enum FONT_SIZE { SMALL = 0, MEDIUM = 1, LARGE = 2, EXTRA_LARGE = 3, EXTRA_SMALL = 4 };
|
||||||
enum LINE_COMPRESSION { TIGHT = 0, NORMAL = 1, WIDE = 2 };
|
enum LINE_COMPRESSION { TIGHT = 0, NORMAL = 1, WIDE = 2 };
|
||||||
enum PARAGRAPH_ALIGNMENT { JUSTIFIED = 0, LEFT_ALIGN = 1, CENTER_ALIGN = 2, RIGHT_ALIGN = 3 };
|
|
||||||
|
|
||||||
// Auto-sleep timeout options (in minutes)
|
|
||||||
enum SLEEP_TIMEOUT { SLEEP_1_MIN = 0, SLEEP_5_MIN = 1, SLEEP_10_MIN = 2, SLEEP_15_MIN = 3, SLEEP_30_MIN = 4 };
|
|
||||||
|
|
||||||
// E-ink refresh frequency (pages between full refreshes)
|
|
||||||
enum REFRESH_FREQUENCY { REFRESH_1 = 0, REFRESH_5 = 1, REFRESH_10 = 2, REFRESH_15 = 3, REFRESH_30 = 4 };
|
|
||||||
|
|
||||||
// Short power button press actions
|
|
||||||
enum SHORT_PWRBTN { IGNORE = 0, SLEEP = 1, PAGE_TURN = 2 };
|
|
||||||
|
|
||||||
// Hide battery percentage
|
|
||||||
enum HIDE_BATTERY_PERCENTAGE { HIDE_NEVER = 0, HIDE_READER = 1, HIDE_ALWAYS = 2 };
|
|
||||||
|
|
||||||
// Sleep screen settings
|
// Sleep screen settings
|
||||||
uint8_t sleepScreen = DARK;
|
uint8_t sleepScreen = DARK;
|
||||||
// Sleep screen cover mode settings
|
|
||||||
uint8_t sleepScreenCoverMode = FIT;
|
|
||||||
// Status bar settings
|
// Status bar settings
|
||||||
uint8_t statusBar = FULL;
|
uint8_t statusBar = FULL;
|
||||||
// Text rendering settings
|
// Text rendering settings
|
||||||
uint8_t extraParagraphSpacing = 1;
|
uint8_t extraParagraphSpacing = 1;
|
||||||
uint8_t textAntiAliasing = 1;
|
// Duration of the power button press
|
||||||
// Short power button click behaviour
|
uint8_t shortPwrBtn = 0;
|
||||||
uint8_t shortPwrBtn = IGNORE;
|
|
||||||
// EPUB reading orientation settings
|
// EPUB reading orientation settings
|
||||||
// 0 = portrait (default), 1 = landscape clockwise, 2 = inverted, 3 = landscape counter-clockwise
|
// 0 = portrait (default), 1 = landscape clockwise, 2 = inverted, 3 = landscape counter-clockwise
|
||||||
uint8_t orientation = PORTRAIT;
|
uint8_t orientation = PORTRAIT;
|
||||||
@ -79,36 +62,19 @@ class CrossPointSettings {
|
|||||||
uint8_t fontFamily = BOOKERLY;
|
uint8_t fontFamily = BOOKERLY;
|
||||||
uint8_t fontSize = MEDIUM;
|
uint8_t fontSize = MEDIUM;
|
||||||
uint8_t lineSpacing = NORMAL;
|
uint8_t lineSpacing = NORMAL;
|
||||||
uint8_t paragraphAlignment = JUSTIFIED;
|
|
||||||
// Auto-sleep timeout setting (default 10 minutes)
|
|
||||||
uint8_t sleepTimeout = SLEEP_10_MIN;
|
|
||||||
// E-ink refresh frequency (default 15 pages)
|
|
||||||
uint8_t refreshFrequency = REFRESH_15;
|
|
||||||
// Reader screen margin settings
|
|
||||||
uint8_t screenMargin = 5;
|
|
||||||
// OPDS browser settings
|
|
||||||
char opdsServerUrl[128] = "";
|
|
||||||
// Hide battery percentage
|
|
||||||
uint8_t hideBatteryPercentage = HIDE_NEVER;
|
|
||||||
// Long-press chapter skip on side buttons
|
|
||||||
uint8_t longPressChapterSkip = 1;
|
|
||||||
|
|
||||||
~CrossPointSettings() = default;
|
~CrossPointSettings() = default;
|
||||||
|
|
||||||
// Get singleton instance
|
// Get singleton instance
|
||||||
static CrossPointSettings& getInstance() { return instance; }
|
static CrossPointSettings& getInstance() { return instance; }
|
||||||
|
|
||||||
uint16_t getPowerButtonDuration() const {
|
uint16_t getPowerButtonDuration() const { return shortPwrBtn ? 10 : 400; }
|
||||||
return (shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::SLEEP) ? 10 : 400;
|
|
||||||
}
|
|
||||||
int getReaderFontId() const;
|
int getReaderFontId() const;
|
||||||
|
|
||||||
bool saveToFile() const;
|
bool saveToFile() const;
|
||||||
bool loadFromFile();
|
bool loadFromFile();
|
||||||
|
|
||||||
float getReaderLineCompression() const;
|
float getReaderLineCompression() const;
|
||||||
unsigned long getSleepTimeoutMs() const;
|
|
||||||
int getRefreshFrequency() const;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper macro to access settings
|
// Helper macro to access settings
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
#include <Serialization.h>
|
#include <Serialization.h>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
constexpr uint8_t STATE_FILE_VERSION = 2;
|
constexpr uint8_t STATE_FILE_VERSION = 1;
|
||||||
constexpr char STATE_FILE[] = "/.crosspoint/state.bin";
|
constexpr char STATE_FILE[] = "/.crosspoint/state.bin";
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
@ -19,7 +19,6 @@ bool CrossPointState::saveToFile() const {
|
|||||||
|
|
||||||
serialization::writePod(outputFile, STATE_FILE_VERSION);
|
serialization::writePod(outputFile, STATE_FILE_VERSION);
|
||||||
serialization::writeString(outputFile, openEpubPath);
|
serialization::writeString(outputFile, openEpubPath);
|
||||||
serialization::writePod(outputFile, lastSleepImage);
|
|
||||||
outputFile.close();
|
outputFile.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -32,18 +31,13 @@ bool CrossPointState::loadFromFile() {
|
|||||||
|
|
||||||
uint8_t version;
|
uint8_t version;
|
||||||
serialization::readPod(inputFile, version);
|
serialization::readPod(inputFile, version);
|
||||||
if (version > STATE_FILE_VERSION) {
|
if (version != STATE_FILE_VERSION) {
|
||||||
Serial.printf("[%lu] [CPS] Deserialization failed: Unknown version %u\n", millis(), version);
|
Serial.printf("[%lu] [CPS] Deserialization failed: Unknown version %u\n", millis(), version);
|
||||||
inputFile.close();
|
inputFile.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
serialization::readString(inputFile, openEpubPath);
|
serialization::readString(inputFile, openEpubPath);
|
||||||
if (version >= 2) {
|
|
||||||
serialization::readPod(inputFile, lastSleepImage);
|
|
||||||
} else {
|
|
||||||
lastSleepImage = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
inputFile.close();
|
inputFile.close();
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@ -8,7 +8,6 @@ class CrossPointState {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
std::string openEpubPath;
|
std::string openEpubPath;
|
||||||
uint8_t lastSleepImage;
|
|
||||||
~CrossPointState() = default;
|
~CrossPointState() = default;
|
||||||
|
|
||||||
// Get singleton instance
|
// Get singleton instance
|
||||||
|
|||||||
@ -2,36 +2,34 @@
|
|||||||
|
|
||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "Battery.h"
|
#include "Battery.h"
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
|
|
||||||
void ScreenComponents::drawBattery(const GfxRenderer& renderer, const int left, const int top,
|
void ScreenComponents::drawBattery(const GfxRenderer& renderer, const int left, const int top) {
|
||||||
const bool showPercentage) {
|
|
||||||
// Left aligned battery icon and percentage
|
// Left aligned battery icon and percentage
|
||||||
const uint16_t percentage = battery.readPercentage();
|
const uint16_t percentage = battery.readPercentage();
|
||||||
const auto percentageText = showPercentage ? std::to_string(percentage) + "%" : "";
|
const auto percentageText = std::to_string(percentage) + "%";
|
||||||
renderer.drawText(SMALL_FONT_ID, left + 20, top, percentageText.c_str());
|
renderer.drawText(SMALL_FONT_ID, left + 20, top, percentageText.c_str());
|
||||||
|
|
||||||
// 1 column on left, 2 columns on right, 5 columns of battery body
|
// 1 column on left, 2 columns on right, 5 columns of battery body
|
||||||
constexpr int batteryWidth = 15;
|
constexpr int batteryWidth = 15;
|
||||||
constexpr int batteryHeight = 12;
|
constexpr int batteryHeight = 10;
|
||||||
const int x = left;
|
const int x = left;
|
||||||
const int y = top + 6;
|
const int y = top + 8;
|
||||||
|
|
||||||
// Top line
|
// Top line
|
||||||
renderer.drawLine(x + 1, y, x + batteryWidth - 3, y);
|
renderer.drawLine(x, y, x + batteryWidth - 4, y);
|
||||||
// Bottom line
|
// Bottom line
|
||||||
renderer.drawLine(x + 1, y + batteryHeight - 1, x + batteryWidth - 3, y + batteryHeight - 1);
|
renderer.drawLine(x, y + batteryHeight - 1, x + batteryWidth - 4, y + batteryHeight - 1);
|
||||||
// Left line
|
// Left line
|
||||||
renderer.drawLine(x, y + 1, x, y + batteryHeight - 2);
|
renderer.drawLine(x, y, x, y + batteryHeight - 1);
|
||||||
// Battery end
|
// Battery end
|
||||||
renderer.drawLine(x + batteryWidth - 2, y + 1, x + batteryWidth - 2, y + batteryHeight - 2);
|
renderer.drawLine(x + batteryWidth - 4, y, x + batteryWidth - 4, y + batteryHeight - 1);
|
||||||
renderer.drawPixel(x + batteryWidth - 1, y + 3);
|
renderer.drawLine(x + batteryWidth - 3, y + 2, x + batteryWidth - 1, y + 2);
|
||||||
renderer.drawPixel(x + batteryWidth - 1, y + batteryHeight - 4);
|
renderer.drawLine(x + batteryWidth - 3, y + batteryHeight - 3, x + batteryWidth - 1, y + batteryHeight - 3);
|
||||||
renderer.drawLine(x + batteryWidth - 0, y + 4, x + batteryWidth - 0, y + batteryHeight - 5);
|
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
|
// The +1 is to round up, so that we always fill at least one pixel
|
||||||
int filledWidth = percentage * (batteryWidth - 5) / 100 + 1;
|
int filledWidth = percentage * (batteryWidth - 5) / 100 + 1;
|
||||||
@ -39,28 +37,5 @@ void ScreenComponents::drawBattery(const GfxRenderer& renderer, const int left,
|
|||||||
filledWidth = batteryWidth - 5; // Ensure we don't overflow
|
filledWidth = batteryWidth - 5; // Ensure we don't overflow
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer.fillRect(x + 2, y + 2, filledWidth, batteryHeight - 4);
|
renderer.fillRect(x + 1, y + 1, filledWidth, batteryHeight - 2);
|
||||||
}
|
|
||||||
|
|
||||||
void ScreenComponents::drawProgressBar(const GfxRenderer& renderer, const int x, const int y, const int width,
|
|
||||||
const int height, const size_t current, const size_t total) {
|
|
||||||
if (total == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use 64-bit arithmetic to avoid overflow for large files
|
|
||||||
const int percent = static_cast<int>((static_cast<uint64_t>(current) * 100) / total);
|
|
||||||
|
|
||||||
// Draw outline
|
|
||||||
renderer.drawRect(x, y, width, height);
|
|
||||||
|
|
||||||
// Draw filled portion
|
|
||||||
const int fillWidth = (width - 4) * percent / 100;
|
|
||||||
if (fillWidth > 0) {
|
|
||||||
renderer.fillRect(x + 2, y + 2, fillWidth, height - 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw percentage text centered below bar
|
|
||||||
const std::string percentText = std::to_string(percent) + "%";
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, y + height + 15, percentText.c_str());
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,24 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstddef>
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
class GfxRenderer;
|
class GfxRenderer;
|
||||||
|
|
||||||
class ScreenComponents {
|
class ScreenComponents {
|
||||||
public:
|
public:
|
||||||
static void drawBattery(const GfxRenderer& renderer, int left, int top, bool showPercentage = true);
|
static void drawBattery(const GfxRenderer& renderer, int left, int top);
|
||||||
|
|
||||||
/**
|
|
||||||
* Draw a progress bar with percentage text.
|
|
||||||
* @param renderer The graphics renderer
|
|
||||||
* @param x Left position of the bar
|
|
||||||
* @param y Top position of the bar
|
|
||||||
* @param width Width of the bar
|
|
||||||
* @param height Height of the bar
|
|
||||||
* @param current Current progress value
|
|
||||||
* @param total Total value for 100% progress
|
|
||||||
*/
|
|
||||||
static void drawProgressBar(const GfxRenderer& renderer, int x, int y, int width, int height, size_t current,
|
|
||||||
size_t total);
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -22,5 +22,4 @@ class Activity {
|
|||||||
virtual void onExit() { Serial.printf("[%lu] [ACT] Exiting 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 void loop() {}
|
||||||
virtual bool skipLoopDelay() { return false; }
|
virtual bool skipLoopDelay() { return false; }
|
||||||
virtual bool preventAutoSleep() { return false; }
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,23 +3,33 @@
|
|||||||
#include <Epub.h>
|
#include <Epub.h>
|
||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
#include <SDCardManager.h>
|
#include <SDCardManager.h>
|
||||||
#include <Txt.h>
|
|
||||||
#include <Xtc.h>
|
#include <Xtc.h>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "CrossPointSettings.h"
|
#include "CrossPointSettings.h"
|
||||||
#include "CrossPointState.h"
|
#include "CrossPointState.h"
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
#include "images/CrossLarge.h"
|
#include "images/CrossLarge.h"
|
||||||
#include "util/StringUtils.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() {
|
void SleepActivity::onEnter() {
|
||||||
Activity::onEnter();
|
Activity::onEnter();
|
||||||
renderPopup("Entering Sleep...");
|
renderPopup("Entering Sleep...");
|
||||||
|
|
||||||
if (SETTINGS.sleepScreen == CrossPointSettings::SLEEP_SCREEN_MODE::BLANK) {
|
|
||||||
return renderBlankSleepScreen();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SETTINGS.sleepScreen == CrossPointSettings::SLEEP_SCREEN_MODE::CUSTOM) {
|
if (SETTINGS.sleepScreen == CrossPointSettings::SLEEP_SCREEN_MODE::CUSTOM) {
|
||||||
return renderCustomSleepScreen();
|
return renderCustomSleepScreen();
|
||||||
}
|
}
|
||||||
@ -32,16 +42,16 @@ void SleepActivity::onEnter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SleepActivity::renderPopup(const char* message) const {
|
void SleepActivity::renderPopup(const char* message) const {
|
||||||
const int textWidth = renderer.getTextWidth(UI_12_FONT_ID, message, EpdFontFamily::BOLD);
|
const int textWidth = renderer.getTextWidth(UI_12_FONT_ID, message);
|
||||||
constexpr int margin = 20;
|
constexpr int margin = 20;
|
||||||
const int x = (renderer.getScreenWidth() - textWidth - margin * 2) / 2;
|
const int x = (renderer.getScreenWidth() - textWidth - margin * 2) / 2;
|
||||||
constexpr int y = 117;
|
constexpr int y = 117;
|
||||||
const int w = textWidth + margin * 2;
|
const int w = textWidth + margin * 2;
|
||||||
const int h = renderer.getLineHeight(UI_12_FONT_ID) + margin * 2;
|
const int h = renderer.getLineHeight(UI_12_FONT_ID) + margin * 2;
|
||||||
// renderer.clearScreen();
|
// renderer.clearScreen();
|
||||||
renderer.fillRect(x - 5, y - 5, w + 10, h + 10, true);
|
|
||||||
renderer.fillRect(x + 5, y + 5, w - 10, h - 10, false);
|
renderer.fillRect(x + 5, y + 5, w - 10, h - 10, false);
|
||||||
renderer.drawText(UI_12_FONT_ID, x + margin, y + margin, message, true, EpdFontFamily::BOLD);
|
renderer.drawText(UI_12_FONT_ID, x + margin, y + margin, message);
|
||||||
|
renderer.drawRect(x + 5, y + 5, w - 10, h - 10);
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +60,7 @@ void SleepActivity::renderCustomSleepScreen() const {
|
|||||||
auto dir = SdMan.open("/sleep");
|
auto dir = SdMan.open("/sleep");
|
||||||
if (dir && dir.isDirectory()) {
|
if (dir && dir.isDirectory()) {
|
||||||
std::vector<std::string> files;
|
std::vector<std::string> files;
|
||||||
char name[500];
|
char name[128];
|
||||||
// collect all valid BMP files
|
// collect all valid BMP files
|
||||||
for (auto file = dir.openNextFile(); file; file = dir.openNextFile()) {
|
for (auto file = dir.openNextFile(); file; file = dir.openNextFile()) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
@ -81,19 +91,13 @@ void SleepActivity::renderCustomSleepScreen() const {
|
|||||||
const auto numFiles = files.size();
|
const auto numFiles = files.size();
|
||||||
if (numFiles > 0) {
|
if (numFiles > 0) {
|
||||||
// Generate a random number between 1 and numFiles
|
// Generate a random number between 1 and numFiles
|
||||||
auto randomFileIndex = random(numFiles);
|
const auto randomFileIndex = random(numFiles);
|
||||||
// If we picked the same image as last time, reroll
|
|
||||||
while (numFiles > 1 && randomFileIndex == APP_STATE.lastSleepImage) {
|
|
||||||
randomFileIndex = random(numFiles);
|
|
||||||
}
|
|
||||||
APP_STATE.lastSleepImage = randomFileIndex;
|
|
||||||
APP_STATE.saveToFile();
|
|
||||||
const auto filename = "/sleep/" + files[randomFileIndex];
|
const auto filename = "/sleep/" + files[randomFileIndex];
|
||||||
FsFile file;
|
FsFile file;
|
||||||
if (SdMan.openFileForRead("SLP", filename, file)) {
|
if (SdMan.openFileForRead("SLP", filename, file)) {
|
||||||
Serial.printf("[%lu] [SLP] Randomly loading: /sleep/%s\n", millis(), files[randomFileIndex].c_str());
|
Serial.printf("[%lu] [SLP] Randomly loading: /sleep/%s\n", millis(), files[randomFileIndex].c_str());
|
||||||
delay(100);
|
delay(100);
|
||||||
Bitmap bitmap(file, true);
|
Bitmap bitmap(file);
|
||||||
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
||||||
renderBitmapSleepScreen(bitmap);
|
renderBitmapSleepScreen(bitmap);
|
||||||
dir.close();
|
dir.close();
|
||||||
@ -108,7 +112,7 @@ void SleepActivity::renderCustomSleepScreen() const {
|
|||||||
// render a custom sleep screen instead of the default.
|
// render a custom sleep screen instead of the default.
|
||||||
FsFile file;
|
FsFile file;
|
||||||
if (SdMan.openFileForRead("SLP", "/sleep.bmp", file)) {
|
if (SdMan.openFileForRead("SLP", "/sleep.bmp", file)) {
|
||||||
Bitmap bitmap(file, true);
|
Bitmap bitmap(file);
|
||||||
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
||||||
Serial.printf("[%lu] [SLP] Loading: /sleep.bmp\n", millis());
|
Serial.printf("[%lu] [SLP] Loading: /sleep.bmp\n", millis());
|
||||||
renderBitmapSleepScreen(bitmap);
|
renderBitmapSleepScreen(bitmap);
|
||||||
@ -140,36 +144,20 @@ void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) const {
|
|||||||
int x, y;
|
int x, y;
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
const auto pageHeight = renderer.getScreenHeight();
|
const auto pageHeight = renderer.getScreenHeight();
|
||||||
float cropX = 0, cropY = 0;
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [SLP] bitmap %d x %d, screen %d x %d\n", millis(), bitmap.getWidth(), bitmap.getHeight(),
|
|
||||||
pageWidth, pageHeight);
|
|
||||||
if (bitmap.getWidth() > pageWidth || bitmap.getHeight() > pageHeight) {
|
if (bitmap.getWidth() > pageWidth || bitmap.getHeight() > pageHeight) {
|
||||||
// image will scale, make sure placement is right
|
// image will scale, make sure placement is right
|
||||||
float ratio = static_cast<float>(bitmap.getWidth()) / static_cast<float>(bitmap.getHeight());
|
const float ratio = static_cast<float>(bitmap.getWidth()) / static_cast<float>(bitmap.getHeight());
|
||||||
const float screenRatio = static_cast<float>(pageWidth) / static_cast<float>(pageHeight);
|
const float screenRatio = static_cast<float>(pageWidth) / static_cast<float>(pageHeight);
|
||||||
|
|
||||||
Serial.printf("[%lu] [SLP] bitmap ratio: %f, screen ratio: %f\n", millis(), ratio, screenRatio);
|
|
||||||
if (ratio > screenRatio) {
|
if (ratio > screenRatio) {
|
||||||
// image wider than viewport ratio, scaled down image needs to be centered vertically
|
// image wider than viewport ratio, scaled down image needs to be centered vertically
|
||||||
if (SETTINGS.sleepScreenCoverMode == CrossPointSettings::SLEEP_SCREEN_COVER_MODE::CROP) {
|
|
||||||
cropX = 1.0f - (screenRatio / ratio);
|
|
||||||
Serial.printf("[%lu] [SLP] Cropping bitmap x: %f\n", millis(), cropX);
|
|
||||||
ratio = (1.0f - cropX) * static_cast<float>(bitmap.getWidth()) / static_cast<float>(bitmap.getHeight());
|
|
||||||
}
|
|
||||||
x = 0;
|
x = 0;
|
||||||
y = std::round((static_cast<float>(pageHeight) - static_cast<float>(pageWidth) / ratio) / 2);
|
y = (pageHeight - pageWidth / ratio) / 2;
|
||||||
Serial.printf("[%lu] [SLP] Centering with ratio %f to y=%d\n", millis(), ratio, y);
|
|
||||||
} else {
|
} else {
|
||||||
// image taller than viewport ratio, scaled down image needs to be centered horizontally
|
// image taller than viewport ratio, scaled down image needs to be centered horizontally
|
||||||
if (SETTINGS.sleepScreenCoverMode == CrossPointSettings::SLEEP_SCREEN_COVER_MODE::CROP) {
|
x = (pageWidth - pageHeight * ratio) / 2;
|
||||||
cropY = 1.0f - (ratio / screenRatio);
|
|
||||||
Serial.printf("[%lu] [SLP] Cropping bitmap y: %f\n", millis(), cropY);
|
|
||||||
ratio = static_cast<float>(bitmap.getWidth()) / ((1.0f - cropY) * static_cast<float>(bitmap.getHeight()));
|
|
||||||
}
|
|
||||||
x = std::round((pageWidth - pageHeight * ratio) / 2);
|
|
||||||
y = 0;
|
y = 0;
|
||||||
Serial.printf("[%lu] [SLP] Centering with ratio %f to x=%d\n", millis(), ratio, x);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// center the image
|
// center the image
|
||||||
@ -177,22 +165,21 @@ void SleepActivity::renderBitmapSleepScreen(const Bitmap& bitmap) const {
|
|||||||
y = (pageHeight - bitmap.getHeight()) / 2;
|
y = (pageHeight - bitmap.getHeight()) / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.printf("[%lu] [SLP] drawing to %d x %d\n", millis(), x, y);
|
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight, cropX, cropY);
|
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight);
|
||||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
||||||
|
|
||||||
if (bitmap.hasGreyscale()) {
|
if (bitmap.hasGreyscale()) {
|
||||||
bitmap.rewindToData();
|
bitmap.rewindToData();
|
||||||
renderer.clearScreen(0x00);
|
renderer.clearScreen(0x00);
|
||||||
renderer.setRenderMode(GfxRenderer::GRAYSCALE_LSB);
|
renderer.setRenderMode(GfxRenderer::GRAYSCALE_LSB);
|
||||||
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight, cropX, cropY);
|
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight);
|
||||||
renderer.copyGrayscaleLsbBuffers();
|
renderer.copyGrayscaleLsbBuffers();
|
||||||
|
|
||||||
bitmap.rewindToData();
|
bitmap.rewindToData();
|
||||||
renderer.clearScreen(0x00);
|
renderer.clearScreen(0x00);
|
||||||
renderer.setRenderMode(GfxRenderer::GRAYSCALE_MSB);
|
renderer.setRenderMode(GfxRenderer::GRAYSCALE_MSB);
|
||||||
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight, cropX, cropY);
|
renderer.drawBitmap(bitmap, x, y, pageWidth, pageHeight);
|
||||||
renderer.copyGrayscaleMsbBuffers();
|
renderer.copyGrayscaleMsbBuffers();
|
||||||
|
|
||||||
renderer.displayGrayBuffer();
|
renderer.displayGrayBuffer();
|
||||||
@ -206,11 +193,9 @@ void SleepActivity::renderCoverSleepScreen() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string coverBmpPath;
|
std::string coverBmpPath;
|
||||||
bool cropped = SETTINGS.sleepScreenCoverMode == CrossPointSettings::SLEEP_SCREEN_COVER_MODE::CROP;
|
|
||||||
|
|
||||||
// Check if the current book is XTC, TXT, or EPUB
|
// Check if the current book is XTC or EPUB
|
||||||
if (StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".xtc") ||
|
if (isXtcFile(APP_STATE.openEpubPath)) {
|
||||||
StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".xtch")) {
|
|
||||||
// Handle XTC file
|
// Handle XTC file
|
||||||
Xtc lastXtc(APP_STATE.openEpubPath, "/.crosspoint");
|
Xtc lastXtc(APP_STATE.openEpubPath, "/.crosspoint");
|
||||||
if (!lastXtc.load()) {
|
if (!lastXtc.load()) {
|
||||||
@ -224,21 +209,7 @@ void SleepActivity::renderCoverSleepScreen() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
coverBmpPath = lastXtc.getCoverBmpPath();
|
coverBmpPath = lastXtc.getCoverBmpPath();
|
||||||
} else if (StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".txt")) {
|
} else {
|
||||||
// Handle TXT file - looks for cover image in the same folder
|
|
||||||
Txt lastTxt(APP_STATE.openEpubPath, "/.crosspoint");
|
|
||||||
if (!lastTxt.load()) {
|
|
||||||
Serial.println("[SLP] Failed to load last TXT");
|
|
||||||
return renderDefaultSleepScreen();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!lastTxt.generateCoverBmp()) {
|
|
||||||
Serial.println("[SLP] No cover image found for TXT file");
|
|
||||||
return renderDefaultSleepScreen();
|
|
||||||
}
|
|
||||||
|
|
||||||
coverBmpPath = lastTxt.getCoverBmpPath();
|
|
||||||
} else if (StringUtils::checkFileExtension(APP_STATE.openEpubPath, ".epub")) {
|
|
||||||
// Handle EPUB file
|
// Handle EPUB file
|
||||||
Epub lastEpub(APP_STATE.openEpubPath, "/.crosspoint");
|
Epub lastEpub(APP_STATE.openEpubPath, "/.crosspoint");
|
||||||
if (!lastEpub.load()) {
|
if (!lastEpub.load()) {
|
||||||
@ -246,14 +217,12 @@ void SleepActivity::renderCoverSleepScreen() const {
|
|||||||
return renderDefaultSleepScreen();
|
return renderDefaultSleepScreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lastEpub.generateCoverBmp(cropped)) {
|
if (!lastEpub.generateCoverBmp()) {
|
||||||
Serial.println("[SLP] Failed to generate cover bmp");
|
Serial.println("[SLP] Failed to generate cover bmp");
|
||||||
return renderDefaultSleepScreen();
|
return renderDefaultSleepScreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
coverBmpPath = lastEpub.getCoverBmpPath(cropped);
|
coverBmpPath = lastEpub.getCoverBmpPath();
|
||||||
} else {
|
|
||||||
return renderDefaultSleepScreen();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FsFile file;
|
FsFile file;
|
||||||
@ -267,8 +236,3 @@ void SleepActivity::renderCoverSleepScreen() const {
|
|||||||
|
|
||||||
renderDefaultSleepScreen();
|
renderDefaultSleepScreen();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SleepActivity::renderBlankSleepScreen() const {
|
|
||||||
renderer.clearScreen();
|
|
||||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
|
||||||
}
|
|
||||||
|
|||||||
@ -15,5 +15,4 @@ class SleepActivity final : public Activity {
|
|||||||
void renderCustomSleepScreen() const;
|
void renderCustomSleepScreen() const;
|
||||||
void renderCoverSleepScreen() const;
|
void renderCoverSleepScreen() const;
|
||||||
void renderBitmapSleepScreen(const Bitmap& bitmap) const;
|
void renderBitmapSleepScreen(const Bitmap& bitmap) const;
|
||||||
void renderBlankSleepScreen() const;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,408 +0,0 @@
|
|||||||
#include "OpdsBookBrowserActivity.h"
|
|
||||||
|
|
||||||
#include <GfxRenderer.h>
|
|
||||||
#include <HardwareSerial.h>
|
|
||||||
#include <WiFi.h>
|
|
||||||
|
|
||||||
#include "CrossPointSettings.h"
|
|
||||||
#include "MappedInputManager.h"
|
|
||||||
#include "ScreenComponents.h"
|
|
||||||
#include "activities/network/WifiSelectionActivity.h"
|
|
||||||
#include "fontIds.h"
|
|
||||||
#include "network/HttpDownloader.h"
|
|
||||||
#include "util/StringUtils.h"
|
|
||||||
#include "util/UrlUtils.h"
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
constexpr int PAGE_ITEMS = 23;
|
|
||||||
constexpr int SKIP_PAGE_MS = 700;
|
|
||||||
constexpr char OPDS_ROOT_PATH[] = "opds"; // No leading slash - relative to server URL
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::taskTrampoline(void* param) {
|
|
||||||
auto* self = static_cast<OpdsBookBrowserActivity*>(param);
|
|
||||||
self->displayTaskLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::onEnter() {
|
|
||||||
ActivityWithSubactivity::onEnter();
|
|
||||||
|
|
||||||
renderingMutex = xSemaphoreCreateMutex();
|
|
||||||
state = BrowserState::CHECK_WIFI;
|
|
||||||
entries.clear();
|
|
||||||
navigationHistory.clear();
|
|
||||||
currentPath = OPDS_ROOT_PATH;
|
|
||||||
selectorIndex = 0;
|
|
||||||
errorMessage.clear();
|
|
||||||
statusMessage = "Checking WiFi...";
|
|
||||||
updateRequired = true;
|
|
||||||
|
|
||||||
xTaskCreate(&OpdsBookBrowserActivity::taskTrampoline, "OpdsBookBrowserTask",
|
|
||||||
4096, // Stack size (larger for HTTP operations)
|
|
||||||
this, // Parameters
|
|
||||||
1, // Priority
|
|
||||||
&displayTaskHandle // Task handle
|
|
||||||
);
|
|
||||||
|
|
||||||
// Check WiFi and connect if needed, then fetch feed
|
|
||||||
checkAndConnectWifi();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::onExit() {
|
|
||||||
ActivityWithSubactivity::onExit();
|
|
||||||
|
|
||||||
// Turn off WiFi when exiting
|
|
||||||
WiFi.mode(WIFI_OFF);
|
|
||||||
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
if (displayTaskHandle) {
|
|
||||||
vTaskDelete(displayTaskHandle);
|
|
||||||
displayTaskHandle = nullptr;
|
|
||||||
}
|
|
||||||
vSemaphoreDelete(renderingMutex);
|
|
||||||
renderingMutex = nullptr;
|
|
||||||
entries.clear();
|
|
||||||
navigationHistory.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::loop() {
|
|
||||||
// Handle WiFi selection subactivity
|
|
||||||
if (state == BrowserState::WIFI_SELECTION) {
|
|
||||||
ActivityWithSubactivity::loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle error state - Confirm retries, Back goes back or home
|
|
||||||
if (state == BrowserState::ERROR) {
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
|
||||||
// Check if WiFi is still connected
|
|
||||||
if (WiFi.status() == WL_CONNECTED && WiFi.localIP() != IPAddress(0, 0, 0, 0)) {
|
|
||||||
// WiFi connected - just retry fetching the feed
|
|
||||||
Serial.printf("[%lu] [OPDS] Retry: WiFi connected, retrying fetch\n", millis());
|
|
||||||
state = BrowserState::LOADING;
|
|
||||||
statusMessage = "Loading...";
|
|
||||||
updateRequired = true;
|
|
||||||
fetchFeed(currentPath);
|
|
||||||
} else {
|
|
||||||
// WiFi not connected - launch WiFi selection
|
|
||||||
Serial.printf("[%lu] [OPDS] Retry: WiFi not connected, launching selection\n", millis());
|
|
||||||
launchWifiSelection();
|
|
||||||
}
|
|
||||||
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
|
||||||
navigateBack();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle WiFi check state - only Back works
|
|
||||||
if (state == BrowserState::CHECK_WIFI) {
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
|
||||||
onGoHome();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle loading state - only Back works
|
|
||||||
if (state == BrowserState::LOADING) {
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
|
||||||
navigateBack();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle downloading state - no input allowed
|
|
||||||
if (state == BrowserState::DOWNLOADING) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle browsing state
|
|
||||||
if (state == BrowserState::BROWSING) {
|
|
||||||
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::Up) ||
|
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Left);
|
|
||||||
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::Down) ||
|
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Right);
|
|
||||||
const bool skipPage = mappedInput.getHeldTime() > SKIP_PAGE_MS;
|
|
||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
|
||||||
if (!entries.empty()) {
|
|
||||||
const auto& entry = entries[selectorIndex];
|
|
||||||
if (entry.type == OpdsEntryType::BOOK) {
|
|
||||||
downloadBook(entry);
|
|
||||||
} else {
|
|
||||||
navigateToEntry(entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
|
||||||
navigateBack();
|
|
||||||
} else if (prevReleased && !entries.empty()) {
|
|
||||||
if (skipPage) {
|
|
||||||
selectorIndex = ((selectorIndex / PAGE_ITEMS - 1) * PAGE_ITEMS + entries.size()) % entries.size();
|
|
||||||
} else {
|
|
||||||
selectorIndex = (selectorIndex + entries.size() - 1) % entries.size();
|
|
||||||
}
|
|
||||||
updateRequired = true;
|
|
||||||
} else if (nextReleased && !entries.empty()) {
|
|
||||||
if (skipPage) {
|
|
||||||
selectorIndex = ((selectorIndex / PAGE_ITEMS + 1) * PAGE_ITEMS) % entries.size();
|
|
||||||
} else {
|
|
||||||
selectorIndex = (selectorIndex + 1) % entries.size();
|
|
||||||
}
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::displayTaskLoop() {
|
|
||||||
while (true) {
|
|
||||||
if (updateRequired) {
|
|
||||||
updateRequired = false;
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
render();
|
|
||||||
xSemaphoreGive(renderingMutex);
|
|
||||||
}
|
|
||||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::render() const {
|
|
||||||
renderer.clearScreen();
|
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
|
||||||
const auto pageHeight = renderer.getScreenHeight();
|
|
||||||
|
|
||||||
renderer.drawCenteredText(UI_12_FONT_ID, 15, "Calibre Library", true, EpdFontFamily::BOLD);
|
|
||||||
|
|
||||||
if (state == BrowserState::CHECK_WIFI) {
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2, statusMessage.c_str());
|
|
||||||
const auto labels = mappedInput.mapLabels("« Back", "", "", "");
|
|
||||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
||||||
renderer.displayBuffer();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == BrowserState::LOADING) {
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2, statusMessage.c_str());
|
|
||||||
const auto labels = mappedInput.mapLabels("« Back", "", "", "");
|
|
||||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
||||||
renderer.displayBuffer();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == BrowserState::ERROR) {
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 - 20, "Error:");
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 + 10, errorMessage.c_str());
|
|
||||||
const auto labels = mappedInput.mapLabels("« Back", "Retry", "", "");
|
|
||||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
||||||
renderer.displayBuffer();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (state == BrowserState::DOWNLOADING) {
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 - 40, "Downloading...");
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2 - 10, statusMessage.c_str());
|
|
||||||
if (downloadTotal > 0) {
|
|
||||||
const int barWidth = pageWidth - 100;
|
|
||||||
constexpr int barHeight = 20;
|
|
||||||
constexpr int barX = 50;
|
|
||||||
const int barY = pageHeight / 2 + 20;
|
|
||||||
ScreenComponents::drawProgressBar(renderer, barX, barY, barWidth, barHeight, downloadProgress, downloadTotal);
|
|
||||||
}
|
|
||||||
renderer.displayBuffer();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Browsing state
|
|
||||||
// Show appropriate button hint based on selected entry type
|
|
||||||
const char* confirmLabel = "Open";
|
|
||||||
if (!entries.empty() && entries[selectorIndex].type == OpdsEntryType::BOOK) {
|
|
||||||
confirmLabel = "Download";
|
|
||||||
}
|
|
||||||
const auto labels = mappedInput.mapLabels("« Back", confirmLabel, "", "");
|
|
||||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
||||||
|
|
||||||
if (entries.empty()) {
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight / 2, "No entries found");
|
|
||||||
renderer.displayBuffer();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
|
|
||||||
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 - 2, pageWidth - 1, 30);
|
|
||||||
|
|
||||||
for (size_t i = pageStartIndex; i < entries.size() && i < static_cast<size_t>(pageStartIndex + PAGE_ITEMS); i++) {
|
|
||||||
const auto& entry = entries[i];
|
|
||||||
|
|
||||||
// Format display text with type indicator
|
|
||||||
std::string displayText;
|
|
||||||
if (entry.type == OpdsEntryType::NAVIGATION) {
|
|
||||||
displayText = "> " + entry.title; // Folder/navigation indicator
|
|
||||||
} else {
|
|
||||||
// Book: "Title - Author" or just "Title"
|
|
||||||
displayText = entry.title;
|
|
||||||
if (!entry.author.empty()) {
|
|
||||||
displayText += " - " + entry.author;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto item = renderer.truncatedText(UI_10_FONT_ID, displayText.c_str(), renderer.getScreenWidth() - 40);
|
|
||||||
renderer.drawText(UI_10_FONT_ID, 20, 60 + (i % PAGE_ITEMS) * 30, item.c_str(),
|
|
||||||
i != static_cast<size_t>(selectorIndex));
|
|
||||||
}
|
|
||||||
|
|
||||||
renderer.displayBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::fetchFeed(const std::string& path) {
|
|
||||||
const char* serverUrl = SETTINGS.opdsServerUrl;
|
|
||||||
if (strlen(serverUrl) == 0) {
|
|
||||||
state = BrowserState::ERROR;
|
|
||||||
errorMessage = "No server URL configured";
|
|
||||||
updateRequired = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string url = UrlUtils::buildUrl(serverUrl, path);
|
|
||||||
Serial.printf("[%lu] [OPDS] Fetching: %s\n", millis(), url.c_str());
|
|
||||||
|
|
||||||
std::string content;
|
|
||||||
if (!HttpDownloader::fetchUrl(url, content)) {
|
|
||||||
state = BrowserState::ERROR;
|
|
||||||
errorMessage = "Failed to fetch feed";
|
|
||||||
updateRequired = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
OpdsParser parser;
|
|
||||||
if (!parser.parse(content.c_str(), content.size())) {
|
|
||||||
state = BrowserState::ERROR;
|
|
||||||
errorMessage = "Failed to parse feed";
|
|
||||||
updateRequired = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
entries = parser.getEntries();
|
|
||||||
selectorIndex = 0;
|
|
||||||
|
|
||||||
if (entries.empty()) {
|
|
||||||
state = BrowserState::ERROR;
|
|
||||||
errorMessage = "No entries found";
|
|
||||||
updateRequired = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
state = BrowserState::BROWSING;
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::navigateToEntry(const OpdsEntry& entry) {
|
|
||||||
// Push current path to history before navigating
|
|
||||||
navigationHistory.push_back(currentPath);
|
|
||||||
currentPath = entry.href;
|
|
||||||
|
|
||||||
state = BrowserState::LOADING;
|
|
||||||
statusMessage = "Loading...";
|
|
||||||
entries.clear();
|
|
||||||
selectorIndex = 0;
|
|
||||||
updateRequired = true;
|
|
||||||
|
|
||||||
fetchFeed(currentPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::navigateBack() {
|
|
||||||
if (navigationHistory.empty()) {
|
|
||||||
// At root, go home
|
|
||||||
onGoHome();
|
|
||||||
} else {
|
|
||||||
// Go back to previous catalog
|
|
||||||
currentPath = navigationHistory.back();
|
|
||||||
navigationHistory.pop_back();
|
|
||||||
|
|
||||||
state = BrowserState::LOADING;
|
|
||||||
statusMessage = "Loading...";
|
|
||||||
entries.clear();
|
|
||||||
selectorIndex = 0;
|
|
||||||
updateRequired = true;
|
|
||||||
|
|
||||||
fetchFeed(currentPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::downloadBook(const OpdsEntry& book) {
|
|
||||||
state = BrowserState::DOWNLOADING;
|
|
||||||
statusMessage = book.title;
|
|
||||||
downloadProgress = 0;
|
|
||||||
downloadTotal = 0;
|
|
||||||
updateRequired = true;
|
|
||||||
|
|
||||||
// Build full download URL
|
|
||||||
std::string downloadUrl = UrlUtils::buildUrl(SETTINGS.opdsServerUrl, book.href);
|
|
||||||
|
|
||||||
// Create sanitized filename: "Title - Author.epub" or just "Title.epub" if no author
|
|
||||||
std::string baseName = book.title;
|
|
||||||
if (!book.author.empty()) {
|
|
||||||
baseName += " - " + book.author;
|
|
||||||
}
|
|
||||||
std::string filename = "/" + StringUtils::sanitizeFilename(baseName) + ".epub";
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [OPDS] Downloading: %s -> %s\n", millis(), downloadUrl.c_str(), filename.c_str());
|
|
||||||
|
|
||||||
const auto result =
|
|
||||||
HttpDownloader::downloadToFile(downloadUrl, filename, [this](const size_t downloaded, const size_t total) {
|
|
||||||
downloadProgress = downloaded;
|
|
||||||
downloadTotal = total;
|
|
||||||
updateRequired = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result == HttpDownloader::OK) {
|
|
||||||
Serial.printf("[%lu] [OPDS] Download complete: %s\n", millis(), filename.c_str());
|
|
||||||
state = BrowserState::BROWSING;
|
|
||||||
updateRequired = true;
|
|
||||||
} else {
|
|
||||||
state = BrowserState::ERROR;
|
|
||||||
errorMessage = "Download failed";
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::checkAndConnectWifi() {
|
|
||||||
// Already connected? Verify connection is valid by checking IP
|
|
||||||
if (WiFi.status() == WL_CONNECTED && WiFi.localIP() != IPAddress(0, 0, 0, 0)) {
|
|
||||||
state = BrowserState::LOADING;
|
|
||||||
statusMessage = "Loading...";
|
|
||||||
updateRequired = true;
|
|
||||||
fetchFeed(currentPath);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Not connected - launch WiFi selection screen directly
|
|
||||||
launchWifiSelection();
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::launchWifiSelection() {
|
|
||||||
state = BrowserState::WIFI_SELECTION;
|
|
||||||
updateRequired = true;
|
|
||||||
|
|
||||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
|
|
||||||
[this](const bool connected) { onWifiSelectionComplete(connected); }));
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpdsBookBrowserActivity::onWifiSelectionComplete(const bool connected) {
|
|
||||||
exitActivity();
|
|
||||||
|
|
||||||
if (connected) {
|
|
||||||
Serial.printf("[%lu] [OPDS] WiFi connected via selection, fetching feed\n", millis());
|
|
||||||
state = BrowserState::LOADING;
|
|
||||||
statusMessage = "Loading...";
|
|
||||||
updateRequired = true;
|
|
||||||
fetchFeed(currentPath);
|
|
||||||
} else {
|
|
||||||
Serial.printf("[%lu] [OPDS] WiFi selection cancelled/failed\n", millis());
|
|
||||||
// Force disconnect to ensure clean state for next retry
|
|
||||||
// This prevents stale connection status from interfering
|
|
||||||
WiFi.disconnect();
|
|
||||||
WiFi.mode(WIFI_OFF);
|
|
||||||
state = BrowserState::ERROR;
|
|
||||||
errorMessage = "WiFi connection failed";
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,65 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <OpdsParser.h>
|
|
||||||
#include <freertos/FreeRTOS.h>
|
|
||||||
#include <freertos/semphr.h>
|
|
||||||
#include <freertos/task.h>
|
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "../ActivityWithSubactivity.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Activity for browsing and downloading books from an OPDS server.
|
|
||||||
* Supports navigation through catalog hierarchy and downloading EPUBs.
|
|
||||||
* When WiFi connection fails, launches WiFi selection to let user connect.
|
|
||||||
*/
|
|
||||||
class OpdsBookBrowserActivity final : public ActivityWithSubactivity {
|
|
||||||
public:
|
|
||||||
enum class BrowserState {
|
|
||||||
CHECK_WIFI, // Checking WiFi connection
|
|
||||||
WIFI_SELECTION, // WiFi selection subactivity is active
|
|
||||||
LOADING, // Fetching OPDS feed
|
|
||||||
BROWSING, // Displaying entries (navigation or books)
|
|
||||||
DOWNLOADING, // Downloading selected EPUB
|
|
||||||
ERROR // Error state with message
|
|
||||||
};
|
|
||||||
|
|
||||||
explicit OpdsBookBrowserActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
|
||||||
const std::function<void()>& onGoHome)
|
|
||||||
: ActivityWithSubactivity("OpdsBookBrowser", renderer, mappedInput), onGoHome(onGoHome) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
|
||||||
void onExit() override;
|
|
||||||
void loop() override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
TaskHandle_t displayTaskHandle = nullptr;
|
|
||||||
SemaphoreHandle_t renderingMutex = nullptr;
|
|
||||||
bool updateRequired = false;
|
|
||||||
|
|
||||||
BrowserState state = BrowserState::LOADING;
|
|
||||||
std::vector<OpdsEntry> entries;
|
|
||||||
std::vector<std::string> navigationHistory; // Stack of previous feed paths for back navigation
|
|
||||||
std::string currentPath; // Current feed path being displayed
|
|
||||||
int selectorIndex = 0;
|
|
||||||
std::string errorMessage;
|
|
||||||
std::string statusMessage;
|
|
||||||
size_t downloadProgress = 0;
|
|
||||||
size_t downloadTotal = 0;
|
|
||||||
|
|
||||||
const std::function<void()> onGoHome;
|
|
||||||
|
|
||||||
static void taskTrampoline(void* param);
|
|
||||||
[[noreturn]] void displayTaskLoop();
|
|
||||||
void render() const;
|
|
||||||
|
|
||||||
void checkAndConnectWifi();
|
|
||||||
void launchWifiSelection();
|
|
||||||
void onWifiSelectionComplete(bool connected);
|
|
||||||
void fetchFeed(const std::string& path);
|
|
||||||
void navigateToEntry(const OpdsEntry& entry);
|
|
||||||
void navigateBack();
|
|
||||||
void downloadBook(const OpdsEntry& book);
|
|
||||||
};
|
|
||||||
@ -1,33 +1,20 @@
|
|||||||
#include "HomeActivity.h"
|
#include "HomeActivity.h"
|
||||||
|
|
||||||
#include <Bitmap.h>
|
|
||||||
#include <Epub.h>
|
#include <Epub.h>
|
||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
#include <SDCardManager.h>
|
#include <SDCardManager.h>
|
||||||
#include <Xtc.h>
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "Battery.h"
|
|
||||||
#include "CrossPointSettings.h"
|
|
||||||
#include "CrossPointState.h"
|
#include "CrossPointState.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "ScreenComponents.h"
|
#include "ScreenComponents.h"
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
#include "util/StringUtils.h"
|
|
||||||
|
|
||||||
void HomeActivity::taskTrampoline(void* param) {
|
void HomeActivity::taskTrampoline(void* param) {
|
||||||
auto* self = static_cast<HomeActivity*>(param);
|
auto* self = static_cast<HomeActivity*>(param);
|
||||||
self->displayTaskLoop();
|
self->displayTaskLoop();
|
||||||
}
|
}
|
||||||
|
|
||||||
int HomeActivity::getMenuItemCount() const {
|
int HomeActivity::getMenuItemCount() const { return hasContinueReading ? 4 : 3; }
|
||||||
int count = 3; // Browse files, File transfer, Settings
|
|
||||||
if (hasContinueReading) count++;
|
|
||||||
if (hasOpdsUrl) count++;
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
void HomeActivity::onEnter() {
|
void HomeActivity::onEnter() {
|
||||||
Activity::onEnter();
|
Activity::onEnter();
|
||||||
@ -37,9 +24,6 @@ void HomeActivity::onEnter() {
|
|||||||
// Check if we have a book to continue reading
|
// Check if we have a book to continue reading
|
||||||
hasContinueReading = !APP_STATE.openEpubPath.empty() && SdMan.exists(APP_STATE.openEpubPath.c_str());
|
hasContinueReading = !APP_STATE.openEpubPath.empty() && SdMan.exists(APP_STATE.openEpubPath.c_str());
|
||||||
|
|
||||||
// Check if OPDS browser URL is configured
|
|
||||||
hasOpdsUrl = strlen(SETTINGS.opdsServerUrl) > 0;
|
|
||||||
|
|
||||||
if (hasContinueReading) {
|
if (hasContinueReading) {
|
||||||
// Extract filename from path for display
|
// Extract filename from path for display
|
||||||
lastBookTitle = APP_STATE.openEpubPath;
|
lastBookTitle = APP_STATE.openEpubPath;
|
||||||
@ -48,8 +32,10 @@ void HomeActivity::onEnter() {
|
|||||||
lastBookTitle = lastBookTitle.substr(lastSlash + 1);
|
lastBookTitle = lastBookTitle.substr(lastSlash + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If epub, try to load the metadata for title/author and cover
|
const std::string ext4 = lastBookTitle.length() >= 4 ? lastBookTitle.substr(lastBookTitle.length() - 4) : "";
|
||||||
if (StringUtils::checkFileExtension(lastBookTitle, ".epub")) {
|
const std::string ext5 = lastBookTitle.length() >= 5 ? lastBookTitle.substr(lastBookTitle.length() - 5) : "";
|
||||||
|
// If epub, try to load the metadata for title/author
|
||||||
|
if (ext5 == ".epub") {
|
||||||
Epub epub(APP_STATE.openEpubPath, "/.crosspoint");
|
Epub epub(APP_STATE.openEpubPath, "/.crosspoint");
|
||||||
epub.load(false);
|
epub.load(false);
|
||||||
if (!epub.getTitle().empty()) {
|
if (!epub.getTitle().empty()) {
|
||||||
@ -58,33 +44,12 @@ void HomeActivity::onEnter() {
|
|||||||
if (!epub.getAuthor().empty()) {
|
if (!epub.getAuthor().empty()) {
|
||||||
lastBookAuthor = std::string(epub.getAuthor());
|
lastBookAuthor = std::string(epub.getAuthor());
|
||||||
}
|
}
|
||||||
// Try to generate thumbnail image for Continue Reading card
|
} else if (ext5 == ".xtch") {
|
||||||
if (epub.generateThumbBmp()) {
|
|
||||||
coverBmpPath = epub.getThumbBmpPath();
|
|
||||||
hasCoverImage = true;
|
|
||||||
}
|
|
||||||
} else if (StringUtils::checkFileExtension(lastBookTitle, ".xtch") ||
|
|
||||||
StringUtils::checkFileExtension(lastBookTitle, ".xtc")) {
|
|
||||||
// Handle XTC file
|
|
||||||
Xtc xtc(APP_STATE.openEpubPath, "/.crosspoint");
|
|
||||||
if (xtc.load()) {
|
|
||||||
if (!xtc.getTitle().empty()) {
|
|
||||||
lastBookTitle = std::string(xtc.getTitle());
|
|
||||||
}
|
|
||||||
// Try to generate thumbnail image for Continue Reading card
|
|
||||||
if (xtc.generateThumbBmp()) {
|
|
||||||
coverBmpPath = xtc.getThumbBmpPath();
|
|
||||||
hasCoverImage = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Remove extension from title if we don't have metadata
|
|
||||||
if (StringUtils::checkFileExtension(lastBookTitle, ".xtch")) {
|
|
||||||
lastBookTitle.resize(lastBookTitle.length() - 5);
|
lastBookTitle.resize(lastBookTitle.length() - 5);
|
||||||
} else if (StringUtils::checkFileExtension(lastBookTitle, ".xtc")) {
|
} else if (ext4 == ".xtc") {
|
||||||
lastBookTitle.resize(lastBookTitle.length() - 4);
|
lastBookTitle.resize(lastBookTitle.length() - 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
selectorIndex = 0;
|
selectorIndex = 0;
|
||||||
|
|
||||||
@ -92,7 +57,7 @@ void HomeActivity::onEnter() {
|
|||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
|
|
||||||
xTaskCreate(&HomeActivity::taskTrampoline, "HomeActivityTask",
|
xTaskCreate(&HomeActivity::taskTrampoline, "HomeActivityTask",
|
||||||
4096, // Stack size (increased for cover image rendering)
|
2048, // Stack size
|
||||||
this, // Parameters
|
this, // Parameters
|
||||||
1, // Priority
|
1, // Priority
|
||||||
&displayTaskHandle // Task handle
|
&displayTaskHandle // Task handle
|
||||||
@ -110,51 +75,6 @@ void HomeActivity::onExit() {
|
|||||||
}
|
}
|
||||||
vSemaphoreDelete(renderingMutex);
|
vSemaphoreDelete(renderingMutex);
|
||||||
renderingMutex = nullptr;
|
renderingMutex = nullptr;
|
||||||
|
|
||||||
// Free the stored cover buffer if any
|
|
||||||
freeCoverBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HomeActivity::storeCoverBuffer() {
|
|
||||||
uint8_t* frameBuffer = renderer.getFrameBuffer();
|
|
||||||
if (!frameBuffer) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Free any existing buffer first
|
|
||||||
freeCoverBuffer();
|
|
||||||
|
|
||||||
const size_t bufferSize = GfxRenderer::getBufferSize();
|
|
||||||
coverBuffer = static_cast<uint8_t*>(malloc(bufferSize));
|
|
||||||
if (!coverBuffer) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(coverBuffer, frameBuffer, bufferSize);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool HomeActivity::restoreCoverBuffer() {
|
|
||||||
if (!coverBuffer) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t* frameBuffer = renderer.getFrameBuffer();
|
|
||||||
if (!frameBuffer) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t bufferSize = GfxRenderer::getBufferSize();
|
|
||||||
memcpy(frameBuffer, coverBuffer, bufferSize);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void HomeActivity::freeCoverBuffer() {
|
|
||||||
if (coverBuffer) {
|
|
||||||
free(coverBuffer);
|
|
||||||
coverBuffer = nullptr;
|
|
||||||
}
|
|
||||||
coverBufferStored = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HomeActivity::loop() {
|
void HomeActivity::loop() {
|
||||||
@ -166,25 +86,27 @@ void HomeActivity::loop() {
|
|||||||
const int menuCount = getMenuItemCount();
|
const int menuCount = getMenuItemCount();
|
||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
// Calculate dynamic indices based on which options are available
|
if (hasContinueReading) {
|
||||||
int idx = 0;
|
// Menu: Continue Reading, Browse, File transfer, Settings
|
||||||
const int continueIdx = hasContinueReading ? idx++ : -1;
|
if (selectorIndex == 0) {
|
||||||
const int browseFilesIdx = idx++;
|
|
||||||
const int opdsLibraryIdx = hasOpdsUrl ? idx++ : -1;
|
|
||||||
const int fileTransferIdx = idx++;
|
|
||||||
const int settingsIdx = idx;
|
|
||||||
|
|
||||||
if (selectorIndex == continueIdx) {
|
|
||||||
onContinueReading();
|
onContinueReading();
|
||||||
} else if (selectorIndex == browseFilesIdx) {
|
} else if (selectorIndex == 1) {
|
||||||
onReaderOpen();
|
onReaderOpen();
|
||||||
} else if (selectorIndex == opdsLibraryIdx) {
|
} else if (selectorIndex == 2) {
|
||||||
onOpdsBrowserOpen();
|
|
||||||
} else if (selectorIndex == fileTransferIdx) {
|
|
||||||
onFileTransferOpen();
|
onFileTransferOpen();
|
||||||
} else if (selectorIndex == settingsIdx) {
|
} else if (selectorIndex == 3) {
|
||||||
onSettingsOpen();
|
onSettingsOpen();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Menu: Browse, File transfer, Settings
|
||||||
|
if (selectorIndex == 0) {
|
||||||
|
onReaderOpen();
|
||||||
|
} else if (selectorIndex == 1) {
|
||||||
|
onFileTransferOpen();
|
||||||
|
} else if (selectorIndex == 2) {
|
||||||
|
onSettingsOpen();
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (prevPressed) {
|
} else if (prevPressed) {
|
||||||
selectorIndex = (selectorIndex + menuCount - 1) % menuCount;
|
selectorIndex = (selectorIndex + menuCount - 1) % menuCount;
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
@ -206,12 +128,8 @@ void HomeActivity::displayTaskLoop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HomeActivity::render() {
|
void HomeActivity::render() const {
|
||||||
// If we have a stored cover buffer, restore it instead of clearing
|
|
||||||
const bool bufferRestored = coverBufferStored && restoreCoverBuffer();
|
|
||||||
if (!bufferRestored) {
|
|
||||||
renderer.clearScreen();
|
renderer.clearScreen();
|
||||||
}
|
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
const auto pageWidth = renderer.getScreenWidth();
|
||||||
const auto pageHeight = renderer.getScreenHeight();
|
const auto pageHeight = renderer.getScreenHeight();
|
||||||
@ -226,101 +144,34 @@ void HomeActivity::render() {
|
|||||||
constexpr int bookY = 30;
|
constexpr int bookY = 30;
|
||||||
const bool bookSelected = hasContinueReading && selectorIndex == 0;
|
const bool bookSelected = hasContinueReading && selectorIndex == 0;
|
||||||
|
|
||||||
// Bookmark dimensions (used in multiple places)
|
|
||||||
const int bookmarkWidth = bookWidth / 8;
|
|
||||||
const int bookmarkHeight = bookHeight / 5;
|
|
||||||
const int bookmarkX = bookX + bookWidth - bookmarkWidth - 10;
|
|
||||||
const int bookmarkY = bookY + 5;
|
|
||||||
|
|
||||||
// Draw book card regardless, fill with message based on `hasContinueReading`
|
// Draw book card regardless, fill with message based on `hasContinueReading`
|
||||||
{
|
{
|
||||||
// Draw cover image as background if available (inside the box)
|
|
||||||
// Only load from SD on first render, then use stored buffer
|
|
||||||
if (hasContinueReading && hasCoverImage && !coverBmpPath.empty() && !coverRendered) {
|
|
||||||
// First time: load cover from SD and render
|
|
||||||
FsFile file;
|
|
||||||
if (SdMan.openFileForRead("HOME", coverBmpPath, file)) {
|
|
||||||
Bitmap bitmap(file);
|
|
||||||
if (bitmap.parseHeaders() == BmpReaderError::Ok) {
|
|
||||||
// Calculate position to center image within the book card
|
|
||||||
int coverX, coverY;
|
|
||||||
|
|
||||||
if (bitmap.getWidth() > bookWidth || bitmap.getHeight() > bookHeight) {
|
|
||||||
const float imgRatio = static_cast<float>(bitmap.getWidth()) / static_cast<float>(bitmap.getHeight());
|
|
||||||
const float boxRatio = static_cast<float>(bookWidth) / static_cast<float>(bookHeight);
|
|
||||||
|
|
||||||
if (imgRatio > boxRatio) {
|
|
||||||
coverX = bookX;
|
|
||||||
coverY = bookY + (bookHeight - static_cast<int>(bookWidth / imgRatio)) / 2;
|
|
||||||
} else {
|
|
||||||
coverX = bookX + (bookWidth - static_cast<int>(bookHeight * imgRatio)) / 2;
|
|
||||||
coverY = bookY;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
coverX = bookX + (bookWidth - bitmap.getWidth()) / 2;
|
|
||||||
coverY = bookY + (bookHeight - bitmap.getHeight()) / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw the cover image centered within the book card
|
|
||||||
renderer.drawBitmap(bitmap, coverX, coverY, bookWidth, bookHeight);
|
|
||||||
|
|
||||||
// Draw border around the card
|
|
||||||
renderer.drawRect(bookX, bookY, bookWidth, bookHeight);
|
|
||||||
|
|
||||||
// No bookmark ribbon when cover is shown - it would just cover the art
|
|
||||||
|
|
||||||
// Store the buffer with cover image for fast navigation
|
|
||||||
coverBufferStored = storeCoverBuffer();
|
|
||||||
coverRendered = true;
|
|
||||||
|
|
||||||
// First render: if selected, draw selection indicators now
|
|
||||||
if (bookSelected) {
|
|
||||||
renderer.drawRect(bookX + 1, bookY + 1, bookWidth - 2, bookHeight - 2);
|
|
||||||
renderer.drawRect(bookX + 2, bookY + 2, bookWidth - 4, bookHeight - 4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file.close();
|
|
||||||
}
|
|
||||||
} else if (!bufferRestored && !coverRendered) {
|
|
||||||
// No cover image: draw border or fill, plus bookmark as visual flair
|
|
||||||
if (bookSelected) {
|
if (bookSelected) {
|
||||||
renderer.fillRect(bookX, bookY, bookWidth, bookHeight);
|
renderer.fillRect(bookX, bookY, bookWidth, bookHeight);
|
||||||
} else {
|
} else {
|
||||||
renderer.drawRect(bookX, bookY, bookWidth, bookHeight);
|
renderer.drawRect(bookX, bookY, bookWidth, bookHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw bookmark ribbon when no cover image (visual decoration)
|
// Bookmark icon in the top-right corner of the card
|
||||||
if (hasContinueReading) {
|
const int bookmarkWidth = bookWidth / 8;
|
||||||
const int notchDepth = bookmarkHeight / 3;
|
const int bookmarkHeight = bookHeight / 5;
|
||||||
const int centerX = bookmarkX + bookmarkWidth / 2;
|
const int bookmarkX = bookX + bookWidth - bookmarkWidth - 8;
|
||||||
|
constexpr int bookmarkY = bookY + 1;
|
||||||
|
|
||||||
const int xPoints[5] = {
|
// Main bookmark body (solid)
|
||||||
bookmarkX, // top-left
|
renderer.fillRect(bookmarkX, bookmarkY, bookmarkWidth, bookmarkHeight, !bookSelected);
|
||||||
bookmarkX + bookmarkWidth, // top-right
|
|
||||||
bookmarkX + bookmarkWidth, // bottom-right
|
|
||||||
centerX, // center notch point
|
|
||||||
bookmarkX // bottom-left
|
|
||||||
};
|
|
||||||
const int yPoints[5] = {
|
|
||||||
bookmarkY, // top-left
|
|
||||||
bookmarkY, // top-right
|
|
||||||
bookmarkY + bookmarkHeight, // bottom-right
|
|
||||||
bookmarkY + bookmarkHeight - notchDepth, // center notch point
|
|
||||||
bookmarkY + bookmarkHeight // bottom-left
|
|
||||||
};
|
|
||||||
|
|
||||||
// Draw bookmark ribbon (inverted if selected)
|
// Carve out an inverted triangle notch at the bottom center to create angled points
|
||||||
renderer.fillPolygon(xPoints, yPoints, 5, !bookSelected);
|
const int notchHeight = bookmarkHeight / 2; // depth of the notch
|
||||||
|
for (int i = 0; i < notchHeight; ++i) {
|
||||||
|
const int y = bookmarkY + bookmarkHeight - 1 - i;
|
||||||
|
const int xStart = bookmarkX + i;
|
||||||
|
const int width = bookmarkWidth - 2 * i;
|
||||||
|
if (width <= 0) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
// Draw a horizontal strip in the opposite color to "cut" the notch
|
||||||
|
renderer.fillRect(xStart, y, width, 1, bookSelected);
|
||||||
// If buffer was restored, draw selection indicators if needed
|
|
||||||
if (bufferRestored && bookSelected && coverRendered) {
|
|
||||||
// Draw selection border (no bookmark inversion needed since cover has no bookmark)
|
|
||||||
renderer.drawRect(bookX + 1, bookY + 1, bookWidth - 2, bookHeight - 2);
|
|
||||||
renderer.drawRect(bookX + 2, bookY + 2, bookWidth - 4, bookHeight - 4);
|
|
||||||
} else if (!coverRendered && !bufferRestored) {
|
|
||||||
// Selection border already handled above in the no-cover case
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -357,25 +208,18 @@ void HomeActivity::render() {
|
|||||||
lines.back().append("...");
|
lines.back().append("...");
|
||||||
|
|
||||||
while (!lines.back().empty() && renderer.getTextWidth(UI_12_FONT_ID, lines.back().c_str()) > maxLineWidth) {
|
while (!lines.back().empty() && renderer.getTextWidth(UI_12_FONT_ID, lines.back().c_str()) > maxLineWidth) {
|
||||||
// Remove "..." first, then remove one UTF-8 char, then add "..." back
|
lines.back().resize(lines.back().size() - 5);
|
||||||
lines.back().resize(lines.back().size() - 3); // Remove "..."
|
|
||||||
StringUtils::utf8RemoveLastChar(lines.back());
|
|
||||||
lines.back().append("...");
|
lines.back().append("...");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wordWidth = renderer.getTextWidth(UI_12_FONT_ID, i.c_str());
|
int wordWidth = renderer.getTextWidth(UI_12_FONT_ID, i.c_str());
|
||||||
while (wordWidth > maxLineWidth && !i.empty()) {
|
while (wordWidth > maxLineWidth && i.size() > 5) {
|
||||||
// Word itself is too long, trim it (UTF-8 safe)
|
// Word itself is too long, trim it
|
||||||
StringUtils::utf8RemoveLastChar(i);
|
i.resize(i.size() - 5);
|
||||||
// Check if we have room for ellipsis
|
i.append("...");
|
||||||
std::string withEllipsis = i + "...";
|
wordWidth = renderer.getTextWidth(UI_12_FONT_ID, i.c_str());
|
||||||
wordWidth = renderer.getTextWidth(UI_12_FONT_ID, withEllipsis.c_str());
|
|
||||||
if (wordWidth <= maxLineWidth) {
|
|
||||||
i = withEllipsis;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int newLineWidth = renderer.getTextWidth(UI_12_FONT_ID, currentLine.c_str());
|
int newLineWidth = renderer.getTextWidth(UI_12_FONT_ID, currentLine.c_str());
|
||||||
@ -407,85 +251,24 @@ void HomeActivity::render() {
|
|||||||
// Vertically center the title block within the card
|
// Vertically center the title block within the card
|
||||||
int titleYStart = bookY + (bookHeight - totalTextHeight) / 2;
|
int titleYStart = bookY + (bookHeight - totalTextHeight) / 2;
|
||||||
|
|
||||||
// If cover image was rendered, draw white box behind title and author
|
|
||||||
if (coverRendered) {
|
|
||||||
constexpr int boxPadding = 8;
|
|
||||||
// Calculate the max text width for the box
|
|
||||||
int maxTextWidth = 0;
|
|
||||||
for (const auto& line : lines) {
|
for (const auto& line : lines) {
|
||||||
const int lineWidth = renderer.getTextWidth(UI_12_FONT_ID, line.c_str());
|
renderer.drawCenteredText(UI_12_FONT_ID, titleYStart, line.c_str(), !bookSelected);
|
||||||
if (lineWidth > maxTextWidth) {
|
|
||||||
maxTextWidth = lineWidth;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!lastBookAuthor.empty()) {
|
|
||||||
std::string trimmedAuthor = lastBookAuthor;
|
|
||||||
while (renderer.getTextWidth(UI_10_FONT_ID, trimmedAuthor.c_str()) > maxLineWidth && !trimmedAuthor.empty()) {
|
|
||||||
StringUtils::utf8RemoveLastChar(trimmedAuthor);
|
|
||||||
}
|
|
||||||
if (renderer.getTextWidth(UI_10_FONT_ID, trimmedAuthor.c_str()) <
|
|
||||||
renderer.getTextWidth(UI_10_FONT_ID, lastBookAuthor.c_str())) {
|
|
||||||
trimmedAuthor.append("...");
|
|
||||||
}
|
|
||||||
const int authorWidth = renderer.getTextWidth(UI_10_FONT_ID, trimmedAuthor.c_str());
|
|
||||||
if (authorWidth > maxTextWidth) {
|
|
||||||
maxTextWidth = authorWidth;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const int boxWidth = maxTextWidth + boxPadding * 2;
|
|
||||||
const int boxHeight = totalTextHeight + boxPadding * 2;
|
|
||||||
const int boxX = (pageWidth - boxWidth) / 2;
|
|
||||||
const int boxY = titleYStart - boxPadding;
|
|
||||||
|
|
||||||
// Draw white filled box
|
|
||||||
renderer.fillRect(boxX, boxY, boxWidth, boxHeight, false);
|
|
||||||
// Draw black border around the box
|
|
||||||
renderer.drawRect(boxX, boxY, boxWidth, boxHeight, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto& line : lines) {
|
|
||||||
renderer.drawCenteredText(UI_12_FONT_ID, titleYStart, line.c_str(), !bookSelected || coverRendered);
|
|
||||||
titleYStart += renderer.getLineHeight(UI_12_FONT_ID);
|
titleYStart += renderer.getLineHeight(UI_12_FONT_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lastBookAuthor.empty()) {
|
if (!lastBookAuthor.empty()) {
|
||||||
titleYStart += renderer.getLineHeight(UI_10_FONT_ID) / 2;
|
titleYStart += renderer.getLineHeight(UI_10_FONT_ID) / 2;
|
||||||
std::string trimmedAuthor = lastBookAuthor;
|
std::string trimmedAuthor = lastBookAuthor;
|
||||||
// Trim author if too long (UTF-8 safe)
|
// Trim author if too long
|
||||||
bool wasTrimmed = false;
|
|
||||||
while (renderer.getTextWidth(UI_10_FONT_ID, trimmedAuthor.c_str()) > maxLineWidth && !trimmedAuthor.empty()) {
|
while (renderer.getTextWidth(UI_10_FONT_ID, trimmedAuthor.c_str()) > maxLineWidth && !trimmedAuthor.empty()) {
|
||||||
StringUtils::utf8RemoveLastChar(trimmedAuthor);
|
trimmedAuthor.resize(trimmedAuthor.size() - 5);
|
||||||
wasTrimmed = true;
|
|
||||||
}
|
|
||||||
if (wasTrimmed && !trimmedAuthor.empty()) {
|
|
||||||
// Make room for ellipsis
|
|
||||||
while (renderer.getTextWidth(UI_10_FONT_ID, (trimmedAuthor + "...").c_str()) > maxLineWidth &&
|
|
||||||
!trimmedAuthor.empty()) {
|
|
||||||
StringUtils::utf8RemoveLastChar(trimmedAuthor);
|
|
||||||
}
|
|
||||||
trimmedAuthor.append("...");
|
trimmedAuthor.append("...");
|
||||||
}
|
}
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, titleYStart, trimmedAuthor.c_str(), !bookSelected || coverRendered);
|
renderer.drawCenteredText(UI_10_FONT_ID, titleYStart, trimmedAuthor.c_str(), !bookSelected);
|
||||||
}
|
}
|
||||||
|
|
||||||
// "Continue Reading" label at the bottom
|
renderer.drawCenteredText(UI_10_FONT_ID, bookY + bookHeight - renderer.getLineHeight(UI_10_FONT_ID) * 3 / 2,
|
||||||
const int continueY = bookY + bookHeight - renderer.getLineHeight(UI_10_FONT_ID) * 3 / 2;
|
"Continue Reading", !bookSelected);
|
||||||
if (coverRendered) {
|
|
||||||
// Draw white box behind "Continue Reading" text
|
|
||||||
const char* continueText = "Continue Reading";
|
|
||||||
const int continueTextWidth = renderer.getTextWidth(UI_10_FONT_ID, continueText);
|
|
||||||
constexpr int continuePadding = 6;
|
|
||||||
const int continueBoxWidth = continueTextWidth + continuePadding * 2;
|
|
||||||
const int continueBoxHeight = renderer.getLineHeight(UI_10_FONT_ID) + continuePadding;
|
|
||||||
const int continueBoxX = (pageWidth - continueBoxWidth) / 2;
|
|
||||||
const int continueBoxY = continueY - continuePadding / 2;
|
|
||||||
renderer.fillRect(continueBoxX, continueBoxY, continueBoxWidth, continueBoxHeight, false);
|
|
||||||
renderer.drawRect(continueBoxX, continueBoxY, continueBoxWidth, continueBoxHeight, true);
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, continueY, continueText, true);
|
|
||||||
} else {
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, continueY, "Continue Reading", !bookSelected);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// No book to continue reading
|
// No book to continue reading
|
||||||
const int y =
|
const int y =
|
||||||
@ -494,31 +277,24 @@ void HomeActivity::render() {
|
|||||||
renderer.drawCenteredText(UI_10_FONT_ID, y + renderer.getLineHeight(UI_12_FONT_ID), "Start reading below");
|
renderer.drawCenteredText(UI_10_FONT_ID, y + renderer.getLineHeight(UI_12_FONT_ID), "Start reading below");
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Bottom menu tiles ---
|
// --- Bottom menu tiles (indices 1-3) ---
|
||||||
// Build menu items dynamically
|
|
||||||
std::vector<const char*> menuItems = {"Browse Files", "File Transfer", "Settings"};
|
|
||||||
if (hasOpdsUrl) {
|
|
||||||
// Insert Calibre Library after Browse Files
|
|
||||||
menuItems.insert(menuItems.begin() + 1, "Calibre Library");
|
|
||||||
}
|
|
||||||
|
|
||||||
const int menuTileWidth = pageWidth - 2 * margin;
|
const int menuTileWidth = pageWidth - 2 * margin;
|
||||||
constexpr int menuTileHeight = 45;
|
constexpr int menuTileHeight = 50;
|
||||||
constexpr int menuSpacing = 8;
|
constexpr int menuSpacing = 10;
|
||||||
const int totalMenuHeight =
|
constexpr int totalMenuHeight = 3 * menuTileHeight + 2 * menuSpacing;
|
||||||
static_cast<int>(menuItems.size()) * menuTileHeight + (static_cast<int>(menuItems.size()) - 1) * menuSpacing;
|
|
||||||
|
|
||||||
int menuStartY = bookY + bookHeight + 15;
|
int menuStartY = bookY + bookHeight + 20;
|
||||||
// Ensure we don't collide with the bottom button legend
|
// Ensure we don't collide with the bottom button legend
|
||||||
const int maxMenuStartY = pageHeight - bottomMargin - totalMenuHeight - margin;
|
const int maxMenuStartY = pageHeight - bottomMargin - totalMenuHeight - margin;
|
||||||
if (menuStartY > maxMenuStartY) {
|
if (menuStartY > maxMenuStartY) {
|
||||||
menuStartY = maxMenuStartY;
|
menuStartY = maxMenuStartY;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < menuItems.size(); ++i) {
|
for (int i = 0; i < 3; ++i) {
|
||||||
const int overallIndex = static_cast<int>(i) + (hasContinueReading ? 1 : 0);
|
constexpr const char* items[3] = {"Browse files", "File transfer", "Settings"};
|
||||||
|
const int overallIndex = i + (getMenuItemCount() - 3);
|
||||||
constexpr int tileX = margin;
|
constexpr int tileX = margin;
|
||||||
const int tileY = menuStartY + static_cast<int>(i) * (menuTileHeight + menuSpacing);
|
const int tileY = menuStartY + i * (menuTileHeight + menuSpacing);
|
||||||
const bool selected = selectorIndex == overallIndex;
|
const bool selected = selectorIndex == overallIndex;
|
||||||
|
|
||||||
if (selected) {
|
if (selected) {
|
||||||
@ -527,7 +303,7 @@ void HomeActivity::render() {
|
|||||||
renderer.drawRect(tileX, tileY, menuTileWidth, menuTileHeight);
|
renderer.drawRect(tileX, tileY, menuTileWidth, menuTileHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* label = menuItems[i];
|
const char* label = items[i];
|
||||||
const int textWidth = renderer.getTextWidth(UI_10_FONT_ID, label);
|
const int textWidth = renderer.getTextWidth(UI_10_FONT_ID, label);
|
||||||
const int textX = tileX + (menuTileWidth - textWidth) / 2;
|
const int textX = tileX + (menuTileWidth - textWidth) / 2;
|
||||||
const int lineHeight = renderer.getLineHeight(UI_10_FONT_ID);
|
const int lineHeight = renderer.getLineHeight(UI_10_FONT_ID);
|
||||||
@ -540,13 +316,7 @@ void HomeActivity::render() {
|
|||||||
const auto labels = mappedInput.mapLabels("", "Confirm", "Up", "Down");
|
const auto labels = mappedInput.mapLabels("", "Confirm", "Up", "Down");
|
||||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
||||||
|
|
||||||
const bool showBatteryPercentage =
|
ScreenComponents::drawBattery(renderer, 20, pageHeight - 70);
|
||||||
SETTINGS.hideBatteryPercentage != CrossPointSettings::HIDE_BATTERY_PERCENTAGE::HIDE_ALWAYS;
|
|
||||||
// get percentage so we can align text properly
|
|
||||||
const uint16_t percentage = battery.readPercentage();
|
|
||||||
const auto percentageText = showBatteryPercentage ? std::to_string(percentage) + "%" : "";
|
|
||||||
const auto batteryX = pageWidth - 25 - renderer.getTextWidth(SMALL_FONT_ID, percentageText.c_str());
|
|
||||||
ScreenComponents::drawBattery(renderer, batteryX, 10, showBatteryPercentage);
|
|
||||||
|
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,39 +13,27 @@ class HomeActivity final : public Activity {
|
|||||||
int selectorIndex = 0;
|
int selectorIndex = 0;
|
||||||
bool updateRequired = false;
|
bool updateRequired = false;
|
||||||
bool hasContinueReading = false;
|
bool hasContinueReading = false;
|
||||||
bool hasOpdsUrl = false;
|
|
||||||
bool hasCoverImage = false;
|
|
||||||
bool coverRendered = false; // Track if cover has been rendered once
|
|
||||||
bool coverBufferStored = false; // Track if cover buffer is stored
|
|
||||||
uint8_t* coverBuffer = nullptr; // HomeActivity's own buffer for cover image
|
|
||||||
std::string lastBookTitle;
|
std::string lastBookTitle;
|
||||||
std::string lastBookAuthor;
|
std::string lastBookAuthor;
|
||||||
std::string coverBmpPath;
|
|
||||||
const std::function<void()> onContinueReading;
|
const std::function<void()> onContinueReading;
|
||||||
const std::function<void()> onReaderOpen;
|
const std::function<void()> onReaderOpen;
|
||||||
const std::function<void()> onSettingsOpen;
|
const std::function<void()> onSettingsOpen;
|
||||||
const std::function<void()> onFileTransferOpen;
|
const std::function<void()> onFileTransferOpen;
|
||||||
const std::function<void()> onOpdsBrowserOpen;
|
|
||||||
|
|
||||||
static void taskTrampoline(void* param);
|
static void taskTrampoline(void* param);
|
||||||
[[noreturn]] void displayTaskLoop();
|
[[noreturn]] void displayTaskLoop();
|
||||||
void render();
|
void render() const;
|
||||||
int getMenuItemCount() const;
|
int getMenuItemCount() const;
|
||||||
bool storeCoverBuffer(); // Store frame buffer for cover image
|
|
||||||
bool restoreCoverBuffer(); // Restore frame buffer from stored cover
|
|
||||||
void freeCoverBuffer(); // Free the stored cover buffer
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit HomeActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit HomeActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||||
const std::function<void()>& onContinueReading, const std::function<void()>& onReaderOpen,
|
const std::function<void()>& onContinueReading, const std::function<void()>& onReaderOpen,
|
||||||
const std::function<void()>& onSettingsOpen, const std::function<void()>& onFileTransferOpen,
|
const std::function<void()>& onSettingsOpen, const std::function<void()>& onFileTransferOpen)
|
||||||
const std::function<void()>& onOpdsBrowserOpen)
|
|
||||||
: Activity("Home", renderer, mappedInput),
|
: Activity("Home", renderer, mappedInput),
|
||||||
onContinueReading(onContinueReading),
|
onContinueReading(onContinueReading),
|
||||||
onReaderOpen(onReaderOpen),
|
onReaderOpen(onReaderOpen),
|
||||||
onSettingsOpen(onSettingsOpen),
|
onSettingsOpen(onSettingsOpen),
|
||||||
onFileTransferOpen(onFileTransferOpen),
|
onFileTransferOpen(onFileTransferOpen) {}
|
||||||
onOpdsBrowserOpen(onOpdsBrowserOpen) {}
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
|
|||||||
@ -1,756 +0,0 @@
|
|||||||
#include "CalibreWirelessActivity.h"
|
|
||||||
|
|
||||||
#include <GfxRenderer.h>
|
|
||||||
#include <HardwareSerial.h>
|
|
||||||
#include <SDCardManager.h>
|
|
||||||
#include <WiFi.h>
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
#include "MappedInputManager.h"
|
|
||||||
#include "ScreenComponents.h"
|
|
||||||
#include "fontIds.h"
|
|
||||||
#include "util/StringUtils.h"
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
constexpr uint16_t UDP_PORTS[] = {54982, 48123, 39001, 44044, 59678};
|
|
||||||
constexpr uint16_t LOCAL_UDP_PORT = 8134; // Port to receive responses
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::displayTaskTrampoline(void* param) {
|
|
||||||
auto* self = static_cast<CalibreWirelessActivity*>(param);
|
|
||||||
self->displayTaskLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::networkTaskTrampoline(void* param) {
|
|
||||||
auto* self = static_cast<CalibreWirelessActivity*>(param);
|
|
||||||
self->networkTaskLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::onEnter() {
|
|
||||||
Activity::onEnter();
|
|
||||||
|
|
||||||
renderingMutex = xSemaphoreCreateMutex();
|
|
||||||
stateMutex = xSemaphoreCreateMutex();
|
|
||||||
|
|
||||||
state = WirelessState::DISCOVERING;
|
|
||||||
statusMessage = "Discovering Calibre...";
|
|
||||||
errorMessage.clear();
|
|
||||||
calibreHostname.clear();
|
|
||||||
calibreHost.clear();
|
|
||||||
calibrePort = 0;
|
|
||||||
calibreAltPort = 0;
|
|
||||||
currentFilename.clear();
|
|
||||||
currentFileSize = 0;
|
|
||||||
bytesReceived = 0;
|
|
||||||
inBinaryMode = false;
|
|
||||||
recvBuffer.clear();
|
|
||||||
|
|
||||||
updateRequired = true;
|
|
||||||
|
|
||||||
// Start UDP listener for Calibre responses
|
|
||||||
udp.begin(LOCAL_UDP_PORT);
|
|
||||||
|
|
||||||
// Create display task
|
|
||||||
xTaskCreate(&CalibreWirelessActivity::displayTaskTrampoline, "CalDisplayTask", 2048, this, 1, &displayTaskHandle);
|
|
||||||
|
|
||||||
// Create network task with larger stack for JSON parsing
|
|
||||||
xTaskCreate(&CalibreWirelessActivity::networkTaskTrampoline, "CalNetworkTask", 12288, this, 2, &networkTaskHandle);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::onExit() {
|
|
||||||
Activity::onExit();
|
|
||||||
|
|
||||||
// Turn off WiFi when exiting
|
|
||||||
WiFi.mode(WIFI_OFF);
|
|
||||||
|
|
||||||
// Stop UDP listening
|
|
||||||
udp.stop();
|
|
||||||
|
|
||||||
// Close TCP client if connected
|
|
||||||
if (tcpClient.connected()) {
|
|
||||||
tcpClient.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close any open file
|
|
||||||
if (currentFile) {
|
|
||||||
currentFile.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Acquire stateMutex before deleting network task to avoid race condition
|
|
||||||
xSemaphoreTake(stateMutex, portMAX_DELAY);
|
|
||||||
if (networkTaskHandle) {
|
|
||||||
vTaskDelete(networkTaskHandle);
|
|
||||||
networkTaskHandle = nullptr;
|
|
||||||
}
|
|
||||||
xSemaphoreGive(stateMutex);
|
|
||||||
|
|
||||||
// Acquire renderingMutex before deleting display task
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
if (displayTaskHandle) {
|
|
||||||
vTaskDelete(displayTaskHandle);
|
|
||||||
displayTaskHandle = nullptr;
|
|
||||||
}
|
|
||||||
vSemaphoreDelete(renderingMutex);
|
|
||||||
renderingMutex = nullptr;
|
|
||||||
|
|
||||||
vSemaphoreDelete(stateMutex);
|
|
||||||
stateMutex = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::loop() {
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
|
||||||
onComplete();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::displayTaskLoop() {
|
|
||||||
while (true) {
|
|
||||||
if (updateRequired) {
|
|
||||||
updateRequired = false;
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
render();
|
|
||||||
xSemaphoreGive(renderingMutex);
|
|
||||||
}
|
|
||||||
vTaskDelay(50 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::networkTaskLoop() {
|
|
||||||
while (true) {
|
|
||||||
xSemaphoreTake(stateMutex, portMAX_DELAY);
|
|
||||||
const auto currentState = state;
|
|
||||||
xSemaphoreGive(stateMutex);
|
|
||||||
|
|
||||||
switch (currentState) {
|
|
||||||
case WirelessState::DISCOVERING:
|
|
||||||
listenForDiscovery();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case WirelessState::CONNECTING:
|
|
||||||
case WirelessState::WAITING:
|
|
||||||
case WirelessState::RECEIVING:
|
|
||||||
handleTcpClient();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case WirelessState::COMPLETE:
|
|
||||||
case WirelessState::DISCONNECTED:
|
|
||||||
case WirelessState::ERROR:
|
|
||||||
// Just wait, user will exit
|
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::listenForDiscovery() {
|
|
||||||
// Broadcast "hello" on all UDP discovery ports to find Calibre
|
|
||||||
for (const uint16_t port : UDP_PORTS) {
|
|
||||||
udp.beginPacket("255.255.255.255", port);
|
|
||||||
udp.write(reinterpret_cast<const uint8_t*>("hello"), 5);
|
|
||||||
udp.endPacket();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for Calibre's response
|
|
||||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
|
||||||
|
|
||||||
// Check for response
|
|
||||||
const int packetSize = udp.parsePacket();
|
|
||||||
if (packetSize > 0) {
|
|
||||||
char buffer[256];
|
|
||||||
const int len = udp.read(buffer, sizeof(buffer) - 1);
|
|
||||||
if (len > 0) {
|
|
||||||
buffer[len] = '\0';
|
|
||||||
|
|
||||||
// Parse Calibre's response format:
|
|
||||||
// "calibre wireless device client (on hostname);port,content_server_port"
|
|
||||||
// or just the hostname and port info
|
|
||||||
std::string response(buffer);
|
|
||||||
|
|
||||||
// Try to extract host and port
|
|
||||||
// Format: "calibre wireless device client (on HOSTNAME);PORT,..."
|
|
||||||
size_t onPos = response.find("(on ");
|
|
||||||
size_t closePos = response.find(')');
|
|
||||||
size_t semiPos = response.find(';');
|
|
||||||
size_t commaPos = response.find(',', semiPos);
|
|
||||||
|
|
||||||
if (semiPos != std::string::npos) {
|
|
||||||
// Get ports after semicolon (format: "port1,port2")
|
|
||||||
std::string portStr;
|
|
||||||
if (commaPos != std::string::npos && commaPos > semiPos) {
|
|
||||||
portStr = response.substr(semiPos + 1, commaPos - semiPos - 1);
|
|
||||||
// Get alternative port after comma
|
|
||||||
std::string altPortStr = response.substr(commaPos + 1);
|
|
||||||
// Trim whitespace and non-digits from alt port
|
|
||||||
size_t altEnd = 0;
|
|
||||||
while (altEnd < altPortStr.size() && altPortStr[altEnd] >= '0' && altPortStr[altEnd] <= '9') {
|
|
||||||
altEnd++;
|
|
||||||
}
|
|
||||||
if (altEnd > 0) {
|
|
||||||
calibreAltPort = static_cast<uint16_t>(std::stoi(altPortStr.substr(0, altEnd)));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
portStr = response.substr(semiPos + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Trim whitespace from main port
|
|
||||||
while (!portStr.empty() && (portStr[0] == ' ' || portStr[0] == '\t')) {
|
|
||||||
portStr = portStr.substr(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!portStr.empty()) {
|
|
||||||
calibrePort = static_cast<uint16_t>(std::stoi(portStr));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get hostname if present, otherwise use sender IP
|
|
||||||
if (onPos != std::string::npos && closePos != std::string::npos && closePos > onPos + 4) {
|
|
||||||
calibreHostname = response.substr(onPos + 4, closePos - onPos - 4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use the sender's IP as the host to connect to
|
|
||||||
calibreHost = udp.remoteIP().toString().c_str();
|
|
||||||
if (calibreHostname.empty()) {
|
|
||||||
calibreHostname = calibreHost;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (calibrePort > 0) {
|
|
||||||
// Connect to Calibre's TCP server - try main port first, then alt port
|
|
||||||
setState(WirelessState::CONNECTING);
|
|
||||||
setStatus("Connecting to " + calibreHostname + "...");
|
|
||||||
|
|
||||||
// Small delay before connecting
|
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
||||||
|
|
||||||
bool connected = false;
|
|
||||||
|
|
||||||
// Try main port first
|
|
||||||
if (tcpClient.connect(calibreHost.c_str(), calibrePort, 5000)) {
|
|
||||||
connected = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try alternative port if main failed
|
|
||||||
if (!connected && calibreAltPort > 0) {
|
|
||||||
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
||||||
if (tcpClient.connect(calibreHost.c_str(), calibreAltPort, 5000)) {
|
|
||||||
connected = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (connected) {
|
|
||||||
setState(WirelessState::WAITING);
|
|
||||||
setStatus("Connected to " + calibreHostname + "\nWaiting for commands...");
|
|
||||||
} else {
|
|
||||||
// Don't set error yet, keep trying discovery
|
|
||||||
setState(WirelessState::DISCOVERING);
|
|
||||||
setStatus("Discovering Calibre...\n(Connection failed, retrying)");
|
|
||||||
calibrePort = 0;
|
|
||||||
calibreAltPort = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::handleTcpClient() {
|
|
||||||
if (!tcpClient.connected()) {
|
|
||||||
setState(WirelessState::DISCONNECTED);
|
|
||||||
setStatus("Calibre disconnected");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inBinaryMode) {
|
|
||||||
receiveBinaryData();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string message;
|
|
||||||
if (readJsonMessage(message)) {
|
|
||||||
// Parse opcode from JSON array format: [opcode, {...}]
|
|
||||||
// Find the opcode (first number after '[')
|
|
||||||
size_t start = message.find('[');
|
|
||||||
if (start != std::string::npos) {
|
|
||||||
start++;
|
|
||||||
size_t end = message.find(',', start);
|
|
||||||
if (end != std::string::npos) {
|
|
||||||
const int opcodeInt = std::stoi(message.substr(start, end - start));
|
|
||||||
if (opcodeInt < 0 || opcodeInt >= OpCode::ERROR) {
|
|
||||||
Serial.printf("[%lu] [CAL] Invalid opcode: %d\n", millis(), opcodeInt);
|
|
||||||
sendJsonResponse(OpCode::OK, "{}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const auto opcode = static_cast<OpCode>(opcodeInt);
|
|
||||||
|
|
||||||
// Extract data object (everything after the comma until the last ']')
|
|
||||||
size_t dataStart = end + 1;
|
|
||||||
size_t dataEnd = message.rfind(']');
|
|
||||||
std::string data = "";
|
|
||||||
if (dataEnd != std::string::npos && dataEnd > dataStart) {
|
|
||||||
data = message.substr(dataStart, dataEnd - dataStart);
|
|
||||||
}
|
|
||||||
|
|
||||||
handleCommand(opcode, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CalibreWirelessActivity::readJsonMessage(std::string& message) {
|
|
||||||
// Read available data into buffer
|
|
||||||
int available = tcpClient.available();
|
|
||||||
if (available > 0) {
|
|
||||||
// Limit buffer growth to prevent memory issues
|
|
||||||
if (recvBuffer.size() > 100000) {
|
|
||||||
recvBuffer.clear();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// Read in chunks
|
|
||||||
char buf[1024];
|
|
||||||
while (available > 0) {
|
|
||||||
int toRead = std::min(available, static_cast<int>(sizeof(buf)));
|
|
||||||
int bytesRead = tcpClient.read(reinterpret_cast<uint8_t*>(buf), toRead);
|
|
||||||
if (bytesRead > 0) {
|
|
||||||
recvBuffer.append(buf, bytesRead);
|
|
||||||
available -= bytesRead;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (recvBuffer.empty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find '[' which marks the start of JSON
|
|
||||||
size_t bracketPos = recvBuffer.find('[');
|
|
||||||
if (bracketPos == std::string::npos) {
|
|
||||||
// No '[' found - if buffer is getting large, something is wrong
|
|
||||||
if (recvBuffer.size() > 1000) {
|
|
||||||
recvBuffer.clear();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to extract length from digits before '['
|
|
||||||
// Calibre ALWAYS sends a length prefix, so if it's not valid digits, it's garbage
|
|
||||||
size_t msgLen = 0;
|
|
||||||
bool validPrefix = false;
|
|
||||||
|
|
||||||
if (bracketPos > 0 && bracketPos <= 12) {
|
|
||||||
// Check if prefix is all digits
|
|
||||||
bool allDigits = true;
|
|
||||||
for (size_t i = 0; i < bracketPos; i++) {
|
|
||||||
char c = recvBuffer[i];
|
|
||||||
if (c < '0' || c > '9') {
|
|
||||||
allDigits = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (allDigits) {
|
|
||||||
msgLen = std::stoul(recvBuffer.substr(0, bracketPos));
|
|
||||||
validPrefix = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!validPrefix) {
|
|
||||||
// Not a valid length prefix - discard everything up to '[' and treat '[' as start
|
|
||||||
if (bracketPos > 0) {
|
|
||||||
recvBuffer = recvBuffer.substr(bracketPos);
|
|
||||||
}
|
|
||||||
// Without length prefix, we can't reliably parse - wait for more data
|
|
||||||
// that hopefully starts with a proper length prefix
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sanity check the message length
|
|
||||||
if (msgLen > 1000000) {
|
|
||||||
recvBuffer = recvBuffer.substr(bracketPos + 1); // Skip past this '[' and try again
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if we have the complete message
|
|
||||||
size_t totalNeeded = bracketPos + msgLen;
|
|
||||||
if (recvBuffer.size() < totalNeeded) {
|
|
||||||
// Not enough data yet - wait for more
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract the message
|
|
||||||
message = recvBuffer.substr(bracketPos, msgLen);
|
|
||||||
|
|
||||||
// Keep the rest in buffer (may contain binary data or next message)
|
|
||||||
if (recvBuffer.size() > totalNeeded) {
|
|
||||||
recvBuffer = recvBuffer.substr(totalNeeded);
|
|
||||||
} else {
|
|
||||||
recvBuffer.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::sendJsonResponse(const OpCode opcode, const std::string& data) {
|
|
||||||
// Format: length + [opcode, {data}]
|
|
||||||
std::string json = "[" + std::to_string(opcode) + "," + data + "]";
|
|
||||||
const std::string lengthPrefix = std::to_string(json.length());
|
|
||||||
json.insert(0, lengthPrefix);
|
|
||||||
|
|
||||||
tcpClient.write(reinterpret_cast<const uint8_t*>(json.c_str()), json.length());
|
|
||||||
tcpClient.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::handleCommand(const OpCode opcode, const std::string& data) {
|
|
||||||
switch (opcode) {
|
|
||||||
case OpCode::GET_INITIALIZATION_INFO:
|
|
||||||
handleGetInitializationInfo(data);
|
|
||||||
break;
|
|
||||||
case OpCode::GET_DEVICE_INFORMATION:
|
|
||||||
handleGetDeviceInformation();
|
|
||||||
break;
|
|
||||||
case OpCode::FREE_SPACE:
|
|
||||||
handleFreeSpace();
|
|
||||||
break;
|
|
||||||
case OpCode::GET_BOOK_COUNT:
|
|
||||||
handleGetBookCount();
|
|
||||||
break;
|
|
||||||
case OpCode::SEND_BOOK:
|
|
||||||
handleSendBook(data);
|
|
||||||
break;
|
|
||||||
case OpCode::SEND_BOOK_METADATA:
|
|
||||||
handleSendBookMetadata(data);
|
|
||||||
break;
|
|
||||||
case OpCode::DISPLAY_MESSAGE:
|
|
||||||
handleDisplayMessage(data);
|
|
||||||
break;
|
|
||||||
case OpCode::NOOP:
|
|
||||||
handleNoop(data);
|
|
||||||
break;
|
|
||||||
case OpCode::SET_CALIBRE_DEVICE_INFO:
|
|
||||||
case OpCode::SET_CALIBRE_DEVICE_NAME:
|
|
||||||
// These set metadata about the connected Calibre instance.
|
|
||||||
// We don't need this info, just acknowledge receipt.
|
|
||||||
sendJsonResponse(OpCode::OK, "{}");
|
|
||||||
break;
|
|
||||||
case OpCode::SET_LIBRARY_INFO:
|
|
||||||
// Library metadata (name, UUID) - not needed for receiving books
|
|
||||||
sendJsonResponse(OpCode::OK, "{}");
|
|
||||||
break;
|
|
||||||
case OpCode::SEND_BOOKLISTS:
|
|
||||||
// Calibre asking us to send our book list. We report 0 books in
|
|
||||||
// handleGetBookCount, so this is effectively a no-op.
|
|
||||||
sendJsonResponse(OpCode::OK, "{}");
|
|
||||||
break;
|
|
||||||
case OpCode::TOTAL_SPACE:
|
|
||||||
handleFreeSpace();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
Serial.printf("[%lu] [CAL] Unknown opcode: %d\n", millis(), opcode);
|
|
||||||
sendJsonResponse(OpCode::OK, "{}");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::handleGetInitializationInfo(const std::string& data) {
|
|
||||||
setState(WirelessState::WAITING);
|
|
||||||
setStatus("Connected to " + calibreHostname +
|
|
||||||
"\nWaiting for transfer...\n\nIf transfer fails, enable\n'Ignore free space' in Calibre's\nSmartDevice "
|
|
||||||
"plugin settings.");
|
|
||||||
|
|
||||||
// Build response with device capabilities
|
|
||||||
// Format must match what Calibre expects from a smart device
|
|
||||||
std::string response = "{";
|
|
||||||
response += "\"appName\":\"CrossPoint\",";
|
|
||||||
response += "\"acceptedExtensions\":[\"epub\"],";
|
|
||||||
response += "\"cacheUsesLpaths\":true,";
|
|
||||||
response += "\"canAcceptLibraryInfo\":true,";
|
|
||||||
response += "\"canDeleteMultipleBooks\":true,";
|
|
||||||
response += "\"canReceiveBookBinary\":true,";
|
|
||||||
response += "\"canSendOkToSendbook\":true,";
|
|
||||||
response += "\"canStreamBooks\":true,";
|
|
||||||
response += "\"canStreamMetadata\":true,";
|
|
||||||
response += "\"canUseCachedMetadata\":true,";
|
|
||||||
// ccVersionNumber: Calibre Companion protocol version. 212 matches CC 5.4.20+.
|
|
||||||
// Using a known version ensures compatibility with Calibre's feature detection.
|
|
||||||
response += "\"ccVersionNumber\":212,";
|
|
||||||
// coverHeight: Max cover image height. We don't process covers, so this is informational only.
|
|
||||||
response += "\"coverHeight\":800,";
|
|
||||||
response += "\"deviceKind\":\"CrossPoint\",";
|
|
||||||
response += "\"deviceName\":\"CrossPoint\",";
|
|
||||||
response += "\"extensionPathLengths\":{\"epub\":37},";
|
|
||||||
response += "\"maxBookContentPacketLen\":4096,";
|
|
||||||
response += "\"passwordHash\":\"\",";
|
|
||||||
response += "\"useUuidFileNames\":false,";
|
|
||||||
response += "\"versionOK\":true";
|
|
||||||
response += "}";
|
|
||||||
|
|
||||||
sendJsonResponse(OpCode::OK, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::handleGetDeviceInformation() {
|
|
||||||
std::string response = "{";
|
|
||||||
response += "\"device_info\":{";
|
|
||||||
response += "\"device_store_uuid\":\"" + getDeviceUuid() + "\",";
|
|
||||||
response += "\"device_name\":\"CrossPoint Reader\",";
|
|
||||||
response += "\"device_version\":\"" CROSSPOINT_VERSION "\"";
|
|
||||||
response += "},";
|
|
||||||
response += "\"version\":1,";
|
|
||||||
response += "\"device_version\":\"" CROSSPOINT_VERSION "\"";
|
|
||||||
response += "}";
|
|
||||||
|
|
||||||
sendJsonResponse(OpCode::OK, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::handleFreeSpace() {
|
|
||||||
// TODO: Report actual SD card free space instead of hardcoded value
|
|
||||||
// Report 10GB free space for now
|
|
||||||
sendJsonResponse(OpCode::OK, "{\"free_space_on_device\":10737418240}");
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::handleGetBookCount() {
|
|
||||||
// We report 0 books - Calibre will send books without checking for duplicates
|
|
||||||
std::string response = "{\"count\":0,\"willStream\":true,\"willScan\":false}";
|
|
||||||
sendJsonResponse(OpCode::OK, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::handleSendBook(const std::string& data) {
|
|
||||||
// Manually extract lpath and length from SEND_BOOK data
|
|
||||||
// Full JSON parsing crashes on large metadata, so we just extract what we need
|
|
||||||
|
|
||||||
// Extract "lpath" field - format: "lpath": "value"
|
|
||||||
std::string lpath;
|
|
||||||
size_t lpathPos = data.find("\"lpath\"");
|
|
||||||
if (lpathPos != std::string::npos) {
|
|
||||||
size_t colonPos = data.find(':', lpathPos + 7);
|
|
||||||
if (colonPos != std::string::npos) {
|
|
||||||
size_t quoteStart = data.find('"', colonPos + 1);
|
|
||||||
if (quoteStart != std::string::npos) {
|
|
||||||
size_t quoteEnd = data.find('"', quoteStart + 1);
|
|
||||||
if (quoteEnd != std::string::npos) {
|
|
||||||
lpath = data.substr(quoteStart + 1, quoteEnd - quoteStart - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract top-level "length" field - must track depth to skip nested objects
|
|
||||||
// The metadata contains nested "length" fields (e.g., cover image length)
|
|
||||||
size_t length = 0;
|
|
||||||
int depth = 0;
|
|
||||||
for (size_t i = 0; i < data.size(); i++) {
|
|
||||||
char c = data[i];
|
|
||||||
if (c == '{' || c == '[') {
|
|
||||||
depth++;
|
|
||||||
} else if (c == '}' || c == ']') {
|
|
||||||
depth--;
|
|
||||||
} else if (depth == 1 && c == '"') {
|
|
||||||
// At top level, check if this is "length"
|
|
||||||
if (i + 9 < data.size() && data.substr(i, 8) == "\"length\"") {
|
|
||||||
// Found top-level "length" - extract the number after ':'
|
|
||||||
size_t colonPos = data.find(':', i + 8);
|
|
||||||
if (colonPos != std::string::npos) {
|
|
||||||
size_t numStart = colonPos + 1;
|
|
||||||
while (numStart < data.size() && (data[numStart] == ' ' || data[numStart] == '\t')) {
|
|
||||||
numStart++;
|
|
||||||
}
|
|
||||||
size_t numEnd = numStart;
|
|
||||||
while (numEnd < data.size() && data[numEnd] >= '0' && data[numEnd] <= '9') {
|
|
||||||
numEnd++;
|
|
||||||
}
|
|
||||||
if (numEnd > numStart) {
|
|
||||||
length = std::stoul(data.substr(numStart, numEnd - numStart));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lpath.empty() || length == 0) {
|
|
||||||
sendJsonResponse(OpCode::ERROR, "{\"message\":\"Invalid book data\"}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract filename from lpath
|
|
||||||
std::string filename = lpath;
|
|
||||||
const size_t lastSlash = filename.rfind('/');
|
|
||||||
if (lastSlash != std::string::npos) {
|
|
||||||
filename = filename.substr(lastSlash + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sanitize and create full path
|
|
||||||
currentFilename = "/" + StringUtils::sanitizeFilename(filename);
|
|
||||||
if (!StringUtils::checkFileExtension(currentFilename, ".epub")) {
|
|
||||||
currentFilename += ".epub";
|
|
||||||
}
|
|
||||||
currentFileSize = length;
|
|
||||||
bytesReceived = 0;
|
|
||||||
|
|
||||||
setState(WirelessState::RECEIVING);
|
|
||||||
setStatus("Receiving: " + filename);
|
|
||||||
|
|
||||||
// Open file for writing
|
|
||||||
if (!SdMan.openFileForWrite("CAL", currentFilename.c_str(), currentFile)) {
|
|
||||||
setError("Failed to create file");
|
|
||||||
sendJsonResponse(OpCode::ERROR, "{\"message\":\"Failed to create file\"}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send OK to start receiving binary data
|
|
||||||
sendJsonResponse(OpCode::OK, "{}");
|
|
||||||
|
|
||||||
// Switch to binary mode
|
|
||||||
inBinaryMode = true;
|
|
||||||
binaryBytesRemaining = length;
|
|
||||||
|
|
||||||
// Check if recvBuffer has leftover data (binary file data that arrived with the JSON)
|
|
||||||
if (!recvBuffer.empty()) {
|
|
||||||
size_t toWrite = std::min(recvBuffer.size(), binaryBytesRemaining);
|
|
||||||
size_t written = currentFile.write(reinterpret_cast<const uint8_t*>(recvBuffer.data()), toWrite);
|
|
||||||
bytesReceived += written;
|
|
||||||
binaryBytesRemaining -= written;
|
|
||||||
recvBuffer = recvBuffer.substr(toWrite);
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::handleSendBookMetadata(const std::string& data) {
|
|
||||||
// We receive metadata after the book - just acknowledge
|
|
||||||
sendJsonResponse(OpCode::OK, "{}");
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::handleDisplayMessage(const std::string& data) {
|
|
||||||
// Calibre may send messages to display
|
|
||||||
// Check messageKind - 1 means password error
|
|
||||||
if (data.find("\"messageKind\":1") != std::string::npos) {
|
|
||||||
setError("Password required");
|
|
||||||
}
|
|
||||||
sendJsonResponse(OpCode::OK, "{}");
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::handleNoop(const std::string& data) {
|
|
||||||
// Check for ejecting flag
|
|
||||||
if (data.find("\"ejecting\":true") != std::string::npos) {
|
|
||||||
setState(WirelessState::DISCONNECTED);
|
|
||||||
setStatus("Calibre disconnected");
|
|
||||||
}
|
|
||||||
sendJsonResponse(OpCode::NOOP, "{}");
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::receiveBinaryData() {
|
|
||||||
const int available = tcpClient.available();
|
|
||||||
if (available == 0) {
|
|
||||||
// Check if connection is still alive
|
|
||||||
if (!tcpClient.connected()) {
|
|
||||||
currentFile.close();
|
|
||||||
inBinaryMode = false;
|
|
||||||
setError("Transfer interrupted");
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t buffer[1024];
|
|
||||||
const size_t toRead = std::min(sizeof(buffer), binaryBytesRemaining);
|
|
||||||
const size_t bytesRead = tcpClient.read(buffer, toRead);
|
|
||||||
|
|
||||||
if (bytesRead > 0) {
|
|
||||||
currentFile.write(buffer, bytesRead);
|
|
||||||
bytesReceived += bytesRead;
|
|
||||||
binaryBytesRemaining -= bytesRead;
|
|
||||||
updateRequired = true;
|
|
||||||
|
|
||||||
if (binaryBytesRemaining == 0) {
|
|
||||||
// Transfer complete
|
|
||||||
currentFile.flush();
|
|
||||||
currentFile.close();
|
|
||||||
inBinaryMode = false;
|
|
||||||
|
|
||||||
setState(WirelessState::WAITING);
|
|
||||||
setStatus("Received: " + currentFilename + "\nWaiting for more...");
|
|
||||||
|
|
||||||
// Send OK to acknowledge completion
|
|
||||||
sendJsonResponse(OpCode::OK, "{}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::render() const {
|
|
||||||
renderer.clearScreen();
|
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
|
||||||
const auto pageHeight = renderer.getScreenHeight();
|
|
||||||
|
|
||||||
// Draw header
|
|
||||||
renderer.drawCenteredText(UI_12_FONT_ID, 30, "Calibre Wireless", true, EpdFontFamily::BOLD);
|
|
||||||
|
|
||||||
// Draw IP address
|
|
||||||
const std::string ipAddr = WiFi.localIP().toString().c_str();
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, 60, ("IP: " + ipAddr).c_str());
|
|
||||||
|
|
||||||
// Draw status message
|
|
||||||
int statusY = pageHeight / 2 - 40;
|
|
||||||
|
|
||||||
// Split status message by newlines and draw each line
|
|
||||||
std::string status = statusMessage;
|
|
||||||
size_t pos = 0;
|
|
||||||
while ((pos = status.find('\n')) != std::string::npos) {
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, statusY, status.substr(0, pos).c_str());
|
|
||||||
statusY += 25;
|
|
||||||
status = status.substr(pos + 1);
|
|
||||||
}
|
|
||||||
if (!status.empty()) {
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, statusY, status.c_str());
|
|
||||||
statusY += 25;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw progress if receiving
|
|
||||||
if (state == WirelessState::RECEIVING && currentFileSize > 0) {
|
|
||||||
const int barWidth = pageWidth - 100;
|
|
||||||
constexpr int barHeight = 20;
|
|
||||||
constexpr int barX = 50;
|
|
||||||
const int barY = statusY + 20;
|
|
||||||
ScreenComponents::drawProgressBar(renderer, barX, barY, barWidth, barHeight, bytesReceived, currentFileSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw error if present
|
|
||||||
if (!errorMessage.empty()) {
|
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, pageHeight - 120, errorMessage.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw button hints
|
|
||||||
const auto labels = mappedInput.mapLabels("Back", "", "", "");
|
|
||||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
||||||
|
|
||||||
renderer.displayBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string CalibreWirelessActivity::getDeviceUuid() const {
|
|
||||||
// Generate a consistent UUID based on MAC address
|
|
||||||
uint8_t mac[6];
|
|
||||||
WiFi.macAddress(mac);
|
|
||||||
|
|
||||||
char uuid[37];
|
|
||||||
snprintf(uuid, sizeof(uuid), "%02x%02x%02x%02x-%02x%02x-4000-8000-%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2],
|
|
||||||
mac[3], mac[4], mac[5], mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
||||||
|
|
||||||
return std::string(uuid);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::setState(WirelessState newState) {
|
|
||||||
xSemaphoreTake(stateMutex, portMAX_DELAY);
|
|
||||||
state = newState;
|
|
||||||
xSemaphoreGive(stateMutex);
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::setStatus(const std::string& message) {
|
|
||||||
statusMessage = message;
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreWirelessActivity::setError(const std::string& message) {
|
|
||||||
errorMessage = message;
|
|
||||||
setState(WirelessState::ERROR);
|
|
||||||
}
|
|
||||||
@ -1,135 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <SDCardManager.h>
|
|
||||||
#include <WiFiClient.h>
|
|
||||||
#include <WiFiUdp.h>
|
|
||||||
#include <freertos/FreeRTOS.h>
|
|
||||||
#include <freertos/semphr.h>
|
|
||||||
#include <freertos/task.h>
|
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "activities/Activity.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* CalibreWirelessActivity implements Calibre's "wireless device" protocol.
|
|
||||||
* This allows Calibre desktop to send books directly to the device over WiFi.
|
|
||||||
*
|
|
||||||
* Protocol specification sourced from Calibre's smart device driver:
|
|
||||||
* https://github.com/kovidgoyal/calibre/blob/master/src/calibre/devices/smart_device_app/driver.py
|
|
||||||
*
|
|
||||||
* Protocol overview:
|
|
||||||
* 1. Device broadcasts "hello" on UDP ports 54982, 48123, 39001, 44044, 59678
|
|
||||||
* 2. Calibre responds with its TCP server address
|
|
||||||
* 3. Device connects to Calibre's TCP server
|
|
||||||
* 4. Calibre sends JSON commands with length-prefixed messages
|
|
||||||
* 5. Books are transferred as binary data after SEND_BOOK command
|
|
||||||
*/
|
|
||||||
class CalibreWirelessActivity final : public Activity {
|
|
||||||
// Calibre wireless device states
|
|
||||||
enum class WirelessState {
|
|
||||||
DISCOVERING, // Listening for Calibre server broadcasts
|
|
||||||
CONNECTING, // Establishing TCP connection
|
|
||||||
WAITING, // Connected, waiting for commands
|
|
||||||
RECEIVING, // Receiving a book file
|
|
||||||
COMPLETE, // Transfer complete
|
|
||||||
DISCONNECTED, // Calibre disconnected
|
|
||||||
ERROR // Connection/transfer error
|
|
||||||
};
|
|
||||||
|
|
||||||
// Calibre protocol opcodes (from calibre/devices/smart_device_app/driver.py)
|
|
||||||
enum OpCode : uint8_t {
|
|
||||||
OK = 0,
|
|
||||||
SET_CALIBRE_DEVICE_INFO = 1,
|
|
||||||
SET_CALIBRE_DEVICE_NAME = 2,
|
|
||||||
GET_DEVICE_INFORMATION = 3,
|
|
||||||
TOTAL_SPACE = 4,
|
|
||||||
FREE_SPACE = 5,
|
|
||||||
GET_BOOK_COUNT = 6,
|
|
||||||
SEND_BOOKLISTS = 7,
|
|
||||||
SEND_BOOK = 8,
|
|
||||||
GET_INITIALIZATION_INFO = 9,
|
|
||||||
BOOK_DONE = 11,
|
|
||||||
NOOP = 12, // Was incorrectly 18
|
|
||||||
DELETE_BOOK = 13,
|
|
||||||
GET_BOOK_FILE_SEGMENT = 14,
|
|
||||||
GET_BOOK_METADATA = 15,
|
|
||||||
SEND_BOOK_METADATA = 16,
|
|
||||||
DISPLAY_MESSAGE = 17,
|
|
||||||
CALIBRE_BUSY = 18,
|
|
||||||
SET_LIBRARY_INFO = 19,
|
|
||||||
ERROR = 20,
|
|
||||||
};
|
|
||||||
|
|
||||||
TaskHandle_t displayTaskHandle = nullptr;
|
|
||||||
TaskHandle_t networkTaskHandle = nullptr;
|
|
||||||
SemaphoreHandle_t renderingMutex = nullptr;
|
|
||||||
SemaphoreHandle_t stateMutex = nullptr;
|
|
||||||
bool updateRequired = false;
|
|
||||||
|
|
||||||
WirelessState state = WirelessState::DISCOVERING;
|
|
||||||
const std::function<void()> onComplete;
|
|
||||||
|
|
||||||
// UDP discovery
|
|
||||||
WiFiUDP udp;
|
|
||||||
|
|
||||||
// TCP connection (we connect to Calibre)
|
|
||||||
WiFiClient tcpClient;
|
|
||||||
std::string calibreHost;
|
|
||||||
uint16_t calibrePort = 0;
|
|
||||||
uint16_t calibreAltPort = 0; // Alternative port (content server)
|
|
||||||
std::string calibreHostname;
|
|
||||||
|
|
||||||
// Transfer state
|
|
||||||
std::string currentFilename;
|
|
||||||
size_t currentFileSize = 0;
|
|
||||||
size_t bytesReceived = 0;
|
|
||||||
std::string statusMessage;
|
|
||||||
std::string errorMessage;
|
|
||||||
|
|
||||||
// Protocol state
|
|
||||||
bool inBinaryMode = false;
|
|
||||||
size_t binaryBytesRemaining = 0;
|
|
||||||
FsFile currentFile;
|
|
||||||
std::string recvBuffer; // Buffer for incoming data (like KOReader)
|
|
||||||
|
|
||||||
static void displayTaskTrampoline(void* param);
|
|
||||||
static void networkTaskTrampoline(void* param);
|
|
||||||
[[noreturn]] void displayTaskLoop();
|
|
||||||
[[noreturn]] void networkTaskLoop();
|
|
||||||
void render() const;
|
|
||||||
|
|
||||||
// Network operations
|
|
||||||
void listenForDiscovery();
|
|
||||||
void handleTcpClient();
|
|
||||||
bool readJsonMessage(std::string& message);
|
|
||||||
void sendJsonResponse(OpCode opcode, const std::string& data);
|
|
||||||
void handleCommand(OpCode opcode, const std::string& data);
|
|
||||||
void receiveBinaryData();
|
|
||||||
|
|
||||||
// Protocol handlers
|
|
||||||
void handleGetInitializationInfo(const std::string& data);
|
|
||||||
void handleGetDeviceInformation();
|
|
||||||
void handleFreeSpace();
|
|
||||||
void handleGetBookCount();
|
|
||||||
void handleSendBook(const std::string& data);
|
|
||||||
void handleSendBookMetadata(const std::string& data);
|
|
||||||
void handleDisplayMessage(const std::string& data);
|
|
||||||
void handleNoop(const std::string& data);
|
|
||||||
|
|
||||||
// Utility
|
|
||||||
std::string getDeviceUuid() const;
|
|
||||||
void setState(WirelessState newState);
|
|
||||||
void setStatus(const std::string& message);
|
|
||||||
void setError(const std::string& message);
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit CalibreWirelessActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
|
||||||
const std::function<void()>& onComplete)
|
|
||||||
: Activity("CalibreWireless", renderer, mappedInput), onComplete(onComplete) {}
|
|
||||||
void onEnter() override;
|
|
||||||
void onExit() override;
|
|
||||||
void loop() override;
|
|
||||||
bool preventAutoSleep() override { return true; }
|
|
||||||
bool skipLoopDelay() override { return true; }
|
|
||||||
};
|
|
||||||
@ -4,7 +4,6 @@
|
|||||||
#include <ESPmDNS.h>
|
#include <ESPmDNS.h>
|
||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <esp_task_wdt.h>
|
|
||||||
#include <qrcode.h>
|
#include <qrcode.h>
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
@ -84,8 +83,9 @@ void CrossPointWebServerActivity::onExit() {
|
|||||||
dnsServer = nullptr;
|
dnsServer = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Brief wait for LWIP stack to flush pending packets
|
// CRITICAL: Wait for LWIP stack to flush any pending packets
|
||||||
delay(50);
|
Serial.printf("[%lu] [WEBACT] Waiting 500ms for network stack to flush pending packets...\n", millis());
|
||||||
|
delay(500);
|
||||||
|
|
||||||
// Disconnect WiFi gracefully
|
// Disconnect WiFi gracefully
|
||||||
if (isApMode) {
|
if (isApMode) {
|
||||||
@ -95,11 +95,11 @@ void CrossPointWebServerActivity::onExit() {
|
|||||||
Serial.printf("[%lu] [WEBACT] Disconnecting WiFi (graceful)...\n", millis());
|
Serial.printf("[%lu] [WEBACT] Disconnecting WiFi (graceful)...\n", millis());
|
||||||
WiFi.disconnect(false); // false = don't erase credentials, send disconnect frame
|
WiFi.disconnect(false); // false = don't erase credentials, send disconnect frame
|
||||||
}
|
}
|
||||||
delay(30); // Allow disconnect frame to be sent
|
delay(100); // Allow disconnect frame to be sent
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEBACT] Setting WiFi mode OFF...\n", millis());
|
Serial.printf("[%lu] [WEBACT] Setting WiFi mode OFF...\n", millis());
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
delay(30); // Allow WiFi hardware to power down
|
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());
|
Serial.printf("[%lu] [WEBACT] [MEM] Free heap after WiFi disconnect: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
|
|
||||||
@ -283,28 +283,8 @@ void CrossPointWebServerActivity::loop() {
|
|||||||
dnsServer->processNextRequest();
|
dnsServer->processNextRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
// STA mode: Monitor WiFi connection health
|
// Handle web server requests - call handleClient multiple times per loop
|
||||||
if (!isApMode && webServer && webServer->isRunning()) {
|
// to improve responsiveness and upload throughput
|
||||||
static unsigned long lastWifiCheck = 0;
|
|
||||||
if (millis() - lastWifiCheck > 2000) { // Check every 2 seconds
|
|
||||||
lastWifiCheck = millis();
|
|
||||||
const wl_status_t wifiStatus = WiFi.status();
|
|
||||||
if (wifiStatus != WL_CONNECTED) {
|
|
||||||
Serial.printf("[%lu] [WEBACT] WiFi disconnected! Status: %d\n", millis(), wifiStatus);
|
|
||||||
// Show error and exit gracefully
|
|
||||||
state = WebServerActivityState::SHUTTING_DOWN;
|
|
||||||
updateRequired = true;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Log weak signal warnings
|
|
||||||
const int rssi = WiFi.RSSI();
|
|
||||||
if (rssi < -75) {
|
|
||||||
Serial.printf("[%lu] [WEBACT] Warning: Weak WiFi signal: %d dBm\n", millis(), rssi);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle web server requests - maximize throughput with watchdog safety
|
|
||||||
if (webServer && webServer->isRunning()) {
|
if (webServer && webServer->isRunning()) {
|
||||||
const unsigned long timeSinceLastHandleClient = millis() - lastHandleClientTime;
|
const unsigned long timeSinceLastHandleClient = millis() - lastHandleClientTime;
|
||||||
|
|
||||||
@ -314,32 +294,17 @@ void CrossPointWebServerActivity::loop() {
|
|||||||
timeSinceLastHandleClient);
|
timeSinceLastHandleClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset watchdog BEFORE processing - HTTP header parsing can be slow
|
// Call handleClient multiple times to process pending requests faster
|
||||||
esp_task_wdt_reset();
|
// This is critical for upload performance - HTTP file uploads send data
|
||||||
|
// in chunks and each handleClient() call processes incoming data
|
||||||
// Process HTTP requests in tight loop for maximum throughput
|
constexpr int HANDLE_CLIENT_ITERATIONS = 10;
|
||||||
// More iterations = more data processed per main loop cycle
|
for (int i = 0; i < HANDLE_CLIENT_ITERATIONS && webServer->isRunning(); i++) {
|
||||||
constexpr int MAX_ITERATIONS = 500;
|
|
||||||
for (int i = 0; i < MAX_ITERATIONS && webServer->isRunning(); i++) {
|
|
||||||
webServer->handleClient();
|
webServer->handleClient();
|
||||||
// Reset watchdog every 32 iterations
|
|
||||||
if ((i & 0x1F) == 0x1F) {
|
|
||||||
esp_task_wdt_reset();
|
|
||||||
}
|
|
||||||
// Yield and check for exit button every 64 iterations
|
|
||||||
if ((i & 0x3F) == 0x3F) {
|
|
||||||
yield();
|
|
||||||
// Check for exit button inside loop for responsiveness
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
|
||||||
onGoBack();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
lastHandleClientTime = millis();
|
lastHandleClientTime = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle exit on Back button (also check outside loop)
|
// Handle exit on Back button
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
||||||
onGoBack();
|
onGoBack();
|
||||||
return;
|
return;
|
||||||
@ -416,7 +381,7 @@ void CrossPointWebServerActivity::renderServerRunning() const {
|
|||||||
renderer.drawCenteredText(SMALL_FONT_ID, startY + LINE_SPACING * 3,
|
renderer.drawCenteredText(SMALL_FONT_ID, startY + LINE_SPACING * 3,
|
||||||
"or scan QR code with your phone to connect to Wifi.");
|
"or scan QR code with your phone to connect to Wifi.");
|
||||||
// Show QR code for URL
|
// Show QR code for URL
|
||||||
const std::string wifiConfig = std::string("WIFI:S:") + connectedSSID + ";;";
|
std::string wifiConfig = std::string("WIFI:T:WPA;S:") + connectedSSID + ";P:" + "" + ";;";
|
||||||
drawQRCode(renderer, (480 - 6 * 33) / 2, startY + LINE_SPACING * 4, wifiConfig);
|
drawQRCode(renderer, (480 - 6 * 33) / 2, startY + LINE_SPACING * 4, wifiConfig);
|
||||||
|
|
||||||
startY += 6 * 29 + 3 * LINE_SPACING;
|
startY += 6 * 29 + 3 * LINE_SPACING;
|
||||||
|
|||||||
@ -70,5 +70,4 @@ class CrossPointWebServerActivity final : public ActivityWithSubactivity {
|
|||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
bool skipLoopDelay() override { return webServer && webServer->isRunning(); }
|
bool skipLoopDelay() override { return webServer && webServer->isRunning(); }
|
||||||
bool preventAutoSleep() override { return webServer && webServer->isRunning(); }
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -37,14 +37,6 @@ void WifiSelectionActivity::onEnter() {
|
|||||||
savePromptSelection = 0;
|
savePromptSelection = 0;
|
||||||
forgetPromptSelection = 0;
|
forgetPromptSelection = 0;
|
||||||
|
|
||||||
// Cache MAC address for display
|
|
||||||
uint8_t mac[6];
|
|
||||||
WiFi.macAddress(mac);
|
|
||||||
char macStr[32];
|
|
||||||
snprintf(macStr, sizeof(macStr), "MAC address: %02x-%02x-%02x-%02x-%02x-%02x", mac[0], mac[1], mac[2], mac[3], mac[4],
|
|
||||||
mac[5]);
|
|
||||||
cachedMacAddress = std::string(macStr);
|
|
||||||
|
|
||||||
// Trigger first update to show scanning message
|
// Trigger first update to show scanning message
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
|
|
||||||
@ -455,16 +447,7 @@ std::string WifiSelectionActivity::getSignalStrengthIndicator(const int32_t rssi
|
|||||||
|
|
||||||
void WifiSelectionActivity::displayTaskLoop() {
|
void WifiSelectionActivity::displayTaskLoop() {
|
||||||
while (true) {
|
while (true) {
|
||||||
// If a subactivity is active, yield CPU time but don't render
|
|
||||||
if (subActivity) {
|
if (subActivity) {
|
||||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't render if we're in PASSWORD_ENTRY state - we're just transitioning
|
|
||||||
// from the keyboard subactivity back to the main activity
|
|
||||||
if (state == WifiSelectionState::PASSWORD_ENTRY) {
|
|
||||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -580,9 +563,6 @@ void WifiSelectionActivity::renderNetworkList() const {
|
|||||||
renderer.drawText(SMALL_FONT_ID, 20, pageHeight - 90, countStr);
|
renderer.drawText(SMALL_FONT_ID, 20, pageHeight - 90, countStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show MAC address above the network count and legend
|
|
||||||
renderer.drawText(SMALL_FONT_ID, 20, pageHeight - 105, cachedMacAddress.c_str());
|
|
||||||
|
|
||||||
// Draw help text
|
// Draw help text
|
||||||
renderer.drawText(SMALL_FONT_ID, 20, pageHeight - 75, "* = Encrypted | + = Saved");
|
renderer.drawText(SMALL_FONT_ID, 20, pageHeight - 75, "* = Encrypted | + = Saved");
|
||||||
const auto labels = mappedInput.mapLabels("« Back", "Connect", "", "");
|
const auto labels = mappedInput.mapLabels("« Back", "Connect", "", "");
|
||||||
|
|||||||
@ -62,9 +62,6 @@ class WifiSelectionActivity final : public ActivityWithSubactivity {
|
|||||||
// Password to potentially save (from keyboard or saved credentials)
|
// Password to potentially save (from keyboard or saved credentials)
|
||||||
std::string enteredPassword;
|
std::string enteredPassword;
|
||||||
|
|
||||||
// Cached MAC address string for display
|
|
||||||
std::string cachedMacAddress;
|
|
||||||
|
|
||||||
// Whether network was connected using a saved password (skip save prompt)
|
// Whether network was connected using a saved password (skip save prompt)
|
||||||
bool usedSavedPassword = false;
|
bool usedSavedPassword = false;
|
||||||
|
|
||||||
|
|||||||
@ -13,9 +13,11 @@
|
|||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
// pagesPerRefresh now comes from SETTINGS.getRefreshFrequency()
|
constexpr int pagesPerRefresh = 15;
|
||||||
constexpr unsigned long skipChapterMs = 700;
|
constexpr unsigned long skipChapterMs = 700;
|
||||||
constexpr unsigned long goHomeMs = 1000;
|
constexpr unsigned long goHomeMs = 1000;
|
||||||
|
constexpr int topPadding = 5;
|
||||||
|
constexpr int horizontalPadding = 5;
|
||||||
constexpr int statusBarMargin = 19;
|
constexpr int statusBarMargin = 19;
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
@ -152,8 +154,6 @@ void EpubReaderActivity::loop() {
|
|||||||
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::PageBack) ||
|
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::PageBack) ||
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Left);
|
mappedInput.wasReleased(MappedInputManager::Button::Left);
|
||||||
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::PageForward) ||
|
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::PageForward) ||
|
||||||
(SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
|
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Power)) ||
|
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Right);
|
mappedInput.wasReleased(MappedInputManager::Button::Right);
|
||||||
|
|
||||||
if (!prevReleased && !nextReleased) {
|
if (!prevReleased && !nextReleased) {
|
||||||
@ -168,7 +168,7 @@ void EpubReaderActivity::loop() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool skipChapter = SETTINGS.longPressChapterSkip && mappedInput.getHeldTime() > skipChapterMs;
|
const bool skipChapter = mappedInput.getHeldTime() > skipChapterMs;
|
||||||
|
|
||||||
if (skipChapter) {
|
if (skipChapter) {
|
||||||
// We don't want to delete the section mid-render, so grab the semaphore
|
// We don't want to delete the section mid-render, so grab the semaphore
|
||||||
@ -253,9 +253,9 @@ void EpubReaderActivity::renderScreen() {
|
|||||||
int orientedMarginTop, orientedMarginRight, orientedMarginBottom, orientedMarginLeft;
|
int orientedMarginTop, orientedMarginRight, orientedMarginBottom, orientedMarginLeft;
|
||||||
renderer.getOrientedViewableTRBL(&orientedMarginTop, &orientedMarginRight, &orientedMarginBottom,
|
renderer.getOrientedViewableTRBL(&orientedMarginTop, &orientedMarginRight, &orientedMarginBottom,
|
||||||
&orientedMarginLeft);
|
&orientedMarginLeft);
|
||||||
orientedMarginTop += SETTINGS.screenMargin;
|
orientedMarginTop += topPadding;
|
||||||
orientedMarginLeft += SETTINGS.screenMargin;
|
orientedMarginLeft += horizontalPadding;
|
||||||
orientedMarginRight += SETTINGS.screenMargin;
|
orientedMarginRight += horizontalPadding;
|
||||||
orientedMarginBottom += statusBarMargin;
|
orientedMarginBottom += statusBarMargin;
|
||||||
|
|
||||||
if (!section) {
|
if (!section) {
|
||||||
@ -267,8 +267,7 @@ void EpubReaderActivity::renderScreen() {
|
|||||||
const uint16_t viewportHeight = renderer.getScreenHeight() - orientedMarginTop - orientedMarginBottom;
|
const uint16_t viewportHeight = renderer.getScreenHeight() - orientedMarginTop - orientedMarginBottom;
|
||||||
|
|
||||||
if (!section->loadSectionFile(SETTINGS.getReaderFontId(), SETTINGS.getReaderLineCompression(),
|
if (!section->loadSectionFile(SETTINGS.getReaderFontId(), SETTINGS.getReaderLineCompression(),
|
||||||
SETTINGS.extraParagraphSpacing, SETTINGS.paragraphAlignment, viewportWidth,
|
SETTINGS.extraParagraphSpacing, viewportWidth, viewportHeight)) {
|
||||||
viewportHeight)) {
|
|
||||||
Serial.printf("[%lu] [ERS] Cache not found, building...\n", millis());
|
Serial.printf("[%lu] [ERS] Cache not found, building...\n", millis());
|
||||||
|
|
||||||
// Progress bar dimensions
|
// Progress bar dimensions
|
||||||
@ -312,8 +311,8 @@ void EpubReaderActivity::renderScreen() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (!section->createSectionFile(SETTINGS.getReaderFontId(), SETTINGS.getReaderLineCompression(),
|
if (!section->createSectionFile(SETTINGS.getReaderFontId(), SETTINGS.getReaderLineCompression(),
|
||||||
SETTINGS.extraParagraphSpacing, SETTINGS.paragraphAlignment, viewportWidth,
|
SETTINGS.extraParagraphSpacing, viewportWidth, viewportHeight, progressSetup,
|
||||||
viewportHeight, progressSetup, progressCallback)) {
|
progressCallback)) {
|
||||||
Serial.printf("[%lu] [ERS] Failed to persist page data to SD\n", millis());
|
Serial.printf("[%lu] [ERS] Failed to persist page data to SD\n", millis());
|
||||||
section.reset();
|
section.reset();
|
||||||
return;
|
return;
|
||||||
@ -379,7 +378,7 @@ void EpubReaderActivity::renderContents(std::unique_ptr<Page> page, const int or
|
|||||||
renderStatusBar(orientedMarginRight, orientedMarginBottom, orientedMarginLeft);
|
renderStatusBar(orientedMarginRight, orientedMarginBottom, orientedMarginLeft);
|
||||||
if (pagesUntilFullRefresh <= 1) {
|
if (pagesUntilFullRefresh <= 1) {
|
||||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
||||||
pagesUntilFullRefresh = SETTINGS.getRefreshFrequency();
|
pagesUntilFullRefresh = pagesPerRefresh;
|
||||||
} else {
|
} else {
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
pagesUntilFullRefresh--;
|
pagesUntilFullRefresh--;
|
||||||
@ -390,7 +389,7 @@ void EpubReaderActivity::renderContents(std::unique_ptr<Page> page, const int or
|
|||||||
|
|
||||||
// grayscale rendering
|
// grayscale rendering
|
||||||
// TODO: Only do this if font supports it
|
// TODO: Only do this if font supports it
|
||||||
if (SETTINGS.textAntiAliasing) {
|
{
|
||||||
renderer.clearScreen(0x00);
|
renderer.clearScreen(0x00);
|
||||||
renderer.setRenderMode(GfxRenderer::GRAYSCALE_LSB);
|
renderer.setRenderMode(GfxRenderer::GRAYSCALE_LSB);
|
||||||
page->render(renderer, SETTINGS.getReaderFontId(), orientedMarginLeft, orientedMarginTop);
|
page->render(renderer, SETTINGS.getReaderFontId(), orientedMarginLeft, orientedMarginTop);
|
||||||
@ -419,8 +418,6 @@ void EpubReaderActivity::renderStatusBar(const int orientedMarginRight, const in
|
|||||||
SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::FULL;
|
SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::FULL;
|
||||||
const bool showChapterTitle = SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::NO_PROGRESS ||
|
const bool showChapterTitle = SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::NO_PROGRESS ||
|
||||||
SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::FULL;
|
SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::FULL;
|
||||||
const bool showBatteryPercentage =
|
|
||||||
SETTINGS.hideBatteryPercentage == CrossPointSettings::HIDE_BATTERY_PERCENTAGE::HIDE_NEVER;
|
|
||||||
|
|
||||||
// Position status bar near the bottom of the logical screen, regardless of orientation
|
// Position status bar near the bottom of the logical screen, regardless of orientation
|
||||||
const auto screenHeight = renderer.getScreenHeight();
|
const auto screenHeight = renderer.getScreenHeight();
|
||||||
@ -441,7 +438,7 @@ void EpubReaderActivity::renderStatusBar(const int orientedMarginRight, const in
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (showBattery) {
|
if (showBattery) {
|
||||||
ScreenComponents::drawBattery(renderer, orientedMarginLeft + 1, textY, showBatteryPercentage);
|
ScreenComponents::drawBattery(renderer, orientedMarginLeft, textY);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (showChapterTitle) {
|
if (showChapterTitle) {
|
||||||
|
|||||||
@ -16,9 +16,7 @@ int EpubReaderChapterSelectionActivity::getPageItems() const {
|
|||||||
constexpr int lineHeight = 30;
|
constexpr int lineHeight = 30;
|
||||||
|
|
||||||
const int screenHeight = renderer.getScreenHeight();
|
const int screenHeight = renderer.getScreenHeight();
|
||||||
const int endY = screenHeight - lineHeight;
|
const int availableHeight = screenHeight - startY;
|
||||||
|
|
||||||
const int availableHeight = endY - startY;
|
|
||||||
int items = availableHeight / lineHeight;
|
int items = availableHeight / lineHeight;
|
||||||
|
|
||||||
// Ensure we always have at least one item per page to avoid division by zero
|
// Ensure we always have at least one item per page to avoid division by zero
|
||||||
@ -136,8 +134,5 @@ void EpubReaderChapterSelectionActivity::renderScreen() {
|
|||||||
tocIndex != selectorIndex);
|
tocIndex != selectorIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto labels = mappedInput.mapLabels("« Back", "Select", "Up", "Down");
|
|
||||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
||||||
|
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
#include "util/StringUtils.h"
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
constexpr int PAGE_ITEMS = 23;
|
constexpr int PAGE_ITEMS = 23;
|
||||||
@ -30,6 +29,7 @@ void FileSelectionActivity::taskTrampoline(void* param) {
|
|||||||
|
|
||||||
void FileSelectionActivity::loadFiles() {
|
void FileSelectionActivity::loadFiles() {
|
||||||
files.clear();
|
files.clear();
|
||||||
|
selectorIndex = 0;
|
||||||
|
|
||||||
auto root = SdMan.open(basepath.c_str());
|
auto root = SdMan.open(basepath.c_str());
|
||||||
if (!root || !root.isDirectory()) {
|
if (!root || !root.isDirectory()) {
|
||||||
@ -39,7 +39,7 @@ void FileSelectionActivity::loadFiles() {
|
|||||||
|
|
||||||
root.rewindDirectory();
|
root.rewindDirectory();
|
||||||
|
|
||||||
char name[500];
|
char name[128];
|
||||||
for (auto file = root.openNextFile(); file; file = root.openNextFile()) {
|
for (auto file = root.openNextFile(); file; file = root.openNextFile()) {
|
||||||
file.getName(name, sizeof(name));
|
file.getName(name, sizeof(name));
|
||||||
if (name[0] == '.' || strcmp(name, "System Volume Information") == 0) {
|
if (name[0] == '.' || strcmp(name, "System Volume Information") == 0) {
|
||||||
@ -51,8 +51,9 @@ void FileSelectionActivity::loadFiles() {
|
|||||||
files.emplace_back(std::string(name) + "/");
|
files.emplace_back(std::string(name) + "/");
|
||||||
} else {
|
} else {
|
||||||
auto filename = std::string(name);
|
auto filename = std::string(name);
|
||||||
if (StringUtils::checkFileExtension(filename, ".epub") || StringUtils::checkFileExtension(filename, ".xtch") ||
|
std::string ext4 = filename.length() >= 4 ? filename.substr(filename.length() - 4) : "";
|
||||||
StringUtils::checkFileExtension(filename, ".xtc") || StringUtils::checkFileExtension(filename, ".txt")) {
|
std::string ext5 = filename.length() >= 5 ? filename.substr(filename.length() - 5) : "";
|
||||||
|
if (ext5 == ".epub" || ext5 == ".xtch" || ext4 == ".xtc") {
|
||||||
files.emplace_back(filename);
|
files.emplace_back(filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,7 +124,6 @@ void FileSelectionActivity::loop() {
|
|||||||
if (files[selectorIndex].back() == '/') {
|
if (files[selectorIndex].back() == '/') {
|
||||||
basepath += files[selectorIndex].substr(0, files[selectorIndex].length() - 1);
|
basepath += files[selectorIndex].substr(0, files[selectorIndex].length() - 1);
|
||||||
loadFiles();
|
loadFiles();
|
||||||
selectorIndex = 0;
|
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
} else {
|
} else {
|
||||||
onSelect(basepath + files[selectorIndex]);
|
onSelect(basepath + files[selectorIndex]);
|
||||||
@ -132,16 +132,9 @@ void FileSelectionActivity::loop() {
|
|||||||
// Short press: go up one directory, or go home if at root
|
// Short press: go up one directory, or go home if at root
|
||||||
if (mappedInput.getHeldTime() < GO_HOME_MS) {
|
if (mappedInput.getHeldTime() < GO_HOME_MS) {
|
||||||
if (basepath != "/") {
|
if (basepath != "/") {
|
||||||
const std::string oldPath = basepath;
|
|
||||||
|
|
||||||
basepath.replace(basepath.find_last_of('/'), std::string::npos, "");
|
basepath.replace(basepath.find_last_of('/'), std::string::npos, "");
|
||||||
if (basepath.empty()) basepath = "/";
|
if (basepath.empty()) basepath = "/";
|
||||||
loadFiles();
|
loadFiles();
|
||||||
|
|
||||||
const auto pos = oldPath.find_last_of('/');
|
|
||||||
const std::string dirName = oldPath.substr(pos + 1) + "/";
|
|
||||||
selectorIndex = findEntry(dirName);
|
|
||||||
|
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
} else {
|
} else {
|
||||||
onGoHome();
|
onGoHome();
|
||||||
@ -194,16 +187,10 @@ void FileSelectionActivity::render() const {
|
|||||||
|
|
||||||
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
|
const auto pageStartIndex = selectorIndex / PAGE_ITEMS * PAGE_ITEMS;
|
||||||
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 - 2, pageWidth - 1, 30);
|
renderer.fillRect(0, 60 + (selectorIndex % PAGE_ITEMS) * 30 - 2, pageWidth - 1, 30);
|
||||||
for (size_t i = pageStartIndex; i < files.size() && i < pageStartIndex + PAGE_ITEMS; i++) {
|
for (int i = pageStartIndex; i < files.size() && i < pageStartIndex + PAGE_ITEMS; i++) {
|
||||||
auto item = renderer.truncatedText(UI_10_FONT_ID, files[i].c_str(), renderer.getScreenWidth() - 40);
|
auto item = renderer.truncatedText(UI_10_FONT_ID, files[i].c_str(), renderer.getScreenWidth() - 40);
|
||||||
renderer.drawText(UI_10_FONT_ID, 20, 60 + (i % PAGE_ITEMS) * 30, item.c_str(), i != selectorIndex);
|
renderer.drawText(UI_10_FONT_ID, 20, 60 + (i % PAGE_ITEMS) * 30, item.c_str(), i != selectorIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t FileSelectionActivity::findEntry(const std::string& name) const {
|
|
||||||
for (size_t i = 0; i < files.size(); i++)
|
|
||||||
if (files[i] == name) return i;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@ class FileSelectionActivity final : public Activity {
|
|||||||
SemaphoreHandle_t renderingMutex = nullptr;
|
SemaphoreHandle_t renderingMutex = nullptr;
|
||||||
std::string basepath = "/";
|
std::string basepath = "/";
|
||||||
std::vector<std::string> files;
|
std::vector<std::string> files;
|
||||||
size_t selectorIndex = 0;
|
int selectorIndex = 0;
|
||||||
bool updateRequired = false;
|
bool updateRequired = false;
|
||||||
const std::function<void(const std::string&)> onSelect;
|
const std::function<void(const std::string&)> onSelect;
|
||||||
const std::function<void()> onGoHome;
|
const std::function<void()> onGoHome;
|
||||||
@ -24,8 +24,6 @@ class FileSelectionActivity final : public Activity {
|
|||||||
void render() const;
|
void render() const;
|
||||||
void loadFiles();
|
void loadFiles();
|
||||||
|
|
||||||
size_t findEntry(const std::string& name) const;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit FileSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit FileSelectionActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
||||||
const std::function<void(const std::string&)>& onSelect,
|
const std::function<void(const std::string&)>& onSelect,
|
||||||
|
|||||||
@ -3,12 +3,9 @@
|
|||||||
#include "Epub.h"
|
#include "Epub.h"
|
||||||
#include "EpubReaderActivity.h"
|
#include "EpubReaderActivity.h"
|
||||||
#include "FileSelectionActivity.h"
|
#include "FileSelectionActivity.h"
|
||||||
#include "Txt.h"
|
|
||||||
#include "TxtReaderActivity.h"
|
|
||||||
#include "Xtc.h"
|
#include "Xtc.h"
|
||||||
#include "XtcReaderActivity.h"
|
#include "XtcReaderActivity.h"
|
||||||
#include "activities/util/FullScreenMessageActivity.h"
|
#include "activities/util/FullScreenMessageActivity.h"
|
||||||
#include "util/StringUtils.h"
|
|
||||||
|
|
||||||
std::string ReaderActivity::extractFolderPath(const std::string& filePath) {
|
std::string ReaderActivity::extractFolderPath(const std::string& filePath) {
|
||||||
const auto lastSlash = filePath.find_last_of('/');
|
const auto lastSlash = filePath.find_last_of('/');
|
||||||
@ -19,13 +16,14 @@ std::string ReaderActivity::extractFolderPath(const std::string& filePath) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ReaderActivity::isXtcFile(const std::string& path) {
|
bool ReaderActivity::isXtcFile(const std::string& path) {
|
||||||
return StringUtils::checkFileExtension(path, ".xtc") || StringUtils::checkFileExtension(path, ".xtch");
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ReaderActivity::isTxtFile(const std::string& path) {
|
|
||||||
if (path.length() < 4) return false;
|
if (path.length() < 4) return false;
|
||||||
std::string ext4 = path.substr(path.length() - 4);
|
std::string ext4 = path.substr(path.length() - 4);
|
||||||
return ext4 == ".txt" || ext4 == ".TXT";
|
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) {
|
std::unique_ptr<Epub> ReaderActivity::loadEpub(const std::string& path) {
|
||||||
@ -58,21 +56,6 @@ std::unique_ptr<Xtc> ReaderActivity::loadXtc(const std::string& path) {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Txt> ReaderActivity::loadTxt(const std::string& path) {
|
|
||||||
if (!SdMan.exists(path.c_str())) {
|
|
||||||
Serial.printf("[%lu] [ ] File does not exist: %s\n", millis(), path.c_str());
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto txt = std::unique_ptr<Txt>(new Txt(path, "/.crosspoint"));
|
|
||||||
if (txt->load()) {
|
|
||||||
return txt;
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [ ] Failed to load TXT\n", millis());
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ReaderActivity::onSelectBookFile(const std::string& path) {
|
void ReaderActivity::onSelectBookFile(const std::string& path) {
|
||||||
currentBookPath = path; // Track current book path
|
currentBookPath = path; // Track current book path
|
||||||
exitActivity();
|
exitActivity();
|
||||||
@ -90,18 +73,6 @@ void ReaderActivity::onSelectBookFile(const std::string& path) {
|
|||||||
delay(2000);
|
delay(2000);
|
||||||
onGoToFileSelection();
|
onGoToFileSelection();
|
||||||
}
|
}
|
||||||
} else if (isTxtFile(path)) {
|
|
||||||
// Load TXT file
|
|
||||||
auto txt = loadTxt(path);
|
|
||||||
if (txt) {
|
|
||||||
onGoToTxtReader(std::move(txt));
|
|
||||||
} else {
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new FullScreenMessageActivity(renderer, mappedInput, "Failed to load TXT",
|
|
||||||
EpdFontFamily::REGULAR, EInkDisplay::HALF_REFRESH));
|
|
||||||
delay(2000);
|
|
||||||
onGoToFileSelection();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Load EPUB file
|
// Load EPUB file
|
||||||
auto epub = loadEpub(path);
|
auto epub = loadEpub(path);
|
||||||
@ -143,15 +114,6 @@ void ReaderActivity::onGoToXtcReader(std::unique_ptr<Xtc> xtc) {
|
|||||||
[this] { onGoBack(); }));
|
[this] { onGoBack(); }));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReaderActivity::onGoToTxtReader(std::unique_ptr<Txt> txt) {
|
|
||||||
const auto txtPath = txt->getPath();
|
|
||||||
currentBookPath = txtPath;
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new TxtReaderActivity(
|
|
||||||
renderer, mappedInput, std::move(txt), [this, txtPath] { onGoToFileSelection(txtPath); },
|
|
||||||
[this] { onGoBack(); }));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ReaderActivity::onEnter() {
|
void ReaderActivity::onEnter() {
|
||||||
ActivityWithSubactivity::onEnter();
|
ActivityWithSubactivity::onEnter();
|
||||||
|
|
||||||
@ -169,13 +131,6 @@ void ReaderActivity::onEnter() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
onGoToXtcReader(std::move(xtc));
|
onGoToXtcReader(std::move(xtc));
|
||||||
} else if (isTxtFile(initialBookPath)) {
|
|
||||||
auto txt = loadTxt(initialBookPath);
|
|
||||||
if (!txt) {
|
|
||||||
onGoBack();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
onGoToTxtReader(std::move(txt));
|
|
||||||
} else {
|
} else {
|
||||||
auto epub = loadEpub(initialBookPath);
|
auto epub = loadEpub(initialBookPath);
|
||||||
if (!epub) {
|
if (!epub) {
|
||||||
|
|||||||
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
class Epub;
|
class Epub;
|
||||||
class Xtc;
|
class Xtc;
|
||||||
class Txt;
|
|
||||||
|
|
||||||
class ReaderActivity final : public ActivityWithSubactivity {
|
class ReaderActivity final : public ActivityWithSubactivity {
|
||||||
std::string initialBookPath;
|
std::string initialBookPath;
|
||||||
@ -13,16 +12,13 @@ class ReaderActivity final : public ActivityWithSubactivity {
|
|||||||
const std::function<void()> onGoBack;
|
const std::function<void()> onGoBack;
|
||||||
static std::unique_ptr<Epub> loadEpub(const std::string& path);
|
static std::unique_ptr<Epub> loadEpub(const std::string& path);
|
||||||
static std::unique_ptr<Xtc> loadXtc(const std::string& path);
|
static std::unique_ptr<Xtc> loadXtc(const std::string& path);
|
||||||
static std::unique_ptr<Txt> loadTxt(const std::string& path);
|
|
||||||
static bool isXtcFile(const std::string& path);
|
static bool isXtcFile(const std::string& path);
|
||||||
static bool isTxtFile(const std::string& path);
|
|
||||||
|
|
||||||
static std::string extractFolderPath(const std::string& filePath);
|
static std::string extractFolderPath(const std::string& filePath);
|
||||||
void onSelectBookFile(const std::string& path);
|
void onSelectBookFile(const std::string& path);
|
||||||
void onGoToFileSelection(const std::string& fromBookPath = "");
|
void onGoToFileSelection(const std::string& fromBookPath = "");
|
||||||
void onGoToEpubReader(std::unique_ptr<Epub> epub);
|
void onGoToEpubReader(std::unique_ptr<Epub> epub);
|
||||||
void onGoToXtcReader(std::unique_ptr<Xtc> xtc);
|
void onGoToXtcReader(std::unique_ptr<Xtc> xtc);
|
||||||
void onGoToTxtReader(std::unique_ptr<Txt> txt);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string initialBookPath,
|
explicit ReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::string initialBookPath,
|
||||||
|
|||||||
@ -1,700 +0,0 @@
|
|||||||
#include "TxtReaderActivity.h"
|
|
||||||
|
|
||||||
#include <GfxRenderer.h>
|
|
||||||
#include <SDCardManager.h>
|
|
||||||
#include <Serialization.h>
|
|
||||||
#include <Utf8.h>
|
|
||||||
|
|
||||||
#include "CrossPointSettings.h"
|
|
||||||
#include "CrossPointState.h"
|
|
||||||
#include "MappedInputManager.h"
|
|
||||||
#include "ScreenComponents.h"
|
|
||||||
#include "fontIds.h"
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
constexpr unsigned long goHomeMs = 1000;
|
|
||||||
constexpr int statusBarMargin = 25;
|
|
||||||
constexpr size_t CHUNK_SIZE = 8 * 1024; // 8KB chunk for reading
|
|
||||||
|
|
||||||
// Cache file magic and version
|
|
||||||
constexpr uint32_t CACHE_MAGIC = 0x54585449; // "TXTI"
|
|
||||||
constexpr uint8_t CACHE_VERSION = 2; // Increment when cache format changes
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
void TxtReaderActivity::taskTrampoline(void* param) {
|
|
||||||
auto* self = static_cast<TxtReaderActivity*>(param);
|
|
||||||
self->displayTaskLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::onEnter() {
|
|
||||||
ActivityWithSubactivity::onEnter();
|
|
||||||
|
|
||||||
if (!txt) {
|
|
||||||
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();
|
|
||||||
|
|
||||||
txt->setupCacheDir();
|
|
||||||
|
|
||||||
// Save current txt as last opened file
|
|
||||||
APP_STATE.openEpubPath = txt->getPath();
|
|
||||||
APP_STATE.saveToFile();
|
|
||||||
|
|
||||||
// Trigger first update
|
|
||||||
updateRequired = true;
|
|
||||||
|
|
||||||
xTaskCreate(&TxtReaderActivity::taskTrampoline, "TxtReaderActivityTask",
|
|
||||||
6144, // Stack size
|
|
||||||
this, // Parameters
|
|
||||||
1, // Priority
|
|
||||||
&displayTaskHandle // Task handle
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::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
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
if (displayTaskHandle) {
|
|
||||||
vTaskDelete(displayTaskHandle);
|
|
||||||
displayTaskHandle = nullptr;
|
|
||||||
}
|
|
||||||
vSemaphoreDelete(renderingMutex);
|
|
||||||
renderingMutex = nullptr;
|
|
||||||
pageOffsets.clear();
|
|
||||||
currentPageLines.clear();
|
|
||||||
txt.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::loop() {
|
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Long press BACK (1s+) goes directly to home
|
|
||||||
if (mappedInput.isPressed(MappedInputManager::Button::Back) && mappedInput.getHeldTime() >= goHomeMs) {
|
|
||||||
onGoHome();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Short press BACK goes to file selection
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back) && mappedInput.getHeldTime() < goHomeMs) {
|
|
||||||
onGoBack();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::PageBack) ||
|
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Left);
|
|
||||||
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::PageForward) ||
|
|
||||||
(SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
|
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Power)) ||
|
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Right);
|
|
||||||
|
|
||||||
if (!prevReleased && !nextReleased) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (prevReleased && currentPage > 0) {
|
|
||||||
currentPage--;
|
|
||||||
updateRequired = true;
|
|
||||||
} else if (nextReleased && currentPage < totalPages - 1) {
|
|
||||||
currentPage++;
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::displayTaskLoop() {
|
|
||||||
while (true) {
|
|
||||||
if (updateRequired) {
|
|
||||||
updateRequired = false;
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
renderScreen();
|
|
||||||
xSemaphoreGive(renderingMutex);
|
|
||||||
}
|
|
||||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::initializeReader() {
|
|
||||||
if (initialized) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store current settings for cache validation
|
|
||||||
cachedFontId = SETTINGS.getReaderFontId();
|
|
||||||
cachedScreenMargin = SETTINGS.screenMargin;
|
|
||||||
cachedParagraphAlignment = SETTINGS.paragraphAlignment;
|
|
||||||
|
|
||||||
// Calculate viewport dimensions
|
|
||||||
int orientedMarginTop, orientedMarginRight, orientedMarginBottom, orientedMarginLeft;
|
|
||||||
renderer.getOrientedViewableTRBL(&orientedMarginTop, &orientedMarginRight, &orientedMarginBottom,
|
|
||||||
&orientedMarginLeft);
|
|
||||||
orientedMarginTop += cachedScreenMargin;
|
|
||||||
orientedMarginLeft += cachedScreenMargin;
|
|
||||||
orientedMarginRight += cachedScreenMargin;
|
|
||||||
orientedMarginBottom += statusBarMargin;
|
|
||||||
|
|
||||||
viewportWidth = renderer.getScreenWidth() - orientedMarginLeft - orientedMarginRight;
|
|
||||||
const int viewportHeight = renderer.getScreenHeight() - orientedMarginTop - orientedMarginBottom;
|
|
||||||
const int lineHeight = renderer.getLineHeight(cachedFontId);
|
|
||||||
|
|
||||||
linesPerPage = viewportHeight / lineHeight;
|
|
||||||
if (linesPerPage < 1) linesPerPage = 1;
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [TRS] Viewport: %dx%d, lines per page: %d\n", millis(), viewportWidth, viewportHeight,
|
|
||||||
linesPerPage);
|
|
||||||
|
|
||||||
// Try to load cached page index first
|
|
||||||
if (!loadPageIndexCache()) {
|
|
||||||
// Cache not found, build page index
|
|
||||||
buildPageIndex();
|
|
||||||
// Save to cache for next time
|
|
||||||
savePageIndexCache();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load saved progress
|
|
||||||
loadProgress();
|
|
||||||
|
|
||||||
initialized = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::buildPageIndex() {
|
|
||||||
pageOffsets.clear();
|
|
||||||
pageOffsets.push_back(0); // First page starts at offset 0
|
|
||||||
|
|
||||||
size_t offset = 0;
|
|
||||||
const size_t fileSize = txt->getFileSize();
|
|
||||||
int lastProgressPercent = -1;
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [TRS] Building page index for %zu bytes...\n", millis(), fileSize);
|
|
||||||
|
|
||||||
// Progress bar dimensions (matching EpubReaderActivity style)
|
|
||||||
constexpr int barWidth = 200;
|
|
||||||
constexpr int barHeight = 10;
|
|
||||||
constexpr int boxMargin = 20;
|
|
||||||
const int textWidth = renderer.getTextWidth(UI_12_FONT_ID, "Indexing...");
|
|
||||||
const int boxWidth = (barWidth > textWidth ? barWidth : textWidth) + boxMargin * 2;
|
|
||||||
const int boxHeight = renderer.getLineHeight(UI_12_FONT_ID) + barHeight + boxMargin * 3;
|
|
||||||
const int boxX = (renderer.getScreenWidth() - boxWidth) / 2;
|
|
||||||
constexpr int boxY = 50;
|
|
||||||
const int barX = boxX + (boxWidth - barWidth) / 2;
|
|
||||||
const int barY = boxY + renderer.getLineHeight(UI_12_FONT_ID) + boxMargin * 2;
|
|
||||||
|
|
||||||
// Draw initial progress box
|
|
||||||
renderer.fillRect(boxX, boxY, boxWidth, boxHeight, false);
|
|
||||||
renderer.drawText(UI_12_FONT_ID, boxX + boxMargin, boxY + boxMargin, "Indexing...");
|
|
||||||
renderer.drawRect(boxX + 5, boxY + 5, boxWidth - 10, boxHeight - 10);
|
|
||||||
renderer.drawRect(barX, barY, barWidth, barHeight);
|
|
||||||
renderer.displayBuffer();
|
|
||||||
|
|
||||||
while (offset < fileSize) {
|
|
||||||
std::vector<std::string> tempLines;
|
|
||||||
size_t nextOffset = offset;
|
|
||||||
|
|
||||||
if (!loadPageAtOffset(offset, tempLines, nextOffset)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nextOffset <= offset) {
|
|
||||||
// No progress made, avoid infinite loop
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
offset = nextOffset;
|
|
||||||
if (offset < fileSize) {
|
|
||||||
pageOffsets.push_back(offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update progress bar every 10% (matching EpubReaderActivity logic)
|
|
||||||
int progressPercent = (offset * 100) / fileSize;
|
|
||||||
if (lastProgressPercent / 10 != progressPercent / 10) {
|
|
||||||
lastProgressPercent = progressPercent;
|
|
||||||
|
|
||||||
// Fill progress bar
|
|
||||||
const int fillWidth = (barWidth - 2) * progressPercent / 100;
|
|
||||||
renderer.fillRect(barX + 1, barY + 1, fillWidth, barHeight - 2, true);
|
|
||||||
renderer.displayBuffer(EInkDisplay::FAST_REFRESH);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Yield to other tasks periodically
|
|
||||||
if (pageOffsets.size() % 20 == 0) {
|
|
||||||
vTaskDelay(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
totalPages = pageOffsets.size();
|
|
||||||
Serial.printf("[%lu] [TRS] Built page index: %d pages\n", millis(), totalPages);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TxtReaderActivity::loadPageAtOffset(size_t offset, std::vector<std::string>& outLines, size_t& nextOffset) {
|
|
||||||
outLines.clear();
|
|
||||||
const size_t fileSize = txt->getFileSize();
|
|
||||||
|
|
||||||
if (offset >= fileSize) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read a chunk from file
|
|
||||||
size_t chunkSize = std::min(CHUNK_SIZE, fileSize - offset);
|
|
||||||
auto* buffer = static_cast<uint8_t*>(malloc(chunkSize + 1));
|
|
||||||
if (!buffer) {
|
|
||||||
Serial.printf("[%lu] [TRS] Failed to allocate %zu bytes\n", millis(), chunkSize);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!txt->readContent(buffer, offset, chunkSize)) {
|
|
||||||
free(buffer);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
buffer[chunkSize] = '\0';
|
|
||||||
|
|
||||||
// Parse lines from buffer
|
|
||||||
size_t pos = 0;
|
|
||||||
|
|
||||||
while (pos < chunkSize && static_cast<int>(outLines.size()) < linesPerPage) {
|
|
||||||
// Find end of line
|
|
||||||
size_t lineEnd = pos;
|
|
||||||
while (lineEnd < chunkSize && buffer[lineEnd] != '\n') {
|
|
||||||
lineEnd++;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if we have a complete line
|
|
||||||
bool lineComplete = (lineEnd < chunkSize) || (offset + lineEnd >= fileSize);
|
|
||||||
|
|
||||||
if (!lineComplete && static_cast<int>(outLines.size()) > 0) {
|
|
||||||
// Incomplete line and we already have some lines, stop here
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate the actual length of line content in the buffer (excluding newline)
|
|
||||||
size_t lineContentLen = lineEnd - pos;
|
|
||||||
|
|
||||||
// Check for carriage return
|
|
||||||
bool hasCR = (lineContentLen > 0 && buffer[pos + lineContentLen - 1] == '\r');
|
|
||||||
size_t displayLen = hasCR ? lineContentLen - 1 : lineContentLen;
|
|
||||||
|
|
||||||
// Extract line content for display (without CR/LF)
|
|
||||||
std::string line(reinterpret_cast<char*>(buffer + pos), displayLen);
|
|
||||||
|
|
||||||
// Track position within this source line (in bytes from pos)
|
|
||||||
size_t lineBytePos = 0;
|
|
||||||
|
|
||||||
// Word wrap if needed
|
|
||||||
while (!line.empty() && static_cast<int>(outLines.size()) < linesPerPage) {
|
|
||||||
int lineWidth = renderer.getTextWidth(cachedFontId, line.c_str());
|
|
||||||
|
|
||||||
if (lineWidth <= viewportWidth) {
|
|
||||||
outLines.push_back(line);
|
|
||||||
lineBytePos = displayLen; // Consumed entire display content
|
|
||||||
line.clear();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find break point
|
|
||||||
size_t breakPos = line.length();
|
|
||||||
while (breakPos > 0 && renderer.getTextWidth(cachedFontId, line.substr(0, breakPos).c_str()) > viewportWidth) {
|
|
||||||
// Try to break at space
|
|
||||||
size_t spacePos = line.rfind(' ', breakPos - 1);
|
|
||||||
if (spacePos != std::string::npos && spacePos > 0) {
|
|
||||||
breakPos = spacePos;
|
|
||||||
} else {
|
|
||||||
// Break at character boundary for UTF-8
|
|
||||||
breakPos--;
|
|
||||||
// Make sure we don't break in the middle of a UTF-8 sequence
|
|
||||||
while (breakPos > 0 && (line[breakPos] & 0xC0) == 0x80) {
|
|
||||||
breakPos--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (breakPos == 0) {
|
|
||||||
breakPos = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
outLines.push_back(line.substr(0, breakPos));
|
|
||||||
|
|
||||||
// Skip space at break point
|
|
||||||
size_t skipChars = breakPos;
|
|
||||||
if (breakPos < line.length() && line[breakPos] == ' ') {
|
|
||||||
skipChars++;
|
|
||||||
}
|
|
||||||
lineBytePos += skipChars;
|
|
||||||
line = line.substr(skipChars);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine how much of the source buffer we consumed
|
|
||||||
if (line.empty()) {
|
|
||||||
// Fully consumed this source line, move past the newline
|
|
||||||
pos = lineEnd + 1;
|
|
||||||
} else {
|
|
||||||
// Partially consumed - page is full mid-line
|
|
||||||
// Move pos to where we stopped in the line (NOT past the line)
|
|
||||||
pos = pos + lineBytePos;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure we make progress even if calculations go wrong
|
|
||||||
if (pos == 0 && !outLines.empty()) {
|
|
||||||
// Fallback: at minimum, consume something to avoid infinite loop
|
|
||||||
pos = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
nextOffset = offset + pos;
|
|
||||||
|
|
||||||
// Make sure we don't go past the file
|
|
||||||
if (nextOffset > fileSize) {
|
|
||||||
nextOffset = fileSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(buffer);
|
|
||||||
|
|
||||||
return !outLines.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::renderScreen() {
|
|
||||||
if (!txt) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize reader if not done
|
|
||||||
if (!initialized) {
|
|
||||||
renderer.clearScreen();
|
|
||||||
renderer.drawCenteredText(UI_12_FONT_ID, 300, "Indexing...", true, EpdFontFamily::BOLD);
|
|
||||||
renderer.displayBuffer();
|
|
||||||
initializeReader();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pageOffsets.empty()) {
|
|
||||||
renderer.clearScreen();
|
|
||||||
renderer.drawCenteredText(UI_12_FONT_ID, 300, "Empty file", true, EpdFontFamily::BOLD);
|
|
||||||
renderer.displayBuffer();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bounds check
|
|
||||||
if (currentPage < 0) currentPage = 0;
|
|
||||||
if (currentPage >= totalPages) currentPage = totalPages - 1;
|
|
||||||
|
|
||||||
// Load current page content
|
|
||||||
size_t offset = pageOffsets[currentPage];
|
|
||||||
size_t nextOffset;
|
|
||||||
currentPageLines.clear();
|
|
||||||
loadPageAtOffset(offset, currentPageLines, nextOffset);
|
|
||||||
|
|
||||||
renderer.clearScreen();
|
|
||||||
renderPage();
|
|
||||||
|
|
||||||
// Save progress
|
|
||||||
saveProgress();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::renderPage() {
|
|
||||||
int orientedMarginTop, orientedMarginRight, orientedMarginBottom, orientedMarginLeft;
|
|
||||||
renderer.getOrientedViewableTRBL(&orientedMarginTop, &orientedMarginRight, &orientedMarginBottom,
|
|
||||||
&orientedMarginLeft);
|
|
||||||
orientedMarginTop += cachedScreenMargin;
|
|
||||||
orientedMarginLeft += cachedScreenMargin;
|
|
||||||
orientedMarginRight += cachedScreenMargin;
|
|
||||||
orientedMarginBottom += statusBarMargin;
|
|
||||||
|
|
||||||
const int lineHeight = renderer.getLineHeight(cachedFontId);
|
|
||||||
const int contentWidth = viewportWidth;
|
|
||||||
|
|
||||||
// Render text lines with alignment
|
|
||||||
auto renderLines = [&]() {
|
|
||||||
int y = orientedMarginTop;
|
|
||||||
for (const auto& line : currentPageLines) {
|
|
||||||
if (!line.empty()) {
|
|
||||||
int x = orientedMarginLeft;
|
|
||||||
|
|
||||||
// Apply text alignment
|
|
||||||
switch (cachedParagraphAlignment) {
|
|
||||||
case CrossPointSettings::LEFT_ALIGN:
|
|
||||||
default:
|
|
||||||
// x already set to left margin
|
|
||||||
break;
|
|
||||||
case CrossPointSettings::CENTER_ALIGN: {
|
|
||||||
int textWidth = renderer.getTextWidth(cachedFontId, line.c_str());
|
|
||||||
x = orientedMarginLeft + (contentWidth - textWidth) / 2;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case CrossPointSettings::RIGHT_ALIGN: {
|
|
||||||
int textWidth = renderer.getTextWidth(cachedFontId, line.c_str());
|
|
||||||
x = orientedMarginLeft + contentWidth - textWidth;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case CrossPointSettings::JUSTIFIED:
|
|
||||||
// For plain text, justified is treated as left-aligned
|
|
||||||
// (true justification would require word spacing adjustments)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
renderer.drawText(cachedFontId, x, y, line.c_str());
|
|
||||||
}
|
|
||||||
y += lineHeight;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// First pass: BW rendering
|
|
||||||
renderLines();
|
|
||||||
renderStatusBar(orientedMarginRight, orientedMarginBottom, orientedMarginLeft);
|
|
||||||
|
|
||||||
if (pagesUntilFullRefresh <= 1) {
|
|
||||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
|
||||||
pagesUntilFullRefresh = SETTINGS.getRefreshFrequency();
|
|
||||||
} else {
|
|
||||||
renderer.displayBuffer();
|
|
||||||
pagesUntilFullRefresh--;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Grayscale rendering pass (for anti-aliased fonts)
|
|
||||||
if (SETTINGS.textAntiAliasing) {
|
|
||||||
// Save BW buffer for restoration after grayscale pass
|
|
||||||
renderer.storeBwBuffer();
|
|
||||||
|
|
||||||
renderer.clearScreen(0x00);
|
|
||||||
renderer.setRenderMode(GfxRenderer::GRAYSCALE_LSB);
|
|
||||||
renderLines();
|
|
||||||
renderer.copyGrayscaleLsbBuffers();
|
|
||||||
|
|
||||||
renderer.clearScreen(0x00);
|
|
||||||
renderer.setRenderMode(GfxRenderer::GRAYSCALE_MSB);
|
|
||||||
renderLines();
|
|
||||||
renderer.copyGrayscaleMsbBuffers();
|
|
||||||
|
|
||||||
renderer.displayGrayBuffer();
|
|
||||||
renderer.setRenderMode(GfxRenderer::BW);
|
|
||||||
|
|
||||||
// Restore BW buffer
|
|
||||||
renderer.restoreBwBuffer();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::renderStatusBar(const int orientedMarginRight, const int orientedMarginBottom,
|
|
||||||
const int orientedMarginLeft) const {
|
|
||||||
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 showTitle = SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::NO_PROGRESS ||
|
|
||||||
SETTINGS.statusBar == CrossPointSettings::STATUS_BAR_MODE::FULL;
|
|
||||||
|
|
||||||
const auto screenHeight = renderer.getScreenHeight();
|
|
||||||
const auto textY = screenHeight - orientedMarginBottom - 4;
|
|
||||||
int progressTextWidth = 0;
|
|
||||||
|
|
||||||
if (showProgress) {
|
|
||||||
const int progress = totalPages > 0 ? (currentPage + 1) * 100 / totalPages : 0;
|
|
||||||
const std::string progressStr =
|
|
||||||
std::to_string(currentPage + 1) + "/" + std::to_string(totalPages) + " " + std::to_string(progress) + "%";
|
|
||||||
progressTextWidth = renderer.getTextWidth(SMALL_FONT_ID, progressStr.c_str());
|
|
||||||
renderer.drawText(SMALL_FONT_ID, renderer.getScreenWidth() - orientedMarginRight - progressTextWidth, textY,
|
|
||||||
progressStr.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (showBattery) {
|
|
||||||
ScreenComponents::drawBattery(renderer, orientedMarginLeft, textY);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (showTitle) {
|
|
||||||
const int titleMarginLeft = 50 + 30 + orientedMarginLeft;
|
|
||||||
const int titleMarginRight = progressTextWidth + 30 + orientedMarginRight;
|
|
||||||
const int availableTextWidth = renderer.getScreenWidth() - titleMarginLeft - titleMarginRight;
|
|
||||||
|
|
||||||
std::string title = txt->getTitle();
|
|
||||||
int 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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::saveProgress() const {
|
|
||||||
FsFile f;
|
|
||||||
if (SdMan.openFileForWrite("TRS", txt->getCachePath() + "/progress.bin", f)) {
|
|
||||||
uint8_t data[4];
|
|
||||||
data[0] = currentPage & 0xFF;
|
|
||||||
data[1] = (currentPage >> 8) & 0xFF;
|
|
||||||
data[2] = 0;
|
|
||||||
data[3] = 0;
|
|
||||||
f.write(data, 4);
|
|
||||||
f.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::loadProgress() {
|
|
||||||
FsFile f;
|
|
||||||
if (SdMan.openFileForRead("TRS", txt->getCachePath() + "/progress.bin", f)) {
|
|
||||||
uint8_t data[4];
|
|
||||||
if (f.read(data, 4) == 4) {
|
|
||||||
currentPage = data[0] + (data[1] << 8);
|
|
||||||
if (currentPage >= totalPages) {
|
|
||||||
currentPage = totalPages - 1;
|
|
||||||
}
|
|
||||||
if (currentPage < 0) {
|
|
||||||
currentPage = 0;
|
|
||||||
}
|
|
||||||
Serial.printf("[%lu] [TRS] Loaded progress: page %d/%d\n", millis(), currentPage, totalPages);
|
|
||||||
}
|
|
||||||
f.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TxtReaderActivity::loadPageIndexCache() {
|
|
||||||
// Cache file format (using serialization module):
|
|
||||||
// - uint32_t: magic "TXTI"
|
|
||||||
// - uint8_t: cache version
|
|
||||||
// - uint32_t: file size (to validate cache)
|
|
||||||
// - int32_t: viewport width
|
|
||||||
// - int32_t: lines per page
|
|
||||||
// - int32_t: font ID (to invalidate cache on font change)
|
|
||||||
// - int32_t: screen margin (to invalidate cache on margin change)
|
|
||||||
// - uint8_t: paragraph alignment (to invalidate cache on alignment change)
|
|
||||||
// - uint32_t: total pages count
|
|
||||||
// - N * uint32_t: page offsets
|
|
||||||
|
|
||||||
std::string cachePath = txt->getCachePath() + "/index.bin";
|
|
||||||
FsFile f;
|
|
||||||
if (!SdMan.openFileForRead("TRS", cachePath, f)) {
|
|
||||||
Serial.printf("[%lu] [TRS] No page index cache found\n", millis());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read and validate header using serialization module
|
|
||||||
uint32_t magic;
|
|
||||||
serialization::readPod(f, magic);
|
|
||||||
if (magic != CACHE_MAGIC) {
|
|
||||||
Serial.printf("[%lu] [TRS] Cache magic mismatch, rebuilding\n", millis());
|
|
||||||
f.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t version;
|
|
||||||
serialization::readPod(f, version);
|
|
||||||
if (version != CACHE_VERSION) {
|
|
||||||
Serial.printf("[%lu] [TRS] Cache version mismatch (%d != %d), rebuilding\n", millis(), version, CACHE_VERSION);
|
|
||||||
f.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t fileSize;
|
|
||||||
serialization::readPod(f, fileSize);
|
|
||||||
if (fileSize != txt->getFileSize()) {
|
|
||||||
Serial.printf("[%lu] [TRS] Cache file size mismatch, rebuilding\n", millis());
|
|
||||||
f.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t cachedWidth;
|
|
||||||
serialization::readPod(f, cachedWidth);
|
|
||||||
if (cachedWidth != viewportWidth) {
|
|
||||||
Serial.printf("[%lu] [TRS] Cache viewport width mismatch, rebuilding\n", millis());
|
|
||||||
f.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t cachedLines;
|
|
||||||
serialization::readPod(f, cachedLines);
|
|
||||||
if (cachedLines != linesPerPage) {
|
|
||||||
Serial.printf("[%lu] [TRS] Cache lines per page mismatch, rebuilding\n", millis());
|
|
||||||
f.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t fontId;
|
|
||||||
serialization::readPod(f, fontId);
|
|
||||||
if (fontId != cachedFontId) {
|
|
||||||
Serial.printf("[%lu] [TRS] Cache font ID mismatch (%d != %d), rebuilding\n", millis(), fontId, cachedFontId);
|
|
||||||
f.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t margin;
|
|
||||||
serialization::readPod(f, margin);
|
|
||||||
if (margin != cachedScreenMargin) {
|
|
||||||
Serial.printf("[%lu] [TRS] Cache screen margin mismatch, rebuilding\n", millis());
|
|
||||||
f.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t alignment;
|
|
||||||
serialization::readPod(f, alignment);
|
|
||||||
if (alignment != cachedParagraphAlignment) {
|
|
||||||
Serial.printf("[%lu] [TRS] Cache paragraph alignment mismatch, rebuilding\n", millis());
|
|
||||||
f.close();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t numPages;
|
|
||||||
serialization::readPod(f, numPages);
|
|
||||||
|
|
||||||
// Read page offsets
|
|
||||||
pageOffsets.clear();
|
|
||||||
pageOffsets.reserve(numPages);
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < numPages; i++) {
|
|
||||||
uint32_t offset;
|
|
||||||
serialization::readPod(f, offset);
|
|
||||||
pageOffsets.push_back(offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
f.close();
|
|
||||||
totalPages = pageOffsets.size();
|
|
||||||
Serial.printf("[%lu] [TRS] Loaded page index cache: %d pages\n", millis(), totalPages);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TxtReaderActivity::savePageIndexCache() const {
|
|
||||||
std::string cachePath = txt->getCachePath() + "/index.bin";
|
|
||||||
FsFile f;
|
|
||||||
if (!SdMan.openFileForWrite("TRS", cachePath, f)) {
|
|
||||||
Serial.printf("[%lu] [TRS] Failed to save page index cache\n", millis());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write header using serialization module
|
|
||||||
serialization::writePod(f, CACHE_MAGIC);
|
|
||||||
serialization::writePod(f, CACHE_VERSION);
|
|
||||||
serialization::writePod(f, static_cast<uint32_t>(txt->getFileSize()));
|
|
||||||
serialization::writePod(f, static_cast<int32_t>(viewportWidth));
|
|
||||||
serialization::writePod(f, static_cast<int32_t>(linesPerPage));
|
|
||||||
serialization::writePod(f, static_cast<int32_t>(cachedFontId));
|
|
||||||
serialization::writePod(f, static_cast<int32_t>(cachedScreenMargin));
|
|
||||||
serialization::writePod(f, cachedParagraphAlignment);
|
|
||||||
serialization::writePod(f, static_cast<uint32_t>(pageOffsets.size()));
|
|
||||||
|
|
||||||
// Write page offsets
|
|
||||||
for (size_t offset : pageOffsets) {
|
|
||||||
serialization::writePod(f, static_cast<uint32_t>(offset));
|
|
||||||
}
|
|
||||||
|
|
||||||
f.close();
|
|
||||||
Serial.printf("[%lu] [TRS] Saved page index cache: %d pages\n", millis(), totalPages);
|
|
||||||
}
|
|
||||||
@ -1,60 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <Txt.h>
|
|
||||||
#include <freertos/FreeRTOS.h>
|
|
||||||
#include <freertos/semphr.h>
|
|
||||||
#include <freertos/task.h>
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "CrossPointSettings.h"
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
|
||||||
|
|
||||||
class TxtReaderActivity final : public ActivityWithSubactivity {
|
|
||||||
std::unique_ptr<Txt> txt;
|
|
||||||
TaskHandle_t displayTaskHandle = nullptr;
|
|
||||||
SemaphoreHandle_t renderingMutex = nullptr;
|
|
||||||
int currentPage = 0;
|
|
||||||
int totalPages = 1;
|
|
||||||
int pagesUntilFullRefresh = 0;
|
|
||||||
bool updateRequired = false;
|
|
||||||
const std::function<void()> onGoBack;
|
|
||||||
const std::function<void()> onGoHome;
|
|
||||||
|
|
||||||
// Streaming text reader - stores file offsets for each page
|
|
||||||
std::vector<size_t> pageOffsets; // File offset for start of each page
|
|
||||||
std::vector<std::string> currentPageLines;
|
|
||||||
int linesPerPage = 0;
|
|
||||||
int viewportWidth = 0;
|
|
||||||
bool initialized = false;
|
|
||||||
|
|
||||||
// Cached settings for cache validation (different fonts/margins require re-indexing)
|
|
||||||
int cachedFontId = 0;
|
|
||||||
int cachedScreenMargin = 0;
|
|
||||||
uint8_t cachedParagraphAlignment = CrossPointSettings::LEFT_ALIGN;
|
|
||||||
|
|
||||||
static void taskTrampoline(void* param);
|
|
||||||
[[noreturn]] void displayTaskLoop();
|
|
||||||
void renderScreen();
|
|
||||||
void renderPage();
|
|
||||||
void renderStatusBar(int orientedMarginRight, int orientedMarginBottom, int orientedMarginLeft) const;
|
|
||||||
|
|
||||||
void initializeReader();
|
|
||||||
bool loadPageAtOffset(size_t offset, std::vector<std::string>& outLines, size_t& nextOffset);
|
|
||||||
void buildPageIndex();
|
|
||||||
bool loadPageIndexCache();
|
|
||||||
void savePageIndexCache() const;
|
|
||||||
void saveProgress() const;
|
|
||||||
void loadProgress();
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit TxtReaderActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, std::unique_ptr<Txt> txt,
|
|
||||||
const std::function<void()>& onGoBack, const std::function<void()>& onGoHome)
|
|
||||||
: ActivityWithSubactivity("TxtReader", renderer, mappedInput),
|
|
||||||
txt(std::move(txt)),
|
|
||||||
onGoBack(onGoBack),
|
|
||||||
onGoHome(onGoHome) {}
|
|
||||||
void onEnter() override;
|
|
||||||
void onExit() override;
|
|
||||||
void loop() override;
|
|
||||||
};
|
|
||||||
@ -11,13 +11,13 @@
|
|||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
#include <SDCardManager.h>
|
#include <SDCardManager.h>
|
||||||
|
|
||||||
#include "CrossPointSettings.h"
|
|
||||||
#include "CrossPointState.h"
|
#include "CrossPointState.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "XtcReaderChapterSelectionActivity.h"
|
#include "XtcReaderChapterSelectionActivity.h"
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
constexpr int pagesPerRefresh = 15;
|
||||||
constexpr unsigned long skipPageMs = 700;
|
constexpr unsigned long skipPageMs = 700;
|
||||||
constexpr unsigned long goHomeMs = 1000;
|
constexpr unsigned long goHomeMs = 1000;
|
||||||
} // namespace
|
} // namespace
|
||||||
@ -112,8 +112,6 @@ void XtcReaderActivity::loop() {
|
|||||||
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::PageBack) ||
|
const bool prevReleased = mappedInput.wasReleased(MappedInputManager::Button::PageBack) ||
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Left);
|
mappedInput.wasReleased(MappedInputManager::Button::Left);
|
||||||
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::PageForward) ||
|
const bool nextReleased = mappedInput.wasReleased(MappedInputManager::Button::PageForward) ||
|
||||||
(SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::PAGE_TURN &&
|
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Power)) ||
|
|
||||||
mappedInput.wasReleased(MappedInputManager::Button::Right);
|
mappedInput.wasReleased(MappedInputManager::Button::Right);
|
||||||
|
|
||||||
if (!prevReleased && !nextReleased) {
|
if (!prevReleased && !nextReleased) {
|
||||||
@ -127,7 +125,7 @@ void XtcReaderActivity::loop() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool skipPages = SETTINGS.longPressChapterSkip && mappedInput.getHeldTime() > skipPageMs;
|
const bool skipPages = mappedInput.getHeldTime() > skipPageMs;
|
||||||
const int skipAmount = skipPages ? 10 : 1;
|
const int skipAmount = skipPages ? 10 : 1;
|
||||||
|
|
||||||
if (prevReleased) {
|
if (prevReleased) {
|
||||||
@ -268,7 +266,7 @@ void XtcReaderActivity::renderPage() {
|
|||||||
// Display BW with conditional refresh based on pagesUntilFullRefresh
|
// Display BW with conditional refresh based on pagesUntilFullRefresh
|
||||||
if (pagesUntilFullRefresh <= 1) {
|
if (pagesUntilFullRefresh <= 1) {
|
||||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
||||||
pagesUntilFullRefresh = SETTINGS.getRefreshFrequency();
|
pagesUntilFullRefresh = pagesPerRefresh;
|
||||||
} else {
|
} else {
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
pagesUntilFullRefresh--;
|
pagesUntilFullRefresh--;
|
||||||
@ -348,7 +346,7 @@ void XtcReaderActivity::renderPage() {
|
|||||||
// Display with appropriate refresh
|
// Display with appropriate refresh
|
||||||
if (pagesUntilFullRefresh <= 1) {
|
if (pagesUntilFullRefresh <= 1) {
|
||||||
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
renderer.displayBuffer(EInkDisplay::HALF_REFRESH);
|
||||||
pagesUntilFullRefresh = SETTINGS.getRefreshFrequency();
|
pagesUntilFullRefresh = pagesPerRefresh;
|
||||||
} else {
|
} else {
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
pagesUntilFullRefresh--;
|
pagesUntilFullRefresh--;
|
||||||
|
|||||||
@ -14,9 +14,7 @@ int XtcReaderChapterSelectionActivity::getPageItems() const {
|
|||||||
constexpr int lineHeight = 30;
|
constexpr int lineHeight = 30;
|
||||||
|
|
||||||
const int screenHeight = renderer.getScreenHeight();
|
const int screenHeight = renderer.getScreenHeight();
|
||||||
const int endY = screenHeight - lineHeight;
|
const int availableHeight = screenHeight - startY;
|
||||||
|
|
||||||
const int availableHeight = endY - startY;
|
|
||||||
int items = availableHeight / lineHeight;
|
int items = availableHeight / lineHeight;
|
||||||
if (items < 1) {
|
if (items < 1) {
|
||||||
items = 1;
|
items = 1;
|
||||||
@ -149,8 +147,5 @@ void XtcReaderChapterSelectionActivity::renderScreen() {
|
|||||||
renderer.drawText(UI_10_FONT_ID, 20, 60 + (i % pageItems) * 30, title, i != selectorIndex);
|
renderer.drawText(UI_10_FONT_ID, 20, 60 + (i % pageItems) * 30, title, i != selectorIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto labels = mappedInput.mapLabels("« Back", "Select", "Up", "Down");
|
|
||||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
||||||
|
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,169 +0,0 @@
|
|||||||
#include "CalibreSettingsActivity.h"
|
|
||||||
|
|
||||||
#include <GfxRenderer.h>
|
|
||||||
#include <WiFi.h>
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
#include "CrossPointSettings.h"
|
|
||||||
#include "MappedInputManager.h"
|
|
||||||
#include "activities/network/CalibreWirelessActivity.h"
|
|
||||||
#include "activities/network/WifiSelectionActivity.h"
|
|
||||||
#include "activities/util/KeyboardEntryActivity.h"
|
|
||||||
#include "fontIds.h"
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
constexpr int MENU_ITEMS = 2;
|
|
||||||
const char* menuNames[MENU_ITEMS] = {"Calibre Web URL", "Connect as Wireless Device"};
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
void CalibreSettingsActivity::taskTrampoline(void* param) {
|
|
||||||
auto* self = static_cast<CalibreSettingsActivity*>(param);
|
|
||||||
self->displayTaskLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreSettingsActivity::onEnter() {
|
|
||||||
ActivityWithSubactivity::onEnter();
|
|
||||||
|
|
||||||
renderingMutex = xSemaphoreCreateMutex();
|
|
||||||
selectedIndex = 0;
|
|
||||||
updateRequired = true;
|
|
||||||
|
|
||||||
xTaskCreate(&CalibreSettingsActivity::taskTrampoline, "CalibreSettingsTask",
|
|
||||||
4096, // Stack size
|
|
||||||
this, // Parameters
|
|
||||||
1, // Priority
|
|
||||||
&displayTaskHandle // Task handle
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreSettingsActivity::onExit() {
|
|
||||||
ActivityWithSubactivity::onExit();
|
|
||||||
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
if (displayTaskHandle) {
|
|
||||||
vTaskDelete(displayTaskHandle);
|
|
||||||
displayTaskHandle = nullptr;
|
|
||||||
}
|
|
||||||
vSemaphoreDelete(renderingMutex);
|
|
||||||
renderingMutex = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreSettingsActivity::loop() {
|
|
||||||
if (subActivity) {
|
|
||||||
subActivity->loop();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
|
|
||||||
onBack();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
|
|
||||||
handleSelection();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mappedInput.wasPressed(MappedInputManager::Button::Up) ||
|
|
||||||
mappedInput.wasPressed(MappedInputManager::Button::Left)) {
|
|
||||||
selectedIndex = (selectedIndex + MENU_ITEMS - 1) % MENU_ITEMS;
|
|
||||||
updateRequired = true;
|
|
||||||
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
|
|
||||||
mappedInput.wasPressed(MappedInputManager::Button::Right)) {
|
|
||||||
selectedIndex = (selectedIndex + 1) % MENU_ITEMS;
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreSettingsActivity::handleSelection() {
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
|
|
||||||
if (selectedIndex == 0) {
|
|
||||||
// Calibre Web URL
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new KeyboardEntryActivity(
|
|
||||||
renderer, mappedInput, "Calibre Web URL", SETTINGS.opdsServerUrl, 10,
|
|
||||||
127, // maxLength
|
|
||||||
false, // not password
|
|
||||||
[this](const std::string& url) {
|
|
||||||
strncpy(SETTINGS.opdsServerUrl, url.c_str(), sizeof(SETTINGS.opdsServerUrl) - 1);
|
|
||||||
SETTINGS.opdsServerUrl[sizeof(SETTINGS.opdsServerUrl) - 1] = '\0';
|
|
||||||
SETTINGS.saveToFile();
|
|
||||||
exitActivity();
|
|
||||||
updateRequired = true;
|
|
||||||
},
|
|
||||||
[this]() {
|
|
||||||
exitActivity();
|
|
||||||
updateRequired = true;
|
|
||||||
}));
|
|
||||||
} else if (selectedIndex == 1) {
|
|
||||||
// Wireless Device - launch the activity (handles WiFi connection internally)
|
|
||||||
exitActivity();
|
|
||||||
if (WiFi.status() != WL_CONNECTED) {
|
|
||||||
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput, [this](bool connected) {
|
|
||||||
exitActivity();
|
|
||||||
if (connected) {
|
|
||||||
enterNewActivity(new CalibreWirelessActivity(renderer, mappedInput, [this] {
|
|
||||||
exitActivity();
|
|
||||||
updateRequired = true;
|
|
||||||
}));
|
|
||||||
} else {
|
|
||||||
updateRequired = true;
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
} else {
|
|
||||||
enterNewActivity(new CalibreWirelessActivity(renderer, mappedInput, [this] {
|
|
||||||
exitActivity();
|
|
||||||
updateRequired = true;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
xSemaphoreGive(renderingMutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreSettingsActivity::displayTaskLoop() {
|
|
||||||
while (true) {
|
|
||||||
if (updateRequired && !subActivity) {
|
|
||||||
updateRequired = false;
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
render();
|
|
||||||
xSemaphoreGive(renderingMutex);
|
|
||||||
}
|
|
||||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CalibreSettingsActivity::render() {
|
|
||||||
renderer.clearScreen();
|
|
||||||
|
|
||||||
const auto pageWidth = renderer.getScreenWidth();
|
|
||||||
|
|
||||||
// Draw header
|
|
||||||
renderer.drawCenteredText(UI_12_FONT_ID, 15, "Calibre", true, EpdFontFamily::BOLD);
|
|
||||||
|
|
||||||
// Draw selection highlight
|
|
||||||
renderer.fillRect(0, 60 + selectedIndex * 30 - 2, pageWidth - 1, 30);
|
|
||||||
|
|
||||||
// Draw menu items
|
|
||||||
for (int i = 0; i < MENU_ITEMS; i++) {
|
|
||||||
const int settingY = 60 + i * 30;
|
|
||||||
const bool isSelected = (i == selectedIndex);
|
|
||||||
|
|
||||||
renderer.drawText(UI_10_FONT_ID, 20, settingY, menuNames[i], !isSelected);
|
|
||||||
|
|
||||||
// Draw status for URL setting
|
|
||||||
if (i == 0) {
|
|
||||||
const char* status = (strlen(SETTINGS.opdsServerUrl) > 0) ? "[Set]" : "[Not Set]";
|
|
||||||
const auto width = renderer.getTextWidth(UI_10_FONT_ID, status);
|
|
||||||
renderer.drawText(UI_10_FONT_ID, pageWidth - 20 - width, settingY, status, !isSelected);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw button hints
|
|
||||||
const auto labels = mappedInput.mapLabels("« Back", "Select", "", "");
|
|
||||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
|
||||||
|
|
||||||
renderer.displayBuffer();
|
|
||||||
}
|
|
||||||
@ -1,36 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <freertos/FreeRTOS.h>
|
|
||||||
#include <freertos/semphr.h>
|
|
||||||
#include <freertos/task.h>
|
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
|
|
||||||
#include "activities/ActivityWithSubactivity.h"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Submenu for Calibre settings.
|
|
||||||
* Shows Calibre Web URL and Calibre Wireless Device options.
|
|
||||||
*/
|
|
||||||
class CalibreSettingsActivity final : public ActivityWithSubactivity {
|
|
||||||
public:
|
|
||||||
explicit CalibreSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
|
||||||
const std::function<void()>& onBack)
|
|
||||||
: ActivityWithSubactivity("CalibreSettings", renderer, mappedInput), onBack(onBack) {}
|
|
||||||
|
|
||||||
void onEnter() override;
|
|
||||||
void onExit() override;
|
|
||||||
void loop() override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
TaskHandle_t displayTaskHandle = nullptr;
|
|
||||||
SemaphoreHandle_t renderingMutex = nullptr;
|
|
||||||
bool updateRequired = false;
|
|
||||||
|
|
||||||
int selectedIndex = 0;
|
|
||||||
const std::function<void()> onBack;
|
|
||||||
|
|
||||||
static void taskTrampoline(void* param);
|
|
||||||
[[noreturn]] void displayTaskLoop();
|
|
||||||
void render();
|
|
||||||
void handleSelection();
|
|
||||||
};
|
|
||||||
@ -41,5 +41,4 @@ class OtaUpdateActivity : public ActivityWithSubactivity {
|
|||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
void loop() override;
|
void loop() override;
|
||||||
bool preventAutoSleep() override { return state == CHECKING_FOR_UPDATE || state == UPDATE_IN_PROGRESS; }
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,7 @@
|
|||||||
#include "SettingsActivity.h"
|
#include "SettingsActivity.h"
|
||||||
|
|
||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
#include <HardwareSerial.h>
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
#include "CalibreSettingsActivity.h"
|
|
||||||
#include "CrossPointSettings.h"
|
#include "CrossPointSettings.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "OtaUpdateActivity.h"
|
#include "OtaUpdateActivity.h"
|
||||||
@ -13,36 +9,36 @@
|
|||||||
|
|
||||||
// Define the static settings list
|
// Define the static settings list
|
||||||
namespace {
|
namespace {
|
||||||
constexpr int settingsCount = 20;
|
constexpr int settingsCount = 11;
|
||||||
const SettingInfo settingsList[settingsCount] = {
|
const SettingInfo settingsList[settingsCount] = {
|
||||||
// Should match with SLEEP_SCREEN_MODE
|
// Should match with SLEEP_SCREEN_MODE
|
||||||
SettingInfo::Enum("Sleep Screen", &CrossPointSettings::sleepScreen, {"Dark", "Light", "Custom", "Cover", "None"}),
|
{"Sleep Screen", SettingType::ENUM, &CrossPointSettings::sleepScreen, {"Dark", "Light", "Custom", "Cover"}},
|
||||||
SettingInfo::Enum("Sleep Screen Cover Mode", &CrossPointSettings::sleepScreenCoverMode, {"Fit", "Crop"}),
|
{"Status Bar", SettingType::ENUM, &CrossPointSettings::statusBar, {"None", "No Progress", "Full"}},
|
||||||
SettingInfo::Enum("Status Bar", &CrossPointSettings::statusBar, {"None", "No Progress", "Full"}),
|
{"Extra Paragraph Spacing", SettingType::TOGGLE, &CrossPointSettings::extraParagraphSpacing, {}},
|
||||||
SettingInfo::Enum("Hide Battery %", &CrossPointSettings::hideBatteryPercentage, {"Never", "In Reader", "Always"}),
|
{"Short Power Button Click", SettingType::TOGGLE, &CrossPointSettings::shortPwrBtn, {}},
|
||||||
SettingInfo::Toggle("Extra Paragraph Spacing", &CrossPointSettings::extraParagraphSpacing),
|
{"Reading Orientation",
|
||||||
SettingInfo::Toggle("Text Anti-Aliasing", &CrossPointSettings::textAntiAliasing),
|
SettingType::ENUM,
|
||||||
SettingInfo::Enum("Short Power Button Click", &CrossPointSettings::shortPwrBtn, {"Ignore", "Sleep", "Page Turn"}),
|
&CrossPointSettings::orientation,
|
||||||
SettingInfo::Enum("Reading Orientation", &CrossPointSettings::orientation,
|
{"Portrait", "Landscape CW", "Inverted", "Landscape CCW"}},
|
||||||
{"Portrait", "Landscape CW", "Inverted", "Landscape CCW"}),
|
{"Front Button Layout",
|
||||||
SettingInfo::Enum("Front Button Layout", &CrossPointSettings::frontButtonLayout,
|
SettingType::ENUM,
|
||||||
{"Bck, Cnfrm, Lft, Rght", "Lft, Rght, Bck, Cnfrm", "Lft, Bck, Cnfrm, Rght"}),
|
&CrossPointSettings::frontButtonLayout,
|
||||||
SettingInfo::Enum("Side Button Layout (reader)", &CrossPointSettings::sideButtonLayout,
|
{"Bck, Cnfrm, Lft, Rght", "Lft, Rght, Bck, Cnfrm", "Lft, Bck, Cnfrm, Rght"}},
|
||||||
{"Prev, Next", "Next, Prev"}),
|
{"Side Button Layout (reader)",
|
||||||
SettingInfo::Toggle("Long-press Chapter Skip", &CrossPointSettings::longPressChapterSkip),
|
SettingType::ENUM,
|
||||||
SettingInfo::Enum("Reader Font Family", &CrossPointSettings::fontFamily,
|
&CrossPointSettings::sideButtonLayout,
|
||||||
{"Bookerly", "Noto Sans", "Open Dyslexic"}),
|
{"Prev, Next", "Next, Prev"}},
|
||||||
SettingInfo::Enum("Reader Font Size", &CrossPointSettings::fontSize, {"Small", "Medium", "Large", "X Large", "X Small"}),
|
{"Reader Font Family",
|
||||||
SettingInfo::Enum("Reader Line Spacing", &CrossPointSettings::lineSpacing, {"Tight", "Normal", "Wide"}),
|
SettingType::ENUM,
|
||||||
SettingInfo::Value("Reader Screen Margin", &CrossPointSettings::screenMargin, {5, 40, 5}),
|
&CrossPointSettings::fontFamily,
|
||||||
SettingInfo::Enum("Reader Paragraph Alignment", &CrossPointSettings::paragraphAlignment,
|
{"Bookerly", "Noto Sans", "Open Dyslexic"}},
|
||||||
{"Justify", "Left", "Center", "Right"}),
|
{"Reader Font Size",
|
||||||
SettingInfo::Enum("Time to Sleep", &CrossPointSettings::sleepTimeout,
|
SettingType::ENUM,
|
||||||
{"1 min", "5 min", "10 min", "15 min", "30 min"}),
|
&CrossPointSettings::fontSize,
|
||||||
SettingInfo::Enum("Refresh Frequency", &CrossPointSettings::refreshFrequency,
|
{"Small", "Medium", "Large", "X Large", "X Small"}},
|
||||||
{"1 page", "5 pages", "10 pages", "15 pages", "30 pages"}),
|
{"Reader Line Spacing", SettingType::ENUM, &CrossPointSettings::lineSpacing, {"Tight", "Normal", "Wide"}},
|
||||||
SettingInfo::Action("Calibre Settings"),
|
{"Check for updates", SettingType::ACTION, nullptr, {}},
|
||||||
SettingInfo::Action("Check for updates")};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void SettingsActivity::taskTrampoline(void* param) {
|
void SettingsActivity::taskTrampoline(void* param) {
|
||||||
@ -52,6 +48,7 @@ void SettingsActivity::taskTrampoline(void* param) {
|
|||||||
|
|
||||||
void SettingsActivity::onEnter() {
|
void SettingsActivity::onEnter() {
|
||||||
Activity::onEnter();
|
Activity::onEnter();
|
||||||
|
|
||||||
renderingMutex = xSemaphoreCreateMutex();
|
renderingMutex = xSemaphoreCreateMutex();
|
||||||
|
|
||||||
// Reset selection to first item
|
// Reset selection to first item
|
||||||
@ -61,7 +58,7 @@ void SettingsActivity::onEnter() {
|
|||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
|
|
||||||
xTaskCreate(&SettingsActivity::taskTrampoline, "SettingsActivityTask",
|
xTaskCreate(&SettingsActivity::taskTrampoline, "SettingsActivityTask",
|
||||||
4096, // Stack size
|
2048, // Stack size
|
||||||
this, // Parameters
|
this, // Parameters
|
||||||
1, // Priority
|
1, // Priority
|
||||||
&displayTaskHandle // Task handle
|
&displayTaskHandle // Task handle
|
||||||
@ -108,11 +105,13 @@ void SettingsActivity::loop() {
|
|||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
|
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
|
||||||
mappedInput.wasPressed(MappedInputManager::Button::Right)) {
|
mappedInput.wasPressed(MappedInputManager::Button::Right)) {
|
||||||
// Move selection down (with wrap around)
|
// Move selection down
|
||||||
selectedSettingIndex = (selectedSettingIndex < settingsCount - 1) ? (selectedSettingIndex + 1) : 0;
|
if (selectedSettingIndex < settingsCount - 1) {
|
||||||
|
selectedSettingIndex++;
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SettingsActivity::toggleCurrentSetting() {
|
void SettingsActivity::toggleCurrentSetting() {
|
||||||
// Validate index
|
// Validate index
|
||||||
@ -129,25 +128,8 @@ void SettingsActivity::toggleCurrentSetting() {
|
|||||||
} else if (setting.type == SettingType::ENUM && setting.valuePtr != nullptr) {
|
} else if (setting.type == SettingType::ENUM && setting.valuePtr != nullptr) {
|
||||||
const uint8_t currentValue = SETTINGS.*(setting.valuePtr);
|
const uint8_t currentValue = SETTINGS.*(setting.valuePtr);
|
||||||
SETTINGS.*(setting.valuePtr) = (currentValue + 1) % static_cast<uint8_t>(setting.enumValues.size());
|
SETTINGS.*(setting.valuePtr) = (currentValue + 1) % static_cast<uint8_t>(setting.enumValues.size());
|
||||||
} else if (setting.type == SettingType::VALUE && setting.valuePtr != nullptr) {
|
|
||||||
// Decreasing would also be nice for large ranges I think but oh well can't have everything
|
|
||||||
const int8_t currentValue = SETTINGS.*(setting.valuePtr);
|
|
||||||
// Wrap to minValue if exceeding setting value boundary
|
|
||||||
if (currentValue + setting.valueRange.step > setting.valueRange.max) {
|
|
||||||
SETTINGS.*(setting.valuePtr) = setting.valueRange.min;
|
|
||||||
} else {
|
|
||||||
SETTINGS.*(setting.valuePtr) = currentValue + setting.valueRange.step;
|
|
||||||
}
|
|
||||||
} else if (setting.type == SettingType::ACTION) {
|
} else if (setting.type == SettingType::ACTION) {
|
||||||
if (strcmp(setting.name, "Calibre Settings") == 0) {
|
if (std::string(setting.name) == "Check for updates") {
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new CalibreSettingsActivity(renderer, mappedInput, [this] {
|
|
||||||
exitActivity();
|
|
||||||
updateRequired = true;
|
|
||||||
}));
|
|
||||||
xSemaphoreGive(renderingMutex);
|
|
||||||
} else if (strcmp(setting.name, "Check for updates") == 0) {
|
|
||||||
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
xSemaphoreTake(renderingMutex, portMAX_DELAY);
|
||||||
exitActivity();
|
exitActivity();
|
||||||
enterNewActivity(new OtaUpdateActivity(renderer, mappedInput, [this] {
|
enterNewActivity(new OtaUpdateActivity(renderer, mappedInput, [this] {
|
||||||
@ -204,8 +186,6 @@ void SettingsActivity::render() const {
|
|||||||
} else if (settingsList[i].type == SettingType::ENUM && settingsList[i].valuePtr != nullptr) {
|
} else if (settingsList[i].type == SettingType::ENUM && settingsList[i].valuePtr != nullptr) {
|
||||||
const uint8_t value = SETTINGS.*(settingsList[i].valuePtr);
|
const uint8_t value = SETTINGS.*(settingsList[i].valuePtr);
|
||||||
valueText = settingsList[i].enumValues[value];
|
valueText = settingsList[i].enumValues[value];
|
||||||
} else if (settingsList[i].type == SettingType::VALUE && settingsList[i].valuePtr != nullptr) {
|
|
||||||
valueText = std::to_string(SETTINGS.*(settingsList[i].valuePtr));
|
|
||||||
}
|
}
|
||||||
const auto width = renderer.getTextWidth(UI_10_FONT_ID, valueText.c_str());
|
const auto width = renderer.getTextWidth(UI_10_FONT_ID, valueText.c_str());
|
||||||
renderer.drawText(UI_10_FONT_ID, pageWidth - 20 - width, settingY, valueText.c_str(), i != selectedSettingIndex);
|
renderer.drawText(UI_10_FONT_ID, pageWidth - 20 - width, settingY, valueText.c_str(), i != selectedSettingIndex);
|
||||||
|
|||||||
@ -11,37 +11,14 @@
|
|||||||
|
|
||||||
class CrossPointSettings;
|
class CrossPointSettings;
|
||||||
|
|
||||||
enum class SettingType { TOGGLE, ENUM, ACTION, VALUE };
|
enum class SettingType { TOGGLE, ENUM, ACTION };
|
||||||
|
|
||||||
// Structure to hold setting information
|
// Structure to hold setting information
|
||||||
struct SettingInfo {
|
struct SettingInfo {
|
||||||
const char* name; // Display name of the setting
|
const char* name; // Display name of the setting
|
||||||
SettingType type; // Type of setting
|
SettingType type; // Type of setting
|
||||||
uint8_t CrossPointSettings::* valuePtr; // Pointer to member in CrossPointSettings (for TOGGLE/ENUM/VALUE)
|
uint8_t CrossPointSettings::* valuePtr; // Pointer to member in CrossPointSettings (for TOGGLE/ENUM)
|
||||||
std::vector<std::string> enumValues;
|
std::vector<std::string> enumValues;
|
||||||
|
|
||||||
struct ValueRange {
|
|
||||||
uint8_t min;
|
|
||||||
uint8_t max;
|
|
||||||
uint8_t step;
|
|
||||||
};
|
|
||||||
// Bounds/step for VALUE type settings
|
|
||||||
ValueRange valueRange;
|
|
||||||
|
|
||||||
// Static constructors
|
|
||||||
static SettingInfo Toggle(const char* name, uint8_t CrossPointSettings::* ptr) {
|
|
||||||
return {name, SettingType::TOGGLE, ptr};
|
|
||||||
}
|
|
||||||
|
|
||||||
static SettingInfo Enum(const char* name, uint8_t CrossPointSettings::* ptr, std::vector<std::string> values) {
|
|
||||||
return {name, SettingType::ENUM, ptr, std::move(values)};
|
|
||||||
}
|
|
||||||
|
|
||||||
static SettingInfo Action(const char* name) { return {name, SettingType::ACTION, nullptr}; }
|
|
||||||
|
|
||||||
static SettingInfo Value(const char* name, uint8_t CrossPointSettings::* ptr, const ValueRange valueRange) {
|
|
||||||
return {name, SettingType::VALUE, ptr, {}, valueRange};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class SettingsActivity final : public ActivityWithSubactivity {
|
class SettingsActivity final : public ActivityWithSubactivity {
|
||||||
|
|||||||
@ -329,13 +329,9 @@ void KeyboardEntryActivity::render() const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw help text
|
// Draw help text at absolute bottom of screen (consistent with other screens)
|
||||||
const auto labels = mappedInput.mapLabels("« Back", "Select", "Left", "Right");
|
const auto pageHeight = renderer.getScreenHeight();
|
||||||
renderer.drawButtonHints(UI_10_FONT_ID, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
|
renderer.drawText(SMALL_FONT_ID, 10, pageHeight - 30, "Navigate: D-pad | Select: OK | Cancel: BACK");
|
||||||
|
|
||||||
// Draw side button hints for Up/Down navigation
|
|
||||||
renderer.drawSideButtonHints(UI_10_FONT_ID, "Up", "Down");
|
|
||||||
|
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
47
src/main.cpp
47
src/main.cpp
@ -7,15 +7,12 @@
|
|||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <builtinFonts/all.h>
|
#include <builtinFonts/all.h>
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
#include "Battery.h"
|
#include "Battery.h"
|
||||||
#include "CrossPointSettings.h"
|
#include "CrossPointSettings.h"
|
||||||
#include "CrossPointState.h"
|
#include "CrossPointState.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
#include "activities/boot_sleep/BootActivity.h"
|
#include "activities/boot_sleep/BootActivity.h"
|
||||||
#include "activities/boot_sleep/SleepActivity.h"
|
#include "activities/boot_sleep/SleepActivity.h"
|
||||||
#include "activities/browser/OpdsBookBrowserActivity.h"
|
|
||||||
#include "activities/home/HomeActivity.h"
|
#include "activities/home/HomeActivity.h"
|
||||||
#include "activities/network/CrossPointWebServerActivity.h"
|
#include "activities/network/CrossPointWebServerActivity.h"
|
||||||
#include "activities/reader/ReaderActivity.h"
|
#include "activities/reader/ReaderActivity.h"
|
||||||
@ -43,13 +40,6 @@ GfxRenderer renderer(einkDisplay);
|
|||||||
Activity* currentActivity;
|
Activity* currentActivity;
|
||||||
|
|
||||||
// Fonts
|
// Fonts
|
||||||
EpdFont bookerly14RegularFont(&bookerly_14_regular);
|
|
||||||
EpdFont bookerly14BoldFont(&bookerly_14_bold);
|
|
||||||
EpdFont bookerly14ItalicFont(&bookerly_14_italic);
|
|
||||||
EpdFont bookerly14BoldItalicFont(&bookerly_14_bolditalic);
|
|
||||||
EpdFontFamily bookerly14FontFamily(&bookerly14RegularFont, &bookerly14BoldFont, &bookerly14ItalicFont,
|
|
||||||
&bookerly14BoldItalicFont);
|
|
||||||
#ifndef OMIT_FONTS
|
|
||||||
EpdFont bookerly10RegularFont(&bookerly_10_regular);
|
EpdFont bookerly10RegularFont(&bookerly_10_regular);
|
||||||
EpdFont bookerly10BoldFont(&bookerly_10_bold);
|
EpdFont bookerly10BoldFont(&bookerly_10_bold);
|
||||||
EpdFont bookerly10ItalicFont(&bookerly_10_italic);
|
EpdFont bookerly10ItalicFont(&bookerly_10_italic);
|
||||||
@ -62,6 +52,12 @@ EpdFont bookerly12ItalicFont(&bookerly_12_italic);
|
|||||||
EpdFont bookerly12BoldItalicFont(&bookerly_12_bolditalic);
|
EpdFont bookerly12BoldItalicFont(&bookerly_12_bolditalic);
|
||||||
EpdFontFamily bookerly12FontFamily(&bookerly12RegularFont, &bookerly12BoldFont, &bookerly12ItalicFont,
|
EpdFontFamily bookerly12FontFamily(&bookerly12RegularFont, &bookerly12BoldFont, &bookerly12ItalicFont,
|
||||||
&bookerly12BoldItalicFont);
|
&bookerly12BoldItalicFont);
|
||||||
|
EpdFont bookerly14RegularFont(&bookerly_14_regular);
|
||||||
|
EpdFont bookerly14BoldFont(&bookerly_14_bold);
|
||||||
|
EpdFont bookerly14ItalicFont(&bookerly_14_italic);
|
||||||
|
EpdFont bookerly14BoldItalicFont(&bookerly_14_bolditalic);
|
||||||
|
EpdFontFamily bookerly14FontFamily(&bookerly14RegularFont, &bookerly14BoldFont, &bookerly14ItalicFont,
|
||||||
|
&bookerly14BoldItalicFont);
|
||||||
EpdFont bookerly16RegularFont(&bookerly_16_regular);
|
EpdFont bookerly16RegularFont(&bookerly_16_regular);
|
||||||
EpdFont bookerly16BoldFont(&bookerly_16_bold);
|
EpdFont bookerly16BoldFont(&bookerly_16_bold);
|
||||||
EpdFont bookerly16ItalicFont(&bookerly_16_italic);
|
EpdFont bookerly16ItalicFont(&bookerly_16_italic);
|
||||||
@ -136,7 +132,6 @@ EpdFont opendyslexic14ItalicFont(&opendyslexic_14_italic);
|
|||||||
EpdFont opendyslexic14BoldItalicFont(&opendyslexic_14_bolditalic);
|
EpdFont opendyslexic14BoldItalicFont(&opendyslexic_14_bolditalic);
|
||||||
EpdFontFamily opendyslexic14FontFamily(&opendyslexic14RegularFont, &opendyslexic14BoldFont, &opendyslexic14ItalicFont,
|
EpdFontFamily opendyslexic14FontFamily(&opendyslexic14RegularFont, &opendyslexic14BoldFont, &opendyslexic14ItalicFont,
|
||||||
&opendyslexic14BoldItalicFont);
|
&opendyslexic14BoldItalicFont);
|
||||||
#endif // OMIT_FONTS
|
|
||||||
|
|
||||||
EpdFont smallFont(¬osans_8_regular);
|
EpdFont smallFont(¬osans_8_regular);
|
||||||
EpdFontFamily smallFontFamily(&smallFont);
|
EpdFontFamily smallFontFamily(&smallFont);
|
||||||
@ -149,6 +144,8 @@ EpdFont ui12RegularFont(&ubuntu_12_regular);
|
|||||||
EpdFont ui12BoldFont(&ubuntu_12_bold);
|
EpdFont ui12BoldFont(&ubuntu_12_bold);
|
||||||
EpdFontFamily ui12FontFamily(&ui12RegularFont, &ui12BoldFont);
|
EpdFontFamily ui12FontFamily(&ui12RegularFont, &ui12BoldFont);
|
||||||
|
|
||||||
|
// 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
|
// measurement of power button press duration calibration value
|
||||||
unsigned long t1 = 0;
|
unsigned long t1 = 0;
|
||||||
unsigned long t2 = 0;
|
unsigned long t2 = 0;
|
||||||
@ -171,10 +168,8 @@ void verifyWakeupLongPress() {
|
|||||||
// Give the user up to 1000ms to start holding the power button, and must hold for SETTINGS.getPowerButtonDuration()
|
// Give the user up to 1000ms to start holding the power button, and must hold for SETTINGS.getPowerButtonDuration()
|
||||||
const auto start = millis();
|
const auto start = millis();
|
||||||
bool abort = false;
|
bool abort = false;
|
||||||
// Subtract the current time, because inputManager only starts counting the HeldTime from the first update()
|
// It takes us some time to wake up from deep sleep, so we need to subtract that from the duration
|
||||||
// This way, we remove the time we already took to reach here from the duration,
|
constexpr uint16_t calibration = 29;
|
||||||
// assuming the button was held until now from millis()==0 (i.e. device start time).
|
|
||||||
const uint16_t calibration = start;
|
|
||||||
const uint16_t calibratedPressDuration =
|
const uint16_t calibratedPressDuration =
|
||||||
(calibration < SETTINGS.getPowerButtonDuration()) ? SETTINGS.getPowerButtonDuration() - calibration : 1;
|
(calibration < SETTINGS.getPowerButtonDuration()) ? SETTINGS.getPowerButtonDuration() - calibration : 1;
|
||||||
|
|
||||||
@ -245,24 +240,18 @@ void onGoToSettings() {
|
|||||||
enterNewActivity(new SettingsActivity(renderer, mappedInputManager, onGoHome));
|
enterNewActivity(new SettingsActivity(renderer, mappedInputManager, onGoHome));
|
||||||
}
|
}
|
||||||
|
|
||||||
void onGoToBrowser() {
|
|
||||||
exitActivity();
|
|
||||||
enterNewActivity(new OpdsBookBrowserActivity(renderer, mappedInputManager, onGoHome));
|
|
||||||
}
|
|
||||||
|
|
||||||
void onGoHome() {
|
void onGoHome() {
|
||||||
exitActivity();
|
exitActivity();
|
||||||
enterNewActivity(new HomeActivity(renderer, mappedInputManager, onContinueReading, onGoToReaderHome, onGoToSettings,
|
enterNewActivity(new HomeActivity(renderer, mappedInputManager, onContinueReading, onGoToReaderHome, onGoToSettings,
|
||||||
onGoToFileTransfer, onGoToBrowser));
|
onGoToFileTransfer));
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupDisplayAndFonts() {
|
void setupDisplayAndFonts() {
|
||||||
einkDisplay.begin();
|
einkDisplay.begin();
|
||||||
Serial.printf("[%lu] [ ] Display initialized\n", millis());
|
Serial.printf("[%lu] [ ] Display initialized\n", millis());
|
||||||
renderer.insertFont(BOOKERLY_14_FONT_ID, bookerly14FontFamily);
|
|
||||||
#ifndef OMIT_FONTS
|
|
||||||
renderer.insertFont(BOOKERLY_10_FONT_ID, bookerly10FontFamily);
|
renderer.insertFont(BOOKERLY_10_FONT_ID, bookerly10FontFamily);
|
||||||
renderer.insertFont(BOOKERLY_12_FONT_ID, bookerly12FontFamily);
|
renderer.insertFont(BOOKERLY_12_FONT_ID, bookerly12FontFamily);
|
||||||
|
renderer.insertFont(BOOKERLY_14_FONT_ID, bookerly14FontFamily);
|
||||||
renderer.insertFont(BOOKERLY_16_FONT_ID, bookerly16FontFamily);
|
renderer.insertFont(BOOKERLY_16_FONT_ID, bookerly16FontFamily);
|
||||||
renderer.insertFont(BOOKERLY_18_FONT_ID, bookerly18FontFamily);
|
renderer.insertFont(BOOKERLY_18_FONT_ID, bookerly18FontFamily);
|
||||||
renderer.insertFont(NOTOSANS_10_FONT_ID, notosans10FontFamily);
|
renderer.insertFont(NOTOSANS_10_FONT_ID, notosans10FontFamily);
|
||||||
@ -275,7 +264,6 @@ void setupDisplayAndFonts() {
|
|||||||
renderer.insertFont(OPENDYSLEXIC_10_FONT_ID, opendyslexic10FontFamily);
|
renderer.insertFont(OPENDYSLEXIC_10_FONT_ID, opendyslexic10FontFamily);
|
||||||
renderer.insertFont(OPENDYSLEXIC_12_FONT_ID, opendyslexic12FontFamily);
|
renderer.insertFont(OPENDYSLEXIC_12_FONT_ID, opendyslexic12FontFamily);
|
||||||
renderer.insertFont(OPENDYSLEXIC_14_FONT_ID, opendyslexic14FontFamily);
|
renderer.insertFont(OPENDYSLEXIC_14_FONT_ID, opendyslexic14FontFamily);
|
||||||
#endif // OMIT_FONTS
|
|
||||||
renderer.insertFont(UI_10_FONT_ID, ui10FontFamily);
|
renderer.insertFont(UI_10_FONT_ID, ui10FontFamily);
|
||||||
renderer.insertFont(UI_12_FONT_ID, ui12FontFamily);
|
renderer.insertFont(UI_12_FONT_ID, ui12FontFamily);
|
||||||
renderer.insertFont(SMALL_FONT_ID, smallFontFamily);
|
renderer.insertFont(SMALL_FONT_ID, smallFontFamily);
|
||||||
@ -328,7 +316,6 @@ void setup() {
|
|||||||
// Clear app state to avoid getting into a boot loop if the epub doesn't load
|
// Clear app state to avoid getting into a boot loop if the epub doesn't load
|
||||||
const auto path = APP_STATE.openEpubPath;
|
const auto path = APP_STATE.openEpubPath;
|
||||||
APP_STATE.openEpubPath = "";
|
APP_STATE.openEpubPath = "";
|
||||||
APP_STATE.lastSleepImage = 0;
|
|
||||||
APP_STATE.saveToFile();
|
APP_STATE.saveToFile();
|
||||||
onGoToReader(path);
|
onGoToReader(path);
|
||||||
}
|
}
|
||||||
@ -350,16 +337,14 @@ void loop() {
|
|||||||
lastMemPrint = millis();
|
lastMemPrint = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for any user activity (button press or release) or active background work
|
// Check for any user activity (button press or release)
|
||||||
static unsigned long lastActivityTime = millis();
|
static unsigned long lastActivityTime = millis();
|
||||||
if (inputManager.wasAnyPressed() || inputManager.wasAnyReleased() ||
|
if (inputManager.wasAnyPressed() || inputManager.wasAnyReleased()) {
|
||||||
(currentActivity && currentActivity->preventAutoSleep())) {
|
|
||||||
lastActivityTime = millis(); // Reset inactivity timer
|
lastActivityTime = millis(); // Reset inactivity timer
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned long sleepTimeoutMs = SETTINGS.getSleepTimeoutMs();
|
if (millis() - lastActivityTime >= AUTO_SLEEP_TIMEOUT_MS) {
|
||||||
if (millis() - lastActivityTime >= sleepTimeoutMs) {
|
Serial.printf("[%lu] [SLP] Auto-sleep triggered after %lu ms of inactivity\n", millis(), AUTO_SLEEP_TIMEOUT_MS);
|
||||||
Serial.printf("[%lu] [SLP] Auto-sleep triggered after %lu ms of inactivity\n", millis(), sleepTimeoutMs);
|
|
||||||
enterDeepSleep();
|
enterDeepSleep();
|
||||||
// This should never be hit as `enterDeepSleep` calls esp_deep_sleep_start
|
// This should never be hit as `enterDeepSleep` calls esp_deep_sleep_start
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -4,7 +4,6 @@
|
|||||||
#include <FsHelpers.h>
|
#include <FsHelpers.h>
|
||||||
#include <SDCardManager.h>
|
#include <SDCardManager.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <esp_task_wdt.h>
|
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
@ -16,18 +15,6 @@ namespace {
|
|||||||
// Note: Items starting with "." are automatically hidden
|
// Note: Items starting with "." are automatically hidden
|
||||||
const char* HIDDEN_ITEMS[] = {"System Volume Information", "XTCache"};
|
const char* HIDDEN_ITEMS[] = {"System Volume Information", "XTCache"};
|
||||||
constexpr size_t HIDDEN_ITEMS_COUNT = sizeof(HIDDEN_ITEMS) / sizeof(HIDDEN_ITEMS[0]);
|
constexpr size_t HIDDEN_ITEMS_COUNT = sizeof(HIDDEN_ITEMS) / sizeof(HIDDEN_ITEMS[0]);
|
||||||
|
|
||||||
// Static pointer for WebSocket callback (WebSocketsServer requires C-style callback)
|
|
||||||
CrossPointWebServer* wsInstance = nullptr;
|
|
||||||
|
|
||||||
// WebSocket upload state
|
|
||||||
FsFile wsUploadFile;
|
|
||||||
String wsUploadFileName;
|
|
||||||
String wsUploadPath;
|
|
||||||
size_t wsUploadSize = 0;
|
|
||||||
size_t wsUploadReceived = 0;
|
|
||||||
unsigned long wsUploadStartTime = 0;
|
|
||||||
bool wsUploadInProgress = false;
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
// File listing page template - now using generated headers:
|
// File listing page template - now using generated headers:
|
||||||
@ -99,22 +86,12 @@ void CrossPointWebServer::begin() {
|
|||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after route setup: %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [MEM] Free heap after route setup: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
|
|
||||||
server->begin();
|
server->begin();
|
||||||
|
|
||||||
// Start WebSocket server for fast binary uploads
|
|
||||||
Serial.printf("[%lu] [WEB] Starting WebSocket server on port %d...\n", millis(), wsPort);
|
|
||||||
wsServer.reset(new WebSocketsServer(wsPort));
|
|
||||||
wsInstance = const_cast<CrossPointWebServer*>(this);
|
|
||||||
wsServer->begin();
|
|
||||||
wsServer->onEvent(wsEventCallback);
|
|
||||||
Serial.printf("[%lu] [WEB] WebSocket server started\n", millis());
|
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEB] Web server started on port %d\n", millis(), port);
|
Serial.printf("[%lu] [WEB] Web server started on port %d\n", millis(), port);
|
||||||
// Show the correct IP based on network mode
|
// Show the correct IP based on network mode
|
||||||
const String ipAddr = apMode ? WiFi.softAPIP().toString() : WiFi.localIP().toString();
|
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] Access at http://%s/\n", millis(), ipAddr.c_str());
|
||||||
Serial.printf("[%lu] [WEB] WebSocket at ws://%s:%d/\n", millis(), ipAddr.c_str(), wsPort);
|
|
||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after server.begin(): %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [MEM] Free heap after server.begin(): %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -130,29 +107,16 @@ void CrossPointWebServer::stop() {
|
|||||||
|
|
||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap before stop: %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [MEM] Free heap before stop: %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
|
|
||||||
// Close any in-progress WebSocket upload
|
// Add delay to allow any in-flight handleClient() calls to complete
|
||||||
if (wsUploadInProgress && wsUploadFile) {
|
delay(100);
|
||||||
wsUploadFile.close();
|
Serial.printf("[%lu] [WEB] Waited 100ms for handleClient to finish\n", millis());
|
||||||
wsUploadInProgress = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop WebSocket server
|
|
||||||
if (wsServer) {
|
|
||||||
Serial.printf("[%lu] [WEB] Stopping WebSocket server...\n", millis());
|
|
||||||
wsServer->close();
|
|
||||||
wsServer.reset();
|
|
||||||
wsInstance = nullptr;
|
|
||||||
Serial.printf("[%lu] [WEB] WebSocket server stopped\n", millis());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Brief delay to allow any in-flight handleClient() calls to complete
|
|
||||||
delay(20);
|
|
||||||
|
|
||||||
server->stop();
|
server->stop();
|
||||||
Serial.printf("[%lu] [WEB] [MEM] Free heap after server->stop(): %d bytes\n", millis(), ESP.getFreeHeap());
|
Serial.printf("[%lu] [WEB] [MEM] Free heap after server->stop(): %d bytes\n", millis(), ESP.getFreeHeap());
|
||||||
|
|
||||||
// Brief delay before deletion
|
// Add another delay before deletion to ensure server->stop() completes
|
||||||
delay(10);
|
delay(50);
|
||||||
|
Serial.printf("[%lu] [WEB] Waited 50ms before deleting server\n", millis());
|
||||||
|
|
||||||
server.reset();
|
server.reset();
|
||||||
Serial.printf("[%lu] [WEB] Web server stopped and deleted\n", millis());
|
Serial.printf("[%lu] [WEB] Web server stopped and deleted\n", millis());
|
||||||
@ -184,11 +148,6 @@ void CrossPointWebServer::handleClient() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
server->handleClient();
|
server->handleClient();
|
||||||
|
|
||||||
// Handle WebSocket events
|
|
||||||
if (wsServer) {
|
|
||||||
wsServer->loop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CrossPointWebServer::handleRoot() const {
|
void CrossPointWebServer::handleRoot() const {
|
||||||
@ -235,7 +194,7 @@ void CrossPointWebServer::scanFiles(const char* path, const std::function<void(F
|
|||||||
Serial.printf("[%lu] [WEB] Scanning files in: %s\n", millis(), path);
|
Serial.printf("[%lu] [WEB] Scanning files in: %s\n", millis(), path);
|
||||||
|
|
||||||
FsFile file = root.openNextFile();
|
FsFile file = root.openNextFile();
|
||||||
char name[500];
|
char name[128];
|
||||||
while (file) {
|
while (file) {
|
||||||
file.getName(name, sizeof(name));
|
file.getName(name, sizeof(name));
|
||||||
auto fileName = String(name);
|
auto fileName = String(name);
|
||||||
@ -271,7 +230,6 @@ void CrossPointWebServer::scanFiles(const char* path, const std::function<void(F
|
|||||||
|
|
||||||
file.close();
|
file.close();
|
||||||
yield(); // Yield to allow WiFi and other tasks to process during long scans
|
yield(); // Yield to allow WiFi and other tasks to process during long scans
|
||||||
esp_task_wdt_reset(); // Reset watchdog to prevent timeout on large directories
|
|
||||||
file = root.openNextFile();
|
file = root.openNextFile();
|
||||||
}
|
}
|
||||||
root.close();
|
root.close();
|
||||||
@ -343,44 +301,11 @@ static size_t uploadSize = 0;
|
|||||||
static bool uploadSuccess = false;
|
static bool uploadSuccess = false;
|
||||||
static String uploadError = "";
|
static String uploadError = "";
|
||||||
|
|
||||||
// Upload write buffer - batches small writes into larger SD card operations
|
|
||||||
// 4KB is a good balance: large enough to reduce syscall overhead, small enough
|
|
||||||
// to keep individual write times short and avoid watchdog issues
|
|
||||||
constexpr size_t UPLOAD_BUFFER_SIZE = 4096; // 4KB buffer
|
|
||||||
static uint8_t uploadBuffer[UPLOAD_BUFFER_SIZE];
|
|
||||||
static size_t uploadBufferPos = 0;
|
|
||||||
|
|
||||||
// Diagnostic counters for upload performance analysis
|
|
||||||
static unsigned long uploadStartTime = 0;
|
|
||||||
static unsigned long totalWriteTime = 0;
|
|
||||||
static size_t writeCount = 0;
|
|
||||||
|
|
||||||
static bool flushUploadBuffer() {
|
|
||||||
if (uploadBufferPos > 0 && uploadFile) {
|
|
||||||
esp_task_wdt_reset(); // Reset watchdog before potentially slow SD write
|
|
||||||
const unsigned long writeStart = millis();
|
|
||||||
const size_t written = uploadFile.write(uploadBuffer, uploadBufferPos);
|
|
||||||
totalWriteTime += millis() - writeStart;
|
|
||||||
writeCount++;
|
|
||||||
esp_task_wdt_reset(); // Reset watchdog after SD write
|
|
||||||
|
|
||||||
if (written != uploadBufferPos) {
|
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] Buffer flush failed: expected %d, wrote %d\n", millis(), uploadBufferPos,
|
|
||||||
written);
|
|
||||||
uploadBufferPos = 0;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
uploadBufferPos = 0;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CrossPointWebServer::handleUpload() const {
|
void CrossPointWebServer::handleUpload() const {
|
||||||
|
static unsigned long lastWriteTime = 0;
|
||||||
|
static unsigned long uploadStartTime = 0;
|
||||||
static size_t lastLoggedSize = 0;
|
static size_t lastLoggedSize = 0;
|
||||||
|
|
||||||
// Reset watchdog at start of every upload callback - HTTP parsing can be slow
|
|
||||||
esp_task_wdt_reset();
|
|
||||||
|
|
||||||
// Safety check: ensure server is still valid
|
// Safety check: ensure server is still valid
|
||||||
if (!running || !server) {
|
if (!running || !server) {
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] ERROR: handleUpload called but server not running!\n", millis());
|
Serial.printf("[%lu] [WEB] [UPLOAD] ERROR: handleUpload called but server not running!\n", millis());
|
||||||
@ -390,18 +315,13 @@ void CrossPointWebServer::handleUpload() const {
|
|||||||
const HTTPUpload& upload = server->upload();
|
const HTTPUpload& upload = server->upload();
|
||||||
|
|
||||||
if (upload.status == UPLOAD_FILE_START) {
|
if (upload.status == UPLOAD_FILE_START) {
|
||||||
// Reset watchdog - this is the critical 1% crash point
|
|
||||||
esp_task_wdt_reset();
|
|
||||||
|
|
||||||
uploadFileName = upload.filename;
|
uploadFileName = upload.filename;
|
||||||
uploadSize = 0;
|
uploadSize = 0;
|
||||||
uploadSuccess = false;
|
uploadSuccess = false;
|
||||||
uploadError = "";
|
uploadError = "";
|
||||||
uploadStartTime = millis();
|
uploadStartTime = millis();
|
||||||
|
lastWriteTime = millis();
|
||||||
lastLoggedSize = 0;
|
lastLoggedSize = 0;
|
||||||
uploadBufferPos = 0;
|
|
||||||
totalWriteTime = 0;
|
|
||||||
writeCount = 0;
|
|
||||||
|
|
||||||
// Get upload path from query parameter (defaults to root if not specified)
|
// Get upload path from query parameter (defaults to root if not specified)
|
||||||
// Note: We use query parameter instead of form data because multipart form
|
// Note: We use query parameter instead of form data because multipart form
|
||||||
@ -428,82 +348,60 @@ void CrossPointWebServer::handleUpload() const {
|
|||||||
if (!filePath.endsWith("/")) filePath += "/";
|
if (!filePath.endsWith("/")) filePath += "/";
|
||||||
filePath += uploadFileName;
|
filePath += uploadFileName;
|
||||||
|
|
||||||
// Check if file already exists - SD operations can be slow
|
// Check if file already exists
|
||||||
esp_task_wdt_reset();
|
|
||||||
if (SdMan.exists(filePath.c_str())) {
|
if (SdMan.exists(filePath.c_str())) {
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] Overwriting existing file: %s\n", millis(), filePath.c_str());
|
Serial.printf("[%lu] [WEB] [UPLOAD] Overwriting existing file: %s\n", millis(), filePath.c_str());
|
||||||
esp_task_wdt_reset();
|
|
||||||
SdMan.remove(filePath.c_str());
|
SdMan.remove(filePath.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open file for writing - this can be slow due to FAT cluster allocation
|
// Open file for writing
|
||||||
esp_task_wdt_reset();
|
|
||||||
if (!SdMan.openFileForWrite("WEB", filePath, uploadFile)) {
|
if (!SdMan.openFileForWrite("WEB", filePath, uploadFile)) {
|
||||||
uploadError = "Failed to create file on SD card";
|
uploadError = "Failed to create file on SD card";
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] FAILED to create file: %s\n", millis(), filePath.c_str());
|
Serial.printf("[%lu] [WEB] [UPLOAD] FAILED to create file: %s\n", millis(), filePath.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
esp_task_wdt_reset();
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] File created successfully: %s\n", millis(), filePath.c_str());
|
Serial.printf("[%lu] [WEB] [UPLOAD] File created successfully: %s\n", millis(), filePath.c_str());
|
||||||
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||||
if (uploadFile && uploadError.isEmpty()) {
|
if (uploadFile && uploadError.isEmpty()) {
|
||||||
// Buffer incoming data and flush when buffer is full
|
const unsigned long writeStartTime = millis();
|
||||||
// This reduces SD card write operations and improves throughput
|
const size_t written = uploadFile.write(upload.buf, upload.currentSize);
|
||||||
const uint8_t* data = upload.buf;
|
const unsigned long writeEndTime = millis();
|
||||||
size_t remaining = upload.currentSize;
|
const unsigned long writeDuration = writeEndTime - writeStartTime;
|
||||||
|
|
||||||
while (remaining > 0) {
|
if (written != upload.currentSize) {
|
||||||
const size_t space = UPLOAD_BUFFER_SIZE - uploadBufferPos;
|
|
||||||
const size_t toCopy = (remaining < space) ? remaining : space;
|
|
||||||
|
|
||||||
memcpy(uploadBuffer + uploadBufferPos, data, toCopy);
|
|
||||||
uploadBufferPos += toCopy;
|
|
||||||
data += toCopy;
|
|
||||||
remaining -= toCopy;
|
|
||||||
|
|
||||||
// Flush buffer when full
|
|
||||||
if (uploadBufferPos >= UPLOAD_BUFFER_SIZE) {
|
|
||||||
if (!flushUploadBuffer()) {
|
|
||||||
uploadError = "Failed to write to SD card - disk may be full";
|
uploadError = "Failed to write to SD card - disk may be full";
|
||||||
uploadFile.close();
|
uploadFile.close();
|
||||||
return;
|
Serial.printf("[%lu] [WEB] [UPLOAD] WRITE ERROR - expected %d, wrote %d\n", millis(), upload.currentSize,
|
||||||
}
|
written);
|
||||||
}
|
} else {
|
||||||
}
|
uploadSize += written;
|
||||||
|
|
||||||
uploadSize += upload.currentSize;
|
// 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);
|
||||||
|
|
||||||
// Log progress every 100KB
|
Serial.printf(
|
||||||
if (uploadSize - lastLoggedSize >= 102400) {
|
"[%lu] [WEB] [UPLOAD] Progress: %d bytes (%.1f KB), %.1f KB/s, write took %lu ms, gap since last: %lu "
|
||||||
const unsigned long elapsed = millis() - uploadStartTime;
|
"ms\n",
|
||||||
const float kbps = (elapsed > 0) ? (uploadSize / 1024.0) / (elapsed / 1000.0) : 0;
|
millis(), uploadSize, uploadSize / 1024.0, kbps, writeDuration, timeSinceLastWrite);
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] %d bytes (%.1f KB), %.1f KB/s, %d writes\n", millis(), uploadSize,
|
|
||||||
uploadSize / 1024.0, kbps, writeCount);
|
|
||||||
lastLoggedSize = uploadSize;
|
lastLoggedSize = uploadSize;
|
||||||
}
|
}
|
||||||
|
lastWriteTime = millis();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (upload.status == UPLOAD_FILE_END) {
|
} else if (upload.status == UPLOAD_FILE_END) {
|
||||||
if (uploadFile) {
|
if (uploadFile) {
|
||||||
// Flush any remaining buffered data
|
|
||||||
if (!flushUploadBuffer()) {
|
|
||||||
uploadError = "Failed to write final data to SD card";
|
|
||||||
}
|
|
||||||
uploadFile.close();
|
uploadFile.close();
|
||||||
|
|
||||||
if (uploadError.isEmpty()) {
|
if (uploadError.isEmpty()) {
|
||||||
uploadSuccess = true;
|
uploadSuccess = true;
|
||||||
const unsigned long elapsed = millis() - uploadStartTime;
|
Serial.printf("[%lu] [WEB] Upload complete: %s (%d bytes)\n", millis(), uploadFileName.c_str(), uploadSize);
|
||||||
const float avgKbps = (elapsed > 0) ? (uploadSize / 1024.0) / (elapsed / 1000.0) : 0;
|
|
||||||
const float writePercent = (elapsed > 0) ? (totalWriteTime * 100.0 / elapsed) : 0;
|
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] Complete: %s (%d bytes in %lu ms, avg %.1f KB/s)\n", millis(),
|
|
||||||
uploadFileName.c_str(), uploadSize, elapsed, avgKbps);
|
|
||||||
Serial.printf("[%lu] [WEB] [UPLOAD] Diagnostics: %d writes, total write time: %lu ms (%.1f%%)\n", millis(),
|
|
||||||
writeCount, totalWriteTime, writePercent);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (upload.status == UPLOAD_FILE_ABORTED) {
|
} else if (upload.status == UPLOAD_FILE_ABORTED) {
|
||||||
uploadBufferPos = 0; // Discard buffered data
|
|
||||||
if (uploadFile) {
|
if (uploadFile) {
|
||||||
uploadFile.close();
|
uploadFile.close();
|
||||||
// Try to delete the incomplete file
|
// Try to delete the incomplete file
|
||||||
@ -657,143 +555,3 @@ void CrossPointWebServer::handleDelete() const {
|
|||||||
server->send(500, "text/plain", "Failed to delete item");
|
server->send(500, "text/plain", "Failed to delete item");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WebSocket callback trampoline
|
|
||||||
void CrossPointWebServer::wsEventCallback(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
|
|
||||||
if (wsInstance) {
|
|
||||||
wsInstance->onWebSocketEvent(num, type, payload, length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WebSocket event handler for fast binary uploads
|
|
||||||
// Protocol:
|
|
||||||
// 1. Client sends TEXT message: "START:<filename>:<size>:<path>"
|
|
||||||
// 2. Client sends BINARY messages with file data chunks
|
|
||||||
// 3. Server sends TEXT "PROGRESS:<received>:<total>" after each chunk
|
|
||||||
// 4. Server sends TEXT "DONE" or "ERROR:<message>" when complete
|
|
||||||
void CrossPointWebServer::onWebSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
|
|
||||||
switch (type) {
|
|
||||||
case WStype_DISCONNECTED:
|
|
||||||
Serial.printf("[%lu] [WS] Client %u disconnected\n", millis(), num);
|
|
||||||
// Clean up any in-progress upload
|
|
||||||
if (wsUploadInProgress && wsUploadFile) {
|
|
||||||
wsUploadFile.close();
|
|
||||||
// Delete incomplete file
|
|
||||||
String filePath = wsUploadPath;
|
|
||||||
if (!filePath.endsWith("/")) filePath += "/";
|
|
||||||
filePath += wsUploadFileName;
|
|
||||||
SdMan.remove(filePath.c_str());
|
|
||||||
Serial.printf("[%lu] [WS] Deleted incomplete upload: %s\n", millis(), filePath.c_str());
|
|
||||||
}
|
|
||||||
wsUploadInProgress = false;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case WStype_CONNECTED: {
|
|
||||||
Serial.printf("[%lu] [WS] Client %u connected\n", millis(), num);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case WStype_TEXT: {
|
|
||||||
// Parse control messages
|
|
||||||
String msg = String((char*)payload);
|
|
||||||
Serial.printf("[%lu] [WS] Text from client %u: %s\n", millis(), num, msg.c_str());
|
|
||||||
|
|
||||||
if (msg.startsWith("START:")) {
|
|
||||||
// Parse: START:<filename>:<size>:<path>
|
|
||||||
int firstColon = msg.indexOf(':', 6);
|
|
||||||
int secondColon = msg.indexOf(':', firstColon + 1);
|
|
||||||
|
|
||||||
if (firstColon > 0 && secondColon > 0) {
|
|
||||||
wsUploadFileName = msg.substring(6, firstColon);
|
|
||||||
wsUploadSize = msg.substring(firstColon + 1, secondColon).toInt();
|
|
||||||
wsUploadPath = msg.substring(secondColon + 1);
|
|
||||||
wsUploadReceived = 0;
|
|
||||||
wsUploadStartTime = millis();
|
|
||||||
|
|
||||||
// Ensure path is valid
|
|
||||||
if (!wsUploadPath.startsWith("/")) wsUploadPath = "/" + wsUploadPath;
|
|
||||||
if (wsUploadPath.length() > 1 && wsUploadPath.endsWith("/")) {
|
|
||||||
wsUploadPath = wsUploadPath.substring(0, wsUploadPath.length() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build file path
|
|
||||||
String filePath = wsUploadPath;
|
|
||||||
if (!filePath.endsWith("/")) filePath += "/";
|
|
||||||
filePath += wsUploadFileName;
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [WS] Starting upload: %s (%d bytes) to %s\n", millis(), wsUploadFileName.c_str(),
|
|
||||||
wsUploadSize, filePath.c_str());
|
|
||||||
|
|
||||||
// Check if file exists and remove it
|
|
||||||
esp_task_wdt_reset();
|
|
||||||
if (SdMan.exists(filePath.c_str())) {
|
|
||||||
SdMan.remove(filePath.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open file for writing
|
|
||||||
esp_task_wdt_reset();
|
|
||||||
if (!SdMan.openFileForWrite("WS", filePath, wsUploadFile)) {
|
|
||||||
wsServer->sendTXT(num, "ERROR:Failed to create file");
|
|
||||||
wsUploadInProgress = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
esp_task_wdt_reset();
|
|
||||||
|
|
||||||
wsUploadInProgress = true;
|
|
||||||
wsServer->sendTXT(num, "READY");
|
|
||||||
} else {
|
|
||||||
wsServer->sendTXT(num, "ERROR:Invalid START format");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
case WStype_BIN: {
|
|
||||||
if (!wsUploadInProgress || !wsUploadFile) {
|
|
||||||
wsServer->sendTXT(num, "ERROR:No upload in progress");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write binary data directly to file
|
|
||||||
esp_task_wdt_reset();
|
|
||||||
size_t written = wsUploadFile.write(payload, length);
|
|
||||||
esp_task_wdt_reset();
|
|
||||||
|
|
||||||
if (written != length) {
|
|
||||||
wsUploadFile.close();
|
|
||||||
wsUploadInProgress = false;
|
|
||||||
wsServer->sendTXT(num, "ERROR:Write failed - disk full?");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
wsUploadReceived += written;
|
|
||||||
|
|
||||||
// Send progress update (every 64KB or at end)
|
|
||||||
static size_t lastProgressSent = 0;
|
|
||||||
if (wsUploadReceived - lastProgressSent >= 65536 || wsUploadReceived >= wsUploadSize) {
|
|
||||||
String progress = "PROGRESS:" + String(wsUploadReceived) + ":" + String(wsUploadSize);
|
|
||||||
wsServer->sendTXT(num, progress);
|
|
||||||
lastProgressSent = wsUploadReceived;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if upload complete
|
|
||||||
if (wsUploadReceived >= wsUploadSize) {
|
|
||||||
wsUploadFile.close();
|
|
||||||
wsUploadInProgress = false;
|
|
||||||
|
|
||||||
unsigned long elapsed = millis() - wsUploadStartTime;
|
|
||||||
float kbps = (elapsed > 0) ? (wsUploadSize / 1024.0) / (elapsed / 1000.0) : 0;
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [WS] Upload complete: %s (%d bytes in %lu ms, %.1f KB/s)\n", millis(),
|
|
||||||
wsUploadFileName.c_str(), wsUploadSize, elapsed, kbps);
|
|
||||||
|
|
||||||
wsServer->sendTXT(num, "DONE");
|
|
||||||
lastProgressSent = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <WebServer.h>
|
#include <WebServer.h>
|
||||||
#include <WebSocketsServer.h>
|
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -35,15 +34,9 @@ class CrossPointWebServer {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<WebServer> server = nullptr;
|
std::unique_ptr<WebServer> server = nullptr;
|
||||||
std::unique_ptr<WebSocketsServer> wsServer = nullptr;
|
|
||||||
bool running = false;
|
bool running = false;
|
||||||
bool apMode = false; // true when running in AP mode, false for STA mode
|
bool apMode = false; // true when running in AP mode, false for STA mode
|
||||||
uint16_t port = 80;
|
uint16_t port = 80;
|
||||||
uint16_t wsPort = 81; // WebSocket port
|
|
||||||
|
|
||||||
// WebSocket upload state
|
|
||||||
void onWebSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length);
|
|
||||||
static void wsEventCallback(uint8_t num, WStype_t type, uint8_t* payload, size_t length);
|
|
||||||
|
|
||||||
// File scanning
|
// File scanning
|
||||||
void scanFiles(const char* path, const std::function<void(FileInfo)>& callback) const;
|
void scanFiles(const char* path, const std::function<void(FileInfo)>& callback) const;
|
||||||
|
|||||||
@ -1,145 +0,0 @@
|
|||||||
#include "HttpDownloader.h"
|
|
||||||
|
|
||||||
#include <HTTPClient.h>
|
|
||||||
#include <HardwareSerial.h>
|
|
||||||
#include <WiFiClient.h>
|
|
||||||
#include <WiFiClientSecure.h>
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "util/UrlUtils.h"
|
|
||||||
|
|
||||||
bool HttpDownloader::fetchUrl(const std::string& url, std::string& outContent) {
|
|
||||||
// Use WiFiClientSecure for HTTPS, regular WiFiClient for HTTP
|
|
||||||
std::unique_ptr<WiFiClient> client;
|
|
||||||
if (UrlUtils::isHttpsUrl(url)) {
|
|
||||||
auto* secureClient = new WiFiClientSecure();
|
|
||||||
secureClient->setInsecure();
|
|
||||||
client.reset(secureClient);
|
|
||||||
} else {
|
|
||||||
client.reset(new WiFiClient());
|
|
||||||
}
|
|
||||||
HTTPClient http;
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [HTTP] Fetching: %s\n", millis(), url.c_str());
|
|
||||||
|
|
||||||
http.begin(*client, url.c_str());
|
|
||||||
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
|
|
||||||
http.addHeader("User-Agent", "CrossPoint-ESP32-" CROSSPOINT_VERSION);
|
|
||||||
|
|
||||||
const int httpCode = http.GET();
|
|
||||||
if (httpCode != HTTP_CODE_OK) {
|
|
||||||
Serial.printf("[%lu] [HTTP] Fetch failed: %d\n", millis(), httpCode);
|
|
||||||
http.end();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
outContent = http.getString().c_str();
|
|
||||||
http.end();
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [HTTP] Fetched %zu bytes\n", millis(), outContent.size());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpDownloader::DownloadError HttpDownloader::downloadToFile(const std::string& url, const std::string& destPath,
|
|
||||||
ProgressCallback progress) {
|
|
||||||
// Use WiFiClientSecure for HTTPS, regular WiFiClient for HTTP
|
|
||||||
std::unique_ptr<WiFiClient> client;
|
|
||||||
if (UrlUtils::isHttpsUrl(url)) {
|
|
||||||
auto* secureClient = new WiFiClientSecure();
|
|
||||||
secureClient->setInsecure();
|
|
||||||
client.reset(secureClient);
|
|
||||||
} else {
|
|
||||||
client.reset(new WiFiClient());
|
|
||||||
}
|
|
||||||
HTTPClient http;
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [HTTP] Downloading: %s\n", millis(), url.c_str());
|
|
||||||
Serial.printf("[%lu] [HTTP] Destination: %s\n", millis(), destPath.c_str());
|
|
||||||
|
|
||||||
http.begin(*client, url.c_str());
|
|
||||||
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
|
|
||||||
http.addHeader("User-Agent", "CrossPoint-ESP32-" CROSSPOINT_VERSION);
|
|
||||||
|
|
||||||
const int httpCode = http.GET();
|
|
||||||
if (httpCode != HTTP_CODE_OK) {
|
|
||||||
Serial.printf("[%lu] [HTTP] Download failed: %d\n", millis(), httpCode);
|
|
||||||
http.end();
|
|
||||||
return HTTP_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t contentLength = http.getSize();
|
|
||||||
Serial.printf("[%lu] [HTTP] Content-Length: %zu\n", millis(), contentLength);
|
|
||||||
|
|
||||||
// Remove existing file if present
|
|
||||||
if (SdMan.exists(destPath.c_str())) {
|
|
||||||
SdMan.remove(destPath.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open file for writing
|
|
||||||
FsFile file;
|
|
||||||
if (!SdMan.openFileForWrite("HTTP", destPath.c_str(), file)) {
|
|
||||||
Serial.printf("[%lu] [HTTP] Failed to open file for writing\n", millis());
|
|
||||||
http.end();
|
|
||||||
return FILE_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the stream for chunked reading
|
|
||||||
WiFiClient* stream = http.getStreamPtr();
|
|
||||||
if (!stream) {
|
|
||||||
Serial.printf("[%lu] [HTTP] Failed to get stream\n", millis());
|
|
||||||
file.close();
|
|
||||||
SdMan.remove(destPath.c_str());
|
|
||||||
http.end();
|
|
||||||
return HTTP_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Download in chunks
|
|
||||||
uint8_t buffer[DOWNLOAD_CHUNK_SIZE];
|
|
||||||
size_t downloaded = 0;
|
|
||||||
const size_t total = contentLength > 0 ? contentLength : 0;
|
|
||||||
|
|
||||||
while (http.connected() && (contentLength == 0 || downloaded < contentLength)) {
|
|
||||||
const size_t available = stream->available();
|
|
||||||
if (available == 0) {
|
|
||||||
delay(1);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t toRead = available < DOWNLOAD_CHUNK_SIZE ? available : DOWNLOAD_CHUNK_SIZE;
|
|
||||||
const size_t bytesRead = stream->readBytes(buffer, toRead);
|
|
||||||
|
|
||||||
if (bytesRead == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const size_t written = file.write(buffer, bytesRead);
|
|
||||||
if (written != bytesRead) {
|
|
||||||
Serial.printf("[%lu] [HTTP] Write failed: wrote %zu of %zu bytes\n", millis(), written, bytesRead);
|
|
||||||
file.close();
|
|
||||||
SdMan.remove(destPath.c_str());
|
|
||||||
http.end();
|
|
||||||
return FILE_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
downloaded += bytesRead;
|
|
||||||
|
|
||||||
if (progress && total > 0) {
|
|
||||||
progress(downloaded, total);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
file.close();
|
|
||||||
http.end();
|
|
||||||
|
|
||||||
Serial.printf("[%lu] [HTTP] Downloaded %zu bytes\n", millis(), downloaded);
|
|
||||||
|
|
||||||
// Verify download size if known
|
|
||||||
if (contentLength > 0 && downloaded != contentLength) {
|
|
||||||
Serial.printf("[%lu] [HTTP] Size mismatch: got %zu, expected %zu\n", millis(), downloaded, contentLength);
|
|
||||||
SdMan.remove(destPath.c_str());
|
|
||||||
return HTTP_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return OK;
|
|
||||||
}
|
|
||||||
@ -1,42 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <SDCardManager.h>
|
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* HTTP client utility for fetching content and downloading files.
|
|
||||||
* Wraps WiFiClientSecure and HTTPClient for HTTPS requests.
|
|
||||||
*/
|
|
||||||
class HttpDownloader {
|
|
||||||
public:
|
|
||||||
using ProgressCallback = std::function<void(size_t downloaded, size_t total)>;
|
|
||||||
|
|
||||||
enum DownloadError {
|
|
||||||
OK = 0,
|
|
||||||
HTTP_ERROR,
|
|
||||||
FILE_ERROR,
|
|
||||||
ABORTED,
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch text content from a URL.
|
|
||||||
* @param url The URL to fetch
|
|
||||||
* @param outContent The fetched content (output)
|
|
||||||
* @return true if fetch succeeded, false on error
|
|
||||||
*/
|
|
||||||
static bool fetchUrl(const std::string& url, std::string& outContent);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Download a file to the SD card.
|
|
||||||
* @param url The URL to download
|
|
||||||
* @param destPath The destination path on SD card
|
|
||||||
* @param progress Optional progress callback
|
|
||||||
* @return DownloadError indicating success or failure type
|
|
||||||
*/
|
|
||||||
static DownloadError downloadToFile(const std::string& url, const std::string& destPath,
|
|
||||||
ProgressCallback progress = nullptr);
|
|
||||||
|
|
||||||
private:
|
|
||||||
static constexpr size_t DOWNLOAD_CHUNK_SIZE = 1024;
|
|
||||||
};
|
|
||||||
@ -3,9 +3,10 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <Update.h>
|
#include <Update.h>
|
||||||
|
#include <WiFiClientSecure.h>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
constexpr char latestReleaseUrl[] = "https://api.github.com/repos/crosspoint-reader/crosspoint-reader/releases/latest";
|
constexpr char latestReleaseUrl[] = "https://api.github.com/repos/daveallie/crosspoint-reader/releases/latest";
|
||||||
}
|
}
|
||||||
|
|
||||||
OtaUpdater::OtaUpdaterError OtaUpdater::checkForUpdate() {
|
OtaUpdater::OtaUpdaterError OtaUpdater::checkForUpdate() {
|
||||||
@ -68,41 +69,44 @@ OtaUpdater::OtaUpdaterError OtaUpdater::checkForUpdate() {
|
|||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OtaUpdater::isUpdateNewer() const {
|
bool OtaUpdater::isUpdateNewer() {
|
||||||
if (!updateAvailable || latestVersion.empty() || latestVersion == CROSSPOINT_VERSION) {
|
if (!updateAvailable || latestVersion.empty() || latestVersion == CROSSPOINT_VERSION) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int currentMajor, currentMinor, currentPatch;
|
|
||||||
int latestMajor, latestMinor, latestPatch;
|
|
||||||
|
|
||||||
const auto currentVersion = CROSSPOINT_VERSION;
|
|
||||||
|
|
||||||
// semantic version check (only match on 3 segments)
|
// semantic version check (only match on 3 segments)
|
||||||
sscanf(latestVersion.c_str(), "%d.%d.%d", &latestMajor, &latestMinor, &latestPatch);
|
const auto updateMajor = stoi(latestVersion.substr(0, latestVersion.find('.')));
|
||||||
sscanf(currentVersion, "%d.%d.%d", ¤tMajor, ¤tMinor, ¤tPatch);
|
const auto updateMinor = stoi(
|
||||||
|
latestVersion.substr(latestVersion.find('.') + 1, latestVersion.find_last_of('.') - latestVersion.find('.') - 1));
|
||||||
|
const auto updatePatch = stoi(latestVersion.substr(latestVersion.find_last_of('.') + 1));
|
||||||
|
|
||||||
/*
|
std::string currentVersion = CROSSPOINT_VERSION;
|
||||||
* Compare major versions.
|
const auto currentMajor = stoi(currentVersion.substr(0, currentVersion.find('.')));
|
||||||
* If they differ, return true if latest major version greater than current major version
|
const auto currentMinor = stoi(currentVersion.substr(
|
||||||
* otherwise return false.
|
currentVersion.find('.') + 1, currentVersion.find_last_of('.') - currentVersion.find('.') - 1));
|
||||||
*/
|
const auto currentPatch = stoi(currentVersion.substr(currentVersion.find_last_of('.') + 1));
|
||||||
if (latestMajor != currentMajor) return latestMajor > currentMajor;
|
|
||||||
|
|
||||||
/*
|
if (updateMajor > currentMajor) {
|
||||||
* Compare minor versions.
|
return true;
|
||||||
* If they differ, return true if latest minor version greater than current minor version
|
}
|
||||||
* otherwise return false.
|
if (updateMajor < currentMajor) {
|
||||||
*/
|
return false;
|
||||||
if (latestMinor != currentMinor) return latestMinor > currentMinor;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Check patch versions.
|
|
||||||
*/
|
|
||||||
return latestPatch > currentPatch;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& OtaUpdater::getLatestVersion() const { return latestVersion; }
|
if (updateMinor > currentMinor) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (updateMinor < currentMinor) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (updatePatch > currentPatch) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& OtaUpdater::getLatestVersion() { return latestVersion; }
|
||||||
|
|
||||||
OtaUpdater::OtaUpdaterError OtaUpdater::installUpdate(const std::function<void(size_t, size_t)>& onProgress) {
|
OtaUpdater::OtaUpdaterError OtaUpdater::installUpdate(const std::function<void(size_t, size_t)>& onProgress) {
|
||||||
if (!isUpdateNewer()) {
|
if (!isUpdateNewer()) {
|
||||||
|
|||||||
@ -23,8 +23,8 @@ class OtaUpdater {
|
|||||||
size_t totalSize = 0;
|
size_t totalSize = 0;
|
||||||
|
|
||||||
OtaUpdater() = default;
|
OtaUpdater() = default;
|
||||||
bool isUpdateNewer() const;
|
bool isUpdateNewer();
|
||||||
const std::string& getLatestVersion() const;
|
const std::string& getLatestVersion();
|
||||||
OtaUpdaterError checkForUpdate();
|
OtaUpdaterError checkForUpdate();
|
||||||
OtaUpdaterError installUpdate(const std::function<void(size_t, size_t)>& onProgress);
|
OtaUpdaterError installUpdate(const std::function<void(size_t, size_t)>& onProgress);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -341,90 +341,6 @@
|
|||||||
width: 60px;
|
width: 60px;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
/* Failed uploads banner */
|
|
||||||
.failed-uploads-banner {
|
|
||||||
background-color: #fff3cd;
|
|
||||||
border: 1px solid #ffc107;
|
|
||||||
border-radius: 4px;
|
|
||||||
padding: 15px;
|
|
||||||
margin-bottom: 15px;
|
|
||||||
display: none;
|
|
||||||
}
|
|
||||||
.failed-uploads-banner.show {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
.failed-uploads-header {
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
align-items: center;
|
|
||||||
margin-bottom: 10px;
|
|
||||||
}
|
|
||||||
.failed-uploads-title {
|
|
||||||
font-weight: 600;
|
|
||||||
color: #856404;
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
.dismiss-btn {
|
|
||||||
background: none;
|
|
||||||
border: none;
|
|
||||||
font-size: 1.2em;
|
|
||||||
cursor: pointer;
|
|
||||||
color: #856404;
|
|
||||||
padding: 0;
|
|
||||||
line-height: 1;
|
|
||||||
}
|
|
||||||
.dismiss-btn:hover {
|
|
||||||
color: #533f03;
|
|
||||||
}
|
|
||||||
.failed-file-item {
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
align-items: center;
|
|
||||||
padding: 8px 0;
|
|
||||||
border-bottom: 1px solid #ffe69c;
|
|
||||||
}
|
|
||||||
.failed-file-item:last-child {
|
|
||||||
border-bottom: none;
|
|
||||||
}
|
|
||||||
.failed-file-info {
|
|
||||||
flex: 1;
|
|
||||||
}
|
|
||||||
.failed-file-name {
|
|
||||||
font-weight: 500;
|
|
||||||
color: #856404;
|
|
||||||
}
|
|
||||||
.failed-file-error {
|
|
||||||
font-size: 0.85em;
|
|
||||||
color: #856404;
|
|
||||||
opacity: 0.8;
|
|
||||||
}
|
|
||||||
.retry-btn {
|
|
||||||
background-color: #ffc107;
|
|
||||||
color: #533f03;
|
|
||||||
border: none;
|
|
||||||
padding: 6px 12px;
|
|
||||||
border-radius: 4px;
|
|
||||||
cursor: pointer;
|
|
||||||
font-size: 0.9em;
|
|
||||||
font-weight: 600;
|
|
||||||
}
|
|
||||||
.retry-btn:hover {
|
|
||||||
background-color: #e0a800;
|
|
||||||
}
|
|
||||||
.retry-all-btn {
|
|
||||||
background-color: #ffc107;
|
|
||||||
color: #533f03;
|
|
||||||
border: none;
|
|
||||||
padding: 8px 16px;
|
|
||||||
border-radius: 4px;
|
|
||||||
cursor: pointer;
|
|
||||||
font-size: 0.95em;
|
|
||||||
font-weight: 600;
|
|
||||||
margin-top: 10px;
|
|
||||||
}
|
|
||||||
.retry-all-btn:hover {
|
|
||||||
background-color: #e0a800;
|
|
||||||
}
|
|
||||||
/* Delete modal */
|
/* Delete modal */
|
||||||
.delete-warning {
|
.delete-warning {
|
||||||
color: #e74c3c;
|
color: #e74c3c;
|
||||||
@ -589,16 +505,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Failed Uploads Banner -->
|
|
||||||
<div class="failed-uploads-banner" id="failedUploadsBanner">
|
|
||||||
<div class="failed-uploads-header">
|
|
||||||
<h3 class="failed-uploads-title">⚠️ Some files failed to upload</h3>
|
|
||||||
<button class="dismiss-btn" onclick="dismissFailedUploads()" title="Dismiss">×</button>
|
|
||||||
</div>
|
|
||||||
<div id="failedFilesList"></div>
|
|
||||||
<button class="retry-all-btn" onclick="retryAllFailedUploads()">Retry All Failed Uploads</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="contents-header">
|
<div class="contents-header">
|
||||||
<h2 class="contents-title">Contents</h2>
|
<h2 class="contents-title">Contents</h2>
|
||||||
@ -625,7 +531,7 @@
|
|||||||
<h3>📤 Upload file</h3>
|
<h3>📤 Upload file</h3>
|
||||||
<div class="upload-form">
|
<div class="upload-form">
|
||||||
<p class="file-info">Select a file to upload to <strong id="uploadPathDisplay"></strong></p>
|
<p class="file-info">Select a file to upload to <strong id="uploadPathDisplay"></strong></p>
|
||||||
<input type="file" id="fileInput" onchange="validateFile()" multiple>
|
<input type="file" id="fileInput" onchange="validateFile()">
|
||||||
<button id="uploadBtn" class="upload-btn" onclick="uploadFile()" disabled>Upload</button>
|
<button id="uploadBtn" class="upload-btn" onclick="uploadFile()" disabled>Upload</button>
|
||||||
<div id="progress-container">
|
<div id="progress-container">
|
||||||
<div id="progress-bar"><div id="progress-fill"></div></div>
|
<div id="progress-bar"><div id="progress-fill"></div></div>
|
||||||
@ -811,166 +717,22 @@
|
|||||||
function validateFile() {
|
function validateFile() {
|
||||||
const fileInput = document.getElementById('fileInput');
|
const fileInput = document.getElementById('fileInput');
|
||||||
const uploadBtn = document.getElementById('uploadBtn');
|
const uploadBtn = document.getElementById('uploadBtn');
|
||||||
const files = fileInput.files;
|
const file = fileInput.files[0];
|
||||||
uploadBtn.disabled = !(files.length > 0);
|
uploadBtn.disabled = !file;
|
||||||
}
|
|
||||||
|
|
||||||
let failedUploadsGlobal = [];
|
|
||||||
let wsConnection = null;
|
|
||||||
const WS_PORT = 81;
|
|
||||||
const WS_CHUNK_SIZE = 4096; // 4KB chunks - smaller for ESP32 stability
|
|
||||||
|
|
||||||
// Get WebSocket URL based on current page location
|
|
||||||
function getWsUrl() {
|
|
||||||
const host = window.location.hostname;
|
|
||||||
return `ws://${host}:${WS_PORT}/`;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Upload file via WebSocket (faster, binary protocol)
|
|
||||||
function uploadFileWebSocket(file, onProgress, onComplete, onError) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const ws = new WebSocket(getWsUrl());
|
|
||||||
let uploadStarted = false;
|
|
||||||
let sendingChunks = false;
|
|
||||||
|
|
||||||
ws.binaryType = 'arraybuffer';
|
|
||||||
|
|
||||||
ws.onopen = function() {
|
|
||||||
console.log('[WS] Connected, starting upload:', file.name);
|
|
||||||
// Send start message: START:<filename>:<size>:<path>
|
|
||||||
ws.send(`START:${file.name}:${file.size}:${currentPath}`);
|
|
||||||
};
|
|
||||||
|
|
||||||
ws.onmessage = async function(event) {
|
|
||||||
const msg = event.data;
|
|
||||||
console.log('[WS] Message:', msg);
|
|
||||||
|
|
||||||
if (msg === 'READY') {
|
|
||||||
uploadStarted = true;
|
|
||||||
sendingChunks = true;
|
|
||||||
|
|
||||||
// Small delay to let connection stabilize
|
|
||||||
await new Promise(r => setTimeout(r, 50));
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Send file in chunks
|
|
||||||
const totalSize = file.size;
|
|
||||||
let offset = 0;
|
|
||||||
|
|
||||||
while (offset < totalSize && ws.readyState === WebSocket.OPEN) {
|
|
||||||
const chunkSize = Math.min(WS_CHUNK_SIZE, totalSize - offset);
|
|
||||||
const chunk = file.slice(offset, offset + chunkSize);
|
|
||||||
const buffer = await chunk.arrayBuffer();
|
|
||||||
|
|
||||||
// Wait for buffer to clear - more aggressive backpressure
|
|
||||||
while (ws.bufferedAmount > WS_CHUNK_SIZE * 2 && ws.readyState === WebSocket.OPEN) {
|
|
||||||
await new Promise(r => setTimeout(r, 5));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ws.readyState !== WebSocket.OPEN) {
|
|
||||||
throw new Error('WebSocket closed during upload');
|
|
||||||
}
|
|
||||||
|
|
||||||
ws.send(buffer);
|
|
||||||
offset += chunkSize;
|
|
||||||
|
|
||||||
// Update local progress - cap at 95% since server still needs to write
|
|
||||||
// Final 100% shown when server confirms DONE
|
|
||||||
if (onProgress) {
|
|
||||||
const cappedOffset = Math.min(offset, Math.floor(totalSize * 0.95));
|
|
||||||
onProgress(cappedOffset, totalSize);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sendingChunks = false;
|
|
||||||
console.log('[WS] All chunks sent, waiting for DONE');
|
|
||||||
} catch (err) {
|
|
||||||
console.error('[WS] Error sending chunks:', err);
|
|
||||||
sendingChunks = false;
|
|
||||||
ws.close();
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
} else if (msg.startsWith('PROGRESS:')) {
|
|
||||||
// Server confirmed progress - log for debugging but don't update UI
|
|
||||||
// (local progress is smoother, server progress causes jumping)
|
|
||||||
console.log('[WS] Server progress:', msg);
|
|
||||||
} else if (msg === 'DONE') {
|
|
||||||
// Show 100% when server confirms completion
|
|
||||||
if (onProgress) onProgress(file.size, file.size);
|
|
||||||
ws.close();
|
|
||||||
if (onComplete) onComplete();
|
|
||||||
resolve();
|
|
||||||
} else if (msg.startsWith('ERROR:')) {
|
|
||||||
const error = msg.substring(6);
|
|
||||||
ws.close();
|
|
||||||
if (onError) onError(error);
|
|
||||||
reject(new Error(error));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ws.onerror = function(event) {
|
|
||||||
console.error('[WS] Error:', event);
|
|
||||||
if (!uploadStarted) {
|
|
||||||
reject(new Error('WebSocket connection failed'));
|
|
||||||
} else if (!sendingChunks) {
|
|
||||||
reject(new Error('WebSocket error during upload'));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
ws.onclose = function(event) {
|
|
||||||
console.log('[WS] Connection closed, code:', event.code, 'reason:', event.reason);
|
|
||||||
if (sendingChunks) {
|
|
||||||
reject(new Error('WebSocket closed unexpectedly'));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Upload file via HTTP (fallback method)
|
|
||||||
function uploadFileHTTP(file, onProgress, onComplete, onError) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const formData = new FormData();
|
|
||||||
formData.append('file', file);
|
|
||||||
|
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
xhr.open('POST', '/upload?path=' + encodeURIComponent(currentPath), true);
|
|
||||||
|
|
||||||
xhr.upload.onprogress = function(e) {
|
|
||||||
if (e.lengthComputable && onProgress) {
|
|
||||||
onProgress(e.loaded, e.total);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.onload = function() {
|
|
||||||
if (xhr.status === 200) {
|
|
||||||
if (onComplete) onComplete();
|
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
const error = xhr.responseText || 'Upload failed';
|
|
||||||
if (onError) onError(error);
|
|
||||||
reject(new Error(error));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.onerror = function() {
|
|
||||||
const error = 'Network error';
|
|
||||||
if (onError) onError(error);
|
|
||||||
reject(new Error(error));
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.send(formData);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function uploadFile() {
|
function uploadFile() {
|
||||||
const fileInput = document.getElementById('fileInput');
|
const fileInput = document.getElementById('fileInput');
|
||||||
const files = Array.from(fileInput.files);
|
const file = fileInput.files[0];
|
||||||
|
|
||||||
if (files.length === 0) {
|
if (!file) {
|
||||||
alert('Please select at least one file!');
|
alert('Please select a file first!');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', file);
|
||||||
|
|
||||||
const progressContainer = document.getElementById('progress-container');
|
const progressContainer = document.getElementById('progress-container');
|
||||||
const progressFill = document.getElementById('progress-fill');
|
const progressFill = document.getElementById('progress-fill');
|
||||||
const progressText = document.getElementById('progress-text');
|
const progressText = document.getElementById('progress-text');
|
||||||
@ -979,163 +741,39 @@ function uploadFile() {
|
|||||||
progressContainer.style.display = 'block';
|
progressContainer.style.display = 'block';
|
||||||
uploadBtn.disabled = true;
|
uploadBtn.disabled = true;
|
||||||
|
|
||||||
let currentIndex = 0;
|
const xhr = new XMLHttpRequest();
|
||||||
const failedFiles = [];
|
// Include path as query parameter since multipart form data doesn't make
|
||||||
let useWebSocket = true; // Try WebSocket first
|
// form fields available until after file upload completes
|
||||||
|
xhr.open('POST', '/upload?path=' + encodeURIComponent(currentPath), true);
|
||||||
|
|
||||||
async function uploadNextFile() {
|
xhr.upload.onprogress = function(e) {
|
||||||
if (currentIndex >= files.length) {
|
if (e.lengthComputable) {
|
||||||
// All files processed - show summary
|
const percent = Math.round((e.loaded / e.total) * 100);
|
||||||
if (failedFiles.length === 0) {
|
progressFill.style.width = percent + '%';
|
||||||
progressFill.style.backgroundColor = '#4caf50';
|
progressText.textContent = 'Uploading: ' + percent + '%';
|
||||||
progressText.textContent = 'All uploads complete!';
|
}
|
||||||
setTimeout(() => {
|
};
|
||||||
closeUploadModal();
|
|
||||||
hydrate();
|
xhr.onload = function() {
|
||||||
|
if (xhr.status === 200) {
|
||||||
|
progressText.textContent = 'Upload complete!';
|
||||||
|
setTimeout(function() {
|
||||||
|
window.location.reload();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
} else {
|
} else {
|
||||||
|
progressText.textContent = 'Upload failed: ' + xhr.responseText;
|
||||||
progressFill.style.backgroundColor = '#e74c3c';
|
progressFill.style.backgroundColor = '#e74c3c';
|
||||||
const failedList = failedFiles.map(f => f.name).join(', ');
|
uploadBtn.disabled = false;
|
||||||
progressText.textContent = `${files.length - failedFiles.length}/${files.length} uploaded. Failed: ${failedList}`;
|
|
||||||
failedUploadsGlobal = failedFiles;
|
|
||||||
setTimeout(() => {
|
|
||||||
closeUploadModal();
|
|
||||||
showFailedUploadsBanner();
|
|
||||||
hydrate();
|
|
||||||
}, 2000);
|
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const file = files[currentIndex];
|
|
||||||
progressFill.style.width = '0%';
|
|
||||||
progressFill.style.backgroundColor = '#27ae60';
|
|
||||||
const methodText = useWebSocket ? ' [WS]' : ' [HTTP]';
|
|
||||||
progressText.textContent = `Uploading ${file.name} (${currentIndex + 1}/${files.length})${methodText}`;
|
|
||||||
|
|
||||||
const onProgress = (loaded, total) => {
|
|
||||||
const percent = Math.round((loaded / total) * 100);
|
|
||||||
progressFill.style.width = percent + '%';
|
|
||||||
const speed = ''; // Could calculate speed here
|
|
||||||
progressText.textContent = `Uploading ${file.name} (${currentIndex + 1}/${files.length})${methodText} — ${percent}%`;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onComplete = () => {
|
xhr.onerror = function() {
|
||||||
currentIndex++;
|
progressText.textContent = 'Upload failed - network error';
|
||||||
uploadNextFile();
|
progressFill.style.backgroundColor = '#e74c3c';
|
||||||
|
uploadBtn.disabled = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
const onError = (error) => {
|
xhr.send(formData);
|
||||||
failedFiles.push({ name: file.name, error: error, file: file });
|
|
||||||
currentIndex++;
|
|
||||||
uploadNextFile();
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (useWebSocket) {
|
|
||||||
await uploadFileWebSocket(file, onProgress, null, null);
|
|
||||||
onComplete();
|
|
||||||
} else {
|
|
||||||
await uploadFileHTTP(file, onProgress, null, null);
|
|
||||||
onComplete();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Upload error:', error);
|
|
||||||
if (useWebSocket && error.message === 'WebSocket connection failed') {
|
|
||||||
// Fall back to HTTP for all subsequent uploads
|
|
||||||
console.log('WebSocket failed, falling back to HTTP');
|
|
||||||
useWebSocket = false;
|
|
||||||
// Retry this file with HTTP
|
|
||||||
try {
|
|
||||||
await uploadFileHTTP(file, onProgress, null, null);
|
|
||||||
onComplete();
|
|
||||||
} catch (httpError) {
|
|
||||||
onError(httpError.message);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
onError(error.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uploadNextFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
function showFailedUploadsBanner() {
|
|
||||||
const banner = document.getElementById('failedUploadsBanner');
|
|
||||||
const filesList = document.getElementById('failedFilesList');
|
|
||||||
|
|
||||||
filesList.innerHTML = '';
|
|
||||||
|
|
||||||
failedUploadsGlobal.forEach((failedFile, index) => {
|
|
||||||
const item = document.createElement('div');
|
|
||||||
item.className = 'failed-file-item';
|
|
||||||
item.innerHTML = `
|
|
||||||
<div class="failed-file-info">
|
|
||||||
<div class="failed-file-name">📄 ${escapeHtml(failedFile.name)}</div>
|
|
||||||
<div class="failed-file-error">Error: ${escapeHtml(failedFile.error)}</div>
|
|
||||||
</div>
|
|
||||||
<button class="retry-btn" onclick="retrySingleUpload(${index})">Retry</button>
|
|
||||||
`;
|
|
||||||
filesList.appendChild(item);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Ensure retry all button is visible
|
|
||||||
const retryAllBtn = banner.querySelector('.retry-all-btn');
|
|
||||||
if (retryAllBtn) retryAllBtn.style.display = '';
|
|
||||||
|
|
||||||
banner.classList.add('show');
|
|
||||||
}
|
|
||||||
|
|
||||||
function dismissFailedUploads() {
|
|
||||||
const banner = document.getElementById('failedUploadsBanner');
|
|
||||||
banner.classList.remove('show');
|
|
||||||
failedUploadsGlobal = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
function retrySingleUpload(index) {
|
|
||||||
const failedFile = failedUploadsGlobal[index];
|
|
||||||
if (!failedFile) return;
|
|
||||||
|
|
||||||
// Create a DataTransfer to set the file input
|
|
||||||
const dt = new DataTransfer();
|
|
||||||
dt.items.add(failedFile.file);
|
|
||||||
|
|
||||||
const fileInput = document.getElementById('fileInput');
|
|
||||||
fileInput.files = dt.files;
|
|
||||||
|
|
||||||
// Remove this file from failed list
|
|
||||||
failedUploadsGlobal.splice(index, 1);
|
|
||||||
|
|
||||||
// If no more failed files, hide banner
|
|
||||||
if (failedUploadsGlobal.length === 0) {
|
|
||||||
dismissFailedUploads();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open modal and trigger upload
|
|
||||||
openUploadModal();
|
|
||||||
validateFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
function retryAllFailedUploads() {
|
|
||||||
if (failedUploadsGlobal.length === 0) return;
|
|
||||||
|
|
||||||
// Create a DataTransfer with all failed files
|
|
||||||
const dt = new DataTransfer();
|
|
||||||
failedUploadsGlobal.forEach(failedFile => {
|
|
||||||
dt.items.add(failedFile.file);
|
|
||||||
});
|
|
||||||
|
|
||||||
const fileInput = document.getElementById('fileInput');
|
|
||||||
fileInput.files = dt.files;
|
|
||||||
|
|
||||||
// Clear failed files list
|
|
||||||
failedUploadsGlobal = [];
|
|
||||||
dismissFailedUploads();
|
|
||||||
|
|
||||||
// Open modal and trigger upload
|
|
||||||
openUploadModal();
|
|
||||||
validateFile();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createFolder() {
|
function createFolder() {
|
||||||
|
|||||||
@ -1,71 +0,0 @@
|
|||||||
#include "StringUtils.h"
|
|
||||||
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace StringUtils {
|
|
||||||
|
|
||||||
std::string sanitizeFilename(const std::string& name, size_t maxLength) {
|
|
||||||
std::string result;
|
|
||||||
result.reserve(name.size());
|
|
||||||
|
|
||||||
for (char c : name) {
|
|
||||||
// Replace invalid filename characters with underscore
|
|
||||||
if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' || c == '"' || c == '<' || c == '>' || c == '|') {
|
|
||||||
result += '_';
|
|
||||||
} else if (c >= 32 && c < 127) {
|
|
||||||
// Keep printable ASCII characters
|
|
||||||
result += c;
|
|
||||||
}
|
|
||||||
// Skip non-printable characters
|
|
||||||
}
|
|
||||||
|
|
||||||
// Trim leading/trailing spaces and dots
|
|
||||||
size_t start = result.find_first_not_of(" .");
|
|
||||||
if (start == std::string::npos) {
|
|
||||||
return "book"; // Fallback if name is all invalid characters
|
|
||||||
}
|
|
||||||
size_t end = result.find_last_not_of(" .");
|
|
||||||
result = result.substr(start, end - start + 1);
|
|
||||||
|
|
||||||
// Limit filename length
|
|
||||||
if (result.length() > maxLength) {
|
|
||||||
result.resize(maxLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.empty() ? "book" : result;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool checkFileExtension(const std::string& fileName, const char* extension) {
|
|
||||||
if (fileName.length() < strlen(extension)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const std::string fileExt = fileName.substr(fileName.length() - strlen(extension));
|
|
||||||
for (size_t i = 0; i < fileExt.length(); i++) {
|
|
||||||
if (tolower(fileExt[i]) != tolower(extension[i])) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t utf8RemoveLastChar(std::string& str) {
|
|
||||||
if (str.empty()) return 0;
|
|
||||||
size_t pos = str.size() - 1;
|
|
||||||
// Walk back to find the start of the last UTF-8 character
|
|
||||||
// UTF-8 continuation bytes start with 10xxxxxx (0x80-0xBF)
|
|
||||||
while (pos > 0 && (static_cast<unsigned char>(str[pos]) & 0xC0) == 0x80) {
|
|
||||||
--pos;
|
|
||||||
}
|
|
||||||
str.resize(pos);
|
|
||||||
return pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Truncate string by removing N UTF-8 characters from the end
|
|
||||||
void utf8TruncateChars(std::string& str, const size_t numChars) {
|
|
||||||
for (size_t i = 0; i < numChars && !str.empty(); ++i) {
|
|
||||||
utf8RemoveLastChar(str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace StringUtils
|
|
||||||
@ -1,25 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace StringUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sanitize a string for use as a filename.
|
|
||||||
* Replaces invalid characters with underscores, trims spaces/dots,
|
|
||||||
* and limits length to maxLength characters.
|
|
||||||
*/
|
|
||||||
std::string sanitizeFilename(const std::string& name, size_t maxLength = 100);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the given filename ends with the specified extension (case-insensitive).
|
|
||||||
*/
|
|
||||||
bool checkFileExtension(const std::string& fileName, const char* extension);
|
|
||||||
|
|
||||||
// UTF-8 safe string truncation - removes one character from the end
|
|
||||||
// Returns the new size after removing one UTF-8 character
|
|
||||||
size_t utf8RemoveLastChar(std::string& str);
|
|
||||||
|
|
||||||
// Truncate string by removing N UTF-8 characters from the end
|
|
||||||
void utf8TruncateChars(std::string& str, size_t numChars);
|
|
||||||
} // namespace StringUtils
|
|
||||||
@ -1,43 +0,0 @@
|
|||||||
#include "UrlUtils.h"
|
|
||||||
|
|
||||||
namespace UrlUtils {
|
|
||||||
|
|
||||||
bool isHttpsUrl(const std::string& url) { return url.rfind("https://", 0) == 0; }
|
|
||||||
|
|
||||||
std::string ensureProtocol(const std::string& url) {
|
|
||||||
if (url.find("://") == std::string::npos) {
|
|
||||||
return "http://" + url;
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string extractHost(const std::string& url) {
|
|
||||||
const size_t protocolEnd = url.find("://");
|
|
||||||
if (protocolEnd == std::string::npos) {
|
|
||||||
// No protocol, find first slash
|
|
||||||
const size_t firstSlash = url.find('/');
|
|
||||||
return firstSlash == std::string::npos ? url : url.substr(0, firstSlash);
|
|
||||||
}
|
|
||||||
// Find the first slash after the protocol
|
|
||||||
const size_t hostStart = protocolEnd + 3;
|
|
||||||
const size_t pathStart = url.find('/', hostStart);
|
|
||||||
return pathStart == std::string::npos ? url : url.substr(0, pathStart);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string buildUrl(const std::string& serverUrl, const std::string& path) {
|
|
||||||
const std::string urlWithProtocol = ensureProtocol(serverUrl);
|
|
||||||
if (path.empty()) {
|
|
||||||
return urlWithProtocol;
|
|
||||||
}
|
|
||||||
if (path[0] == '/') {
|
|
||||||
// Absolute path - use just the host
|
|
||||||
return extractHost(urlWithProtocol) + path;
|
|
||||||
}
|
|
||||||
// Relative path - append to server URL
|
|
||||||
if (urlWithProtocol.back() == '/') {
|
|
||||||
return urlWithProtocol + path;
|
|
||||||
}
|
|
||||||
return urlWithProtocol + "/" + path;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace UrlUtils
|
|
||||||
@ -1,28 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace UrlUtils {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if URL uses HTTPS protocol
|
|
||||||
*/
|
|
||||||
bool isHttpsUrl(const std::string& url);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prepend http:// if no protocol specified (server will redirect to https if needed)
|
|
||||||
*/
|
|
||||||
std::string ensureProtocol(const std::string& url);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extract host with protocol from URL (e.g., "http://example.com" from "http://example.com/path")
|
|
||||||
*/
|
|
||||||
std::string extractHost(const std::string& url);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Build full URL from server URL and path.
|
|
||||||
* If path starts with /, it's an absolute path from the host root.
|
|
||||||
* Otherwise, it's relative to the server URL.
|
|
||||||
*/
|
|
||||||
std::string buildUrl(const std::string& serverUrl, const std::string& path);
|
|
||||||
|
|
||||||
} // namespace UrlUtils
|
|
||||||
Loading…
Reference in New Issue
Block a user