Repository navigation
feat: Add VidaBay Snap NFC display #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
viteo
wants to merge
1
commit into
fossasia:main
Choose a base branch
from
viteo:vidabay
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Image codec and command payloads for VidaBay NFC e-paper displays | ||
| // (Fudan ISO 14443-4 tag; see vidabay_protocol.dart for the sequence). | ||
|
|
||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:image/image.dart' as img; | ||
|
|
||
| class VidabayCodec { | ||
| /// Pixel codes on the wire (2 bits per pixel, first pixel in the MSBs). | ||
| static const int black = 0, white = 1, yellow = 2, red = 3; | ||
|
|
||
| static const int chunkData = 180; // image bytes per chunk (+1 checksum) | ||
|
|
||
| /// Classifies an (already dithered) pixel into the panel's 4 colours. | ||
| static int pixelCode(int r, int g, int b) { | ||
| if (r > 128 && g > 128 && b < 128) return yellow; | ||
| if (r > 128 && g < 128 && b < 128) return red; | ||
| if (r < 128 && g < 128 && b < 128) return black; // max(R,G,B) < 128 | ||
| return white; | ||
| } | ||
|
|
||
| /// 400 x 400 panels (Snap): the panel expects the image rotated | ||
| /// by 270 degrees (clockwise) and scanned column by column, 4 pixels per | ||
| /// byte, each column starting on a new byte. Equivalent: for every | ||
| /// source row, emit its pixels from right to left. | ||
| static Uint8List encode(img.Image image, | ||
| {int width = 400, int height = 400}) { | ||
| if (width != height) { | ||
| throw ArgumentError('Only square VidaBay panels are supported so far'); | ||
| } | ||
| if (image.width != width || image.height != height) { | ||
| image = img.copyResize(image, width: width, height: height); | ||
| } | ||
| final bytesPerLine = (width + 3) ~/ 4; | ||
| final out = Uint8List(height * bytesPerLine); | ||
| for (int row = 0; row < height; row++) { | ||
| for (int i = 0; i < width; i++) { | ||
| final p = image.getPixel(width - 1 - i, row); | ||
| final code = pixelCode(p.r.toInt(), p.g.toInt(), p.b.toInt()); | ||
| out[row * bytesPerLine + (i >> 2)] |= code << (6 - 2 * (i & 3)); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| /// Splits the encoded image into 181-byte chunks (180 data bytes + sum8). | ||
| /// The last chunk is padded with 0xFF to 180 bytes before its sum. | ||
| static List<Uint8List> chunks(Uint8List data) { | ||
| final out = <Uint8List>[]; | ||
| for (int start = 0; start < data.length; start += chunkData) { | ||
| final end = | ||
| start + chunkData < data.length ? start + chunkData : data.length; | ||
| final body = Uint8List(chunkData) | ||
| ..fillRange(0, chunkData, 0xFF) | ||
| ..setAll(0, data.sublist(start, end)); | ||
| out.add(_withSum(body)); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| static Uint8List _withSum(Uint8List body) { | ||
| int sum = 0; | ||
| for (final b in body) { | ||
| sum = (sum + b) & 0xFF; | ||
| } | ||
| return Uint8List(body.length + 1) | ||
| ..setAll(0, body) | ||
| ..[body.length] = sum; | ||
| } | ||
|
|
||
| // ---- command payloads (each sent as APDU 00 4A 00 00 Lc <payload>) ----- | ||
|
|
||
| static Uint8List _cmd(List<int> head, int totalLength, | ||
| [List<int> tail = const []]) { | ||
| final p = Uint8List(totalLength); | ||
| p.setAll(0, head); | ||
| p.setAll(totalLength - tail.length, tail); | ||
| return p; | ||
| } | ||
|
|
||
| static Uint8List probe() => _cmd([0x00, 0x00, 0x00, 0x00, 0x01], 32); | ||
| static Uint8List getToken() => _cmd([0x00, 0x4A, 0x01, 0x00], 32); | ||
| static Uint8List hwInfo(List<int> token) => | ||
| _cmd([0x00, 0x4A, 0x01, 0x09, 0x00], 32, token); | ||
| static Uint8List startSession(List<int> token) => | ||
| _cmd([0x00, 0x4A, 0x09, 0x00, 0x00, 0xFF], 32, token); | ||
| static Uint8List startTransfer(int chunkCount, List<int> token) => _cmd( | ||
| [0x00, 0x4A, 0x09, 0x01, 0x02, chunkCount >> 8, chunkCount & 0xFF], | ||
| 32, | ||
| token); | ||
| static Uint8List chunkFrame(int index, Uint8List chunk) { | ||
| final p = Uint8List(4 + 2 + chunk.length + 1); | ||
| p.setAll(0, [0x00, 0x4A, 0x09, 0x02, index >> 8, index & 0xFF]); | ||
| p.setAll(6, chunk); | ||
| return p; // trailing 0x00 | ||
| } | ||
|
|
||
| /// End marker | ||
| static Uint8List endMarker(int chunkCount) { | ||
| final i = chunkCount + 1; | ||
| final p = Uint8List(4 + 2 + 180 + 2); | ||
| p.setAll(0, [0x00, 0x4A, 0x09, 0x02, i >> 8, i & 0xFF]); | ||
| return p; | ||
| } | ||
|
|
||
| static Uint8List pollRefresh(List<int> token) => | ||
| _cmd([0x00, 0x4A, 0x05, 0x00], 32, token); | ||
|
|
||
| /// Wraps a payload into the APDU that is sent over IsoDep. | ||
| static Uint8List apdu(Uint8List payload) => | ||
| Uint8List.fromList([0x00, 0x4A, 0x00, 0x00, payload.length, ...payload]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import 'package:flutter/services.dart' show PlatformException; | ||
| import 'package:fluttertoast/fluttertoast.dart'; | ||
| import 'package:material_ui/material_ui.dart'; | ||
| import 'package:image/image.dart' as img; | ||
| import 'package:magicepaperapp/constants/asset_paths.dart'; | ||
| import 'package:magicepaperapp/l10n/app_localizations.dart'; | ||
| import 'package:magicepaperapp/provider/getitlocator.dart'; | ||
| import 'package:magicepaperapp/theme/colors.dart'; | ||
| import 'package:magicepaperapp/utils/epd/brand.dart'; | ||
| import 'package:magicepaperapp/utils/epd/display_device.dart'; | ||
| import 'package:magicepaperapp/utils/epd/driver/waveform.dart'; | ||
| import 'package:magicepaperapp/utils/image_processing/image_processing.dart'; | ||
| import 'package:magicepaperapp/view/widgets/transfer_progress_dialog.dart'; | ||
|
|
||
| import 'vidabay_protocol.dart'; | ||
|
|
||
| AppLocalizations get _l => getIt.get<AppLocalizations>(); | ||
|
|
||
| /// VidaBay "Snap" NFC e-paper display: 2.6", 400 x 400, | ||
| /// black/white/yellow/red, battery-less. | ||
| class VidabaySnap extends DisplayDevice { | ||
| @override | ||
| int get width => 400; | ||
| @override | ||
| int get height => 400; | ||
| @override | ||
| String get name => 'VidaBay Snap 2.6" BWRY'; | ||
| @override | ||
| String get modelId => 'vidabay-snap'; | ||
| @override | ||
| String get imgPath => ImageAssets.vidabaySnap; | ||
| @override | ||
| List<Color> get colors => | ||
| [Colors.white, Colors.black, Colors.red, Colors.yellow]; | ||
| @override | ||
| List<ImageProcessingMethod> get processingMethods => const [ | ||
| ImageProcessing.bwryFloydSteinbergDither, | ||
| ImageProcessing.bwryFalseFloydSteinbergDither, | ||
| ImageProcessing.bwryStuckiDither, | ||
| ImageProcessing.bwryTriColorAtkinsonDither, | ||
| ImageProcessing.bwryThreshold, | ||
| ImageProcessing.bwryBayerDither, | ||
| ImageProcessing.bwrySierra2Dither, | ||
| ImageProcessing.bwryBurkesDither, | ||
| ]; | ||
| @override | ||
| bool get isBeta => true; | ||
| @override | ||
| Brand get brand => Brand.vidabay; | ||
| @override | ||
| List<String>? get displayChips => ['Fudan ISO 14443-4']; | ||
|
|
||
| @override | ||
| Future<void> transfer(BuildContext context, img.Image image, | ||
| {Waveform? waveform}) async { | ||
| if (!context.mounted) return; | ||
| // The protocol reports progress 1.0 when the image data is sent; the | ||
| // dialog then asks to keep the phone close while the panel refreshes. | ||
| // The protocol keeps waiting for the refresh in the background and a | ||
| // toast tells when the tag confirmed it, even if the dialog was already | ||
| // closed. Progress callbacks and a late error are ignored then. | ||
| var closed = false; | ||
| try { | ||
| await TransferProgressDialog.show( | ||
| context: context, | ||
| finalImg: image, | ||
| transferFunction: (image, onProgress, onTagDetected) async { | ||
| try { | ||
| await VidabayProtocol().send( | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
| image, | ||
| onProgress: (p, s) { | ||
| if (!closed) onProgress(p, s); | ||
| }, | ||
| onTagDetected: () { | ||
| if (!closed) onTagDetected(); | ||
| }, | ||
| ); | ||
| Fluttertoast.showToast(msg: _l.transferCompleteMessage); | ||
| } on PlatformException catch (e) { | ||
| if (closed) return; | ||
| if (e.code == '500') { | ||
| throw VidabayException(_l.connectionLostDuringTransfer); | ||
| } | ||
| rethrow; | ||
| } catch (_) { | ||
| if (!closed) rethrow; | ||
| } | ||
| }, | ||
| colorAccent: colorAccent, | ||
| ); | ||
| } finally { | ||
| closed = true; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.