Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/images/displays/vidabay_snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions lib/constants/asset_paths.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ImageAssets {
'assets/images/displays/waveshare_1.54G.jpg';
static const String santekEzSign2_13 =
'assets/images/displays/santek_ez_2.13.png';
static const String vidabaySnap = 'assets/images/displays/vidabay_snap.png';
static const String tempIcon = 'assets/icons/icon.png';
static const String appIcon = 'assets/icons/app_icon_desktop.png';
static const String githubIcon = 'assets/icons/github.png';
Expand Down
2 changes: 2 additions & 0 deletions lib/image_library/utils/epd_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import 'package:magicepaperapp/utils/epd/gdey037z03bw.dart';
import 'package:magicepaperapp/utils/epd/waveshare_displays.dart';
import 'package:magicepaperapp/utils/epd/gdeq031t10.dart';
import 'package:magicepaperapp/utils/epd/santek_displays.dart';
import 'package:magicepaperapp/vidabay/vidabay_display.dart';

class EpdUtils {
static final List<DisplayDevice Function()> _deviceFactories = [
() => SantekEzSign2in13(),
() => VidabaySnap(),
() => Gdey037z03(),
() => Gdey037z03BW(),
() => GDEQ031T10(),
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,7 @@
"brandWaveshare": "Waveshare",
"brandSantek": "Santek",
"brandOpenEpaperLink": "OpenEpaperLink",
"brandVidabay": "VidaBay",
"sortBy": "Sort by",
"sortDefault": "Default",
"sortNameAsc": "Name (A-Z)",
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/epd/brand.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum Brand {
waveshare,
santek,
openEpaperLink,
vidabay,
}

extension BrandLabel on Brand {
Expand All @@ -21,6 +22,8 @@ extension BrandLabel on Brand {
return l.brandSantek;
case Brand.openEpaperLink:
return l.brandOpenEpaperLink;
case Brand.vidabay:
return l.brandVidabay;
}
}
}
112 changes: 112 additions & 0 deletions lib/vidabay/vidabay_codec.dart
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]);
}
95 changes: 95 additions & 0 deletions lib/vidabay/vidabay_display.dart
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(
Comment thread
viteo marked this conversation as resolved.
Resolved
context: context,
finalImg: image,
transferFunction: (image, onProgress, onTagDetected) async {
try {
await VidabayProtocol().send(
Comment thread
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;
}
}
}
Loading