Skip to content

Commit

Permalink
Rearchitect library
Browse files Browse the repository at this point in the history
  • Loading branch information
Craftplacer committed Nov 10, 2024
1 parent 1f569f3 commit 0968085
Show file tree
Hide file tree
Showing 32 changed files with 1,079 additions and 962 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ migrate_working_dir/
/build/
/coverage/
pubspec.lock

# Runtime output
output.ppm
35 changes: 17 additions & 18 deletions packages/sane/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import 'dart:typed_data';

import 'package:logging/logging.dart';
import 'package:sane/sane.dart';
import 'package:sane/src/impl/sane_dev.dart';
import 'package:sane/src/impl/sane_native.dart';

void main(List<String> args) async {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((record) {
print('${record.level.name}: ${record.time}: ${record.message}');
});

final sane = SaneNative(sane: SaneDev());
await sane.spawn();
final sane = NativeSane(MockSane());

await sane.init();
final version = await sane.initialize();

print(version);

final devices = await sane.getDevices(localOnly: false);
for (final device in devices) {
Expand All @@ -26,40 +29,36 @@ void main(List<String> args) async {
return;
}

final handle = await sane.openDevice(devices.first);
final device = devices.first;

final optionDescriptors = await sane.getAllOptionDescriptors(handle);
final optionDescriptors = await device.getAllOptionDescriptors();

for (final optionDescriptor in optionDescriptors) {
if (optionDescriptor.name == 'mode') {
await sane.controlStringOption(
handle: handle,
index: optionDescriptor.index,
action: SaneAction.setValue,
value: 'Color',
await device.controlStringOption(
optionDescriptor.index,
SaneAction.setValue,
'Color',
);
break;
}
}

await sane.start(handle);
await device.start();

final parameters = await sane.getParameters(handle);
final parameters = await device.getParameters();
print('Parameters: format(${parameters.format}), depth(${parameters.depth})');

final rawPixelDataList = <Uint8List>[];
Uint8List? bytes;
while (true) {
bytes = await sane.read(handle, parameters.bytesPerLine);
bytes = await device.read(bufferSize: parameters.bytesPerLine);
if (bytes.isEmpty) break;
rawPixelDataList.add(bytes);
}

await sane.cancel(handle);
await sane.close(handle);
await sane.exit();

sane.kill();
await device.cancel();
await device.close();

Uint8List mergeUint8Lists(List<Uint8List> lists) {
final totalLength = lists.fold(0, (length, list) => length + list.length);
Expand Down
5 changes: 1 addition & 4 deletions packages/sane/lib/sane.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
library;

export 'src/exceptions.dart';
export 'src/impl/sane_dev.dart';
export 'src/impl/sane_native.dart';
export 'src/impl/sane_sync.dart';
export 'src/sane.dart';
export 'src/structures.dart';
export 'src/utils.dart';
11 changes: 11 additions & 0 deletions packages/sane/lib/src/bindings.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ class LibSane {
'sane_strstatus');
late final _sane_strstatus =
_sane_strstatusPtr.asFunction<SANE_String_Const Function(int)>();

late final addresses = _SymbolAddresses(this);
}

class _SymbolAddresses {
final LibSane _library;

_SymbolAddresses(this._library);

ffi.Pointer<ffi.NativeFunction<ffi.Void Function(SANE_Handle)>>
get sane_close => _library._sane_closePtr;
}

enum SANE_Status {
Expand Down
2 changes: 2 additions & 0 deletions packages/sane/lib/src/extensions.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:meta/meta.dart';
import 'package:sane/src/bindings.g.dart';
import 'package:sane/src/exceptions.dart';

@internal
extension SaneStatusExtension on SANE_Status {
/// Throws [SaneException] if the status is not [SANE_Status.STATUS_GOOD].
@pragma('vm:prefer-inline')
Expand Down
Loading

0 comments on commit 0968085

Please sign in to comment.