node.spidevice.pas 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. unit node.spidevice;
  12. {$mode objfpc}
  13. {$modeswitch externalclass}
  14. interface
  15. uses
  16. JS,NodeJS;
  17. Type
  18. TNJSErrorCallBack = reference to procedure (err : TJSError);
  19. TSPIDeviceMessage = class external name 'Object' (TJSObject)
  20. byteLength : Longint; //- number, 32-bit, the number of bytes to transfer
  21. sendBuffer : TNJSBuffer; // - optional Buffer, transmit data
  22. receiveBuffer : TNJSBuffer; // - optional Buffer, receive data
  23. speedHz : longint; // optional number, 32-bit, override of the device's clock frequency in Hertz
  24. microSecondDelay : smallint; // - optional number, 16-bit, delay after the last bit transfer before optionally deselecting the device before the next transfer, default 0
  25. bitsPerWord : byte; // optional number, 8-bit, override of the device's wordsize
  26. chipSelectChange : Boolean; // - optional boolean, true to deselect device before starting the next transfer, default false
  27. end;
  28. TSPIDeviceMessages = Array of TSPIDeviceMessage;
  29. TNJSCompletionCallBack = reference to procedure (err : TJSError; msg : TSPIDeviceMessage);
  30. TSPIDeviceOptions = class external name 'Object' (TJSObject)
  31. mode : byte;
  32. chipSelectHigh : boolean;
  33. lsbFirst : boolean;
  34. threeWire : boolean;
  35. loopback : boolean;
  36. noChipSelect : boolean;
  37. ready : boolean;
  38. bitsPerWord : boolean;
  39. maxSpeedHz : longint;
  40. end;
  41. TNJSDeviceOptionsCallBack = reference to procedure (err : TJSError; opts : TSPIDeviceOptions);
  42. TNJSSPIDevice = class external name 'Object' (TJSObject)
  43. function transfer(message : TSPIDeviceMessages; callback : TNJSCompletionCallBack) : TNJSSPIDevice;
  44. function transferSync(message : TSPIDeviceMessages) : TNJSSPIDevice;
  45. function getOptions(callback : TNJSDeviceOptionsCallBack) : TNJSSPIDevice;
  46. function getOptionsSync : TSPIDeviceOptions;
  47. function setOptions(aOptions : TSPIDeviceOptions; callback : TNJSErrorCallBack) : TNJSSPIDevice;
  48. function setOptionsSync(aOptions : TSPIDeviceOptions) : TNJSSPIDevice;
  49. procedure close(callback : TNJSErrorCallBack);
  50. procedure closeSync;
  51. end;
  52. TNJSSPIDeviceClass = class of TNJSSPIDevice;
  53. TNSJSSPIdeviceModule = class external name 'Object' (TJSObject)
  54. Public
  55. var
  56. MODE0 : byte;
  57. MODE1 : byte;
  58. MODE2 : byte;
  59. MODE3 : byte;
  60. Public
  61. function open (busNumber, deviceNumber : Byte; CallBack: TNJSErrorCallBack) : TNJSSPIDevice;
  62. function open (busNumber, deviceNumber : Byte; Options : TSPIDeviceOptions; CallBack: TNJSErrorCallBack) : TNJSSPIDevice;
  63. function openSync (busNumber, deviceNumber : Byte; Options : TSPIDeviceOptions) : TNJSSPIDevice;
  64. function openSync (busNumber, deviceNumber : Byte) : TNJSSPIDevice;
  65. end;
  66. var
  67. spi : TNSJSSPIdeviceModule;
  68. implementation
  69. initialization
  70. spi:=TNSJSSPIdeviceModule(require('spi-device'));
  71. end.