import 'package:dart_periphery/dart_periphery.dart';
import 'package:logging/logging.dart';
import 'dart:io';

class X9c10x {
  final int _id;
  //final int _ohm; 
  final GPIO _inc;
  final GPIO _ud;
  final GPIO? _ss;
  final _log = Logger("X9C10X");
  final Function(int)? _onSelect;
  final Function(int)? _onRelease;

  X9c10x({
      required int id,
      required int ohm, 
      required GPIO inc,
      required GPIO ud,
      GPIO? ss,
      dynamic Function(int)? onSelect,
      dynamic Function(int)? onRelease,
      }) : _onRelease = onRelease, _onSelect = onSelect, _ss = ss, _ud = ud, _inc = inc, _id = id;

  void _selectChip(){
    _onSelect?.call(_id);
    _ss?.write(false);
    _log.info("_selectChip() <$_id>");
  }
  void _releaseChip(){
    _log.info("_releaseChip() <$_id>");
    _ss?.write(true);
    _onRelease?.call(_id);
  }

  void _skip()=>sleep(const Duration(milliseconds: 3));
  void _storeSkip()=>sleep(const Duration(milliseconds: 20));

  Future<void> wipeUp() async {
    _log.info("wipeUp()");
    _selectChip();
    _ud.write(true);
    _skip();
    _inc.write(false);
    _skip();
    _inc.write(true);
    _releaseChip();
    _storeSkip();
  }

  Future<void> wipeDown() async {
    _log.info("wipeDown()");
    _selectChip();
    _ud.write(false);
    _skip();
    _inc.write(false);
    _skip();
    _inc.write(true);
    _releaseChip();
    _storeSkip();
  }
  void dispose(){
      _inc.dispose();
      _ud.dispose();
      _ss?.dispose();
    }

}
