node.spidevice.pas 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. {
  2. This file is part of the Pas2JS run time library.
  3. Copyright (c) 2020 by Michael Van Canneyt
  4. NodeJS spi-dev module import.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$IFNDEF FPC_DOTTEDUNITS}
  12. unit node.spidevice;
  13. {$ENDIF}
  14. {$mode objfpc}
  15. {$modeswitch externalclass}
  16. interface
  17. uses
  18. {$IFDEF FPC_DOTTEDUNITS}
  19. JSApi.JS, NodeApi.JS;
  20. {$ELSE}
  21. JS,NodeJS;
  22. {$ENDIF}
  23. Type
  24. TNJSErrorCallBack = reference to procedure (err : TJSError);
  25. TSPIDeviceMessage = class external name 'Object' (TJSObject)
  26. byteLength : Longint; //- number, 32-bit, the number of bytes to transfer
  27. sendBuffer : TNJSBuffer; // - optional Buffer, transmit data
  28. receiveBuffer : TNJSBuffer; // - optional Buffer, receive data
  29. speedHz : longint; // optional number, 32-bit, override of the device's clock frequency in Hertz
  30. microSecondDelay : smallint; // - optional number, 16-bit, delay after the last bit transfer before optionally deselecting the device before the next transfer, default 0
  31. bitsPerWord : byte; // optional number, 8-bit, override of the device's wordsize
  32. chipSelectChange : Boolean; // - optional boolean, true to deselect device before starting the next transfer, default false
  33. end;
  34. TSPIDeviceMessages = Array of TSPIDeviceMessage;
  35. TNJSCompletionCallBack = reference to procedure (err : TJSError; msg : TSPIDeviceMessage);
  36. TSPIDeviceOptions = class external name 'Object' (TJSObject)
  37. mode : byte;
  38. chipSelectHigh : boolean;
  39. lsbFirst : boolean;
  40. threeWire : boolean;
  41. loopback : boolean;
  42. noChipSelect : boolean;
  43. ready : boolean;
  44. bitsPerWord : boolean;
  45. maxSpeedHz : longint;
  46. end;
  47. TNJSDeviceOptionsCallBack = reference to procedure (err : TJSError; opts : TSPIDeviceOptions);
  48. TNJSSPIDevice = class external name 'Object' (TJSObject)
  49. function transfer(message : TSPIDeviceMessages; callback : TNJSCompletionCallBack) : TNJSSPIDevice;
  50. function transferSync(message : TSPIDeviceMessages) : TNJSSPIDevice;
  51. function getOptions(callback : TNJSDeviceOptionsCallBack) : TNJSSPIDevice;
  52. function getOptionsSync : TSPIDeviceOptions;
  53. function setOptions(aOptions : TSPIDeviceOptions; callback : TNJSErrorCallBack) : TNJSSPIDevice;
  54. function setOptionsSync(aOptions : TSPIDeviceOptions) : TNJSSPIDevice;
  55. procedure close(callback : TNJSErrorCallBack);
  56. procedure closeSync;
  57. end;
  58. TNJSSPIDeviceClass = class of TNJSSPIDevice;
  59. TNSJSSPIdeviceModule = class external name 'Object' (TJSObject)
  60. Public
  61. var
  62. MODE0 : byte;
  63. MODE1 : byte;
  64. MODE2 : byte;
  65. MODE3 : byte;
  66. Public
  67. function open (busNumber, deviceNumber : Byte; CallBack: TNJSErrorCallBack) : TNJSSPIDevice;
  68. function open (busNumber, deviceNumber : Byte; Options : TSPIDeviceOptions; CallBack: TNJSErrorCallBack) : TNJSSPIDevice;
  69. function openSync (busNumber, deviceNumber : Byte; Options : TSPIDeviceOptions) : TNJSSPIDevice;
  70. function openSync (busNumber, deviceNumber : Byte) : TNJSSPIDevice;
  71. end;
  72. var
  73. spi : TNSJSSPIdeviceModule;
  74. implementation
  75. initialization
  76. spi:=TNSJSSPIdeviceModule(require('spi-device'));
  77. end.