Skip to content

Instantly share code, notes, and snippets.

@stephanebachelier
Forked from mikaelleven/readme.md
Last active December 9, 2023 01:24
Show Gist options
  • Select an option

  • Save stephanebachelier/b26f075aa40ad862379235930866b6e7 to your computer and use it in GitHub Desktop.

Select an option

Save stephanebachelier/b26f075aa40ad862379235930866b6e7 to your computer and use it in GitHub Desktop.

Revisions

  1. stephanebachelier revised this gist Sep 3, 2016. 1 changed file with 11 additions and 8 deletions.
    19 changes: 11 additions & 8 deletions spi_dump.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    // NodeJS SPI Dump for MCP3008 - Created by Mikael Levén
    // Original https://gist.github.com/mikaelleven/c3db08ae7837eb3c8698
    var rpio = require('rpio');

    rpio.spiBegin();
    @@ -15,25 +16,27 @@ for (var channelHeader = 0; channelHeader <= 7; channelHeader++) {
    setInterval(function() {
    for (var channel = 0; channel <= 7; channel++) {
    // Prepare TX buffer [trigger byte = 0x01] [channel 0 = 0x80 (128)] [placeholder = 0x01]
    var sendBuffer = new Buffer([0x01, (8 + channel << 4), 0x01]);
    var txBuffer = new Buffer([0x01, (8 + channel << 4), 0x01]);
    var rxBuffer = new Buffer(txBuffer.byteLength);

    var recieveBuffer = rpio.spiTransfer(sendBuffer, sendBuffer.length); // Send TX buffer and recieve RX buffer
    rpio.spiTransfer(txBuffer, rxBuffer, txBuffer.length); // Send TX buffer and recieve RX buffer

    // Extract value from output buffer. Ignore first byte.
    var junk = recieveBuffer[0],
    MSB = recieveBuffer[1],
    LSB = recieveBuffer[2];
    // Extract value from output buffer. Ignore first byte.
    var junk = rxBuffer[0],
    MSB = rxBuffer[1],
    LSB = rxBuffer[2];

    // Ignore first six bits of MSB, bit shift MSB 8 positions and
    // Ignore first six bits of MSB, bit shift MSB 8 positions and
    // finally combine LSB and MSB to get a full 10 bit value
    var value = ((MSB & 3) << 8) + LSB;
    var value = ((MSB & 3) << 8) + LSB;

    process.stdout.write(value.toString() + (channel == 7 ? '\n' : '\t'));
    };
    }, 1000);


    process.on('SIGTERM', function () {

    process.exit(0);
    });

  2. @mikaelleven mikaelleven revised this gist Dec 13, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ This is a simple script that reads all eight analog channels of an MCP3008 each

    I created this script to ease debugging of the MCP3008 ADC connected to my Raspberry Pi.

    If you need to troubleshoot SPI your can check out my guide how to test SPI using loopback "debug mode" https://mikaelleven.wordpress.com/2015/12/10/troubleshooting-spi-on-raspberry-pi-nodejs/.
    If you need to troubleshoot the SPI connection in itself you can check out my guide how to test SPI through the loopback "debug mode" https://mikaelleven.wordpress.com/2015/12/10/troubleshooting-spi-on-raspberry-pi-nodejs/.

    ### Installation

  3. @mikaelleven mikaelleven revised this gist Dec 13, 2015. 2 changed files with 53 additions and 4 deletions.
    8 changes: 5 additions & 3 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -5,15 +5,17 @@ This is a simple script that reads all eight analog channels of an MCP3008 each

    I created this script to ease debugging of the MCP3008 ADC connected to my Raspberry Pi.

    If you need to troubleshoot SPI your can check out my guide how to test SPI using loopback "debug mode" https://mikaelleven.wordpress.com/2015/12/10/troubleshooting-spi-on-raspberry-pi-nodejs/.

    ### Installation

    Download this gist and run the following command
    ```sh
    npm install rpio
    ```

    ### Usage

    ```js
    xx

    ```sh
    sudo node spi_dump.js
    ```
    49 changes: 48 additions & 1 deletion spi_dump.js
    Original file line number Diff line number Diff line change
    @@ -1 +1,48 @@
    Dump
    // NodeJS SPI Dump for MCP3008 - Created by Mikael Levén
    var rpio = require('rpio');

    rpio.spiBegin();
    //rpio.spiChipSelect(0); /* Use CE0 (slave 0) */
    //rpio.spiSetCSPolarity(0, rpio.LOW); /* Commonly chip enable (CE) pins are active low, and this is the default. */
    //rpio.spiSetClockDivider(256); /* MCP3008 max is ~1MHz, 256 == 0.98MHz */
    //rpio.spiSetDataMode(0);

    process.stdout.write('\x1b[36m');
    for (var channelHeader = 0; channelHeader <= 7; channelHeader++) {
    process.stdout.write('ch' + channelHeader.toString() + (channelHeader == 7 ? '\x1b[0m\n' : '\t'));
    }

    setInterval(function() {
    for (var channel = 0; channel <= 7; channel++) {
    // Prepare TX buffer [trigger byte = 0x01] [channel 0 = 0x80 (128)] [placeholder = 0x01]
    var sendBuffer = new Buffer([0x01, (8 + channel << 4), 0x01]);

    var recieveBuffer = rpio.spiTransfer(sendBuffer, sendBuffer.length); // Send TX buffer and recieve RX buffer

    // Extract value from output buffer. Ignore first byte.
    var junk = recieveBuffer[0],
    MSB = recieveBuffer[1],
    LSB = recieveBuffer[2];

    // Ignore first six bits of MSB, bit shift MSB 8 positions and
    // finally combine LSB and MSB to get a full 10 bit value
    var value = ((MSB & 3) << 8) + LSB;

    process.stdout.write(value.toString() + (channel == 7 ? '\n' : '\t'));
    };
    }, 1000);


    process.on('SIGTERM', function () {
    process.exit(0);
    });

    process.on('SIGINT', function () {
    process.exit(0);
    });

    process.on('exit', function () {
    console.log('\nShutting down, performing GPIO cleanup');
    rpio.spiEnd();
    process.exit(0);
    });
  4. @mikaelleven mikaelleven revised this gist Dec 13, 2015. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,19 @@
    NodeJS SPI Dump for MCP3008
    ===========================

    This is a simple and easy to use **SPI** module for **node.js**.
    This is a simple script that reads all eight analog channels of an MCP3008 each second and outputs the result to the console.

    I created this script to ease debugging of analog values from a MCP3008 ADC connected to my Raspberry Pi.
    I created this script to ease debugging of the MCP3008 ADC connected to my Raspberry Pi.

    ### Installation

    ```sh
    npm install simplespi
    npm install rpio
    ```

    ### Usage

    ```js
    var simplespi = require( "simplespi" );
    xx

    var response = simplespi.send( "AA00FF", "/dev/spidev0.1" );
    ```
  5. @mikaelleven mikaelleven revised this gist Dec 13, 2015. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,20 @@
    Lorem
    NodeJS SPI Dump for MCP3008
    ===========================

    This is a simple and easy to use **SPI** module for **node.js**.

    I created this script to ease debugging of analog values from a MCP3008 ADC connected to my Raspberry Pi.

    ### Installation

    ```sh
    npm install simplespi
    ```

    ### Usage

    ```js
    var simplespi = require( "simplespi" );

    var response = simplespi.send( "AA00FF", "/dev/spidev0.1" );
    ```
  6. @mikaelleven mikaelleven created this gist Dec 13, 2015.
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Lorem
    1 change: 1 addition & 0 deletions spi_dump.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Dump