{"id":13454267,"url":"https://github.com/fivdi/spi-device","last_synced_at":"2025-04-13T06:42:07.864Z","repository":{"id":57367417,"uuid":"58947260","full_name":"fivdi/spi-device","owner":"fivdi","description":"SPI serial bus access with Node.js","archived":false,"fork":false,"pushed_at":"2021-09-26T07:43:13.000Z","size":333,"stargazers_count":118,"open_issues_count":2,"forks_count":16,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-20T00:22:28.044Z","etag":null,"topics":["beaglebone","beaglebone-black","iot","javascript","nodejs","raspberry-pi","spi"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fivdi.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-16T16:12:37.000Z","updated_at":"2024-10-18T08:03:39.000Z","dependencies_parsed_at":"2022-08-23T19:30:37.521Z","dependency_job_id":null,"html_url":"https://github.com/fivdi/spi-device","commit_stats":null,"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fivdi%2Fspi-device","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fivdi%2Fspi-device/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fivdi%2Fspi-device/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fivdi%2Fspi-device/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fivdi","download_url":"https://codeload.github.com/fivdi/spi-device/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248675440,"owners_count":21143763,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["beaglebone","beaglebone-black","iot","javascript","nodejs","raspberry-pi","spi"],"created_at":"2024-07-31T08:00:52.424Z","updated_at":"2025-04-13T06:42:07.833Z","avatar_url":"https://github.com/fivdi.png","language":"C++","funding_links":[],"categories":["Packages","Repository","包","目录","Hardware"],"sub_categories":["Hardware","硬件"],"readme":"[![Build Status](https://app.travis-ci.com/fivdi/spi-device.svg?branch=master)](https://app.travis-ci.com/github/fivdi/spi-device)\n[![npm Version](http://img.shields.io/npm/v/spi-device.svg)](https://www.npmjs.com/package/spi-device)\n[![Downloads Per Month](http://img.shields.io/npm/dm/spi-device.svg)](https://www.npmjs.com/package/spi-device)\n[![Mentioned in Awesome Node.js](https://awesome.re/mentioned-badge.svg)](https://github.com/sindresorhus/awesome-nodejs)\n\n# spi-device\n\nSPI serial bus access with **Node.js** on Linux boards like the Raspberry\nPi or BeagleBone. All methods have asynchronous and synchronous forms.\n\nspi-device supports Node.js versions 10, 12, 14, 15 and 16.\n\n## Contents\n\n * [Installation](#installation)\n * [Usage](#usage)\n * [API Documentation](#api-documentation)\n\n## Installation\n\n```\nnpm install spi-device\n```\n\n## Usage\n\nDetermine the temperature using a TMP36 analog temperature sensor wired to\nchannel 5 on an MCP3008 SPI A/D converter.\n\n\u003cimg src=\"https://raw.githubusercontent.com/fivdi/spi-device/master/example/pi-mcp3008-tmp36.png\"\u003e\n\n```js\nconst spi = require('spi-device');\n\n// The MCP3008 is on bus 0 and it's device 0\nconst mcp3008 = spi.open(0, 0, err =\u003e {\n  // An SPI message is an array of one or more read+write transfers\n  const message = [{\n    sendBuffer: Buffer.from([0x01, 0xd0, 0x00]), // Sent to read channel 5\n    receiveBuffer: Buffer.alloc(3),              // Raw data read from channel 5\n    byteLength: 3,\n    speedHz: 20000 // Use a low bus speed to get a good reading from the TMP36\n  }];\n\n  if (err) throw err;\n\n  mcp3008.transfer(message, (err, message) =\u003e {\n    if (err) throw err;\n\n    // Convert raw value from sensor to celcius and log to console\n    const rawValue = ((message[0].receiveBuffer[1] \u0026 0x03) \u003c\u003c 8) +\n      message[0].receiveBuffer[2];\n    const voltage = rawValue * 3.3 / 1023;\n    const celcius = (voltage - 0.5) * 100;\n\n    console.log(celcius);\n  });\n});\n```\n\nspi-device enables low-level access to SPI devices. Often, high-level access\nis required. When this is the case, high-level packages can be built using\nspi-device. An example of such a package is\n[mcp-spi-adc](https://github.com/fivdi/mcp-spi-adc) which provides a high-level\nAPI for accessing an MCP3008 SPI A/D converter and will generally be more\nuseful than the low-level demonstration code shown above.\n\n## API Documentation\n\nAll methods have asynchronous and synchronous forms.\n\nThe asynchronous form always take a completion callback as its last argument.\nThe arguments passed to the completion callback depend on the method, but the\nfirst argument is always reserved for an exception. If the operation was\ncompleted successfully, then the first argument will be null or undefined.\n\nWhen using the synchronous form any exceptions are immediately thrown. You can\nuse try/catch to handle exceptions or allow them to bubble up. \n\n### Functions\n\n- [open(busNumber, deviceNumber[, options], cb)](#openbusnumber-devicenumber-options-cb)\n- [openSync(busNumber, deviceNumber[, options])](#opensyncbusnumber-devicenumber-options)\n\n### Class SpiDevice\n\n- [device.transfer(message, cb)](#devicetransfermessage-cb)\n- [device.transferSync(message)](#devicetransfersyncmessage)\n- [device.getOptions(cb)](#devicegetoptionscb)\n- [device.getOptionsSync()](#devicegetoptionssync)\n- [device.setOptions(options, cb)](#devicesetoptionsoptions-cb)\n- [device.setOptionsSync(options)](#devicesetoptionssyncoptions)\n- [device.close(cb)](#deviceclosecb)\n- [device.closeSync()](#deviceclosesync)\n\n### Constants\n\n- [MODE0](#mode0)\n- [MODE1](#mode1)\n- [MODE2](#mode2)\n- [MODE3](#mode3)\n\n### open(busNumber, deviceNumber[, options], cb)\n- busNumber - the number of the SPI bus to open, 0 for `/dev/spidev0.n`, 1 for `/dev/spidev1.n`, ...\n- deviceNumber - the number of the SPI device to open, 0 for `/dev/spidevn.0`, 1 for `/dev/spidevn.1`, ...\n- options - an optional object specifying device\n[configuration options](#configuration-options)\n- cb - completion callback\n\nAsynchronous open. Returns a new SpiDevice object. The completion callback gets\none argument (err). The SpiDevice object returned should not be used before the\ncompletion callback is called.\n\n### openSync(busNumber, deviceNumber[, options])\n- busNumber - the number of the SPI bus to open, 0 for `/dev/spidev0.n`, 1 for `/dev/spidev1.n`, ...\n- deviceNumber - the number of the SPI device to open, 0 for `/dev/spidevn.0`, 1 for `/dev/spidevn.1`, ...\n- options - an optional object specifying device\n[configuration options](#configuration-options)\n\nSynchronous open. Returns a new SpiDevice object.\n\n### device.transfer(message, cb)\n- message - an array of one or more read+write transfers\n- cb - completion callback\n\nAsynchronous message transfer. An SPI\n[message](#message) is an array of one or\nmore read+write transfers. The completion callback gets two arguments (err,\nmessage). Returns this.\n\n### device.transferSync(message)\n- message - an array of one or more read+write transfers\n\nSynchronous message transfer. An SPI\n[message](#message) is an array of one or\nmore read+write transfers. Returns this.\n\n### device.getOptions(cb)\n- cb - completion callback\n\nAsynchronously read device\n[configuration options](#configuration-options).\nThe completion callback gets two arguments (err, options) where options is an\nobject describing the device configuration options. Returns this.\n\n### device.getOptionsSync()\n\nSynchronously read device\n[configuration options](#configuration-options).\nReturns an object describing the device configuration options.\n\n### device.setOptions(options, cb)\n- options - an object specifying device\n[configuration options](#configuration-options)\n- cb - completion callback\n\nAsynchronously write device\n[configuration options](#configuration-options).\nThe completion callback gets one argument (err). Returns this.\n\n### device.setOptionsSync(options)\n- options - an object specifying device\n[configuration options](#configuration-options)\n\nSynchronously write device\n[configuration options](#configuration-options).\nReturns this.\n\n### device.close(cb)\n- cb - completion callback\n\nAsynchronous close. Frees system resources used by this instance. The\ncompletion callback gets one argument (err). Returns null.\n\n### device.closeSync()\n\nSynchronous close. Frees system resources used by this instance. Returns null.\n\n### MODE0\n\nSPI mode number 0.\n\n### MODE1\n\nSPI mode number 1.\n\n### MODE2\n\nSPI mode number 2.\n\n### MODE3\n\nSPI mode number 3.\n\n### Message\n\nAn SPI message is an array of one or more read+write transfers. A transfer\nis an object with the properties listed below. Most of the properties are\noptional. Note that although both sendBuffer and receiveBuffer are optional,\nat least one one of them must be specified.\n\n- byteLength - number, 32-bit, the number of bytes to transfer\n- sendBuffer - optional Buffer, transmit data\n- receiveBuffer - optional Buffer, receive data\n- speedHz - optional number, 32-bit, override of the device's clock frequency\nin Hertz\n- microSecondDelay - optional number, 16-bit, delay after the last bit transfer\nbefore optionally deselecting the device before the next transfer, default 0\n- bitsPerWord - optional number, 8-bit, override of the device's wordsize\n- chipSelectChange - optional boolean, true to deselect device before starting\nthe next transfer, default false\n\n### Configuration Options\n\nDevice configuration options can be optionally specified when a device is\nopened with the `open` or `openSync` methods. They can also be specified at a\nlater point with the `setOptions` or `setOptionsSync` methods. When calling\nthese methods, only the options that need to be set need to be specified in the\noptions object passed to those methods. All options are optional and the\nappropriate defaults will be used for options that are not specified.\n\nThe options supported varies from system to system and will depend on the\ndevice drivers used on those systems.\n\nConfigurations options can be read with the `getOptions` and `getOptionsSync`\nmethods.\n\n**IMPORTANT** The semantics of **chipSelectHigh** have changed with Linux\nkernel 5. To the best of my knowledge, the chipSelectHigh option no longer\nserves any purpose when used from user space with Linux kernel 5 and should\nnot be used. With Linux kernel 5, the chip select is assumed to be active low.\nWith Linux kernel 5, if an SPI device has has active high chip select, it's\nchip select must be controlled manually with a GPIO using a module such as\n[onoff](https://github.com/fivdi/onoff). The chipSelectHigh option has been\ncrossed out below but it's still available for usage on older kernels.\n\n- mode - number, 2-bit, MODE0, MODE1, MODE2, or MODE3, default MODE0\n- ~~chipSelectHigh - boolean, true for active high chip select, default false~~\n- lsbFirst - boolean, true for least significant bit first transfer, default\nfalse\n- threeWire - boolean, true for shared MISO/MOSI signals, default false\n- loopback - boolean, true for loopback mode, default false\n- noChipSelect - boolean, true for 1 device per bus, no chip select, default\nfalse\n- ready - boolean, true if device pulls low to pause, default false\n- bitsPerWord - number, 8-bit, device word size, default 8\n- maxSpeedHz - number, 32-bit, device clock frequency in Hertz, default system\nspecific\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffivdi%2Fspi-device","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffivdi%2Fspi-device","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffivdi%2Fspi-device/lists"}