├── unittests ├── README.md ├── iopi │ ├── README.md │ ├── test.sh │ ├── resetInterrupts.js │ ├── invertBus.js │ ├── writeBus.js │ ├── getBusPullups.js │ ├── getBusPolarity.js │ ├── setBusPullups.js │ ├── getBusDirection.js │ ├── getInterruptOnBus.js │ ├── setBusDirection.js │ ├── setInterruptOnBus.js │ ├── readBus.js │ ├── getInterruptPolarity.js │ ├── readPin.js │ ├── mirrorInterrupts.js │ ├── readPort.js │ ├── setInterruptPolarity.js │ ├── getPortPullups.js │ ├── getPortPolarity.js │ ├── getPortDirection.js │ ├── readInterruptCapture.js │ ├── getInterruptOnPort.js │ ├── readInterruptStatus.js │ ├── invertPin.js │ ├── invertPort.js │ ├── getInterruptType.js │ ├── getInterruptDefaults.js │ ├── setInterruptDefaults.js │ ├── writePort.js │ ├── getPinPullup.js │ ├── getPinPolarity.js │ ├── getPinDirection.js │ ├── setPortPullups.js │ ├── getInterruptOnPin.js │ ├── setInterruptType.js │ ├── setPortDirection.js │ ├── setInterruptOnPort.js │ ├── IoPi.js │ ├── writePin.js │ ├── setPinPullup.js │ ├── setPinDirection.js │ └── setInterruptOnPin.js ├── iozero32 │ ├── test.sh │ ├── IOZero32.js │ ├── setBusPolarity.js │ ├── writeBus.js │ ├── getBusPolarity.js │ ├── getBusDirection.js │ ├── setBusDirection.js │ ├── readBus.js │ ├── readPin.js │ ├── readPort.js │ ├── getPortPolarity.js │ ├── getPortDirection.js │ ├── setPinPolarity.js │ ├── setPortPolarity.js │ ├── getPinPolarity.js │ ├── writePort.js │ ├── getPinDirection.js │ ├── setPortDirection.js │ ├── writePin.js │ └── setPinDirection.js └── unittests.js ├── examples ├── adcdacpi │ ├── dacwrite.js │ └── adcread.js ├── expanderpi │ ├── dacwrite.js │ ├── rtcsetdate.js │ ├── iowrite.js │ ├── adcread.js │ ├── adcspeedtest.js │ ├── rtcmemory.js │ └── ioread.js ├── rtcpi │ ├── rtcsetdate.js │ └── rtcmemory.js ├── servopi │ ├── setallcalladdress.js │ ├── pwm.js │ └── servomove.js ├── i2cswitch │ └── switchchannel.js ├── iopi │ ├── iopiwrite.js │ ├── iopiread.js │ └── iopiread2boards.js ├── iozero32 │ ├── iowrite.js │ └── ioread.js ├── adcpi │ ├── demo-logvoltage.js │ └── demo-readvoltage.js └── adcdifferentialpi │ ├── demo-logvoltage.js │ └── demo-readvoltage.js ├── .gitignore ├── LICENSE ├── README.md └── lib ├── adcpi └── README.md ├── adcdifferentialpi └── README.md ├── i2cswitch ├── README.md └── i2cswitch.js ├── adcdacpi ├── README.md └── adcdacpi.js ├── rtcpi ├── README.md └── rtcpi.js ├── iozero32 └── README.md ├── servopi └── README.md └── expanderpi └── README.md /unittests/README.md: -------------------------------------------------------------------------------- 1 | AB Electronics UK IO Zero 32 Node JS Library Test Files 2 | ===== 3 | 4 | This folder contains unit test files for testing the functions available in the Node JS library. 5 | -------------------------------------------------------------------------------- /unittests/iopi/README.md: -------------------------------------------------------------------------------- 1 | AB Electronics UK IO Pi Node JS Library Test Files 2 | ===== 3 | 4 | This folder contains unit test files for testing the functions available in the IO Pi library. 5 | -------------------------------------------------------------------------------- /unittests/iopi/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # build test 4 | 5 | #!/bin/bash 6 | if [ "$#" -eq "0" ] 7 | then 8 | for file in *.js; do 9 | node "$file" 10 | done 11 | exit 1 12 | else 13 | file=$1 14 | node "$file" 15 | fi 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /unittests/iozero32/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # build test 4 | 5 | #!/bin/bash 6 | if [ "$#" -eq "0" ] 7 | then 8 | for file in *.js; do 9 | node "$file" 10 | done 11 | exit 1 12 | else 13 | file=$1 14 | node "$file" 15 | fi 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /examples/adcdacpi/dacwrite.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK ADC DAC Pi DAC demo 4 | * Version 1.0 Created 06/07/2016 5 | * 6 | * Requires rpio to be installed, install with: npm install rpio 7 | * run with: sudo node adcread.js 8 | * ================================================ 9 | */ 10 | 11 | // Initialise the ADC DAC device 12 | 13 | var adcdac = require('../../lib/adcdacpi/adcdacpi'); 14 | 15 | dac = new ADCDAC(); 16 | dac.setDACGain(1); 17 | dac.setDACVoltage(1, 1.0); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /examples/expanderpi/dacwrite.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK Expander Pi - DAC demo 4 | * Version 1.0 Created 19/06/2017 5 | * 6 | * Requires rpio to be installed, install with: npm install rpio 7 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 8 | * 9 | * run with: sudo node dacwrite.js 10 | * ================================================ 11 | */ 12 | 13 | // link to the expanderpi library 14 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 15 | 16 | // create a new instance of the DAC 17 | dac = new ExpanderPiDAC(); 18 | 19 | // set the DAC gain to 1 20 | dac.setDACGain(1); 21 | 22 | // set the voltage 23 | dac.setDACVoltage(1, 0.8); 24 | dac.setDACVoltage(2, 1.5); -------------------------------------------------------------------------------- /unittests/iopi/resetInterrupts.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test resetInterrupts function 4 | * 5 | * run with: node resetInterrupts 6 | * ================================================ 7 | 8 | This test validates the resetInterrupts function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > resetInterrupts()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | bus.resetInterrupts(); 27 | 28 | 29 | 30 | 31 | // get test result 32 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/rtcpi/rtcsetdate.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK RTC Pi demo 4 | * Version 1.0 Created 26/07/2016 5 | * 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node rtcsetdate.js 8 | * ================================================ 9 | */ 10 | 11 | // link to the rtcpi library 12 | var rtcpi = require('../../lib/rtcpi/rtcpi'); 13 | 14 | // create an rtc object 15 | var rtc = new RTCPi(); 16 | 17 | // create a new javascript date object 18 | var d = new Date(2016, 07, 04, 10, 23, 00, 00); 19 | 20 | // set the date on the RTC Pi using the date object 21 | rtc.setDate(d); 22 | 23 | // create a timer object that runs every second 24 | var myClock = setInterval(clockTimer, 1000); 25 | 26 | function clockTimer() { 27 | // read the date from the RTC and write it to the console 28 | console.log(rtc.readDate().toISOString()); 29 | } -------------------------------------------------------------------------------- /examples/servopi/setallcalladdress.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * 4 | * AB Electronics UK Servo Pi demo 5 | * Version 1.0 Created 29/07/2016 6 | * 7 | * Requires rpio to be installed, install with: npm install rpio 8 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 9 | * run with: sudo node setallcalladdress.js 10 | * ================================================ 11 | 12 | * This demo shows how to set the I2C address for the All Call function 13 | * All Call allows you to control several Servo Pi boards simultaneously on the same I2C address 14 | */ 15 | 16 | 17 | // link to the servopi library 18 | var servopi = require('../../lib/servopi/servopi'); 19 | 20 | // create a servopi object 21 | var pwm = new PWM(0x40); 22 | 23 | // Set the all-call address to 0x30 24 | pwm.setAllCallAddress(0x30); 25 | 26 | // Disable the all call address 27 | // pwm.disableAllCallAddress(); 28 | 29 | // Enable the all call address 30 | pwm.enableAllCallAddress(); 31 | -------------------------------------------------------------------------------- /unittests/iopi/invertBus.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test invertBus function 4 | * 5 | * run with: node invertBus 6 | * ================================================ 7 | 8 | This test validates the invertBus function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > invertBus()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | for (var a = 0; a < 65535; a++){ 27 | bus.invertBus(a); 28 | if (a != test.i2c_emulator_read_word_data(test.MCP23017_IPOLA)){ 29 | test.test_fail("unexpected register value"); 30 | } 31 | } 32 | 33 | 34 | 35 | 36 | // get test result 37 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/adcdacpi/adcread.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK ADC DAC Pi ADC demo 4 | * Version 1.0 Created 06/07/2016 5 | * 6 | * Requires rpio to be installed, install with: npm install rpio 7 | * run with: sudo node adcread.js 8 | * ================================================ 9 | */ 10 | 11 | // link to the adcdacpi library 12 | 13 | var adcdac = require('../../lib/adcdacpi/adcdacpi'); 14 | 15 | // create an instance of the adcdac class 16 | 17 | var adc = new ADCDAC(); 18 | 19 | // set the reference voltage 20 | 21 | adc.setADCRefVoltage(3.3); 22 | 23 | // read the raw value and voltage from channels 1 and 2 and print them on the console. 24 | 25 | var x = 0; 26 | 27 | while (x < 10) { 28 | x++; 29 | console.log('Reading 1 Voltage: ' + adc.readADCVoltage(1, 0)); 30 | console.log('Reading 1 Raw: ' + adc.readADCRaw(1, 0)); 31 | console.log('Reading 2 Voltage: ' + adc.readADCVoltage(2, 0)); 32 | console.log('Reading 2 Raw: ' + adc.readADCRaw(2, 0)); 33 | } -------------------------------------------------------------------------------- /examples/expanderpi/rtcsetdate.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK Expander Pi - RTC date demo 4 | * Version 1.0 Created 19/06/2017 5 | * 6 | * Requires rpio to be installed, install with: npm install rpio 7 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 8 | * 9 | * run with: sudo node rtcsetdate.js 10 | * ================================================ 11 | */ 12 | 13 | // link to the expanderpi library 14 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 15 | 16 | // create an rtc object 17 | var rtc = new ExpanderPiRTC(); 18 | 19 | // create a new javascript date object 20 | var d = new Date(2016, 07, 04, 9, 23, 00, 00); 21 | 22 | // set the date on the RTC Pi using the date object 23 | rtc.setDate(d); 24 | 25 | // create a timer object that runs every second 26 | var myClock = setInterval(clockTimer, 1000); 27 | 28 | function clockTimer() { 29 | // read the date from the RTC and write it to the console 30 | console.log(rtc.readDate().toISOString()); 31 | } -------------------------------------------------------------------------------- /unittests/iozero32/IOZero32.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test getBusPullups function 4 | * 5 | * run with: node IOZero32 6 | * ================================================ 7 | 8 | This test validates the IOZero32 init function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > IOZero32()"); 23 | 24 | // out of bounds tests 25 | try 26 | { 27 | var bus1 = new IOZero32(0x19); 28 | test.test_exception_failed("I2C address low out of bounds"); 29 | } 30 | catch(error){ } 31 | 32 | try 33 | { 34 | var bus2 = new IOZero32(0x28); 35 | test.test_exception_failed("I2C address high out of bounds"); 36 | } 37 | catch(error){ } 38 | 39 | // get test result 40 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/setBusPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test setBusPolarity function 4 | * 5 | * run with: node setBusPolarity 6 | * ================================================ 7 | 8 | This test validates the setBusPolarity function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > setBusPolarity()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | for (var a = 0; a < 65535; a++){ 27 | bus.setBusPolarity(a); 28 | if (a != test.i2c_emulator_read_word_data(test.PCA9535_INVERTPORT0)){ 29 | test.test_fail("unexpected register value"); 30 | } 31 | } 32 | 33 | 34 | 35 | 36 | // get test result 37 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/i2cswitch/switchchannel.js: -------------------------------------------------------------------------------- 1 | /** 2 | * AB Electronics UK I2C Switch demo 3 | * Version 1.0 Created 28/11/2019 4 | * 5 | * Requires rpio to be installed, install with: npm install rpio 6 | * Requires i2c-switch to be installed, install with: npm install i2c-switch 7 | * run with: sudo node switchchannel.js 8 | * 9 | * This demo shows how to switch the I2C bus to a selected channel 10 | * and then read the state of that channel to confirm it has been enabled. 11 | */ 12 | 13 | // link to the i2cswitch library 14 | const i2cswitch = require('../../lib/i2cswitch/i2cswitch'); 15 | 16 | // create an I2CSwitch object 17 | const switchobject = new I2CSwitch(0x70); 18 | 19 | // channel to select 20 | const channel = 4 21 | 22 | // Set the switch to channel 4 23 | switchobject.switchChannel(channel); 24 | 25 | // get the status of the selected channel 26 | const result = switchobject.getChannelState(channel) 27 | 28 | if (result === true){ 29 | console.log('Switched to channel ' + channel) 30 | } 31 | else{ 32 | console.log('Error switching to channel ' + channel) 33 | } 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /unittests/iopi/writeBus.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test writeBus function 4 | * 5 | * run with: node writeBus 6 | * ================================================ 7 | 8 | This test validates the writeBus function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > writeBus()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | for (var a = 0; a < 65535; a++){ 27 | bus.writeBus(a); 28 | 29 | var x = 0; 30 | 31 | x = test.i2c_emulator_read_word_data(test.MCP23017_GPIOA); // read value from registers 32 | if (x != a){ 33 | test.test_fail("failed to set bus value"); 34 | break; 35 | } 36 | } 37 | 38 | 39 | 40 | 41 | // get test result 42 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/servopi/pwm.js: -------------------------------------------------------------------------------- 1 | /** 2 | * AB Electronics UK Servo Pi demo 3 | * Version 1.0 Created 29/07/2016 4 | * 5 | * Requires rpio to be installed, install with: npm install rpio 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node pwm.js 8 | * 9 | * This demo shows how to set a 1KHz output frequency and change the pulse width between the minimum and maximum values 10 | */ 11 | 12 | // link to the servopi library 13 | var servopi = require('../../lib/servopi/servopi'); 14 | 15 | // create a pwm object 16 | var pwm = new PWM(0x40); 17 | 18 | // Set PWM frequency to 1 Khz and enable the output 19 | pwm.setPWMFrequency(1000); 20 | pwm.outputEnable(); 21 | 22 | var x = 0; 23 | 24 | // Update the pulse width on channel 1, incrementing it up to 4059 and back down to 0 25 | while (1) { 26 | for (x = 0; x < 4095; x++){ 27 | pwm.setPWM(1, 0, x); 28 | pwm.setPWM(2, 0, x); 29 | pwm.setPWM(3, 0, x); 30 | } 31 | for (x = 4095; x > 0; x--) { 32 | pwm.setPWM(1, 0, x); 33 | pwm.setPWM(2, 0, x); 34 | pwm.setPWM(3, 0, x); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /unittests/iopi/getBusPullups.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getBusPullups function 4 | * 5 | * run with: node getBusPullups 6 | * ================================================ 7 | 8 | This test validates the getBusPullups function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getBusPullups()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | for (var a = 0; a < 65534; a++) { 29 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUA, a); // set register value 30 | x = bus.getBusPullups(); // read value from registers 31 | if (x != a) { 32 | test.test_fail("failed to get bus direction"); 33 | break; 34 | } 35 | } 36 | 37 | // get test result 38 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getBusPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getBusPolarity function 4 | * 5 | * run with: node getBusPolarity 6 | * ================================================ 7 | 8 | This test validates the getBusPolarity function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getBusPolarity()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | for (var a = 0; a < 65534; a++) { 29 | test.i2c_emulator_write_word_data(test.MCP23017_IPOLA, a); // set register value 30 | x = bus.getBusPolarity(); // read value from registers 31 | if (x != a) { 32 | test.test_fail("failed to get bus direction"); 33 | break; 34 | } 35 | } 36 | 37 | // get test result 38 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setBusPullups.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setBusPullups function 4 | * 5 | * run with: node setBusPullups 6 | * ================================================ 7 | 8 | This test validates the setBusPullups function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setBusPullups()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | for (var a = 0; a < 65535; a++){ 27 | bus.setBusPullups(a); 28 | 29 | var x = 0; 30 | 31 | x = test.i2c_emulator_read_word_data(test.MCP23017_GPPUA); // read value from registers 32 | if (x != a){ 33 | test.test_fail("failed to set bus pullup"); 34 | break; 35 | } 36 | } 37 | 38 | 39 | 40 | 41 | // get test result 42 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getBusDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getBusDirection function 4 | * 5 | * run with: node getBusDirection 6 | * ================================================ 7 | 8 | This test validates the getBusDirection function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getBusDirection()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | for (var a = 0; a < 65534; a++) { 29 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRA, a); // set register value 30 | x = bus.getBusDirection(); // read value from registers 31 | if (x != a) { 32 | test.test_fail("failed to get bus direction"); 33 | break; 34 | } 35 | } 36 | 37 | // get test result 38 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/writeBus.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test writeBus function 4 | * 5 | * run with: node writeBus 6 | * ================================================ 7 | 8 | This test validates the writeBus function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > writeBus()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | for (var a = 0; a < 65535; a++){ 27 | bus.writeBus(a); 28 | 29 | var x = 0; 30 | 31 | x = test.i2c_emulator_read_word_data(test.PCA9535_OUTPUTPORT0); // read value from registers 32 | if (x != a){ 33 | test.test_fail("failed to set bus value"); 34 | break; 35 | } 36 | } 37 | 38 | 39 | 40 | 41 | // get test result 42 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/expanderpi/iowrite.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK Expander Pi - IO write demo 4 | * Version 1.0 Created 19/06/2017 5 | * 6 | * Requires rpio to be installed, install with: npm install rpio 7 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 8 | * 9 | * run with: sudo node iowrite.js 10 | * ================================================ 11 | */ 12 | 13 | // This example creates a square wave output on pin 1 of the IO bus by switching a pin on and off at 100ms intervals 14 | 15 | // link to the expanderpi library 16 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 17 | 18 | var io = new ExpanderPiIO(); 19 | 20 | // Set port 0 to be outputs 21 | io.setPortDirection(0, 0x00); 22 | 23 | // Create a timer that runs every 100ms 24 | var x = 0; 25 | var myVar = setInterval(myTimer, 100); 26 | 27 | 28 | // change the state of the output based on the x variable. This will toggle the pin on and off 29 | function myTimer() { 30 | 31 | io.writePin(1, x); 32 | 33 | if (x == 0) { 34 | x = 1; 35 | } 36 | else { x = 0; } 37 | 38 | } -------------------------------------------------------------------------------- /unittests/iopi/getInterruptOnBus.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getInterruptOnBus function 4 | * 5 | * run with: node getInterruptOnBus 6 | * ================================================ 7 | 8 | This test validates the getInterruptOnBus function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getInterruptOnBus()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | for (var a = 0; a < 65535; a++){ 29 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENA, a); 30 | x = bus.getInterruptOnBus(); 31 | if (x != a){ 32 | test.test_fail("failed to get interrupt on bus"); 33 | break; 34 | } 35 | } 36 | 37 | 38 | 39 | 40 | // get test result 41 | test.test_outcome(); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Brian Dorey 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /unittests/iopi/setBusDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setBusDirection function 4 | * 5 | * run with: node setBusDirection 6 | * ================================================ 7 | 8 | This test validates the setBusDirection function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setBusDirection()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | for (var a = 0; a < 65535; a++){ 29 | bus.setBusDirection(a); 30 | 31 | x = test.i2c_emulator_read_word_data(test.MCP23017_IODIRA); // read value from registers 32 | if (x != a){ 33 | test.test_fail("failed to set bus direction"); 34 | break; 35 | } 36 | } 37 | 38 | 39 | 40 | 41 | // get test result 42 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/getBusPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test getBusPolarity function 4 | * 5 | * run with: node getBusPolarity 6 | * ================================================ 7 | 8 | This test validates the getBusPolarity function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > getBusPolarity()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | var x = 0; 27 | 28 | for (var a = 0; a < 65534; a++) { 29 | test.i2c_emulator_write_word_data(test.PCA9535_INVERTPORT0, a); // set register value 30 | x = bus.getBusPolarity(); // read value from registers 31 | if (x != a) { 32 | test.test_fail("failed to get bus direction"); 33 | break; 34 | } 35 | } 36 | 37 | // get test result 38 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setInterruptOnBus.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setInterruptOnBus function 4 | * 5 | * run with: node setInterruptOnBus 6 | * ================================================ 7 | 8 | This test validates the setInterruptOnBus function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setInterruptOnBus()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | for (var a = 0; a < 65535; a++){ 29 | bus.setInterruptOnBus(a); 30 | 31 | x = test.i2c_emulator_read_word_data(test.MCP23017_GPINTENA); // read value from registers 32 | if (x != a){ 33 | test.test_fail("failed to set bus direction"); 34 | break; 35 | } 36 | } 37 | 38 | 39 | 40 | 41 | // get test result 42 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/getBusDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test getBusDirection function 4 | * 5 | * run with: node getBusDirection 6 | * ================================================ 7 | 8 | This test validates the getBusDirection function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > getBusDirection()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | var x = 0; 27 | 28 | for (var a = 0; a < 65534; a++) { 29 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT0, a); // set register value 30 | x = bus.getBusDirection(); // read value from registers 31 | if (x != a) { 32 | test.test_fail("failed to get bus direction"); 33 | break; 34 | } 35 | } 36 | 37 | // get test result 38 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/setBusDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test setBusDirection function 4 | * 5 | * run with: node setBusDirection 6 | * ================================================ 7 | 8 | This test validates the setBusDirection function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > setBusDirection()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | var x = 0; 27 | 28 | for (var a = 0; a < 65535; a++){ 29 | bus.setBusDirection(a); 30 | 31 | x = test.i2c_emulator_read_word_data(test.PCA9535_CONFIGPORT0); // read value from registers 32 | if (x != a){ 33 | test.test_fail("failed to set bus direction"); 34 | break; 35 | } 36 | } 37 | 38 | 39 | 40 | 41 | // get test result 42 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/iopi/iopiwrite.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Pin Write demo 4 | * Version 1.0 Created 07/07/2016 5 | * Version 1.1 Updated 16/ 03 / 2024 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node iopiwrite.js 8 | * ================================================ 9 | * 10 | * This example creates a square wave output on pin 1 of bus 1 by switching a pin on and off at 100ms intervals 11 | * 12 | * Initialise the IOPi device using the default addresses, you will need to 13 | * change the addresses if you have changed the jumpers on the IO Pi 14 | */ 15 | 16 | var iopi = require('../../lib/iopi/iopi'); 17 | 18 | var bus1 = new IoPi(0x20); 19 | 20 | // Set port 0 as outputs 21 | bus1.setPortDirection(0, 0x00); 22 | 23 | // Create a timer that runs every 100ms 24 | var x = 0; 25 | var myVar = setInterval(myTimer, 100); 26 | 27 | 28 | // change the state of the output based on the x variable. This will toggle the pin on and off 29 | function myTimer() { 30 | 31 | bus1.writePin(1, x); 32 | 33 | if (x == 0) { 34 | x = 1; 35 | } 36 | else { x = 0; } 37 | 38 | } -------------------------------------------------------------------------------- /unittests/iopi/readBus.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test readBus function 4 | * 5 | * run with: node readBus 6 | * ================================================ 7 | 8 | This test validates the readBus function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > readBus()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // Reset to 0 27 | bus.writeBus(0x0000); 28 | 29 | // Enable pullups 30 | bus.setBusDirection(0xFFFF); 31 | 32 | var x = 0; 33 | 34 | for (var a = 0; a < 65534; a++){ 35 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOA, a); // set register value 36 | x = bus.readBus(); // read value from registers 37 | if (x != a){ 38 | test.test_fail("failed to get bus value"); 39 | break; 40 | } 41 | } 42 | 43 | 44 | 45 | 46 | // get test result 47 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/iozero32/iowrite.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK - IO Zero 32 - Pin Write demo 4 | * Version 1.0 Created 10/05/2022 5 | * 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node iopiwrite.js 8 | * ================================================ 9 | * 10 | * This example creates a square wave output on pin 1 of bus 1 by switching a pin on and off at 100ms intervals 11 | * 12 | * Initialise the IOZero32 device using the default addresses, you will need to 13 | * change the addresses if you have changed the jumpers on the IO Zero 32 14 | */ 15 | 16 | const IOZero32 = require('../../lib/iozero32/iozero32'); 17 | 18 | const bus1 = new IOZero32(0x20); 19 | 20 | // Set port 0 as outputs 21 | bus1.setPortDirection(0, 0x00); 22 | 23 | // Create a timer that runs every 100ms 24 | let x = 0; 25 | const myVar = setInterval(myTimer, 100); 26 | 27 | 28 | // change the state of the output based on the x variable. This will toggle the pin on and off 29 | function myTimer() { 30 | 31 | bus1.writePin(1, x); 32 | 33 | if (x === 0) { 34 | x = 1; 35 | } 36 | else { x = 0; } 37 | 38 | } -------------------------------------------------------------------------------- /unittests/iozero32/readBus.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test readBus function 4 | * 5 | * run with: node readBus 6 | * ================================================ 7 | 8 | This test validates the readBus function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > readBus()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | // Reset to 0 27 | bus.writeBus(0x0000); 28 | 29 | // Enable pullups 30 | bus.setBusDirection(0xFFFF); 31 | 32 | var x = 0; 33 | 34 | for (var a = 0; a < 65534; a++){ 35 | test.i2c_emulator_write_word_data(test.PCA9535_INPUTPORT0, a); // set register value 36 | x = bus.readBus(); // read value from registers 37 | if (x != a){ 38 | test.test_fail("failed to get bus value"); 39 | break; 40 | } 41 | } 42 | 43 | 44 | 45 | 46 | // get test result 47 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getInterruptPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getInterruptPolarity function 4 | * 5 | * run with: node getInterruptPolarity 6 | * ================================================ 7 | 8 | This test validates the getInterruptPolarity function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getInterruptPolarity()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | test.i2c_emulator_write_byte_data(test.MCP23017_IOCON, 0xfd); 29 | x = bus.getInterruptPolarity(); 30 | if (x != 0){ 31 | test.test_exception_failed("get port failed when set to 0"); 32 | } 33 | 34 | test.i2c_emulator_write_byte_data(test.MCP23017_IOCON, 0x02); 35 | x = bus.getInterruptPolarity(); 36 | if (x != 1){ 37 | test.test_exception_failed("get port failed when set to 0"); 38 | } 39 | 40 | 41 | 42 | 43 | // get test result 44 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/readPin.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test readPin function 4 | * 5 | * run with: node readPin 6 | * ================================================ 7 | 8 | This test validates the readPin function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > readPin()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.readPin(0); 30 | test.test_exception_failed("value low boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | try 35 | { 36 | bus.readPin(17); 37 | test.test_exception_failed("value high boundary out of bounds"); 38 | } 39 | catch(error){ } 40 | 41 | for (var a = 1; a < 17; a++){ 42 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOA, Math.pow(2, a-1)); // set register 43 | if (bus.readPin(a) != 1) 44 | { 45 | test.test_fail("unexpected register value"); 46 | } 47 | } 48 | 49 | 50 | 51 | 52 | // get test result 53 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/readPin.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test readPin function 4 | * 5 | * run with: node readPin 6 | * ================================================ 7 | 8 | This test validates the readPin function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > readPin()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.readPin(0); 30 | test.test_exception_failed("value low boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | try 35 | { 36 | bus.readPin(17); 37 | test.test_exception_failed("value high boundary out of bounds"); 38 | } 39 | catch(error){ } 40 | 41 | for (var a = 1; a < 17; a++){ 42 | test.i2c_emulator_write_word_data(test.PCA9535_INPUTPORT0, Math.pow(2, a-1)); // set register 43 | if (bus.readPin(a) != 1) 44 | { 45 | test.test_fail("unexpected register value"); 46 | } 47 | } 48 | 49 | 50 | 51 | 52 | // get test result 53 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/mirrorInterrupts.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test mirrorInterrupts function 4 | * 5 | * run with: node mirrorInterrupts 6 | * ================================================ 7 | 8 | This test validates the mirrorInterrupts function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > mirrorInterrupts()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.mirrorInterrupts(2); 30 | test.test_exception_failed("high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | bus.mirrorInterrupts(1); 35 | 36 | var a = test.i2c_emulator_read_byte_data(test.MCP23017_IOCON); 37 | if (a != 0x42) // check value has been set in register 38 | { 39 | test.test_fail("unexpected register value"); 40 | } 41 | 42 | bus.mirrorInterrupts(0); 43 | 44 | a = test.i2c_emulator_read_byte_data(test.MCP23017_IOCON); 45 | if (a != 0x02) // check value has been set in register 46 | { 47 | test.test_fail("unexpected register value"); 48 | } 49 | 50 | 51 | 52 | 53 | // get test result 54 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/servopi/servomove.js: -------------------------------------------------------------------------------- 1 | /** 2 | * AB Electronics UK Servo Pi demo 3 | * Version 2.0 Created 04/09/2018 4 | * 5 | * Requires rpio to be installed, install with: npm install rpio 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node servomove.js 8 | * ================================================ 9 | 10 | * This demo shows how to move a servo between 3 different positions. 11 | * The low limit is set with a value of 1ms 12 | * The high limit is set with a value of 2ms 13 | * Both of these values can be adjusted to match the limits on the servo being tested. 14 | */ 15 | 16 | // link to the servopi library 17 | var servo = require('../../lib/servopi/servopi'); 18 | 19 | // create a servo object on I2C channel 0x40 with a lower limit of 1ms, 20 | // a high limit of 2ms and reset the Servo Pi to its default state 21 | servo = new Servo(0x40, 1.0, 2.0, true); 22 | 23 | // Set PWM frequency to 50Hz (20ms) and enable the output 24 | servo.setFrequency(50); 25 | servo.outputEnable(); 26 | 27 | // create a positions array and a counter variable 28 | var positions = [1, 175, 250]; 29 | var count = 0; 30 | 31 | // create a timer object that runs every second 32 | var myTimer = setInterval(clockTimer, 1000); 33 | 34 | function clockTimer() { 35 | // move the servo channel 1 to the three positions 36 | servo.move(1, positions[count], 250); 37 | count++; 38 | if (count >= positions.length) { 39 | count = 0; 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /examples/expanderpi/adcread.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK Expander Pi - ADC Read Demo 4 | * Version 1.0 Created 19/06/2017 5 | * 6 | * Requires rpio to be installed, install with: npm install rpio 7 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 8 | * 9 | * run with: sudo node adcread.js 10 | * ================================================ 11 | */ 12 | 13 | console.reset = function () { 14 | return process.stdout.write('\033c'); 15 | } 16 | 17 | // link to the expanderpi library 18 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 19 | 20 | // create an instance of the adcdac class 21 | 22 | var adc = new ExpanderPiADC(); 23 | 24 | // set the reference voltage 25 | 26 | adc.setADCRefVoltage(4.096); 27 | 28 | // read the raw value and voltage from channels 1 and 2 and print them on the console. 29 | 30 | var x = 0; 31 | 32 | console.log('Reading 1 Voltage: ' + adc.readADCVoltage(1, 0)); 33 | console.log('Reading 2 Voltage: ' + adc.readADCVoltage(2, 0)); 34 | console.log('Reading 3 Voltage: ' + adc.readADCVoltage(3, 0)); 35 | console.log('Reading 4 Voltage: ' + adc.readADCVoltage(4, 0)); 36 | console.log('Reading 5 Voltage: ' + adc.readADCVoltage(5, 0)); 37 | console.log('Reading 6 Voltage: ' + adc.readADCVoltage(6, 0)); 38 | console.log('Reading 7 Voltage: ' + adc.readADCVoltage(7, 0)); 39 | console.log('Reading 8 Voltage: ' + adc.readADCVoltage(8, 0)); 40 | 41 | 42 | -------------------------------------------------------------------------------- /unittests/iopi/readPort.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test readPort function 4 | * 5 | * run with: node readPort 6 | * ================================================ 7 | 8 | This test validates the readPort function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > readPort()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try 30 | { 31 | bus.readPort(2); 32 | test.test_exception_failed("high boundary out of bounds"); 33 | } 34 | catch(error){ } 35 | 36 | for (var a = 0; a < 255; a++){ 37 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOA, a); 38 | x = bus.readPort(0); 39 | if (x != a){ 40 | test.test_exception_failed("get port value failed when set to 0"); 41 | break; 42 | } 43 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOB, a); 44 | x = bus.readPort(1); 45 | if (x != a){ 46 | test.test_exception_failed("get port value failed when set to 1"); 47 | break; 48 | } 49 | } 50 | 51 | 52 | 53 | 54 | // get test result 55 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setInterruptPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setInterruptPolarity function 4 | * 5 | * run with: node setInterruptPolarity 6 | * ================================================ 7 | 8 | This test validates the setInterruptPolarity function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setInterruptPolarity()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setInterruptPolarity(2); 30 | test.test_exception_failed("high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | test.i2c_emulator_write_byte_data(test.MCP23017_IOCON, 0x80); 35 | 36 | bus.setInterruptPolarity(1); 37 | 38 | var x = test.i2c_emulator_read_byte_data(test.MCP23017_IOCON); 39 | if (x != 0x82){ 40 | test.test_exception_failed("failed to set interrupt polarity to 1"); 41 | } 42 | 43 | bus.setInterruptPolarity(0); 44 | x = test.i2c_emulator_read_byte_data(test.MCP23017_IOCON); 45 | if (x != 0x80){ 46 | test.test_exception_failed("failed to set interrupt polarity to 0"); 47 | } 48 | 49 | 50 | 51 | 52 | // get test result 53 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getPortPullups.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getPortPullups function 4 | * 5 | * run with: node getPortPullups 6 | * ================================================ 7 | 8 | This test validates the getPortPullups function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getPortPullups()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try 30 | { 31 | bus.getPortPullups(2); 32 | test.test_exception_failed("port out of bounds"); 33 | } 34 | catch (error){} 35 | 36 | for (var a = 0; a < 255; a++){ 37 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUA, a); 38 | x = bus.getPortPullups(0); 39 | if (x != a){ 40 | test.test_exception_failed("get port failed when set to 0"); 41 | break; 42 | } 43 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUB, a); 44 | x = bus.getPortPullups(1); 45 | if (x != a){ 46 | test.test_exception_failed("get port failed when set to 1"); 47 | break; 48 | } 49 | } 50 | 51 | 52 | 53 | 54 | // get test result 55 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getPortPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getPortPolarity function 4 | * 5 | * run with: node getPortPolarity 6 | * ================================================ 7 | 8 | This test validates the getPortPolarity function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getPortPolarity()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try 30 | { 31 | bus.getPortPolarity(2); 32 | test.test_exception_failed("port out of bounds"); 33 | } 34 | catch (error){} 35 | 36 | for (var a = 0; a < 255; a++){ 37 | test.i2c_emulator_write_word_data(test.MCP23017_IPOLA, a); 38 | x = bus.getPortPolarity(0); 39 | if (x != a){ 40 | test.test_exception_failed("get port failed when set to 0"); 41 | break; 42 | } 43 | test.i2c_emulator_write_word_data(test.MCP23017_IPOLB, a); 44 | x = bus.getPortPolarity(1); 45 | if (x != a){ 46 | test.test_exception_failed("get port failed when set to 1"); 47 | break; 48 | } 49 | } 50 | 51 | 52 | 53 | 54 | // get test result 55 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/readPort.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test readPort function 4 | * 5 | * run with: node readPort 6 | * ================================================ 7 | 8 | This test validates the readPort function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > readPort()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try 30 | { 31 | bus.readPort(2); 32 | test.test_exception_failed("high boundary out of bounds"); 33 | } 34 | catch(error){ } 35 | 36 | for (var a = 0; a < 255; a++){ 37 | test.i2c_emulator_write_word_data(test.PCA9535_INPUTPORT0, a); 38 | x = bus.readPort(0); 39 | if (x != a){ 40 | test.test_exception_failed("get port value failed when set to 0"); 41 | break; 42 | } 43 | test.i2c_emulator_write_word_data(test.PCA9535_INPUTPORT1, a); 44 | x = bus.readPort(1); 45 | if (x != a){ 46 | test.test_exception_failed("get port value failed when set to 1"); 47 | break; 48 | } 49 | } 50 | 51 | 52 | 53 | 54 | // get test result 55 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getPortDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getPortDirection function 4 | * 5 | * run with: node getPortDirection 6 | * ================================================ 7 | 8 | This test validates the getPortDirection function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getPortDirection()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try 30 | { 31 | bus.getPortDirection(2); 32 | test.test_exception_failed("port out of bounds"); 33 | } 34 | 35 | catch (error){} 36 | 37 | for (var a = 0; a < 255; a++){ 38 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRA, a); 39 | x = bus.getPortDirection(0); 40 | if (x != a){ 41 | test.test_exception_failed("get port failed when set to 0"); 42 | break; 43 | } 44 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRB, a); 45 | x = bus.getPortDirection(1); 46 | if (x != a){ 47 | test.test_exception_failed("get port failed when set to 1"); 48 | break; 49 | } 50 | } 51 | 52 | 53 | 54 | 55 | // get test result 56 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/readInterruptCapture.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test readInterruptCapture function 4 | * 5 | * run with: node readInterruptCapture 6 | * ================================================ 7 | 8 | This test validates the readInterruptCapture function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > readInterruptCapture()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | 27 | // out of bounds tests 28 | try 29 | { 30 | bus.readInterruptCapture(2); 31 | test.test_exception_failed("high boundary out of bounds"); 32 | } 33 | catch(error){ } 34 | 35 | for (var a = 0; a < 255; a++){ 36 | test.i2c_emulator_write_word_data(test.MCP23017_INTCAPA, a); 37 | x = bus.readInterruptCapture(0); 38 | if (x != a){ 39 | test.test_exception_failed("get interrupt capture failed when set to 0"); 40 | break; 41 | } 42 | test.i2c_emulator_write_word_data(test.MCP23017_INTCAPB, a); 43 | x = bus.readInterruptCapture(1); 44 | if (x != a){ 45 | test.test_exception_failed("get interrupt capture failed when set to 1"); 46 | break; 47 | } 48 | } 49 | 50 | 51 | 52 | // get test result 53 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/adcpi/demo-logvoltage.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK ADC Pi 8-Channel ADC logging demo 4 | * Version 1.0 Created 06/07/2016 5 | * 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node demo-logvoltage.js 8 | * ================================================ 9 | */ 10 | 11 | // Initialise the ADC device using the default addresses and sample rate, 12 | // change this value if you have changed the address selection jumpers 13 | 14 | // Sample rate can be 12, 14, 16 or 18 15 | 16 | var adcpi = require('../../lib/adcpi/adcpi'); 17 | var fs = require('fs'); 18 | 19 | var logStream = fs.createWriteStream('./logFile.log', { flags: 'a' }); 20 | 21 | var spawn = require('child_process').spawn, 22 | ls = spawn('ls', ['-lh', '/usr']); 23 | 24 | ls.stdout.pipe(logStream); 25 | ls.stderr.pipe(logStream); 26 | 27 | ls.on('close', function (code) { 28 | console.log('child process exited with code ' + code); 29 | }); 30 | 31 | 32 | var adc = new ADCPi(0x68, 0x69, 18); 33 | 34 | 35 | while (1) { 36 | console.log('Reading 1: ' + adc.readVoltage(1)); 37 | console.log('Reading 2: ' + adc.readVoltage(2)); 38 | console.log('Reading 3: ' + adc.readVoltage(3)); 39 | console.log('Reading 4: ' + adc.readVoltage(4)); 40 | console.log('Reading 5: ' + adc.readVoltage(5)); 41 | console.log('Reading 6: ' + adc.readVoltage(6)); 42 | console.log('Reading 7: ' + adc.readVoltage(7)); 43 | console.log('Reading 8: ' + adc.readVoltage(8)); 44 | 45 | 46 | } -------------------------------------------------------------------------------- /unittests/iopi/getInterruptOnPort.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getInterruptOnPort function 4 | * 5 | * run with: node getInterruptOnPort 6 | * ================================================ 7 | 8 | This test validates the getInterruptOnPort function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getInterruptOnPort()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try 30 | { 31 | bus.getInterruptOnPort(2); 32 | test.test_exception_failed("port out of bounds"); 33 | } 34 | catch (error){} 35 | 36 | for (var a = 0; a < 255; a++){ 37 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENA, a); 38 | x = bus.getInterruptOnPort(0); 39 | if (x != a){ 40 | test.test_exception_failed("get port failed when set to 0"); 41 | break; 42 | } 43 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENB, a); 44 | x = bus.getInterruptOnPort(1); 45 | if (x != a){ 46 | test.test_exception_failed("get port failed when set to 1"); 47 | break; 48 | } 49 | } 50 | 51 | 52 | 53 | 54 | // get test result 55 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/getPortPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test getPortPolarity function 4 | * 5 | * run with: node getPortPolarity 6 | * ================================================ 7 | 8 | This test validates the getPortPolarity function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > getPortPolarity()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try 30 | { 31 | bus.getPortPolarity(2); 32 | test.test_exception_failed("port out of bounds"); 33 | } 34 | catch (error){} 35 | 36 | for (var a = 0; a < 255; a++){ 37 | test.i2c_emulator_write_word_data(test.PCA9535_INVERTPORT0, a); 38 | x = bus.getPortPolarity(0); 39 | if (x != a){ 40 | test.test_exception_failed("get port failed when set to 0"); 41 | break; 42 | } 43 | test.i2c_emulator_write_word_data(test.PCA9535_INVERTPORT1, a); 44 | x = bus.getPortPolarity(1); 45 | if (x != a){ 46 | test.test_exception_failed("get port failed when set to 1"); 47 | break; 48 | } 49 | } 50 | 51 | 52 | 53 | 54 | // get test result 55 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/rtcpi/rtcmemory.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK RTC Pi demo 4 | * Version 1.0 Created 26/07/2016 5 | * 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node rtcmemory.js 8 | * ================================================ 9 | */ 10 | 11 | intToArray = function (int) { 12 | // we want to represent the input as an 8-bytes array 13 | var array = new Uint8Array([0, 0, 0, 0]); 14 | 15 | for (var i = 0; i < array.length; i++) { 16 | var byte = int & 0xff; 17 | array[i] = byte; 18 | int = (int - byte) / 256; 19 | } 20 | return array; 21 | }; 22 | 23 | arrayToInt = function (array) { 24 | var int = 0; 25 | for (var i = array.length - 1; i >= 0; i--) { 26 | int = (int * 256) + array[i]; 27 | } 28 | return int; 29 | }; 30 | 31 | 32 | // link to the rtcpi library 33 | var rtcpi = require('../../lib/rtcpi/rtcpi'); 34 | 35 | // create an rtc object 36 | var rtc = new RTCPi(); 37 | 38 | // create a number to save into memory 39 | 40 | var a = 12345; 41 | 42 | // convert the number into a Uint8Array array 43 | var bytearray = intToArray(a); 44 | 45 | // write the array to the RTC memory address 0x08 46 | 47 | rtc.writeMemory(0x08, bytearray); 48 | 49 | // read the data from memory into an array 50 | 51 | var readarray = rtc.readMemory(0x08, bytearray.length); 52 | 53 | // convert the array back into a number 54 | 55 | var b = arrayToInt(readarray); 56 | 57 | // print the number to the console 58 | 59 | console.log(b); 60 | 61 | -------------------------------------------------------------------------------- /unittests/iopi/readInterruptStatus.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test readInterruptStatus function 4 | * 5 | * run with: node readInterruptStatus 6 | * ================================================ 7 | 8 | This test validates the readInterruptStatus function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > readInterruptStatus()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try 30 | { 31 | bus.readInterruptStatus(2); 32 | test.test_exception_failed("high boundary out of bounds"); 33 | } 34 | catch(error){ } 35 | 36 | for (var a = 0; a < 255; a++){ 37 | test.i2c_emulator_write_word_data(test.MCP23017_INTFA, a); 38 | x = bus.readInterruptStatus(0); 39 | if (x != a){ 40 | test.test_exception_failed("get interrupt status failed when set to 0"); 41 | break; 42 | } 43 | test.i2c_emulator_write_word_data(test.MCP23017_INTFB, a); 44 | x = bus.readInterruptStatus(1); 45 | if (x != a){ 46 | test.test_exception_failed("get interrupt status failed when set to 1"); 47 | break; 48 | } 49 | } 50 | 51 | 52 | 53 | 54 | // get test result 55 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/getPortDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test getPortDirection function 4 | * 5 | * run with: node getPortDirection 6 | * ================================================ 7 | 8 | This test validates the getPortDirection function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > getPortDirection()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try 30 | { 31 | bus.getPortDirection(2); 32 | test.test_exception_failed("port out of bounds"); 33 | } 34 | 35 | catch (error){} 36 | 37 | for (var a = 0; a < 255; a++){ 38 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT0, a); 39 | x = bus.getPortDirection(0); 40 | if (x != a){ 41 | test.test_exception_failed("get port failed when set to 0"); 42 | break; 43 | } 44 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT1, a); 45 | x = bus.getPortDirection(1); 46 | if (x != a){ 47 | test.test_exception_failed("get port failed when set to 1"); 48 | break; 49 | } 50 | } 51 | 52 | 53 | 54 | 55 | // get test result 56 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/invertPin.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test invertPin function 4 | * 5 | * run with: node invertPin 6 | * ================================================ 7 | 8 | This test validates the invertPin function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > invertPin()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.invertPin(0, 0); 30 | test.test_exception_failed("pin low boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | try 35 | { 36 | bus.invertPin(17, 0); 37 | test.test_exception_failed("pin high boundary out of bounds"); 38 | } 39 | catch(error){ } 40 | 41 | try 42 | { 43 | bus.invertPin(1, 2); 44 | test.test_exception_failed("value high boundary out of bounds"); 45 | } 46 | catch(error){ } 47 | 48 | 49 | 50 | for (var x = 1; x < 17; x++) 51 | { 52 | test.i2c_emulator_write_word_data(test.MCP23017_IPOLA, 0); // reset register 53 | bus.invertPin(x, 1); 54 | if (test.i2c_emulator_read_word_data(test.MCP23017_IPOLA) != Math.pow(2, x-1)) // check bit has been set in register 55 | { 56 | test.test_fail("unexpected register value"); 57 | } 58 | } 59 | 60 | 61 | 62 | 63 | // get test result 64 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/adcdifferentialpi/demo-logvoltage.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK ADC Differential Pi 8-Channel ADC logging demo 4 | * Version 1.0 Created 06/07/2016 5 | * Version 1.1 Updated 20/03/2024 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node demo-logvoltage.js 8 | * ================================================ 9 | */ 10 | 11 | // Initialise the ADC device using the default addresses and sample rate, 12 | // change this value if you have changed the address selection jumpers 13 | 14 | // Sample rate can be 12, 14, 16 or 18 15 | 16 | var adcpi = require('../../lib/adcdifferentialpi/adcdifferentialpi'); 17 | var fs = require('fs'); 18 | 19 | var logStream = fs.createWriteStream('./logFile.log', { flags: 'a' }); 20 | 21 | var spawn = require('child_process').spawn, 22 | ls = spawn('ls', ['-lh', '/usr']); 23 | 24 | ls.stdout.pipe(logStream); 25 | ls.stderr.pipe(logStream); 26 | 27 | ls.on('close', function (code) { 28 | console.log('child process exited with code ' + code); 29 | }); 30 | 31 | 32 | var adc = new ADCDifferentialPi(0x68, 0x69, 18); 33 | 34 | 35 | while (1) { 36 | console.log('Reading 1: ' + adc.readVoltage(1)); 37 | console.log('Reading 2: ' + adc.readVoltage(2)); 38 | console.log('Reading 3: ' + adc.readVoltage(3)); 39 | console.log('Reading 4: ' + adc.readVoltage(4)); 40 | console.log('Reading 5: ' + adc.readVoltage(5)); 41 | console.log('Reading 6: ' + adc.readVoltage(6)); 42 | console.log('Reading 7: ' + adc.readVoltage(7)); 43 | console.log('Reading 8: ' + adc.readVoltage(8)); 44 | } -------------------------------------------------------------------------------- /examples/expanderpi/adcspeedtest.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK Expander Pi - ADC Speed Test Demo 4 | * Version 1.0 Created 19/06/2017 5 | * 6 | * Requires rpio to be installed, install with: npm install rpio 7 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 8 | 9 | * run with: sudo node adcspeedtest.js 10 | * ================================================ 11 | */ 12 | 13 | console.reset = function () { 14 | return process.stdout.write('\033c'); 15 | } 16 | 17 | // link to the expanderpi library 18 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 19 | 20 | // create an instance of the adcdac class 21 | 22 | var adc = new ExpanderPiADC(); 23 | 24 | // set the reference voltage 25 | 26 | adc.setADCRefVoltage(4.096); 27 | 28 | // read the raw value and voltage from channels 1 and 2 and print them on the console. 29 | 30 | 31 | var numberOfSamples = 100000; 32 | 33 | var sampleArray = []; 34 | var a = 0; 35 | 36 | var startTime = new Date(); 37 | 38 | for (var i = 0; i <= numberOfSamples; i++) { 39 | sampleArray[i] = adc.readADCVoltage(1, 0); 40 | } 41 | 42 | var endTime = new Date(); 43 | 44 | var sum = 0; 45 | 46 | for (var x = 0; x <= numberOfSamples; x++) { 47 | sum += sampleArray[x]; 48 | } 49 | 50 | 51 | 52 | var elapsedTime = (endTime.getTime() - startTime.getTime()); 53 | var sampleRate = (numberOfSamples / elapsedTime) * 1000; 54 | var average = sum / numberOfSamples; 55 | 56 | console.log("%d samples in %d ms.\nThe sample rate was %d samples per second\nThe average voltage was %dV", numberOfSamples, elapsedTime, sampleRate, average); 57 | -------------------------------------------------------------------------------- /unittests/iopi/invertPort.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test invertPort function 4 | * 5 | * run with: node invertPort 6 | * ================================================ 7 | 8 | This test validates the invertPort function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > invertPort()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.invertPort(2, 0); 30 | test.test_exception_failed("port high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | for (var x = 0; x < 256; x++) 35 | { 36 | test.i2c_emulator_write_word_data(test.MCP23017_IPOLA, 0); // reset registers 37 | 38 | bus.invertPort(0, x); 39 | 40 | if (test.i2c_emulator_read_byte_data(test.MCP23017_IPOLA) != x) // check value has been set in register 41 | { 42 | test.test_fail("unexpected register value"); 43 | } 44 | 45 | test.i2c_emulator_write_word_data(test.MCP23017_IPOLA, 0); // reset registers 46 | 47 | bus.invertPort(1, x); 48 | 49 | if (test.i2c_emulator_read_byte_data(test.MCP23017_IPOLB) != x) // check value has been set in register 50 | { 51 | test.test_fail("unexpected register value"); 52 | } 53 | } 54 | 55 | 56 | 57 | 58 | // get test result 59 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/adcpi/demo-readvoltage.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK ADC Pi 8-Channel ADC read voltage demo 4 | * Version 1.0 Created 06/07/2016 5 | * 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node demo-readvoltage.js 8 | * ================================================ 9 | */ 10 | 11 | // Initialise the ADC device using the default addresses and sample rate, 12 | // change this value if you have changed the address selection jumpers 13 | 14 | // Sample rate can be 12, 14, 16 or 18 15 | 16 | var adcpi = require('../../lib/adcpi/adcpi'); 17 | 18 | 19 | var adc = new ADCPi(0x68, 0x69, 16); 20 | 21 | process.stdout.write('\x1B[2J\x1B[0f'); // Clear console and move cursor to top 22 | 23 | while (1) { 24 | process.stdout.write('\x1B[0f'); // move cursor to top 25 | process.stdout.write('Reading 1: ' + adc.readVoltage(1) + ' \n'); 26 | process.stdout.write('Reading 2: ' + adc.readVoltage(2) + ' \n'); 27 | process.stdout.write('Reading 3: ' + adc.readVoltage(3) + ' \n'); 28 | process.stdout.write('Reading 4: ' + adc.readVoltage(4) + ' \n'); 29 | process.stdout.write('Reading 5: ' + adc.readVoltage(5) + ' \n'); 30 | process.stdout.write('Reading 6: ' + adc.readVoltage(6) + ' \n'); 31 | process.stdout.write('Reading 7: ' + adc.readVoltage(7) + ' \n'); 32 | process.stdout.write('Reading 8: ' + adc.readVoltage(8) + ' \n'); 33 | } -------------------------------------------------------------------------------- /unittests/iopi/getInterruptType.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getInterruptType function 4 | * 5 | * run with: node getInterruptType 6 | * ================================================ 7 | 8 | This test validates the getInterruptType function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getInterruptType()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try 30 | { 31 | bus.getInterruptType(2); 32 | test.test_exception_failed("port out of bounds"); 33 | } 34 | catch (error){} 35 | 36 | for (var a = 0; a < 255; a++){ 37 | test.i2c_emulator_write_byte_data(test.MCP23017_INTCONB, 0); 38 | test.i2c_emulator_write_byte_data(test.MCP23017_INTCONA, a); 39 | x = bus.getInterruptType(0); 40 | if (x != a){ 41 | test.test_exception_failed("get port failed when set to 0"); 42 | break; 43 | } 44 | test.i2c_emulator_write_byte_data(test.MCP23017_INTCONA, 0); 45 | test.i2c_emulator_write_byte_data(test.MCP23017_INTCONB, a); 46 | x = bus.getInterruptType(1); 47 | if (x != a){ 48 | test.test_exception_failed("get port failed when set to 1"); 49 | break; 50 | } 51 | } 52 | 53 | 54 | 55 | 56 | // get test result 57 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getInterruptDefaults.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getInterruptDefaults function 4 | * 5 | * run with: node getInterruptDefaults 6 | * ================================================ 7 | 8 | This test validates the getInterruptDefaults function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getInterruptDefaults()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | 28 | // out of bounds tests 29 | try { 30 | bus.getInterruptDefaults(2); 31 | test.test_exception_failed("getInterruptDefaults high out of bounds"); 32 | } 33 | catch (error) { } 34 | 35 | for (var a = 0; a < 255; a++) { 36 | test.i2c_emulator_write_byte_data(test.MCP23017_DEFVALA, a); 37 | test.i2c_emulator_write_byte_data(test.MCP23017_DEFVALB, 0); 38 | x = bus.getInterruptDefaults(0); 39 | if (x != a) { 40 | test.test_fail("failed to get interrupt default on port 0"); 41 | break; 42 | } 43 | test.i2c_emulator_write_byte_data(test.MCP23017_DEFVALA, 0); 44 | test.i2c_emulator_write_byte_data(test.MCP23017_DEFVALB, a); 45 | x = bus.getInterruptDefaults(1); 46 | if (x != a) { 47 | test.test_fail("failed to get interrupt default on port 1"); 48 | break; 49 | } 50 | } 51 | 52 | 53 | 54 | 55 | // get test result 56 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setInterruptDefaults.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setInterruptDefaults function 4 | * 5 | * run with: node setInterruptDefaults 6 | * ================================================ 7 | 8 | This test validates the setInterruptDefaults function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setInterruptDefaults()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setInterruptDefaults(2, 0); 30 | test.test_exception_failed("port high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | var x = 0; 35 | var y = 0; 36 | 37 | for (var i = 0; i < 256; i++) 38 | { 39 | bus.setInterruptDefaults(0, i); 40 | bus.setInterruptDefaults(1, i); 41 | 42 | x = test.i2c_emulator_read_byte_data(test.MCP23017_DEFVALA); // read value from registers 43 | if (x != i){ 44 | test.test_fail("failed to set interrupt default for port 0"); 45 | break; 46 | } 47 | 48 | y = test.i2c_emulator_read_byte_data(test.MCP23017_DEFVALB); // read value from registers 49 | if (y != i){ 50 | test.test_fail("failed to set interrupt default for port 1"); 51 | break; 52 | } 53 | } 54 | 55 | 56 | 57 | 58 | // get test result 59 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/expanderpi/rtcmemory.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK Expander Pi - RTC SRAM memory demo 4 | * Version 1.0 Created 19/06/2017 5 | * 6 | * Requires rpio to be installed, install with: npm install rpio 7 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 8 | * 9 | * run with: sudo node rtcmemory.js 10 | * ================================================ 11 | */ 12 | 13 | intToArray = function (int) { 14 | // we want to represent the input as an 8-bytes array 15 | var array = new Uint8Array([0, 0, 0, 0]); 16 | 17 | for (var i = 0; i < array.length; i++) { 18 | var byte = int & 0xff; 19 | array[i] = byte; 20 | int = (int - byte) / 256; 21 | } 22 | return array; 23 | }; 24 | 25 | arrayToInt = function (array) { 26 | var int = 0; 27 | for (var i = array.length - 1; i >= 0; i--) { 28 | int = (int * 256) + array[i]; 29 | } 30 | return int; 31 | }; 32 | 33 | 34 | // link to the expanderpi library 35 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 36 | 37 | // create an rtc object 38 | var rtc = new ExpanderPiRTC(); 39 | 40 | // create a number to save into memory 41 | 42 | var a = 123456; 43 | 44 | // convert the number into a Uint8Array array 45 | var bytearray = intToArray(a); 46 | 47 | 48 | // write the array to the RTC memory address 0x08 49 | 50 | rtc.writeMemory(0x09, bytearray); 51 | 52 | // read the data from memory into an array 53 | 54 | var readarray = rtc.readMemory(0x09, bytearray.length); 55 | 56 | // convert the array back into a number 57 | 58 | var b = arrayToInt(readarray); 59 | 60 | // print the number to the console 61 | 62 | console.log(b); 63 | 64 | -------------------------------------------------------------------------------- /unittests/iozero32/setPinPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test setPinPolarity function 4 | * 5 | * run with: node setPinPolarity 6 | * ================================================ 7 | 8 | This test validates the setPinPolarity function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > setPinPolarity()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setPinPolarity(0, 0); 30 | test.test_exception_failed("pin low boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | try 35 | { 36 | bus.setPinPolarity(17, 0); 37 | test.test_exception_failed("pin high boundary out of bounds"); 38 | } 39 | catch(error){ } 40 | 41 | try 42 | { 43 | bus.setPinPolarity(1, 2); 44 | test.test_exception_failed("value high boundary out of bounds"); 45 | } 46 | catch(error){ } 47 | 48 | 49 | 50 | for (var x = 1; x < 17; x++) 51 | { 52 | test.i2c_emulator_write_word_data(test.PCA9535_INVERTPORT0, 0); // reset register 53 | bus.setPinPolarity(x, 1); 54 | if (test.i2c_emulator_read_word_data(test.PCA9535_INVERTPORT0) != Math.pow(2, x-1)) // check bit has been set in register 55 | { 56 | test.test_fail("unexpected register value"); 57 | } 58 | } 59 | 60 | 61 | 62 | 63 | // get test result 64 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/setPortPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test setPortPolarity function 4 | * 5 | * run with: node setPortPolarity 6 | * ================================================ 7 | 8 | This test validates the setPortPolarity function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > setPortPolarity()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setPortPolarity(2, 0); 30 | test.test_exception_failed("port high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | for (var x = 0; x < 256; x++) 35 | { 36 | test.i2c_emulator_write_word_data(test.PCA9535_INVERTPORT0, 0); // reset registers 37 | 38 | bus.setPortPolarity(0, x); 39 | 40 | if (test.i2c_emulator_read_byte_data(test.PCA9535_INVERTPORT0) != x) // check value has been set in register 41 | { 42 | test.test_fail("unexpected register value"); 43 | } 44 | 45 | test.i2c_emulator_write_word_data(test.PCA9535_INVERTPORT0, 0); // reset registers 46 | 47 | bus.setPortPolarity(1, x); 48 | 49 | if (test.i2c_emulator_read_byte_data(test.PCA9535_INVERTPORT1) != x) // check value has been set in register 50 | { 51 | test.test_fail("unexpected register value"); 52 | } 53 | } 54 | 55 | 56 | 57 | 58 | // get test result 59 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/adcdifferentialpi/demo-readvoltage.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK ADC Differential Pi 8-Channel ADC read voltage demo 4 | * Version 1.0 Created 06/07/2016 5 | * Version 1.1 Updated 20/03/2024 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node demo-readvoltage.js 8 | * ================================================ 9 | */ 10 | 11 | // Initialise the ADC device using the default addresses and sample rate, 12 | // change this value if you have changed the address selection jumpers 13 | 14 | // Sample rate can be 12, 14, 16 or 18 15 | 16 | var adcpi = require('../../lib/adcdifferentialpi/adcdifferentialpi'); 17 | 18 | 19 | var adc = new ADCDifferentialPi(0x68, 0x69, 16); 20 | 21 | process.stdout.write('\x1B[2J\x1B[0f'); // Clear console and move cursor to top 22 | 23 | while (1) { 24 | process.stdout.write('\x1B[0f'); // move cursor to top 25 | process.stdout.write('Reading 1: ' + adc.readVoltage(1) + ' \n'); 26 | process.stdout.write('Reading 2: ' + adc.readVoltage(2) + ' \n'); 27 | process.stdout.write('Reading 3: ' + adc.readVoltage(3) + ' \n'); 28 | process.stdout.write('Reading 4: ' + adc.readVoltage(4) + ' \n'); 29 | process.stdout.write('Reading 5: ' + adc.readVoltage(5) + ' \n'); 30 | process.stdout.write('Reading 6: ' + adc.readVoltage(6) + ' \n'); 31 | process.stdout.write('Reading 7: ' + adc.readVoltage(7) + ' \n'); 32 | process.stdout.write('Reading 8: ' + adc.readVoltage(8) + ' \n'); 33 | } -------------------------------------------------------------------------------- /unittests/iopi/writePort.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test writePort function 4 | * 5 | * run with: node writePort 6 | * ================================================ 7 | 8 | This test validates the writePort function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > writePort()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.writePort(2, 0); 30 | test.test_exception_failed("port high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | 35 | var y = 0; 36 | 37 | for (var x = 0; x < 256; x++) 38 | { 39 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOA, 0x00); 40 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOB, 0x00); 41 | 42 | bus.writePort(0, x); 43 | 44 | y = test.i2c_emulator_read_word_data(test.MCP23017_GPIOA); 45 | if (x != y){ 46 | test.test_exception_failed("set port failed on port 0"); 47 | break; 48 | } 49 | 50 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOA, 0x00); 51 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOB, 0x00); 52 | 53 | bus.writePort(1, x); 54 | 55 | y = test.i2c_emulator_read_word_data(test.MCP23017_GPIOB); 56 | if (x != y){ 57 | test.test_exception_failed("set port failed on port 1"); 58 | break; 59 | } 60 | } 61 | 62 | 63 | 64 | 65 | // get test result 66 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getPinPullup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getPinPullup function 4 | * 5 | * run with: node getPinPullup 6 | * ================================================ 7 | 8 | This test validates the getPinPullup function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getPinPullup()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | var y = 0; 28 | 29 | // out of bounds tests 30 | try 31 | { 32 | bus.getPinPullup(0); 33 | test.test_exception_failed("pin low out of bounds"); 34 | } 35 | catch (error){} 36 | 37 | try 38 | { 39 | bus.getPinPullup(17); 40 | test.test_exception_failed("pin high out of bounds"); 41 | } 42 | catch (error){} 43 | 44 | for (var a = 1; a < 17; a++){ 45 | y = 65535; 46 | 47 | y = test.test_set_bit(y, a-1, false); 48 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUA, y); 49 | x = bus.getPinPullup(a); 50 | if (x != 0){ 51 | test.test_exception_failed("get pin failed on set to 0"); 52 | break; 53 | } 54 | y = 0; 55 | y = test.test_set_bit(y, a-1, true); 56 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUA, y); 57 | x = bus.getPinPullup(a); 58 | if (x != 1){ 59 | test.test_exception_failed("get pin failed on set to 1"); 60 | break; 61 | } 62 | } 63 | 64 | 65 | 66 | 67 | 68 | // get test result 69 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getPinPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getPinPolarity function 4 | * 5 | * run with: node getPinPolarity 6 | * ================================================ 7 | 8 | This test validates the getPinPolarity function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getPinPolarity()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | var y = 0; 28 | 29 | // out of bounds tests 30 | try 31 | { 32 | bus.getPinPolarity(0); 33 | test.test_exception_failed("pin low out of bounds"); 34 | } 35 | catch (error){} 36 | 37 | try 38 | { 39 | bus.getPinPolarity(17); 40 | test.test_exception_failed("pin high out of bounds"); 41 | } 42 | catch (error){} 43 | 44 | for (var a = 1; a < 17; a++){ 45 | y = 65535; 46 | 47 | y = test.test_set_bit(y, a-1, false); 48 | test.i2c_emulator_write_word_data(test.MCP23017_IPOLA, y); 49 | x = bus.getPinPolarity(a); 50 | if (x != 0){ 51 | test.test_exception_failed("get pin failed on set to 0"); 52 | break; 53 | } 54 | y = 0; 55 | y = test.test_set_bit(y, a-1, true); 56 | test.i2c_emulator_write_word_data(test.MCP23017_IPOLA, y); 57 | x = bus.getPinPolarity(a); 58 | if (x != 1){ 59 | test.test_exception_failed("get pin failed on set to 1"); 60 | break; 61 | } 62 | } 63 | 64 | 65 | 66 | 67 | // get test result 68 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getPinDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getPinDirection function 4 | * 5 | * run with: node getPinDirection 6 | * ================================================ 7 | 8 | This test validates the getPinDirection function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getPinDirection()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | var y = 0; 28 | 29 | // out of bounds tests 30 | try 31 | { 32 | bus.getPinDirection(0); 33 | test.test_exception_failed("pin low out of bounds"); 34 | } 35 | catch (error){} 36 | 37 | try 38 | { 39 | bus.getPinDirection(17); 40 | test.test_exception_failed("pin high out of bounds"); 41 | } 42 | catch (error){} 43 | 44 | for (var a = 1; a < 17; a++){ 45 | y = 65535; 46 | 47 | y = test.test_set_bit(y, a-1, false); 48 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRA, y); 49 | x = bus.getPinDirection(a); 50 | if (x != 0){ 51 | test.test_exception_failed("get pin failed on set to 0"); 52 | break; 53 | } 54 | y = 0; 55 | y = test.test_set_bit(y, a-1, true); 56 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRA, y); 57 | x = bus.getPinDirection(a); 58 | if (x != 1){ 59 | test.test_exception_failed("get pin failed on set to 1"); 60 | break; 61 | } 62 | } 63 | 64 | 65 | 66 | 67 | // get test result 68 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setPortPullups.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setPortPullups function 4 | * 5 | * run with: node setPortPullups 6 | * ================================================ 7 | 8 | This test validates the setPortPullups function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setPortPullups()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setPortPullups(2, 0); 30 | test.test_exception_failed("port high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | 35 | var y = 0; 36 | 37 | for (var x = 0; x < 256; x++) 38 | { 39 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUA, 0x00); 40 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUB, 0x00); 41 | 42 | bus.setPortPullups(0, x); 43 | 44 | y = test.i2c_emulator_read_word_data(test.MCP23017_GPPUA); 45 | if (x != y){ 46 | test.test_exception_failed("set port failed on port 0"); 47 | break; 48 | } 49 | 50 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUA, 0x00); 51 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUB, 0x00); 52 | 53 | bus.setPortPullups(1, x); 54 | 55 | y = test.i2c_emulator_read_word_data(test.MCP23017_GPPUB); 56 | if (x != y){ 57 | test.test_exception_failed("set port failed on port 1"); 58 | break; 59 | } 60 | } 61 | 62 | 63 | 64 | 65 | // get test result 66 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/getInterruptOnPin.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getInterruptOnPin function 4 | * 5 | * run with: node getInterruptOnPin 6 | * ================================================ 7 | 8 | This test validates the getInterruptOnPin function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > getInterruptOnPin()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | var x = 0; 27 | var y = 0; 28 | 29 | // out of bounds tests 30 | try 31 | { 32 | bus.getInterruptOnPin(0); 33 | test.test_exception_failed("pin low out of bounds"); 34 | } 35 | catch (error){} 36 | 37 | try 38 | { 39 | bus.getInterruptOnPin(17); 40 | test.test_exception_failed("pin high out of bounds"); 41 | } 42 | catch (error){} 43 | 44 | for (var a = 1; a < 17; a++){ 45 | y = 65535; 46 | 47 | y = test.test_set_bit(y, a-1, false); 48 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENA, y); 49 | x = bus.getInterruptOnPin(a); 50 | if (x != 0){ 51 | test.test_exception_failed("get pin failed on set to 0"); 52 | break; 53 | } 54 | y = 0; 55 | y = test.test_set_bit(y, a-1, true); 56 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENA, y); 57 | x = bus.getInterruptOnPin(a); 58 | if (x != 1){ 59 | test.test_exception_failed("get pin failed on set to 1"); 60 | break; 61 | } 62 | } 63 | 64 | 65 | 66 | 67 | // get test result 68 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setInterruptType.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setInterruptType function 4 | * 5 | * run with: node setInterruptType 6 | * ================================================ 7 | 8 | This test validates the setInterruptType function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setInterruptType()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | 27 | // out of bounds tests 28 | try 29 | { 30 | bus.setInterruptType(2, 0); 31 | test.test_exception_failed("port high boundary out of bounds"); 32 | } 33 | catch(error){ } 34 | 35 | var y = 0; 36 | 37 | for (var x = 0; x < 256; x++) 38 | { 39 | test.i2c_emulator_write_word_data(test.MCP23017_INTCONA, 0x00); 40 | test.i2c_emulator_write_word_data(test.MCP23017_INTCONB, 0x00); 41 | 42 | bus.setInterruptType(0, x); 43 | 44 | y = test.i2c_emulator_read_word_data(test.MCP23017_INTCONA); 45 | if (x != y){ 46 | test.test_exception_failed("set port failed on port 0"); 47 | break; 48 | } 49 | 50 | test.i2c_emulator_write_word_data(test.MCP23017_INTCONA, 0x00); 51 | test.i2c_emulator_write_word_data(test.MCP23017_INTCONB, 0x00); 52 | 53 | bus.setInterruptType(1, x); 54 | 55 | y = test.i2c_emulator_read_word_data(test.MCP23017_INTCONB); 56 | if (x != y){ 57 | test.test_exception_failed("set port failed on port 1"); 58 | break; 59 | } 60 | } 61 | 62 | 63 | 64 | // get test result 65 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setPortDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setPortDirection function 4 | * 5 | * run with: node setPortDirection 6 | * ================================================ 7 | 8 | This test validates the setPortDirection function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setPortDirection()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setPortDirection(2, 0); 30 | test.test_exception_failed("port high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | 35 | var y = 0; 36 | 37 | for (var x = 0; x < 256; x++) 38 | { 39 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRA, 0x00); 40 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRB, 0x00); 41 | 42 | bus.setPortDirection(0, x); 43 | 44 | y = test.i2c_emulator_read_word_data(test.MCP23017_IODIRA); 45 | if (x != y){ 46 | test.test_exception_failed("set port failed on port 0"); 47 | break; 48 | } 49 | 50 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRA, 0x00); 51 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRB, 0x00); 52 | 53 | bus.setPortDirection(1, x); 54 | 55 | y = test.i2c_emulator_read_word_data(test.MCP23017_IODIRB); 56 | if (x != y){ 57 | test.test_exception_failed("set port failed on port 1"); 58 | break; 59 | } 60 | } 61 | 62 | 63 | 64 | 65 | // get test result 66 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/getPinPolarity.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test getPinPolarity function 4 | * 5 | * run with: node getPinPolarity 6 | * ================================================ 7 | 8 | This test validates the getPinPolarity function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > getPinPolarity()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | var x = 0; 27 | var y = 0; 28 | 29 | // out of bounds tests 30 | try 31 | { 32 | bus.getPinPolarity(0); 33 | test.test_exception_failed("pin low out of bounds"); 34 | } 35 | catch (error){} 36 | 37 | try 38 | { 39 | bus.getPinPolarity(17); 40 | test.test_exception_failed("pin high out of bounds"); 41 | } 42 | catch (error){} 43 | 44 | for (var a = 1; a < 17; a++){ 45 | y = 65535; 46 | 47 | y = test.test_set_bit(y, a-1, false); 48 | test.i2c_emulator_write_word_data(test.PCA9535_INVERTPORT0, y); 49 | x = bus.getPinPolarity(a); 50 | if (x != 0){ 51 | test.test_exception_failed("get pin failed on set to 0"); 52 | break; 53 | } 54 | y = 0; 55 | y = test.test_set_bit(y, a-1, true); 56 | test.i2c_emulator_write_word_data(test.PCA9535_INVERTPORT0, y); 57 | x = bus.getPinPolarity(a); 58 | if (x != 1){ 59 | test.test_exception_failed("get pin failed on set to 1"); 60 | break; 61 | } 62 | } 63 | 64 | 65 | 66 | 67 | // get test result 68 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/writePort.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test writePort function 4 | * 5 | * run with: node writePort 6 | * ================================================ 7 | 8 | This test validates the writePort function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > writePort()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.writePort(2, 0); 30 | test.test_exception_failed("port high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | 35 | var y = 0; 36 | 37 | for (var x = 0; x < 256; x++) 38 | { 39 | test.i2c_emulator_write_word_data(test.PCA9535_OUTPUTPORT0, 0x00); 40 | test.i2c_emulator_write_word_data(test.PCA9535_OUTPUTPORT1, 0x00); 41 | 42 | bus.writePort(0, x); 43 | 44 | y = test.i2c_emulator_read_word_data(test.PCA9535_OUTPUTPORT0); 45 | if (x != y){ 46 | test.test_exception_failed("set port failed on port 0"); 47 | break; 48 | } 49 | 50 | test.i2c_emulator_write_word_data(test.PCA9535_OUTPUTPORT0, 0x00); 51 | test.i2c_emulator_write_word_data(test.PCA9535_OUTPUTPORT1, 0x00); 52 | 53 | bus.writePort(1, x); 54 | 55 | y = test.i2c_emulator_read_word_data(test.PCA9535_OUTPUTPORT1); 56 | if (x != y){ 57 | test.test_exception_failed("set port failed on port 1"); 58 | break; 59 | } 60 | } 61 | 62 | 63 | 64 | 65 | // get test result 66 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setInterruptOnPort.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setInterruptOnPort function 4 | * 5 | * run with: node setInterruptOnPort 6 | * ================================================ 7 | 8 | This test validates the setInterruptOnPort function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setInterruptOnPort()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setInterruptOnPort(2, 0); 30 | test.test_exception_failed("port high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | var y = 0; 35 | 36 | for (var x = 0; x < 256; x++) 37 | { 38 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENA, 0x00); 39 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENB, 0x00); 40 | bus.setInterruptOnPort(0, x); 41 | 42 | y = test.i2c_emulator_read_word_data(test.MCP23017_GPINTENA); 43 | if (x != y){ 44 | test.test_exception_failed("set port failed on port 0"); 45 | break; 46 | } 47 | 48 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENA, 0x00); 49 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENB, 0x00); 50 | bus.setInterruptOnPort(1, x); 51 | 52 | y = test.i2c_emulator_read_word_data(test.MCP23017_GPINTENB); 53 | if (x != y){ 54 | test.test_exception_failed("set port failed on port 1"); 55 | break; 56 | } 57 | } 58 | 59 | 60 | 61 | 62 | // get test result 63 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/getPinDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test getPinDirection function 4 | * 5 | * run with: node getPinDirection 6 | * ================================================ 7 | 8 | This test validates the getPinDirection function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > getPinDirection()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | var x = 0; 27 | var y = 0; 28 | 29 | // out of bounds tests 30 | try 31 | { 32 | bus.getPinDirection(0); 33 | test.test_exception_failed("pin low out of bounds"); 34 | } 35 | catch (error){} 36 | 37 | try 38 | { 39 | bus.getPinDirection(17); 40 | test.test_exception_failed("pin high out of bounds"); 41 | } 42 | catch (error){} 43 | 44 | for (var a = 1; a < 17; a++){ 45 | y = 65535; 46 | 47 | y = test.test_set_bit(y, a-1, false); 48 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT0, y); 49 | x = bus.getPinDirection(a); 50 | if (x != 0){ 51 | test.test_exception_failed("get pin failed on set to 0"); 52 | break; 53 | } 54 | y = 0; 55 | y = test.test_set_bit(y, a-1, true); 56 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT0, y); 57 | x = bus.getPinDirection(a); 58 | if (x != 1){ 59 | test.test_exception_failed("get pin failed on set to 1"); 60 | break; 61 | } 62 | } 63 | 64 | 65 | 66 | 67 | // get test result 68 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/setPortDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test setPortDirection function 4 | * 5 | * run with: node setPortDirection 6 | * ================================================ 7 | 8 | This test validates the setPortDirection function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > setPortDirection()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setPortDirection(2, 0); 30 | test.test_exception_failed("port high boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | 35 | var y = 0; 36 | 37 | for (var x = 0; x < 256; x++) 38 | { 39 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT0, 0x00); 40 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT1, 0x00); 41 | 42 | bus.setPortDirection(0, x); 43 | 44 | y = test.i2c_emulator_read_word_data(test.PCA9535_CONFIGPORT0); 45 | if (x != y){ 46 | test.test_exception_failed("set port failed on port 0"); 47 | break; 48 | } 49 | 50 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT0, 0x00); 51 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT1, 0x00); 52 | 53 | bus.setPortDirection(1, x); 54 | 55 | y = test.i2c_emulator_read_word_data(test.PCA9535_CONFIGPORT1); 56 | if (x != y){ 57 | test.test_exception_failed("set port failed on port 1"); 58 | break; 59 | } 60 | } 61 | 62 | 63 | 64 | 65 | // get test result 66 | test.test_outcome(); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AB Electronics UK Node JS Libraries 2 | ===== 3 | 4 | Node JS Libraries to work with Raspberry Pi expansion boards from https://www.abelectronics.co.uk 5 | 6 | Raspberry Pi Models: A, B, A+, B+, 2, 3, 4, 400, Compute Module, Zero. 7 | 8 | To download to your Raspberry Pi type in the terminal: 9 | 10 | ``` 11 | git clone https://github.com/abelectronicsuk/ABElectronics_NodeJS_Libraries.git 12 | ``` 13 | 14 | All library files can be found in the lib folder 15 | All example programs can be found in the examples folder 16 | 17 | ### ADCDAC Pi 18 | The adcdacpi directory contains the library to use with the [ADC DAC Pi Zero](https://www.abelectronics.co.uk/p/74/adc-dac-pi-zero-raspberry-pi-adc-and-dac-expansion-board) 19 | ### ADC Pi 20 | The adcpi directory contains the library to use with the [ADC Pi](https://www.abelectronics.co.uk/p/69/adc-pi-raspberry-pi-analogue-to-digital-converter) 21 | ### ADC Differential Pi 22 | The adcdifferentialpi directory contains the library to use with the [ADC Differential Pi](https://www.abelectronics.co.uk/p/65/adc-differential-pi-raspberry-pi-analogue-to-digital-converter) 23 | ### I2C Switch 24 | The i2cswitch directory contains the library to control the functions of the 4-channel [I2C switch](https://www.abelectronics.co.uk/p/84/i2c-switch "I2C Switch") 25 | ### Expander Pi 26 | The expanderpi directory contains the library to control all of the functions on the [Expander Pi](https://www.abelectronics.co.uk/p/50/expander-pi "Expander Pi") 27 | ### IO Pi 28 | The iopi directory contains IO Pi Python Library and demos to use with the [IO Pi Plus](https://www.abelectronics.co.uk/p/54/io-pi-plus) and IO Pi Zero. 29 | ### IO Zero 32 30 | The iozero32 directory contains IO Zero 32 Python Library and demos to use with the [IO Zero 32](https://www.abelectronics.co.uk/p/86/io-zero-32) 31 | ### RTC Pi 32 | The rtcpi directory contains the library to use with the [RTC Pi](https://www.abelectronics.co.uk/p/70/rtc-pi) 33 | -------------------------------------------------------------------------------- /unittests/iopi/IoPi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test getBusPullups function 4 | * 5 | * run with: node IoPi 6 | * ================================================ 7 | 8 | This test validates the IoPi init function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > IoPi()"); 23 | 24 | // out of bounds tests 25 | try 26 | { 27 | var bus1 = new IoPi(0x19); 28 | test.test_exception_failed("I2C address low out of bounds"); 29 | } 30 | catch(error){ } 31 | 32 | try 33 | { 34 | var bus2 = new IoPi(0x28); 35 | test.test_exception_failed("I2C address high out of bounds"); 36 | } 37 | catch(error){ } 38 | 39 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRA, 0x0000); // Set IODIRA and IODIRB to be 0x00 40 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUA, 0xFFFF); // Set GPPUA and GPPUB to be 0xFF 41 | test.i2c_emulator_write_word_data(test.MCP23017_IPOLA, 0xFFFF); // Set IPOLA and IPOLB to be 0xFF 42 | 43 | var bus2 = new IoPi(0x20, true); // create IoPi object 44 | 45 | test.test_i2c_register(test.MCP23017_IOCON, 0x02); // IOCON expected to be 0x02 46 | 47 | test.test_i2c_register(test.MCP23017_IODIRA, 0xFF); // IODIRA expected to be 0xFF 48 | test.test_i2c_register(test.MCP23017_IODIRB, 0xFF); // IODIRB expected to be 0xFF 49 | 50 | test.test_i2c_register(test.MCP23017_GPPUA, 0x00); // GPPUA expected to be 0xFF 51 | test.test_i2c_register(test.MCP23017_GPPUB, 0x00); // IODIRB expected to be 0xFF 52 | 53 | test.test_i2c_register(test.MCP23017_IPOLA, 0x00); // IPOLA expected to be 0xFF 54 | test.test_i2c_register(test.MCP23017_IPOLB, 0x00); // IPOLB expected to be 0xFF 55 | 56 | test.test_outcome(); 57 | 58 | 59 | 60 | 61 | // get test result 62 | test.test_outcome(); -------------------------------------------------------------------------------- /examples/expanderpi/ioread.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK Expander Pi - IO read demo 4 | * Version 1.0 Created 19/06/2017 5 | * 6 | * Requires rpio to be installed, install with: npm install rpio 7 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 8 | * 9 | * run with: sudo node ioread.js 10 | * ================================================ 11 | */ 12 | 13 | // This example reads the first 8 pins on the Exander Pi IO bus. 14 | // The internal pull-up resistors are enabled so each pin will read as 1 unless 15 | // the pin is connected to ground. 16 | 17 | console.reset = function () { // clear the console screen 18 | return process.stdout.write('\033c'); 19 | } 20 | 21 | // link to the expanderpi library 22 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 23 | 24 | var io = new ExpanderPiIO(); 25 | 26 | // We will read the inputs 1 to 16 so set port 0 and port 1 as inputs and 27 | // enable the internal pull-up resistors 28 | 29 | io.setPortDirection(0, 0xFF); 30 | io.setPortPullups(0, 0xFF); 31 | 32 | io.setPortDirection(1, 0xFF); 33 | io.setPortPullups(1, 0xFF); 34 | 35 | // invert the ports so connecting a pin to ground will show as 1 instead of 0 36 | 37 | io.invertPort(0, 0xFF); 38 | io.invertPort(1, 0xFF); 39 | 40 | // create a timer object and read from pins 1 to 8 every 100ms 41 | var myVar = setInterval(myTimer, 100); 42 | 43 | 44 | function myTimer() { 45 | console.reset(); 46 | console.log('Pin 1: %d', io.readPin(1)); 47 | console.log('Pin 2: %d', io.readPin(2)); 48 | console.log('Pin 3: %d', io.readPin(3)); 49 | console.log('Pin 4: %d', io.readPin(4)); 50 | console.log('Pin 5: %d', io.readPin(5)); 51 | console.log('Pin 6: %d', io.readPin(6)); 52 | console.log('Pin 7: %d', io.readPin(7)); 53 | console.log('Pin 8: %d', io.readPin(8)); 54 | console.log('Pin 9: %d', io.readPin(9)); 55 | console.log('Pin 10: %d', io.readPin(10)); 56 | console.log('Pin 11: %d', io.readPin(11)); 57 | console.log('Pin 12: %d', io.readPin(12)); 58 | console.log('Pin 13: %d', io.readPin(13)); 59 | console.log('Pin 14: %d', io.readPin(14)); 60 | console.log('Pin 15: %d', io.readPin(15)); 61 | console.log('Pin 16: %d', io.readPin(16)); 62 | } -------------------------------------------------------------------------------- /examples/iopi/iopiread.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Pin Read demo 4 | * Version 1.0 Created 12/08/2016 5 | * Version 1.1 Updated 16/ 03 / 2024 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | 8 | * run with: sudo node iopiread.js 9 | * ================================================ 10 | */ 11 | 12 | // This example reads the first 8 pins of bus 1 on the IO Pi board. The 13 | // internal pull- up resistors are enabled so each pin will read as 1 unless 14 | // the pin is connected to ground. 15 | 16 | // Initialise the IOPi device using the default addresses, you will need to 17 | // change the addresses if you have changed the jumpers on the IO Pi 18 | 19 | var iopi = require('../../lib/iopi/iopi'); 20 | 21 | var bus1 = new IoPi(0x20); 22 | 23 | // We will read the inputs 1 to 16 from bus 1 so set ports 0 and 1 as inputs and 24 | // enable the internal pull-up resistors 25 | 26 | bus1.setPortDirection(0, 0xFF); 27 | bus1.setPortPullups(0, 0xFF); 28 | 29 | bus1.setPortDirection(1, 0xFF); 30 | bus1.setPortPullups(1, 0xFF); 31 | 32 | // create a timer object and read from pins 1 to 8 on the IO Pi bus every 100ms 33 | var myVar = setInterval(myTimer, 100); 34 | 35 | // read the state from all of the pins on the bus individually and then get the value from the overall bus 36 | function myTimer() { 37 | console.clear(); 38 | console.log('Pin 1: %d', bus1.readPin(1)); 39 | console.log('Pin 2: %d', bus1.readPin(2)); 40 | console.log('Pin 3: %d', bus1.readPin(3)); 41 | console.log('Pin 4: %d', bus1.readPin(4)); 42 | console.log('Pin 5: %d', bus1.readPin(5)); 43 | console.log('Pin 6: %d', bus1.readPin(6)); 44 | console.log('Pin 7: %d', bus1.readPin(7)); 45 | console.log('Pin 8: %d', bus1.readPin(8)); 46 | console.log('Pin 9: %d', bus1.readPin(9)); 47 | console.log('Pin 10: %d', bus1.readPin(10)); 48 | console.log('Pin 11: %d', bus1.readPin(11)); 49 | console.log('Pin 12: %d', bus1.readPin(12)); 50 | console.log('Pin 13: %d', bus1.readPin(13)); 51 | console.log('Pin 14: %d', bus1.readPin(14)); 52 | console.log('Pin 15: %d', bus1.readPin(15)); 53 | console.log('Pin 16: %d', bus1.readPin(16)); 54 | console.log('Bus 1: %d', bus1.readBus()); 55 | } -------------------------------------------------------------------------------- /lib/adcpi/README.md: -------------------------------------------------------------------------------- 1 | # AB Electronics UK ADC Pi Node JS Library 2 | 3 | Node JS Library to use with ADC Pi Raspberry Pi expansion board from http://www.abelectronics.co.uk 4 | 5 | ## Install 6 | 7 | To download to your Raspberry Pi type in the terminal: 8 | 9 | ``` 10 | git clone https://github.com/abelectronicsuk/ABElectronics_NodeJS_Libraries.git 11 | ``` 12 | The ADC Pi library is located in the /lib/adcpi/ directory 13 | 14 | The example files are located in the /examples/adcpi/ directory 15 | 16 | The ADC Pi library requires the i2c-bus library to run. 17 | 18 | Install from https://www.npmjs.com/package/i2c-bus with 19 | ``` 20 | npm install i2c-bus 21 | ``` 22 | 23 | ## Functions: 24 | 25 | ``` 26 | readVoltage(channel) 27 | ``` 28 | Read the voltage from the selected channel 29 | **Parameters:** channel - 1 to 8 30 | **Returns:** number between -2.048 and +2.048 31 | 32 | ``` 33 | readRaw(channel) 34 | ``` 35 | Read the raw int value from the selected channel 36 | **Parameters:** channel - 1 to 8 37 | **Returns:** number 38 | 39 | ``` 40 | setPGA(gain) 41 | ``` 42 | Set the gain of the PGA on the chip 43 | **Parameters:** gain - 1, 2, 4, 8 44 | **Returns:** null 45 | 46 | ``` 47 | setBitRate(rate) 48 | ``` 49 | Set the sample bit rate of the ADC 50 | **Parameters:** rate - 12, 14, 16, 18 51 | **Returns:** null 52 | 12 = 12 bit (240SPS max) 53 | 14 = 14 bit (60SPS max) 54 | 16 = 16 bit (15SPS max) 55 | 18 = 18 bit (3.75SPS max) 56 | 57 | ``` 58 | setConversionMode(mode) 59 | ``` 60 | Set the conversion mode for the ADC 61 | **Parameters:** mode - 0 = One-shot conversion, 1 = Continuous conversion 62 | **Returns:** null 63 | 64 | ## Usage 65 | 66 | To use the ADC Pi library in your code you must first import the library: 67 | ``` 68 | var adcpi = require('../../lib/adcpi/adcpi'); 69 | ``` 70 | 71 | Next, you must initialise the adc object and smbus: 72 | ``` 73 | var adc = new ADCPi(0x68, 0x69, 18); 74 | ``` 75 | The first two arguments are the I2C addresses of the ADC chips. The values shown are the default addresses of the ADC board. 76 | 77 | The third argument is the sample bit rate you want to use on the ADC chips. The sample rate can be 12, 14, 16 or 18 78 | 79 | 80 | You can now read the voltage from channel 1 with: 81 | ``` 82 | adc.readVoltage(1); 83 | ``` 84 | -------------------------------------------------------------------------------- /lib/adcdifferentialpi/README.md: -------------------------------------------------------------------------------- 1 | # AB Electronics UK ADC Differential Pi Node JS Library 2 | 3 | Node JS Library to use with ADC Differential Pi Raspberry Pi expansion board from http://www.abelectronics.co.uk 4 | 5 | ## Install 6 | 7 | To download to your Raspberry Pi type in the terminal: 8 | 9 | ``` 10 | git clone https://github.com/abelectronicsuk/ABElectronics_NodeJS_Libraries.git 11 | ``` 12 | The ADC Differential Pi library is located in the /lib/adcdifferentialpi/ directory 13 | 14 | The example files are located in the /examples/adcdifferentialpi/ directory 15 | 16 | The ADC Differential Pi library requires the i2c-bus library to run. 17 | 18 | Install from https://www.npmjs.com/package/i2c-bus with 19 | ``` 20 | npm install i2c-bus 21 | ``` 22 | 23 | ## Functions: 24 | 25 | ``` 26 | readVoltage(channel) 27 | ``` 28 | Read the voltage from the selected channel 29 | **Parameters:** channel - 1 to 8 30 | **Returns:** number between -2.048 and +2.048 31 | 32 | ``` 33 | readRaw(channel) 34 | ``` 35 | Read the raw int value from the selected channel 36 | **Parameters:** channel - 1 to 8 37 | **Returns:** number 38 | 39 | ``` 40 | setPGA(gain) 41 | ``` 42 | Set the gain of the PGA on the chip 43 | **Parameters:** gain - 1, 2, 4, 8 44 | **Returns:** null 45 | 46 | ``` 47 | setBitRate(rate) 48 | ``` 49 | Set the sample bit rate of the ADC 50 | **Parameters:** rate - 12, 14, 16, 18 51 | **Returns:** null 52 | 12 = 12 bit (240SPS max) 53 | 14 = 14 bit (60SPS max) 54 | 16 = 16 bit (15SPS max) 55 | 18 = 18 bit (3.75SPS max) 56 | 57 | ``` 58 | setConversionMode(mode) 59 | ``` 60 | Set the conversion mode for the ADC 61 | **Parameters:** mode - 0 = One-shot conversion, 1 = Continuous conversion 62 | **Returns:** null 63 | 64 | ## Usage 65 | 66 | To use the ADC Differential Pi library in your code you must first import the library: 67 | ``` 68 | var adcpi = require('../../lib/adcpi/adcpi'); 69 | ``` 70 | 71 | Next, you must initialise the adc object and smbus: 72 | ``` 73 | var adc = new ADCPi(0x68, 0x69, 18); 74 | ``` 75 | The first two arguments are the I2C addresses of the ADC chips. The values shown are the default addresses of the ADC board. 76 | 77 | The third argument is the sample bit rate you want to use on the ADC chips. The sample rate can be 12, 14, 16 or 18 78 | 79 | 80 | You can now read the voltage from channel 1 with: 81 | ``` 82 | adc.readVoltage(1); 83 | ``` 84 | -------------------------------------------------------------------------------- /lib/i2cswitch/README.md: -------------------------------------------------------------------------------- 1 | # AB Electronics UK I2C Switch Node.js Library 2 | 3 | Node.js Library to use with the 4-channel I2C Switch from https://www.abelectronics.co.uk 4 | 5 | ## Install 6 | 7 | To download to your Raspberry Pi type in the terminal: 8 | 9 | ``` 10 | git clone https://github.com/abelectronicsuk/ABElectronics_NodeJS_Libraries.git 11 | ``` 12 | The I2C Switch library is located in the /lib/i2cswitch/ directory 13 | 14 | The example files are located in the /examples/i2cswitch/ directory 15 | 16 | The I2C Switch library requires the i2c-bus and rpio libraries to run. 17 | 18 | Install i2c-bus from https://www.npmjs.com/package/i2c-bus with 19 | ``` 20 | npm install i2c-bus 21 | ``` 22 | 23 | Install rpio from https://www.npmjs.com/package/rpio with 24 | ``` 25 | npm install rpio 26 | ``` 27 | 28 | # I2CSwitch Class 29 | 30 | The I2CSwitch class contains the functions needed to control the channel state and reset the PCA9546A controller used on the I2C Switch. 31 | 32 | ``` 33 | I2CSwitch(address) 34 | ``` 35 | **Parameter:** address - 0x70 to 0x77. I2C address for the target device 36 | 37 | ## Functions: 38 | 39 | ``` 40 | switchChannel(channel) 41 | ``` 42 | Enable the switch on the selected channel and disable all other channels 43 | **Parameter:** channel - 1 to 4 44 | **Returns:** null 45 | 46 | ``` 47 | setChannelState(channel, state) 48 | ``` 49 | Set the output on single channels 50 | **Parameter:** channel - 1 to 4, 51 | **Parameter:** true = channel enabled, false = channel disabled 52 | **Returns:** null 53 | 54 | ``` 55 | getChannelState(channel) 56 | ``` 57 | Get the state of the specified channel 58 | **Parameter:** channel - 1 to 4, 59 | **Returns:** true = channel enabled, false = channel disabled 60 | 61 | ``` 62 | reset() 63 | ``` 64 | Reset the I2C switch. 65 | Resetting allows the switch to recover from a situation in which one of the downstream I2C buses is stuck in a low state. All channels will be set to an off-state. 66 | **Returns:** null 67 | 68 | ## Usage 69 | 70 | To use the I2C Switch library in your code you must first import the library: 71 | ``` 72 | var i2cswitch = require('../../lib/i2cswitch/i2cswitch'); 73 | ``` 74 | Next, you must initialise the I2CSwitch object: 75 | ``` 76 | var switchobject = new I2CSwitch(0x70); 77 | ``` 78 | Set the switch to channel 4 79 | ``` 80 | switchobject.switchChannel(4); 81 | ``` 82 | Get the status of the selected channel and print it to the console 83 | ``` 84 | var result = switchobject.getChannelState(4) 85 | 86 | if (result == true){ 87 | console.log('Switched to channel 4') 88 | } 89 | else{ 90 | console.log('Error switching to channel 4') 91 | } 92 | ``` -------------------------------------------------------------------------------- /examples/iozero32/ioread.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK - IO Zero 32 - Pin Read demo 4 | * Version 1.0 Created 10/05/2022 5 | * 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node iopiread.js 8 | * ================================================ 9 | */ 10 | 11 | // This example reads all 32 pins of both bus 1 and 2 on the IO Zero 32 board. 12 | // Pull-up or pull-down resistors may be required if the inputs are normally left in a floating state. 13 | 14 | // Initialise the IOZero32 device using the default addresses, you will need to 15 | // change the addresses if you have changed the jumpers on the IO Zero 32 16 | 17 | const IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | const bus1 = new IOZero32(0x20); 20 | const bus2 = new IOZero32(0x20); 21 | 22 | // We will read the inputs 1 to 16 from buses 1 and 2 so set both busses to be inputs 23 | 24 | bus1.setBusDirection(0xFFFF); 25 | bus2.setBusDirection(0xFFFF); 26 | 27 | // create a timer object and read from pins 1 to 8 on the IO bus every 100ms 28 | const myVar = setInterval(myTimer, 100); 29 | 30 | function myTimer() { 31 | console.clear(); 32 | console.log('Bus 1 Pin 1: %d Bus 2 Pin 1: %d', bus1.readPin(1), bus2.readPin(1)); 33 | console.log('Bus 1 Pin 2: %d Bus 2 Pin 2: %d', bus1.readPin(2), bus2.readPin(2)); 34 | console.log('Bus 1 Pin 3: %d Bus 2 Pin 3: %d', bus1.readPin(3), bus2.readPin(3)); 35 | console.log('Bus 1 Pin 4: %d Bus 2 Pin 4: %d', bus1.readPin(4), bus2.readPin(4)); 36 | console.log('Bus 1 Pin 5: %d Bus 2 Pin 5: %d', bus1.readPin(5), bus2.readPin(5)); 37 | console.log('Bus 1 Pin 6: %d Bus 2 Pin 6: %d', bus1.readPin(6), bus2.readPin(6)); 38 | console.log('Bus 1 Pin 7: %d Bus 2 Pin 7: %d', bus1.readPin(7), bus2.readPin(7)); 39 | console.log('Bus 1 Pin 8: %d Bus 2 Pin 8: %d', bus1.readPin(8), bus2.readPin(8)); 40 | console.log('Bus 1 Pin 9: %d Bus 2 Pin 9: %d', bus1.readPin(9), bus2.readPin(9)); 41 | console.log('Bus 1 Pin 10: %d Bus 2 Pin 10: %d', bus1.readPin(10), bus2.readPin(10)); 42 | console.log('Bus 1 Pin 11: %d Bus 2 Pin 11: %d', bus1.readPin(11), bus2.readPin(11)); 43 | console.log('Bus 1 Pin 12: %d Bus 2 Pin 12: %d', bus1.readPin(12), bus2.readPin(12)); 44 | console.log('Bus 1 Pin 13: %d Bus 2 Pin 13: %d', bus1.readPin(13), bus2.readPin(13)); 45 | console.log('Bus 1 Pin 14: %d Bus 2 Pin 14: %d', bus1.readPin(14), bus2.readPin(14)); 46 | console.log('Bus 1 Pin 15: %d Bus 2 Pin 15: %d', bus1.readPin(15), bus2.readPin(15)); 47 | console.log('Bus 1 Pin 16: %d Bus 2 Pin 16: %d', bus1.readPin(16), bus2.readPin(16)); 48 | } -------------------------------------------------------------------------------- /unittests/iopi/writePin.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test writePin function 4 | * 5 | * run with: node writePin 6 | * ================================================ 7 | 8 | This test validates the writePin function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > writePin()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.writePin(0, 0); 30 | test.test_exception_failed("pin low boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | try 35 | { 36 | bus.writePin(17, 0); 37 | test.test_exception_failed("pin high boundary out of bounds"); 38 | } 39 | catch(error){ } 40 | 41 | try 42 | { 43 | bus.writePin(1, 2); 44 | test.test_exception_failed("value high boundary out of bounds"); 45 | } 46 | catch(error){ } 47 | 48 | for (var x = 1; x <= 8; x++) 49 | { 50 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOA, 0x00); 51 | bus.writePin(x, 1); 52 | 53 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPIOA); 54 | if (!test.test_get_bit(y, x - 1)){ 55 | test.test_exception_failed("set pin to 1 failed on port 0"); 56 | break; 57 | } 58 | } 59 | 60 | for (var x = 1; x <= 8; x++) 61 | { 62 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOA, 0xFF); 63 | bus.writePin(x, 0); 64 | 65 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPIOA); 66 | if (test.test_get_bit(y, x - 1)){ 67 | test.test_exception_failed("set pin to 0 failed on port 0"); 68 | break; 69 | } 70 | } 71 | 72 | for (var x = 9; x <= 16; x++) 73 | { 74 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOB, 0x00); 75 | bus.writePin(x, 1); 76 | 77 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPIOB); 78 | if (!test.test_get_bit(y, x - 9)){ 79 | test.test_exception_failed("set pin to 1 failed on port 1"); 80 | break; 81 | } 82 | } 83 | 84 | for (var x = 9; x <= 16; x++) 85 | { 86 | test.i2c_emulator_write_word_data(test.MCP23017_GPIOB, 0xFF); 87 | bus.writePin(x, 0); 88 | 89 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPIOB); 90 | if (test.test_get_bit(y, x - 9)){ 91 | test.test_exception_failed("set pin to 1 failed on port 1"); 92 | break; 93 | } 94 | } 95 | 96 | 97 | 98 | 99 | // get test result 100 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setPinPullup.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setPinPullup function 4 | * 5 | * run with: node setPinPullup 6 | * ================================================ 7 | 8 | This test validates the setPinPullup function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setPinPullup()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setPinPullup(0, 0); 30 | test.test_exception_failed("pin low boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | try 35 | { 36 | bus.setPinPullup(17, 0); 37 | test.test_exception_failed("pin high boundary out of bounds"); 38 | } 39 | catch(error){ } 40 | 41 | try 42 | { 43 | bus.setPinPullup(1, 2); 44 | test.test_exception_failed("value high boundary out of bounds"); 45 | } 46 | catch(error){ } 47 | 48 | for (var x = 1; x <= 8; x++) 49 | { 50 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUA, 0x00); 51 | bus.setPinPullup(x, 1); 52 | 53 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPPUA); 54 | if (!test.test_get_bit(y, x - 1)){ 55 | test.test_exception_failed("set pin to 1 failed on port 0"); 56 | break; 57 | } 58 | } 59 | 60 | for (var x = 1; x <= 8; x++) 61 | { 62 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUA, 0xFF); 63 | bus.setPinPullup(x, 0); 64 | 65 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPPUA); 66 | if (test.test_get_bit(y, x - 1)){ 67 | test.test_exception_failed("set pin to 0 failed on port 0"); 68 | break; 69 | } 70 | } 71 | 72 | for (var x = 9; x <= 16; x++) 73 | { 74 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUB, 0x00); 75 | bus.setPinPullup(x, 1); 76 | 77 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPPUB); 78 | if (!test.test_get_bit(y, x - 9)){ 79 | test.test_exception_failed("set pin to 1 failed on port 1"); 80 | break; 81 | } 82 | } 83 | 84 | for (var x = 9; x <= 16; x++) 85 | { 86 | test.i2c_emulator_write_word_data(test.MCP23017_GPPUB, 0xFF); 87 | bus.setPinPullup(x, 0); 88 | 89 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPPUB); 90 | if (test.test_get_bit(y, x - 9)){ 91 | test.test_exception_failed("set pin to 1 failed on port 1"); 92 | break; 93 | } 94 | } 95 | 96 | 97 | 98 | 99 | // get test result 100 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/writePin.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test writePin function 4 | * 5 | * run with: node writePin 6 | * ================================================ 7 | 8 | This test validates the writePin function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > writePin()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.writePin(0, 0); 30 | test.test_exception_failed("pin low boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | try 35 | { 36 | bus.writePin(17, 0); 37 | test.test_exception_failed("pin high boundary out of bounds"); 38 | } 39 | catch(error){ } 40 | 41 | try 42 | { 43 | bus.writePin(1, 2); 44 | test.test_exception_failed("value high boundary out of bounds"); 45 | } 46 | catch(error){ } 47 | 48 | for (var x = 1; x <= 8; x++) 49 | { 50 | test.i2c_emulator_write_word_data(test.PCA9535_OUTPUTPORT0, 0x00); 51 | bus.writePin(x, 1); 52 | 53 | var y = test.i2c_emulator_read_word_data(test.PCA9535_OUTPUTPORT0); 54 | if (!test.test_get_bit(y, x - 1)){ 55 | test.test_exception_failed("set pin to 1 failed on port 0"); 56 | break; 57 | } 58 | } 59 | 60 | for (var x = 1; x <= 8; x++) 61 | { 62 | test.i2c_emulator_write_word_data(test.PCA9535_OUTPUTPORT0, 0xFF); 63 | bus.writePin(x, 0); 64 | 65 | var y = test.i2c_emulator_read_word_data(test.PCA9535_OUTPUTPORT0); 66 | if (test.test_get_bit(y, x - 1)){ 67 | test.test_exception_failed("set pin to 0 failed on port 0"); 68 | break; 69 | } 70 | } 71 | 72 | for (var x = 9; x <= 16; x++) 73 | { 74 | test.i2c_emulator_write_word_data(test.PCA9535_OUTPUTPORT1, 0x00); 75 | bus.writePin(x, 1); 76 | 77 | var y = test.i2c_emulator_read_word_data(test.PCA9535_OUTPUTPORT1); 78 | if (!test.test_get_bit(y, x - 9)){ 79 | test.test_exception_failed("set pin to 1 failed on port 1"); 80 | break; 81 | } 82 | } 83 | 84 | for (var x = 9; x <= 16; x++) 85 | { 86 | test.i2c_emulator_write_word_data(test.PCA9535_OUTPUTPORT1, 0xFF); 87 | bus.writePin(x, 0); 88 | 89 | var y = test.i2c_emulator_read_word_data(test.PCA9535_OUTPUTPORT1); 90 | if (test.test_get_bit(y, x - 9)){ 91 | test.test_exception_failed("set pin to 1 failed on port 1"); 92 | break; 93 | } 94 | } 95 | 96 | 97 | 98 | 99 | // get test result 100 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setPinDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setPinDirection function 4 | * 5 | * run with: node setPinDirection 6 | * ================================================ 7 | 8 | This test validates the setPinDirection function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setPinDirection()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setPinDirection(0, 0); 30 | test.test_exception_failed("pin low boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | try 35 | { 36 | bus.setPinDirection(17, 0); 37 | test.test_exception_failed("pin high boundary out of bounds"); 38 | } 39 | catch(error){ } 40 | 41 | try 42 | { 43 | bus.setPinDirection(1, 2); 44 | test.test_exception_failed("value high boundary out of bounds"); 45 | } 46 | catch(error){ } 47 | 48 | for (var x = 1; x <= 8; x++) 49 | { 50 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRA, 0x00); 51 | bus.setPinDirection(x, 1); 52 | 53 | var y = test.i2c_emulator_read_word_data(test.MCP23017_IODIRA); 54 | if (!test.test_get_bit(y, x - 1)){ 55 | test.test_exception_failed("set pin to 1 failed on port 0"); 56 | break; 57 | } 58 | } 59 | 60 | for (var x = 1; x <= 8; x++) 61 | { 62 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRA, 0xFF); 63 | bus.setPinDirection(x, 0); 64 | 65 | var y = test.i2c_emulator_read_word_data(test.MCP23017_IODIRA); 66 | if (test.test_get_bit(y, x - 1)){ 67 | test.test_exception_failed("set pin to 0 failed on port 0"); 68 | break; 69 | } 70 | } 71 | 72 | for (var x = 9; x <= 16; x++) 73 | { 74 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRB, 0x00); 75 | bus.setPinDirection(x, 1); 76 | 77 | var y = test.i2c_emulator_read_word_data(test.MCP23017_IODIRB); 78 | if (!test.test_get_bit(y, x - 9)){ 79 | test.test_exception_failed("set pin to 1 failed on port 1"); 80 | break; 81 | } 82 | } 83 | 84 | for (var x = 9; x <= 16; x++) 85 | { 86 | test.i2c_emulator_write_word_data(test.MCP23017_IODIRB, 0xFF); 87 | bus.setPinDirection(x, 0); 88 | 89 | var y = test.i2c_emulator_read_word_data(test.MCP23017_IODIRB); 90 | if (test.test_get_bit(y, x - 9)){ 91 | test.test_exception_failed("set pin to 1 failed on port 1"); 92 | break; 93 | } 94 | } 95 | 96 | 97 | 98 | 99 | // get test result 100 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iopi/setInterruptOnPin.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Unit Tests | test setInterruptOnPin function 4 | * 5 | * run with: node setInterruptOnPin 6 | * ================================================ 7 | 8 | This test validates the setInterruptOnPin function in the IoPi class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var iopi = require('../../lib/iopi/iopi'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOPi class > setInterruptOnPin()"); 23 | 24 | var bus = new IoPi(0x20, false); // new IoPi object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setInterruptOnPin(0, 0); 30 | test.test_exception_failed("pin low boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | try 35 | { 36 | bus.setInterruptOnPin(17, 0); 37 | test.test_exception_failed("pin high boundary out of bounds"); 38 | } 39 | catch(error){ } 40 | 41 | try 42 | { 43 | bus.setInterruptOnPin(1, 2); 44 | test.test_exception_failed("value high boundary out of bounds"); 45 | } 46 | catch(error){ } 47 | 48 | for (var x = 1; x <= 8; x++) 49 | { 50 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENA, 0x00); 51 | bus.setInterruptOnPin(x, 1); 52 | 53 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPINTENA); 54 | if (!test.test_get_bit(y, x - 1)){ 55 | test.test_exception_failed("set pin to 1 failed on port 0"); 56 | break; 57 | } 58 | } 59 | 60 | for (var x = 1; x <= 8; x++) 61 | { 62 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENA, 0xFF); 63 | bus.setInterruptOnPin(x, 0); 64 | 65 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPINTENA); 66 | if (test.test_get_bit(y, x - 1)){ 67 | test.test_exception_failed("set pin to 0 failed on port 0"); 68 | break; 69 | } 70 | } 71 | 72 | for (var x = 9; x <= 16; x++) 73 | { 74 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENB, 0x00); 75 | bus.setInterruptOnPin(x, 1); 76 | 77 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPINTENB); 78 | if (!test.test_get_bit(y, x - 9)){ 79 | test.test_exception_failed("set pin to 1 failed on port 1"); 80 | break; 81 | } 82 | } 83 | 84 | for (var x = 9; x <= 16; x++) 85 | { 86 | test.i2c_emulator_write_word_data(test.MCP23017_GPINTENB, 0xFF); 87 | bus.setInterruptOnPin(x, 0); 88 | 89 | var y = test.i2c_emulator_read_word_data(test.MCP23017_GPINTENB); 90 | if (test.test_get_bit(y, x - 9)){ 91 | test.test_exception_failed("set pin to 1 failed on port 1"); 92 | break; 93 | } 94 | } 95 | 96 | 97 | 98 | 99 | // get test result 100 | test.test_outcome(); -------------------------------------------------------------------------------- /unittests/iozero32/setPinDirection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Zero 32 Unit Tests | test setPinDirection function 4 | * 5 | * run with: node setPinDirection 6 | * ================================================ 7 | 8 | This test validates the setPinDirection function in the IOZero32 class. 9 | 10 | === Expected Result ============================ 11 | 12 | > Console Output: 13 | Test Passed 14 | 15 | */ 16 | 17 | var IOZero32 = require('../../lib/iozero32/iozero32'); 18 | 19 | var ut = require("../unittests"); // import test framework 20 | var test = new UnitTests(); 21 | 22 | test.start_test("IOZero32 class > setPinDirection()"); 23 | 24 | var bus = new IOZero32(0x20, false); // new IOZero32 object without initialisation 25 | 26 | // out of bounds tests 27 | try 28 | { 29 | bus.setPinDirection(0, 0); 30 | test.test_exception_failed("pin low boundary out of bounds"); 31 | } 32 | catch(error){ } 33 | 34 | try 35 | { 36 | bus.setPinDirection(17, 0); 37 | test.test_exception_failed("pin high boundary out of bounds"); 38 | } 39 | catch(error){ } 40 | 41 | try 42 | { 43 | bus.setPinDirection(1, 2); 44 | test.test_exception_failed("value high boundary out of bounds"); 45 | } 46 | catch(error){ } 47 | 48 | for (var x = 1; x <= 8; x++) 49 | { 50 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT0, 0x00); 51 | bus.setPinDirection(x, 1); 52 | 53 | var y = test.i2c_emulator_read_word_data(test.PCA9535_CONFIGPORT0); 54 | if (!test.test_get_bit(y, x - 1)){ 55 | test.test_exception_failed("set pin to 1 failed on port 0"); 56 | break; 57 | } 58 | } 59 | 60 | for (var x = 1; x <= 8; x++) 61 | { 62 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT0, 0xFF); 63 | bus.setPinDirection(x, 0); 64 | 65 | var y = test.i2c_emulator_read_word_data(test.PCA9535_CONFIGPORT0); 66 | if (test.test_get_bit(y, x - 1)){ 67 | test.test_exception_failed("set pin to 0 failed on port 0"); 68 | break; 69 | } 70 | } 71 | 72 | for (var x = 9; x <= 16; x++) 73 | { 74 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT1, 0x00); 75 | bus.setPinDirection(x, 1); 76 | 77 | var y = test.i2c_emulator_read_word_data(test.PCA9535_CONFIGPORT1); 78 | if (!test.test_get_bit(y, x - 9)){ 79 | test.test_exception_failed("set pin to 1 failed on port 1"); 80 | break; 81 | } 82 | } 83 | 84 | for (var x = 9; x <= 16; x++) 85 | { 86 | test.i2c_emulator_write_word_data(test.PCA9535_CONFIGPORT1, 0xFF); 87 | bus.setPinDirection(x, 0); 88 | 89 | var y = test.i2c_emulator_read_word_data(test.PCA9535_CONFIGPORT1); 90 | if (test.test_get_bit(y, x - 9)){ 91 | test.test_exception_failed("set pin to 1 failed on port 1"); 92 | break; 93 | } 94 | } 95 | 96 | 97 | 98 | 99 | // get test result 100 | test.test_outcome(); -------------------------------------------------------------------------------- /lib/adcdacpi/README.md: -------------------------------------------------------------------------------- 1 | # AB Electronics UK ADC DAC Pi Node JS Library 2 | 3 | Node JS Library to use with ADC DAC Pi and ADC DAC Pi Zero Raspberry Pi expansion boards from http://www.abelectronics.co.uk 4 | 5 | ## Install 6 | 7 | To download to your Raspberry Pi type in the terminal: 8 | 9 | ``` 10 | git clone https://github.com/abelectronicsuk/ABElectronics_NodeJS_Libraries.git 11 | ``` 12 | The ADCDAC Pi library is located in the /lib/adcdacpi/ directory 13 | 14 | The example files are located in the /examples/adcdacpi/ directory 15 | 16 | The ADC DAC Pi library requires the rpio library to run. 17 | 18 | Install rpio from https://www.npmjs.com/package/rpio with 19 | ``` 20 | npm install rpio 21 | ``` 22 | 23 | ## Functions: 24 | 25 | ``` 26 | readADCVoltage(channel, mode) 27 | ``` 28 | Read the voltage from the selected channel on the ADC 29 | **Parameters:** channel - 1 or 2; mode - 0 = single-ended, 1 = differential 30 | **Returns:** number as a float between 0 and 2.048 31 | 32 | ``` 33 | readADCRaw(channel, mode) 34 | ``` 35 | Read the raw value from the selected channel on the ADC 36 | **Parameters:** channel - 1 or 2; mode - 0 = single-ended, 1 = differential 37 | **Returns:** int 38 | ``` 39 | setADCRefVoltage(voltage) 40 | ``` 41 | Set the reference voltage for the analogue to digital converter. 42 | The ADC uses the raspberry pi 3.3V power as a voltage reference so using this method to set the reference to match the exact output voltage from the 3.3V regulator will increase the accuracy of the ADC readings. 43 | **Parameters:** voltage - float between 0.0 and 7.0 44 | **Returns:** null 45 | 46 | ``` 47 | setDACVoltage(channel, voltage) 48 | ``` 49 | Set the voltage for the selected channel on the DAC. The DAC has two gain values, 1 or 2, which can be set when the ADCDAC object is created. A gain of 1 will give a voltage between 0 and 2.047 volts. A gain of 2 will give a voltage between 0 and 3.3 volts. 50 | **Parameters:** channel - 1 or 2, the voltage can be between 0 and 2.047 volts 51 | **Returns:** null 52 | 53 | ``` 54 | setDACRaw(channel, value) 55 | ``` 56 | Set the raw value from the selected channel on the DAC 57 | **Parameters:** channel - 1 or 2, value int between 0 and 4095 58 | **Returns:** null 59 | 60 | ``` 61 | setDACGain(gain) 62 | ``` 63 | Set the gain for the DAC. This is used to set the output based on the reference voltage of 2.048V. 64 | When the gain is set to 2 the maximum voltage will be approximately 3.3V. 65 | **Parameters:** gain - 1 or 2 66 | **Returns:** null 67 | 68 | 69 | ## Usage 70 | 71 | To use the ADC DAC Pi library in your code you must first import the library: 72 | ``` 73 | var adcdac = require('../../lib/adcdacpi/adcdacpi'); 74 | ``` 75 | Next, you must initialise the adcdac object and set a gain of 1 or 2 for the DAC: 76 | ``` 77 | var adc = new ADCDAC(); 78 | ``` 79 | Set the reference voltage. 80 | ``` 81 | adc.setADCRefVoltage(3.3); 82 | ``` 83 | Read the voltage from channel 1 and display it on the screen 84 | ``` 85 | console.log('Reading 1 Voltage: ' + adc.readADCVoltage(1, 0)); 86 | ``` 87 | -------------------------------------------------------------------------------- /lib/rtcpi/README.md: -------------------------------------------------------------------------------- 1 | # AB Electronics UK RTC Pi Node.js Library 2 | 3 | Node.js Library to use with RTC Pi Raspberry Pi real-time clock board from https://www.abelectronics.co.uk 4 | 5 | ## Install 6 | 7 | To download to your Raspberry Pi type in the terminal: 8 | 9 | ``` 10 | git clone https://github.com/abelectronicsuk/ABElectronics_NodeJS_Libraries.git 11 | ``` 12 | The RTC Pi library is located in the /lib/rtcpi/ directory 13 | 14 | The example files are located in the /examples/rtcpi/ directory 15 | 16 | The RTC Pi library requires the i2c-bus library to run. 17 | 18 | Install from https://www.npmjs.com/package/i2c-bus with 19 | ``` 20 | npm install i2c-bus 21 | ``` 22 | 23 | ## Functions: 24 | 25 | 26 | ``` 27 | setDate(date) 28 | ``` 29 | Set the date and time on the RTC using a javascript Date object 30 | **Parameter:** date 31 | **Returns:** null 32 | 33 | ``` 34 | readDate() 35 | ``` 36 | Returns the date from the RTC as a javascript Date object 37 | **Returns:** date object 38 | 39 | 40 | ``` 41 | enableOutput() 42 | ``` 43 | Enable the square-wave output on the SQW pin. 44 | **Returns:** null 45 | 46 | ``` 47 | disableOutput() 48 | ``` 49 | Disable the square-wave output on the SQW pin. 50 | **Returns:** null 51 | 52 | ``` 53 | setFrequency(frequency) 54 | ``` 55 | Set the frequency for the square-wave output on the SQW pin. 56 | **Parameter:** frequency - options are: 1 = 1Hz, 2 = 4.096KHz, 3 = 8.192KHz, 4 = 32.768KHz 57 | **Returns:** null 58 | 59 | ``` 60 | writeMemory(address, valuearray) 61 | ``` 62 | Write to the memory on the DS1307. The DS1307 contains 56 - Byte, battery-backed RAM with Unlimited Writes 63 | **Parameter:** address - 0x08 to 0x3F 64 | **Parameter:** valuearray - byte array (Uint8Array) containing data to be written to memory 65 | **Returns:** null 66 | 67 | ``` 68 | readMemory(address, valuearray) 69 | ``` 70 | Read from the memory on the DS1307. The DS1307 contains 56 - Byte, battery-backed RAM with Unlimited Writes 71 | **Parameter:** address - 0x08 to 0x3F 72 | **Parameter:** length - Up to 32 bytes. length can not exceed the available address space. 73 | **Returns:** Returns a Uint8Array type array of the data read from memory 74 | 75 | ## Usage 76 | 77 | To use the RTC Pi library in your code you must first import the library: 78 | ``` 79 | var rtcpi = require('../../lib/rtcpi/rtcpi'); 80 | ``` 81 | 82 | Next, you must create an RTCPi object: 83 | 84 | ``` 85 | var rtc = new RTCPi(); 86 | ``` 87 | Set the current time using a date object: 88 | ``` 89 | var d = new Date(2016, 07, 04, 10, 23, 00, 00); 90 | rtc.setDate(d); 91 | ``` 92 | Enable the square-wave output at 8.192KHz on the SQW pin: 93 | ``` 94 | rtc.set_frequency(3) 95 | rtc.enable_output() 96 | ``` 97 | Read the current date and time from the RTC at 1-second intervals: 98 | ``` 99 | var myClock = setInterval(clockTimer, 1000); 100 | 101 | function clockTimer() { 102 | console.log(rtc.readDate().toISOString()); 103 | } 104 | ``` 105 | -------------------------------------------------------------------------------- /examples/iopi/iopiread2boards.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK IO Pi Pin Read demo 2 4 | * Version 1.0 Created 20/01/2016 5 | * Version 1.1 Updated 16/ 03 / 2024 6 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 7 | * run with: sudo node iopiread2boards.js 8 | * ================================================ 9 | * 10 | * This example reads the first 8 pins from both busses on two IO Pi boards. The 11 | * internal pull- up resistors are enabled so each pin will read as 1 unless 12 | * the pin is connected to ground. 13 | * 14 | * Initialise the IOPi devices using the default addresses for board 1 and addresses 0x22 and 0x23 for board 2. 15 | * You will need to change the addresses if you have changed the jumpers on the IO Pi 16 | */ 17 | 18 | var iopi = require('../../lib/iopi/iopi'); 19 | 20 | var bus1 = new IoPi(0x20); 21 | var bus2 = new IoPi(0x21); 22 | var bus3 = new IoPi(0x22); 23 | var bus4 = new IoPi(0x23); 24 | 25 | // We will read the inputs 1 to 8 from each bus so set port 0 as inputs and 26 | // enable the internal pull-up resistors 27 | 28 | bus1.setPortDirection(0, 0xFF); 29 | bus1.setPortPullups(0, 0xFF); 30 | 31 | bus2.setPortDirection(0, 0xFF); 32 | bus2.setPortPullups(0, 0xFF); 33 | 34 | bus3.setPortDirection(0, 0xFF); 35 | bus3.setPortPullups(0, 0xFF); 36 | 37 | bus4.setPortDirection(0, 0xFF); 38 | bus4.setPortPullups(0, 0xFF); 39 | 40 | // create a timer object and read from pins 1 to 8 on the IO Pi busses every 100ms 41 | var myVar = setInterval(myTimer, 500); 42 | 43 | 44 | function myTimer() { 45 | console.log('\033[2J'); // clear the window 46 | 47 | console.log('IO Pi 1 Bus 1 Pin 1: %d', bus1.readPin(1)); 48 | console.log('IO Pi 1 Bus 1 Pin 2: %d', bus1.readPin(2)); 49 | console.log('IO Pi 1 Bus 1 Pin 3: %d', bus1.readPin(3)); 50 | console.log('IO Pi 1 Bus 1 Pin 4: %d', bus1.readPin(4)); 51 | console.log('IO Pi 1 Bus 1 Pin 5: %d', bus1.readPin(5)); 52 | console.log('IO Pi 1 Bus 1 Pin 6: %d', bus1.readPin(6)); 53 | console.log('IO Pi 1 Bus 1 Pin 7: %d', bus1.readPin(7)); 54 | console.log('IO Pi 1 Bus 1 Pin 8: %d', bus1.readPin(8)); 55 | 56 | console.log('IO Pi 1 Bus 2 Pin 1: %d', bus2.readPin(1)); 57 | console.log('IO Pi 1 Bus 2 Pin 2: %d', bus2.readPin(2)); 58 | console.log('IO Pi 1 Bus 2 Pin 3: %d', bus2.readPin(3)); 59 | console.log('IO Pi 1 Bus 2 Pin 4: %d', bus2.readPin(4)); 60 | console.log('IO Pi 1 Bus 2 Pin 5: %d', bus2.readPin(5)); 61 | console.log('IO Pi 1 Bus 2 Pin 6: %d', bus2.readPin(6)); 62 | console.log('IO Pi 1 Bus 2 Pin 7: %d', bus2.readPin(7)); 63 | console.log('IO Pi 1 Bus 2 Pin 8: %d', bus2.readPin(8)); 64 | 65 | console.log('IO Pi 2 Bus 1 Pin 1: %d', bus3.readPin(1)); 66 | console.log('IO Pi 2 Bus 1 Pin 2: %d', bus3.readPin(2)); 67 | console.log('IO Pi 2 Bus 1 Pin 3: %d', bus3.readPin(3)); 68 | console.log('IO Pi 2 Bus 1 Pin 4: %d', bus3.readPin(4)); 69 | console.log('IO Pi 2 Bus 1 Pin 5: %d', bus3.readPin(5)); 70 | console.log('IO Pi 2 Bus 1 Pin 6: %d', bus3.readPin(6)); 71 | console.log('IO Pi 2 Bus 1 Pin 7: %d', bus3.readPin(7)); 72 | console.log('IO Pi 2 Bus 1 Pin 8: %d', bus3.readPin(8)); 73 | 74 | console.log('IO Pi 2 Bus 2 Pin 1: %d', bus4.readPin(1)); 75 | console.log('IO Pi 2 Bus 2 Pin 2: %d', bus4.readPin(2)); 76 | console.log('IO Pi 2 Bus 2 Pin 3: %d', bus4.readPin(3)); 77 | console.log('IO Pi 2 Bus 2 Pin 4: %d', bus4.readPin(4)); 78 | console.log('IO Pi 2 Bus 2 Pin 5: %d', bus4.readPin(5)); 79 | console.log('IO Pi 2 Bus 2 Pin 6: %d', bus4.readPin(6)); 80 | console.log('IO Pi 2 Bus 2 Pin 7: %d', bus4.readPin(7)); 81 | console.log('IO Pi 2 Bus 2 Pin 8: %d', bus4.readPin(8)); 82 | } -------------------------------------------------------------------------------- /lib/i2cswitch/i2cswitch.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK I2C Switch controller 4 | * For use with the 4-channel I2C Switch 5 | * 6 | * Version 1.0 Created 28/11/2019 7 | * Version 1.1 Updated 22/03/2024 8 | * 9 | * Requires rpio to be installed, install with: npm install rpio 10 | * Requires i2c-switch to be installed, install with: npm install i2c-switch 11 | * ================================================ 12 | */ 13 | 14 | /** Class representing the functions for the I2C Switch */ 15 | I2CSwitch = (function (address) { 16 | 17 | // Define GPIO Reset Pin 18 | const RESETPIN = 13; 19 | 20 | // local variables 21 | var ctl = 0x00; 22 | var i2caddress = 0x70; 23 | 24 | // rpio object and i2c address variable 25 | const rpio = require('rpio'); 26 | 27 | const i2c = require('i2c-bus'); 28 | 29 | const busnumber = 1; // The number of the I2C bus/adapter to open, 0 for /dev/i2c-0, 1 for /dev/i2c-1, ... 30 | 31 | /** 32 | * Private function for reading a byte from the i2c bus 33 | * @returns {number} - Value from selected register 34 | */ 35 | function i2cReadByte() { 36 | var rxbuf = Buffer.alloc(1); 37 | const i2c1 = i2c.openSync(busnumber); 38 | i2c1.i2cReadSync(i2caddress, 1, rxbuf); 39 | i2c1.closeSync(); 40 | return rxbuf[0]; 41 | } 42 | 43 | /** 44 | * Private function for writing a byte to the i2c bus 45 | * @param {number} val - Value to write 46 | */ 47 | function i2cWriteByte(val) { 48 | var txbuf = Buffer.alloc(1, val); 49 | const i2c1 = i2c.openSync(busnumber); 50 | i2c1.i2cWriteSync(i2caddress, 1, txbuf); 51 | i2c1.closeSync(); 52 | } 53 | 54 | /** 55 | * Private function for updating a single bit within a variable 56 | * @param {number} oldByte - Variable to be updated 57 | * @param {number} bit - The location of the bit to be changed 58 | * @param {boolean} value - The new value for the bit. true or false 59 | * @returns {number} - Updated number 60 | */ 61 | function updateByte(oldByte, bit, value) { 62 | let newByte = 0; 63 | if (value === false) { 64 | newByte = oldByte & ~(1 << bit); 65 | } else { 66 | 67 | newByte = oldByte | 1 << bit; 68 | } 69 | return newByte; 70 | } 71 | 72 | /** 73 | * Private function for reading the value of a single bit in a byte 74 | * @param {number} byte - Number to be checked 75 | * @param {number} bit - Bit position 76 | * @returns {number} - 0 or 1 77 | */ 78 | function checkBit(byte, bit) { 79 | if (byte & 1 << bit) { return 1; } 80 | else{return 0;} 81 | 82 | } 83 | /** 84 | * Private function to sleep for several milliseconds 85 | */ 86 | function sleep(millis) { 87 | return new Promise(resolve => setTimeout(resolve, millis)); 88 | } 89 | 90 | /** 91 | * Initialize I2CSwitch object with i2c address, default is 0x70 92 | * @param {number} address - 0x70 to 0x77 93 | */ 94 | function I2CSwitch(address) { 95 | i2caddress = address; 96 | // set the GPIO reset pin as an output and set it high. 97 | rpio.open(RESETPIN, rpio.OUTPUT, rpio.HIGH); 98 | } 99 | 100 | 101 | /** 102 | * Enable the switch on the selected channel and disable all other channels 103 | * @param {number} channel - 1 to 4 104 | */ 105 | I2CSwitch.prototype.switchChannel = function (channel) { 106 | 107 | // check if the channel is within range 108 | if (channel < 1 || channel > 4){ 109 | throw new Error("switchChannel: channel must be between 1 and 4"); 110 | } 111 | 112 | // set the channel bit and write the value to the i2c port 113 | let i = 0; 114 | i = updateByte(i, channel - 1, 1); 115 | i2cWriteByte(i); 116 | }; 117 | 118 | /** 119 | * Set the state of the specified channel while leaving the other channels in their current state. 120 | * @param {number} channel - 1 to 4 121 | * @param {boolean} channel - true = channel enabled, false = channel disabled 122 | */ 123 | I2CSwitch.prototype.setChannelState = function (channel, state) { 124 | 125 | // check if the channel is within range 126 | if (channel < 1 || channel > 4){ 127 | throw new Error("setChannelState: channel must be between 1 and 4"); 128 | } 129 | 130 | if (state !== true || state !== false){ 131 | throw new Error("setChannelState: state must be true or false"); 132 | } 133 | 134 | // set the bit for the selected channel and write the value to the i2c port 135 | let i = i2cReadByte(); 136 | if (state === true){ 137 | i = updateByte(i, channel - 1, 1); 138 | } 139 | else{ 140 | i = updateByte(i, channel - 1, 0); 141 | } 142 | i2cWriteByte(i); 143 | }; 144 | 145 | /** 146 | * Get the state of the specified channel 147 | * @param {number} channel - 1 to 4 148 | * @returns {boolean} - true = channel enabled, false = channel disabled 149 | */ 150 | I2CSwitch.prototype.getChannelState = function (channel) { 151 | 152 | // check if channel is within range 153 | if (channel < 1 || channel > 4){ 154 | throw new Error("getChannelState: channel must be between 1 and 4"); 155 | } 156 | 157 | // set the channel bit and write the value to the i2c port 158 | let i = i2cReadByte(); 159 | return checkBit(i, channel - 1,) === 1; 160 | }; 161 | 162 | /** 163 | * Reset the PCA9546A I2C switch. 164 | * Resetting allows the PCA9546A to recover from a situation in which one of the downstream 165 | * I2C buses are stuck in a low state. All channels will be set to an off-state. 166 | */ 167 | I2CSwitch.prototype.reset = function () { 168 | // set reset pin low 169 | rpio.write(RESETPIN, rpio.LOW); 170 | // wait 1 millisecond before setting the pin high again 171 | sleep(1); 172 | rpio.write(RESETPIN, rpio.HIGH); 173 | // wait another 1 millisecond for the device to reset 174 | sleep(1); 175 | }; 176 | 177 | return I2CSwitch; 178 | 179 | })(); 180 | 181 | module.exports = I2CSwitch; -------------------------------------------------------------------------------- /lib/iozero32/README.md: -------------------------------------------------------------------------------- 1 | # AB Electronics UK IO Zero 32 Node.js Library 2 | 3 | 4 | Node.js Library to use with IO Zero 32 Raspberry Pi expansion board from https://www.abelectronics.co.uk 5 | 6 | ## Install 7 | 8 | To download to your Raspberry Pi type in the terminal: 9 | 10 | ``` 11 | git clone https://github.com/abelectronicsuk/ABElectronics_NodeJS_Libraries.git 12 | ``` 13 | The IO Zero 32 library is located in the /lib/iozero32/ directory 14 | 15 | The example files are located in the /examples/iozero32/ directory 16 | 17 | The IO Zero 32 library requires the i2c-bus library to run. 18 | 19 | Install from https://www.npmjs.com/package/i2c-bus with 20 | ``` 21 | npm install i2c-bus 22 | ``` 23 | 24 | ___ 25 | Classes: 26 | ---------- 27 | ``` 28 | IOZero32(address) 29 | ``` 30 | **Parameters:** 31 | address: (number) i2c address for the target device. 0x20 to 0x27 32 | 33 | 34 | Functions: 35 | ---------- 36 | 37 | ___ 38 | ``` 39 | setPinDirection(pin, direction): 40 | ``` 41 | Sets the IO direction for an individual pin 42 | **Parameters:** 43 | pin: (number) 1 to 16 44 | direction: (number) 1 = input, 0 = output 45 | **Returns:** null 46 | ___ 47 | ``` 48 | getPinDirection(pin) 49 | ``` 50 | Get the IO direction for an individual pin 51 | **Parameters:** 52 | pin: (number) pin to read, 1 to 16 53 | **Returns:** (number) 1 = input, 0 = output 54 | ___ 55 | ``` 56 | setPortDirection(port, direction): 57 | ``` 58 | Sets the IO direction for the specified IO port 59 | **Parameters:** 60 | port: (number) 0 = pins 1 to 8, 1 = pins 9 to 16 61 | direction: (number) between 0 and 255 or 0x00 and 0xFF. Each bit in the 8-bit number represents a pin on the port. 1 = input, 0 = output 62 | **Returns:** null 63 | ___ 64 | ``` 65 | getPortDirection(port): 66 | ``` 67 | Get the direction from an IO port 68 | **Parameters:** 69 | port: (number) 0 = pins 1 to 8, 1 = pins 9 to 16 70 | **Returns:** (number) between 0 and 255 (0xFF) 71 | ___ 72 | ``` 73 | setBusDirection(direction): 74 | ``` 75 | Sets the IO direction for all pins on the bus 76 | **Parameters:** 77 | direction: (number) 16-bit number 0 to 65535 (0xFFFF). For each bit 1 = input, 0 = output 78 | **Returns:** null 79 | ___ 80 | ``` 81 | getBusDirection() 82 | ``` 83 | Get the direction for an IO bus 84 | **Returns:** (number) 16-bit number 0 to 65535 (0xFFFF). For each bit 1 = input, 0 = output 85 | 86 | ___ 87 | ``` 88 | writePin(pin, value) 89 | ``` 90 | Write to an individual pin 1 - 16 91 | **Parameters:** 92 | pin: (number) 1 to 16 93 | value: (number) 1 = logic high, 0 = logic low 94 | **Returns:** null 95 | ___ 96 | ``` 97 | writePort(port, value) 98 | ``` 99 | Write to all pins on the selected port 100 | **Parameters:** 101 | port: (number) 0 = pins 1 to 8, 1 = pins 9 to 16 102 | value: (number) between 0 and 255 or 0x00 and 0xFF. Each bit in the 8-bit number represents a pin on the port. 1 = logic high, 0 = logic low 103 | **Returns:** null 104 | ___ 105 | ``` 106 | writeBus(value) 107 | ``` 108 | Write to all pins on the selected bus 109 | **Parameters:** 110 | value: (number) 16-bit number 0 to 65535 (0xFFFF). For each bit 1 = logic high, 0 = logic low 111 | **Returns:** null 112 | ___ 113 | ``` 114 | readPin(pin) 115 | ``` 116 | Read the value of an individual pin 1 - 16 117 | **Parameters:** 118 | pin: (number) 1 to 16 119 | **Returns:** (uint8_t) 0 = logic low, 1 = logic high 120 | ___ 121 | ``` 122 | readPort(port) 123 | ``` 124 | Read all pins on the selected port 125 | **Parameters:** 126 | port: (number) 0 = pins 1 to 8, 1 = pins 9 to 16 127 | **Returns:** (number) between 0 and 255 or 0x00 and 0xFF. Each bit in the 8-bit number represents a pin on the port. 0 = logic low, 1 = logic high 128 | ___ 129 | ``` 130 | readBus() 131 | ``` 132 | Read all pins on the bus 133 | **Returns:** (number) 16-bit number 0 to 65535 (0xFFFF) Each bit in the 16-bit number represents a pin on the port. 0 = logic low, 1 = logic high 134 | ___ 135 | ``` 136 | setPinPolarity(pin, polarity) 137 | ``` 138 | Set the polarity of the selected pin 139 | **Parameters:** 140 | pin: (number) 1 to 16 141 | polarity: (number) 0 = same logic state of the input pin, 1 = inverted logic state of the input pin 142 | **Returns:** null 143 | ___ 144 | ``` 145 | getPinPolarity(pin) 146 | ``` 147 | Get the polarity of the selected pin 148 | **Parameters:** 149 | pin: (number) pin to read, 1 to 16 150 | **Returns:** (number) 0 = same logic state of the input pin, 1 = inverted logic state of the input pin 151 | ___ 152 | ``` 153 | setPortPolarity(port, polarity) 154 | ``` 155 | Set the polarity of the pins on a selected port 156 | **Parameters:** 157 | port: (number) 0 = pins 1 to 8, 1 = pins 9 to 16 158 | polarity: (number) between 0 and 255 or 0x00 and 0xFF. Each bit in the 8-bit number represents a pin on the port. 0 = same logic state of the input pin, 1 = inverted logic state of the input pin 159 | **Returns:** null 160 | ___ 161 | ``` 162 | getPortPolarity(port): 163 | ``` 164 | Get the polarity for the selected IO port 165 | **Parameters:** 166 | port: (number) 0 = pins 1 to 8, 1 = pins 9 to 16 167 | **Returns:** (number) between 0 and 255 (0xFF) 168 | ___ 169 | ``` 170 | setBusPolarity(polarity) 171 | ``` 172 | Set the polarity of the pins on the bus 173 | **Parameters:** 174 | polarity: (number) 16-bit number 0 to 65535 (0xFFFF). For each bit 0 = same logic state of the input pin, 1 = inverted logic state of the input pin 175 | **Returns:** null 176 | ___ 177 | ``` 178 | getBusPolarity() 179 | ``` 180 | Get the polarity of the pins on the bus 181 | **Returns:** (number) 16-bit number 0 to 65535 (0xFFFF). For each bit 0 = same logic state of the input pin, 1 = inverted logic state of the input pin 182 | 183 | 184 | ## Usage 185 | 186 | To use the IO Zero 32 library in your code you must first import the library: 187 | ``` 188 | var IOZero32 = require('../../lib/iozero32/iozero32'); 189 | ``` 190 | 191 | Next, you must initialise the IOZero32 object 192 | 193 | ``` 194 | var bus1 = new IOZero32(0x20); 195 | ``` 196 | 197 | We will read the inputs 1 to 8 from bus 1 so set port 0 to be inputs 198 | 199 | ``` 200 | bus1.setPortDirection(0, 0xFF); 201 | ``` 202 | 203 | You can now read pin 1 with: 204 | ``` 205 | console.log('Pin 1: %d', bus1.readPin(1)); 206 | ``` 207 | -------------------------------------------------------------------------------- /lib/adcdacpi/adcdacpi.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * AB Electronics UK ADC-DAC Pi Zero Analogue to Digital / Digital to Analogue Converter 4 | * https://www.abelectronics.co.uk/p/74/ADC-DAC-Pi-Zero-Raspberry-Pi-ADC-and-DAC-expansion-board 5 | * 6 | * 7 | * Version 1.0 Created 06/07/2016 8 | * Version 1.1 Updated 19/06/2017 - changed library to use rpio and optimised code for speed. 9 | * ================================================ 10 | * 11 | * Based on the Microchip MCP3202 and MCP4822 12 | * 13 | * Requires rpio to be installed, install with: npm install rpio 14 | * 15 | * ================================================ 16 | */ 17 | 18 | var rpio = require('rpio'); 19 | 20 | 21 | 22 | ADCDAC = (function () { 23 | 24 | // variables 25 | var adc; 26 | var dac; 27 | var adcRefVoltage = 3.3; //reference voltage for the ADC chip. 28 | var maxDacVoltage = 2.048; // maximum voltage for the DAC output 29 | var dacGain = 1; 30 | var adctx = new Buffer([0x01, 0xC0, 0x00]); 31 | var adcrx = new Buffer([0, 0, 0]); 32 | var dactx = new Buffer([0x00, 0x00]); 33 | var dacrx = new Buffer(2); 34 | var out; 35 | 36 | /** 37 | * Create ADCDAC object and connect it to the SPI bus 38 | */ 39 | function ADCDAC() { 40 | rpio.spiBegin(); 41 | rpio.spiSetCSPolarity(0, rpio.LOW); 42 | rpio.spiSetDataMode(0); 43 | } 44 | 45 | 46 | /** 47 | * Returns the raw value from the selected ADC channel 48 | * When in differential mode, setting the channel to 1 will make IN1 = IN+ and IN2 = IN- 49 | * When in differential mode, setting the channel to 2 will make IN1 = IN- and IN2 = IN+ 50 | * @param {number} channel - 1 or 2 51 | * @param {number} mode - Mode: 0 = Single Ended or 1 = Differential 52 | * @returns {number} - Value between 0 and 4096 53 | */ 54 | ADCDAC.prototype.readADCRaw = function (channel, mode) { 55 | if (channel === 1) { 56 | if (mode === 0) { 57 | adctx[1] = 0x80; 58 | } else if (mode === 1) { 59 | adctx[1] = 0x00; 60 | } else { 61 | throw new Error("Channel 1 Mode Argument Out Of Range"); 62 | } 63 | } else if (channel === 2) { 64 | if (mode === 0) { 65 | adctx[1] = 0xC0; 66 | } else if (mode === 1) { 67 | adctx[1] = 0x40; 68 | } else { 69 | throw new Error("Channel 2 Mode Argument Out Of Range"); 70 | } 71 | } else { 72 | throw new Error("Channel Argument Out Of Range"); 73 | } 74 | 75 | rpio.spiChipSelect(0); /* Use CE0 */ 76 | rpio.spiSetClockDivider(256); 77 | rpio.spiTransfer(adctx, adcrx, 3); 78 | out = ((adcrx[1] & 0x0F) << 8) + adcrx[2]; 79 | 80 | return out; 81 | }; 82 | 83 | /** 84 | * Returns the voltage from the selected ADC channel 85 | * When in differential mode, setting the channel to 1 will make IN1 = IN+ and IN2 = IN- 86 | * When in differential mode, setting the channel to 2 will make IN1 = IN- and IN2 = IN+ 87 | * @param {number} channel - 1 or 2 88 | * @param {number} mode - Mode: 0 = Single Ended or 1 = Differential 89 | * @returns {number} - Voltage between 0 and vref 90 | */ 91 | ADCDAC.prototype.readADCVoltage = function (channel, mode) { 92 | var raw = this.readADCRaw(channel, mode); 93 | 94 | var voltage = adcRefVoltage / 4096 * raw; 95 | return voltage; 96 | }; 97 | 98 | /** 99 | * Sets the reference voltage for the ADC 100 | * @param {number} voltage - This should be the voltage as measured on the Raspberry Pi 3.3V power rail. 101 | */ 102 | ADCDAC.prototype.setADCRefVoltage = function (voltage) { 103 | adcRefVoltage = voltage; 104 | }; 105 | 106 | /** 107 | * Set the DAC voltage. 108 | * @param {number} channel - 1 or 2 109 | * @param {number} voltage - voltage can be between 0 and 2.047 volts when the gain is 1 or 0 and 3.3V when the gain is 2 110 | */ 111 | ADCDAC.prototype.setDACVoltage = function (channel, voltage) { 112 | if (channel < 1 || channel > 2) { 113 | throw new Error("Channel Out Of Range"); 114 | } 115 | 116 | if (voltage < 0 || voltage > maxDacVoltage) { 117 | throw new Error("Voltage Out Of Range"); 118 | } 119 | 120 | var rawval = voltage / 2.048 * 4096 / dacGain; // convert the voltage into a raw value 121 | this.setDACRaw(channel, rawval); // set the raw value 122 | 123 | }; 124 | 125 | /** 126 | * Set the raw value for the selected DAC channel 127 | * @param {number} channel - 1 to 2 128 | * @param {number} value - 0 to 4095 129 | */ 130 | ADCDAC.prototype.setDACRaw = function (channel, value) { 131 | if (channel < 1 || channel > 2) { 132 | throw new Error("Channel Out Of Range"); 133 | } 134 | 135 | if (value < 0 || value > 4095) { 136 | throw new Error("Value Out Of Range"); 137 | } 138 | 139 | dactx[1] = value & 0xff; 140 | dactx[0] = value >> 8 & 0xff | channel - 1 << 7 | 0x1 << 5 | 1 << 4; 141 | 142 | if (dacGain === 2) { 143 | dactx[0] = dactx[0] &= ~(1 << 5); 144 | } 145 | 146 | rpio.spiChipSelect(1); /* Use CE1 */ 147 | rpio.spiSetClockDivider(14); 148 | rpio.spiTransfer(dactx, dacrx, 2); 149 | }; 150 | 151 | /** 152 | * Set the gain for the DAC. This is used to set the output based on the reference voltage of 2.048V. 153 | * When the gain is set to 2 the maximum voltage will be approximately 3.3V. 154 | * @param {number} gain - 1 or 2 155 | */ 156 | ADCDAC.prototype.setDACGain = function (gain) { 157 | if (gain < 1 || gain > 2) { 158 | throw new Error("Gain Out Of Range"); 159 | } 160 | 161 | if (gain === 1) { 162 | dacGain = 1; 163 | maxDacVoltage = 2.048; 164 | } 165 | if (gain === 2) { 166 | dacGain = 2; 167 | maxDacVoltage = 3.3; 168 | } 169 | }; 170 | 171 | /** 172 | * Close the SPI bus 173 | */ 174 | ADCDAC.prototype.closeSPI = function () { 175 | adc.close(); 176 | dac.close(); 177 | }; 178 | 179 | 180 | return ADCDAC; 181 | 182 | })(); 183 | 184 | module.exports = ADCDAC; -------------------------------------------------------------------------------- /unittests/unittests.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK Unit Test 4 | * 5 | * Functions and Registers for testing 6 | * ================================================ 7 | */ 8 | 9 | 10 | 11 | 12 | UnitTests = (function () { 13 | 14 | // Device Register Addresses 15 | 16 | // Microchip MCP23017 17 | 18 | UnitTests.prototype.MCP23017_IODIRA = 0x00; // IO direction A - 1= input 0 = output 19 | UnitTests.prototype.MCP23017_IODIRB = 0x01; // IO direction B - 1= input 0 = output 20 | UnitTests.prototype.MCP23017_IPOLA = 0x02; // Input polarity A 21 | UnitTests.prototype.MCP23017_IPOLB = 0x03; // Input polarity B 22 | UnitTests.prototype.MCP23017_GPINTENA = 0x04; // Controls the interrupt-onchange on port A 23 | UnitTests.prototype.MCP23017_GPINTENB = 0x05; // Controls the interrupt-onchange on port B 24 | UnitTests.prototype.MCP23017_DEFVALA = 0x06; // Default value for port A 25 | UnitTests.prototype.MCP23017_DEFVALB = 0x07; // Default value for port B 26 | UnitTests.prototype.MCP23017_INTCONA = 0x08; // Interrupt control register for port A 27 | UnitTests.prototype.MCP23017_INTCONB = 0x09; // Interrupt control register for port B 28 | UnitTests.prototype.MCP23017_IOCON = 0x0A; // see datasheet for configuration register 29 | UnitTests.prototype.MCP23017_GPPUA = 0x0C; // pull-up resistors for port A 30 | UnitTests.prototype.MCP23017_GPPUB = 0x0D; // pull-up resistors for port B 31 | UnitTests.prototype.MCP23017_INTFA = 0x0E; // Interrupt condition on port A for any enabled pin 32 | UnitTests.prototype.MCP23017_INTFB = 0x0F; // Interrupt condition on port B for any enabled pin 33 | UnitTests.prototype.MCP23017_INTCAPA = 0x10; // Captures the GPIO port A value at the time the interrupt occurred 34 | UnitTests.prototype.MCP23017_INTCAPB = 0x11; // Captures the GPIO port B value at the time the interrupt occurred 35 | UnitTests.prototype.MCP23017_GPIOA = 0x12; // Data port A 36 | UnitTests.prototype.MCP23017_GPIOB = 0x13; // Data port B 37 | UnitTests.prototype.MCP23017_OLATA = 0x14; // Output latches for port A 38 | UnitTests.prototype.MCP23017_OLATB = 0x15; // Output latches for port B 39 | 40 | // NXP PCA9535 41 | 42 | UnitTests.prototype.PCA9535_INPUTPORT0 = 0x00; // Command byte Input port 0 43 | UnitTests.prototype.PCA9535_INPUTPORT1 = 0x01; // Command byte Input port 1 44 | UnitTests.prototype.PCA9535_OUTPUTPORT0 = 0x02; // Command byte Output port 0 45 | UnitTests.prototype.PCA9535_OUTPUTPORT1 = 0x03; // Command byte Output port 1 46 | UnitTests.prototype.PCA9535_INVERTPORT0 = 0x04; // Command byte Polarity Inversion port 0 47 | UnitTests.prototype.PCA9535_INVERTPORT1 = 0x05; // Command byte Polarity Inversion port 1 48 | UnitTests.prototype.PCA9535_CONFIGPORT0 = 0x06; // Command byte Configuration port 0 49 | UnitTests.prototype.PCA9535_CONFIGPORT1 = 0x07; // Command byte Configuration port 1 50 | 51 | // Test Variables 52 | 53 | var failcount = 0; 54 | var registers = Buffer.alloc(256); 55 | var gpio_state = Buffer.alloc(256); 56 | var gpio_direction = Buffer.alloc(256); 57 | 58 | 59 | const Direction = 60 | { 61 | Input: 0, 62 | Output: 1 63 | }; 64 | 65 | const State = 66 | { 67 | On: 1, 68 | Off: 0 69 | }; 70 | 71 | // text colours 72 | 73 | const REDSTART = "\x1b[1;31m"; 74 | const REDEND = "\x1b[0m"; 75 | 76 | const GREENSTART = "\x1b[1;32m"; 77 | const GREENEND = "\x1b[0m"; 78 | 79 | function UnitTests() { 80 | return; 81 | } 82 | 83 | // I2C functions 84 | 85 | 86 | 87 | UnitTests.prototype.i2c_emulator_write_byte_data = function (reg, value) { 88 | registers[reg] = value; 89 | }; 90 | 91 | UnitTests.prototype.i2c_emulator_write_word_data = function (reg, value) { 92 | registers[reg] = value & (0xff); // lower 8 bits 93 | registers[reg + 1] = (value >> 8) & 0xff; // upper 8 bits 94 | }; 95 | 96 | UnitTests.prototype.i2c_emulator_read_byte_data = function (reg) { 97 | return registers[reg]; 98 | }; 99 | 100 | UnitTests.prototype.i2c_emulator_read_word_data = function (reg) { 101 | var value = (registers[reg + 1] << 8) | registers[reg]; 102 | return value; 103 | }; 104 | 105 | // Wiring Pi Functions 106 | 107 | UnitTests.prototype.digitalWrite = function (pin, value) { 108 | gpio_state[pin] = value; 109 | }; 110 | 111 | UnitTests.prototype.pinMode = function (pin, mode) { 112 | gpio_direction[pin] = mode; 113 | }; 114 | 115 | UnitTests.prototype.wiringPiSetup = function () { 116 | return 1; 117 | }; 118 | 119 | 120 | // Test Functions 121 | 122 | UnitTests.prototype.start_test = function (functionname) { 123 | failcount = 0; 124 | console.log("TESTING: " + functionname); 125 | }; 126 | 127 | UnitTests.prototype.test_outcome = function () { 128 | if (failcount == 0) { console.log(GREENSTART + "TEST PASSED" + GREENEND); } 129 | else { console.log(REDSTART + "TEST FAILED" + REDEND); } 130 | 131 | console.log("============================================================"); 132 | }; 133 | 134 | UnitTests.prototype.test_fail = function (message) { 135 | console.log(message); 136 | failcount += 1; 137 | }; 138 | 139 | UnitTests.prototype.test_i2c_register = function (reg, value) { 140 | // tests if an i2c register has the correct value 141 | if (registers[reg] != value) { 142 | console.log(REDSTART + reg + " Register Set: FAILED" + REDEND); 143 | failcount += 1; 144 | } 145 | }; 146 | 147 | UnitTests.prototype.test_gpio_state = function (gpio, value) { 148 | // tests if the wiring pi digitalWrite set the correct pin and state 149 | if (gpio_state[gpio] != value) { 150 | if (value == State.Off) { console.log(REDSTART + "GPIO " + gpio + " Unexpected State OFF: FAILED" + REDEND); } 151 | else if (value == State.On) { console.log(REDSTART + "GPIO " + gpio + " Unexpected State ON: FAILED" + REDEND); } 152 | else { console.log(REDSTART + "GPIO " + gpio + " Unexpected State UNKNOWN: FAILED" + REDEND); } 153 | failcount += 1; 154 | } 155 | }; 156 | 157 | UnitTests.prototype.test_gpio_direction = function (gpio, value) { 158 | // tests if the wiring pi pinMode function set the correct pin and direction 159 | if (gpio_direction[gpio] != value) { 160 | if (value == Direction.Output) { console.log(REDSTART + "GPIO " + gpio + " Unexpected Direction OUTPUT: FAILED" + REDEND); } 161 | else if (value == Direction.Input) { console.log(REDSTART + "GPIO " + gpio + " Unexpected Direction INPUT: FAILED" + REDEND); } 162 | else { console.log(REDSTART + "GPIO " + gpio + " Unexpected Direction UNKNOWN: FAILED" + REDEND); } 163 | failcount += 1; 164 | } 165 | }; 166 | 167 | UnitTests.prototype.test_exception_failed = function (message) { 168 | // This function is called inside a try catch if an exception failed to be called. 169 | console.log(REDSTART + "Execption Handling on " + message + " : FAILED" + REDEND); 170 | failcount += 1; 171 | }; 172 | 173 | UnitTests.prototype.test_set_bit = function (oldByte, bit, value) { 174 | // update the value of a bit within a variable 175 | var newByte = 0; 176 | if (value == false) { 177 | newByte = oldByte & ~(1 << bit); 178 | } else { 179 | 180 | newByte = oldByte | 1 << bit; 181 | } 182 | return (newByte); 183 | }; 184 | 185 | UnitTests.prototype.test_get_bit = function (byte, bit) { 186 | // checking the status of a bit within an variable 187 | if ((byte >> bit) % 2 != 0) return (true); 188 | else return (false); 189 | }; 190 | return UnitTests; 191 | 192 | })(); 193 | 194 | module.exports = UnitTests; -------------------------------------------------------------------------------- /lib/rtcpi/rtcpi.js: -------------------------------------------------------------------------------- 1 | /** 2 | * ================================================ 3 | * AB Electronics UK RTC Pi real-time clock 4 | * For use with the RTC Pi, RTC Pi Plus and RTC Pi Zero 5 | * Version 1.0 Created 06/07/2016 6 | * Version 1.1 Updated 21/03/2024 7 | * Requires i2c-bus to be installed, install with: npm install i2c-bus 8 | * ================================================ 9 | */ 10 | 11 | // Define registers values from the datasheet 12 | const SECONDS = 0x00; 13 | const MINUTES = 0x01; 14 | const HOURS = 0x02; 15 | const DAYOFWEEK = 0x03; 16 | const DAY = 0x04; 17 | const MONTH = 0x05; 18 | const YEAR = 0x06; 19 | const CONTROL = 0x07; 20 | 21 | // variables 22 | const rtcAddress = 0x68; // I2C address 23 | // initial configuration - square wave and output disabled, frequency set 24 | // to 32.768KHz. 25 | var config = 0x03; 26 | // the DS1307 does not store the current century so that has to be added on 27 | // manually. 28 | var century = 2000; 29 | 30 | const i2c = require('i2c-bus'); 31 | 32 | const busnumber = 1; // The number of the I2C bus/adapter to open, 0 for /dev/i2c-0, 1 for /dev/i2c-1, ... 33 | 34 | /** 35 | * RTC Class 36 | */ 37 | RTCPi = (function () { 38 | 39 | /** 40 | * Initialise the RTC Pi I2C connection and set the default configuration to the control register 41 | */ 42 | function RTCPi() { 43 | i2cWriteByte(CONTROL, config); 44 | } 45 | 46 | // Private functions 47 | 48 | /** 49 | * Write a single byte to the I2C bus. 50 | * @param {number} register - Target register 51 | * @param {number} val - Value to be written 52 | */ 53 | function i2cWriteByte(register, val) { 54 | const i2c1 = i2c.openSync(busnumber); 55 | i2c1.writeByteSync(rtcAddress, register, val); 56 | i2c1.closeSync(); 57 | } 58 | 59 | /** 60 | * Update a single bit within a variable 61 | * @param {number} oldByte - Variable to be updated 62 | * @param {number} bit - The location of the bit to be changed 63 | * @param {boolean} value - The new value for the bit. true or false 64 | * @returns {number} - Updated value 65 | */ 66 | function updateByte(oldByte, bit, value) { 67 | if (!value) { 68 | return oldByte & ~(1 << bit); 69 | } else { 70 | return oldByte | 1 << bit; 71 | } 72 | } 73 | 74 | /** 75 | * Convert a BCD formatted number to decimal. 76 | * @param {number} val - BCD value 77 | * @returns {number} - Decimal value 78 | */ 79 | function bcdToDec(val) { 80 | return val - 6 * (val >> 4); 81 | } 82 | 83 | /** 84 | * Convert a decimal to BCD formatted number. 85 | * @param {number} val - Decimal value 86 | * @returns {number} - BCD value 87 | */ 88 | function decToBcd(val) { 89 | return val / 10 << 4 | val % 10; 90 | } 91 | 92 | /** 93 | * Calculate the current century 94 | * @param {number} val - Year 95 | */ 96 | function getCentury(val) { 97 | if (val.length > 2) { 98 | var y = val[0] + val[1]; 99 | century = int(y) * 100; 100 | } 101 | } 102 | 103 | // public functions 104 | 105 | /** 106 | * Set the date and time on the RTC 107 | * @param {Date} date - Use a javascript Date object 108 | */ 109 | RTCPi.prototype.setDate = function (date) { 110 | getCentury(date.getFullYear()); 111 | i2cWriteByte(SECONDS, decToBcd(date.getSeconds())); 112 | i2cWriteByte(MINUTES, decToBcd(date.getMinutes())); 113 | i2cWriteByte(HOURS, decToBcd(date.getHours())); 114 | i2cWriteByte(DAYOFWEEK, decToBcd(date.getDay())); 115 | i2cWriteByte(DAY, decToBcd(date.getDate())); 116 | i2cWriteByte(MONTH, decToBcd(date.getMonth() - 1)); 117 | i2cWriteByte(YEAR, decToBcd(date.getFullYear() - century)); 118 | }; 119 | 120 | /** 121 | * Read the date and time from the RTC 122 | * @returns {Date} - Returns the date as a javascript Date object 123 | */ 124 | RTCPi.prototype.readDate = function () { 125 | let txbuf = Buffer.alloc(1); 126 | let rxbuf = Buffer.alloc(7); 127 | 128 | const i2c1 = i2c.openSync(busnumber); // open the i2c bus 129 | i2c1.i2cWriteSync(rtcAddress, 1, txbuf); 130 | i2c1.i2cReadSync(rtcAddress, 7, rxbuf); 131 | i2c1.closeSync(); // close the i2c bus 132 | 133 | return new Date(bcdToDec(rxbuf[6]) + century, bcdToDec(rxbuf[5]), bcdToDec(rxbuf[4]), bcdToDec(rxbuf[2] + 1), bcdToDec(rxbuf[1]), bcdToDec(rxbuf[0]),0); 134 | }; 135 | 136 | /** 137 | * Enable the output pin 138 | */ 139 | RTCPi.prototype.enableOutput = function () { 140 | config = updateByte(config, 7, true); 141 | config = updateByte(config, 4, true); 142 | i2cWriteByte(CONTROL, config); 143 | }; 144 | 145 | /** 146 | * Disable the output pin 147 | */ 148 | RTCPi.prototype.disableOutput = function () { 149 | config = updateByte(config, 7, false); 150 | config = updateByte(config, 4, false); 151 | i2cWriteByte(CONTROL, config); 152 | }; 153 | 154 | /** 155 | * Set the frequency of the output pin square-wave 156 | * @param {number} frequency - 1 = 1Hz, 2 = 4.096KHz, 3 = 8.192KHz, 4 = 32.768KHz 157 | */ 158 | RTCPi.prototype.setFrequency = function (frequency) { 159 | switch (frequency) { 160 | case 1: 161 | config = updateByte(config, 0, false); 162 | config = updateByte(config, 1, false); 163 | break; 164 | case 2: 165 | config = updateByte(config, 0, true); 166 | config = updateByte(config, 1, false); 167 | break; 168 | case 3: 169 | config = updateByte(config, 0, false); 170 | config = updateByte(config, 1, true); 171 | break; 172 | case 4: 173 | config = updateByte(config, 0, true); 174 | config = updateByte(config, 1, true); 175 | break; 176 | default: 177 | throw new Error("Argument Out Of Range"); 178 | } 179 | i2cWriteByte(CONTROL, config); 180 | }; 181 | 182 | /** 183 | * Write to the memory on the DS1307 184 | * The DS1307 contains 56 - Byte, battery-backed RAM with Unlimited Writes 185 | * @param {number} address - 0x08 to 0x3F 186 | * @param {Uint8Array} valuearray - byte array containing data to be written to memory. Length can not exceed the available address space. 187 | */ 188 | RTCPi.prototype.writeMemory = function (address, valuearray) { 189 | if (address + valuearray.length <= 0x3F) { 190 | if (address >= 0x08 && address <= 0x3F) { 191 | 192 | const data = Buffer.from(valuearray); 193 | 194 | // Create a new Buffer that is one byte larger than the original 195 | const newBuffer = Buffer.alloc(data.length + 1); 196 | 197 | // Copy the original Buffer into the new Buffer, starting at the second byte 198 | data.copy(newBuffer, 1); 199 | 200 | // Set the first byte to 'address' 201 | newBuffer[0] = address; 202 | 203 | // write the array to the RTC memory 204 | const i2c1 = i2c.openSync(busnumber); // open the i2c bus 205 | i2c1.i2cWriteSync(rtcAddress, newBuffer.length, newBuffer); 206 | i2c1.closeSync(); // close the i2c bus 207 | } 208 | else { 209 | throw new Error("Memory address outside of range: 0x08 to 0x3F"); 210 | } 211 | } 212 | else { 213 | throw new Error("Array is larger than the available memory space"); 214 | } 215 | 216 | }; 217 | 218 | /** 219 | * Read from the memory on the DS1307 220 | * The DS1307 contains 56 - Byte, battery-backed RAM with Unlimited Writes 221 | * @param {Number} address - 0x08 to 0x3F 222 | * @param {Number} length - Up to 32 bytes. length can not exceed the available address space. 223 | * @returns {Uint8Array} - Returns an array of the data read from memory 224 | */ 225 | RTCPi.prototype.readMemory = function (address, length) { 226 | if (address >= 0x08 && address <= 0x3F) { 227 | if (address <= 0x3F - length) { 228 | let tx_buffer = Buffer.alloc(1, address); 229 | let rx_buffer = Buffer.alloc(length); 230 | 231 | const i2c1 = i2c.openSync(busnumber); // open the i2c bus 232 | i2c1.i2cWriteSync(rtcAddress, 1, tx_buffer); 233 | i2c1.i2cReadSync(rtcAddress, rx_buffer.length, rx_buffer); 234 | i2c1.closeSync(); // close the i2c bus 235 | 236 | // Convert it to a Uint8Array and return the array 237 | return new Uint8Array(rx_buffer.buffer, rx_buffer.byteOffset, rx_buffer.byteLength); 238 | } 239 | else { 240 | throw new Error("Memory overflow error: address + length exceeds 0x3F"); 241 | } 242 | } 243 | else { 244 | throw new Error("Memory address outside of range: 0x08 to 0x3F"); 245 | } 246 | }; 247 | 248 | return RTCPi; 249 | 250 | })(); 251 | 252 | module.exports = RTCPi; -------------------------------------------------------------------------------- /lib/servopi/README.md: -------------------------------------------------------------------------------- 1 | # AB Electronics UK Servo Pi Node.js Library 2 | 3 | Node.js Library to use with Servo Pi Raspberry PWM servo control board from https://www.abelectronics.co.uk 4 | 5 | **Note:** Version 2.0 replaces the ServoPi class with the PWM class. The new Servo class is a helper for using the Servo Pi with analogue radio control servos. 6 | 7 | ## Install 8 | 9 | To download to your Raspberry Pi type in the terminal: 10 | 11 | ``` 12 | git clone https://github.com/abelectronicsuk/ABElectronics_NodeJS_Libraries.git 13 | ``` 14 | The Servo Pi library is located in the /lib/servopi/ directory 15 | 16 | The example files are located in the /examples/servopi/ directory 17 | 18 | The Servo Pi library requires the rpio and i2c-bus libraries to run. 19 | 20 | Install rpio from https://www.npmjs.com/package/rpio with 21 | ``` 22 | npm install rpio 23 | ``` 24 | 25 | Install i2c-bus from https://www.npmjs.com/package/i2c-bus with 26 | ``` 27 | npm install i2c-bus 28 | ``` 29 | 30 | # PWM Class 31 | 32 | The PWM class contains the functions needed to control the frequency, pulse duration and other features of the PCA9685 PMW controller used on the Servo Pi. 33 | 34 | ``` 35 | PWM(address) 36 | ``` 37 | **Parameter:** address - 0x40 to 0x7F. I2C address for the target device 38 | 39 | ## Functions: 40 | 41 | ``` 42 | setPWMFrequency(freq, calibration) 43 | ``` 44 | Set the PWM frequency 45 | **Parameter:** freq - Frequency between 40 and 1000 46 | **Parameter:** calibration - Oscillator calibration offset. Use this to adjust for oscillator drift. Value is normally between -10 and 10 47 | **Returns:** null 48 | 49 | ``` 50 | setPWM(channel, on, off) 51 | ``` 52 | Set the output on single channels 53 | **Parameter:** channel - 1 to 16, 54 | **Parameter:** on - 0 to 4095 55 | **Parameter:** off - 0 to 4095 56 | **Returns:** null 57 | 58 | ``` 59 | setPWMOnTime(channel, on) 60 | ``` 61 | Set the on time on a single channel 62 | **Parameter:** channel - 1 to 16, 63 | **Parameter:** on - 0 to 4095 64 | **Returns:** null 65 | 66 | ``` 67 | setPWMOffTime(channel, off) 68 | ``` 69 | Set the off time on a single channel 70 | **Parameter:** channel - 1 to 16, 71 | **Parameter:** off - 0 to 4095 72 | **Returns:** null 73 | 74 | ``` 75 | getPWMOffTime(channel) 76 | ``` 77 | Get the off time on a single channel 78 | **Parameter:** channel - 1 to 16, 79 | **Returns:** 0 to 4095 80 | 81 | ``` 82 | getPWMOnTime(channel, off) 83 | ``` 84 | Get the on time on a single channel 85 | **Parameter:** channel - 1 to 16, 86 | **Returns:** 0 to 4095 87 | 88 | ``` 89 | setAllPWM(on, off) 90 | ``` 91 | Set the output on all channels 92 | **Parameter:** channel - 1 to 16, 93 | **Parameter:** on - 0 to 4095 94 | **Parameter:** off - 0 to 4095 95 | **Returns:** null 96 | 97 | ``` 98 | outputDisable() 99 | ``` 100 | Disable the output via the OE pin 101 | **Parameter:** null 102 | **Returns:** null 103 | 104 | ``` 105 | outputEnable() 106 | ``` 107 | Enable the output via the OE pin 108 | **Parameter:** null 109 | **Returns:** null 110 | 111 | ``` 112 | setAllCallAddress(address) 113 | ``` 114 | Set the I2C address for the All Call function 115 | **Parameter:** address 116 | **Returns:** null 117 | 118 | ``` 119 | enableAllCallAddress() 120 | ``` 121 | Enable the I2C address for the All Call function 122 | **Parameter:** null 123 | **Returns:** null 124 | 125 | ``` 126 | disableAllCallAddress() 127 | ``` 128 | Disable the I2C address for the All Call function 129 | **Parameter:** null 130 | **Returns:** null 131 | 132 | ``` 133 | sleep() 134 | ``` 135 | Put the device into a sleep state 136 | **Parameter:** null 137 | **Returns:** null 138 | 139 | ``` 140 | wake() 141 | ``` 142 | Wake the device from its sleep state 143 | **Parameter:** null 144 | **Returns:** null 145 | 146 | ``` 147 | isSleeping() 148 | ``` 149 | Check the sleep status of the device 150 | **Parameter:** null 151 | **Returns:** true = sleeping, false = awake 152 | 153 | ``` 154 | invertOutput() 155 | ``` 156 | Invert the PWM output on all channels 157 | **Parameter:** null 158 | **Returns:** null 159 | 160 | ## Usage 161 | 162 | To use the Servo Pi library in your code you must first import the library: 163 | ``` 164 | var servopi = require('../../lib/servopi/servopi') 165 | ``` 166 | Next you must initialise the PWM object: 167 | ``` 168 | var pwm = new PWM(0x40); 169 | ``` 170 | Set the PWM frequency to 60 Hz with a calibration value of 0 and enable the output 171 | ``` 172 | pwm.setPWMFrequency(60, 0); 173 | pwm.outputEnable(); 174 | ``` 175 | Create an array of pulse duty cycles 25%, 50% and 75%, and a counter variable 176 | ``` 177 | var positions = [1024, 2048, 3072]; 178 | var count = 0; 179 | ``` 180 | Create a timer object and change the pulse width on pin 1 between three values 181 | ``` 182 | var myTimer = setInterval(clockTimer, 1000); 183 | 184 | function clockTimer() { 185 | // move the 186 | pwm.setPWM(1, 0, positions[count]); 187 | count++; 188 | if (count > positions.length) { 189 | count = 0; 190 | } 191 | } 192 | ``` 193 | 194 | # Servo Class 195 | 196 | The Servo class contains the functions for controlling analogue RC servos using the Servo Pi. 197 | 198 | ``` 199 | Servo(address, low_limit, high_limit, reset) 200 | ``` 201 | **Parameter:** address - 0x40 to 0x7F. I2C address for the target device 202 | **Parameter:** low_limit - Lower servo limit in milliseconds. Typically 1.0 203 | **Parameter:** high_limit - Upper servo limit in milliseconds. Typically 2.0 204 | **Parameter:** reset - True = All channels reset to default off state, False = All channels retain their current state 205 | 206 | ## Functions: 207 | 208 | ``` 209 | move(channel, servopos, steps) 210 | ``` 211 | Move the servo to a new position 212 | **Parameter:** channel - 1 to 16 213 | **Parameter:** servopos - 0 to the number of steps 214 | **Parameter:** steps - Number of steps. Typically 250 for an RC servo. 215 | **Returns:** null 216 | 217 | ``` 218 | getPosition(channel, steps) 219 | ``` 220 | Get the current position of the servo. Due to rounding errors, the returned value may differ from the set value when steps is above 250. 221 | **Parameter:** channel - 1 to 16 222 | **Parameter:** steps - Number of steps. Typically 250 for an RC servo. 223 | **Returns:** current position 224 | 225 | ``` 226 | setLowLimit(low_limit, channel) 227 | ``` 228 | Set the low limit in milliseconds 229 | **Parameter:** low_limit - Typically 1.0 milliseconds. Setting the value too low may damage your servo. 230 | **Parameter:** channel - 1 to 16. If the channel is omitted or set as 0 the value will be set for all channels. 231 | **Returns:** null 232 | 233 | ``` 234 | setHighLimit(high_limit, channel) 235 | ``` 236 | Set the low limit in milliseconds 237 | **Parameter:** high_limit - Typically 2.0 milliseconds. Setting the value too high may damage your servo. 238 | **Parameter:** channel - 1 to 16. If the channel is omitted or set as 0 the value will be set for all channels. 239 | **Returns:** null 240 | 241 | ``` 242 | setPWMFrequency(freq, calibration) 243 | ``` 244 | Set the PWM frequency. For RC servos this should normally be set to 50Hz. 245 | **Parameter:** freq - Frequency between 40 and 1000 246 | **Parameter:** calibration - Oscillator calibration offset. Use this to adjust for oscillator drift. Value is normally between -10 and 10 247 | **Returns:** null 248 | 249 | ``` 250 | outputDisable() 251 | ``` 252 | Disable the output via OE pin 253 | **Parameter:** null 254 | **Returns:** null 255 | 256 | ``` 257 | outputEnable() 258 | ``` 259 | Enable the output via OE pin 260 | **Parameter:** null 261 | **Returns:** null 262 | 263 | ``` 264 | offsetEnable() 265 | ``` 266 | Enable pulse offsets. This will set servo pulses to be staggered across the channels to reduce surges in the current draw 267 | **Parameter:** null 268 | **Returns:** null 269 | 270 | ``` 271 | offsetDisable() 272 | ``` 273 | Disable pulse offsets. This will set all servo pulses to start at the same time 274 | **Parameter:** null 275 | **Returns:** null 276 | 277 | ``` 278 | sleep() 279 | ``` 280 | Put the device into a sleep state 281 | **Parameter:** null 282 | **Returns:** null 283 | 284 | ``` 285 | wake() 286 | ``` 287 | Wake the device from its sleep state 288 | **Parameter:** null 289 | **Returns:** null 290 | 291 | ``` 292 | isSleeping() 293 | ``` 294 | Check the sleep status of the device 295 | **Parameter:** null 296 | **Returns:** true = sleeping, false = awake 297 | 298 | ## Usage 299 | 300 | To use the Servo Pi library in your code you must first import the library: 301 | ``` 302 | var servopi = require('../../lib/servopi/servopi') 303 | ``` 304 | Next, you must initialise the Servo object. I2C channel 40, low limit of 1ms, high limit of 2ms and reset enabled. 305 | ``` 306 | var servo = new Servo(0x40, 1.0, 2.0, true); 307 | ``` 308 | Set the PWM frequency to 60 Hz with a calibration value of 0 and enable the output 309 | ``` 310 | servo.setPWMFrequency(60, 0); 311 | servo.outputEnable(); 312 | ``` 313 | Create a positions array and a counter variable 314 | ``` 315 | var positions = [1, 175, 250]; 316 | var count = 0; 317 | ``` 318 | Create a timer object and move the servo on pin 1 between three points 319 | ``` 320 | var myTimer = setInterval(clockTimer, 1000); 321 | 322 | function clockTimer() { 323 | // move the servo channel 1 to the three positions 324 | servo.move(1, positions[count], 250); 325 | count++; 326 | if (count >= positions.length) { 327 | count = 0; 328 | } 329 | } 330 | ``` -------------------------------------------------------------------------------- /lib/expanderpi/README.md: -------------------------------------------------------------------------------- 1 | AB Electronics UK Expander Pi Node.js Library 2 | ===== 3 | 4 | Node.js Library to use with Expander Pi Raspberry Pi expansion board from https://www.abelectronics.co.uk 5 | 6 | **Note:** Microchip recommends that digital pins 8 (GPA7) and 16 (GPB7) are used as outputs only. This change was made for revision D MCP23017 chips manufactured after June 2020. See the [MCP23017 datasheet](https://www.abelectronics.co.uk/docs/pdf/microchip-mcp23017.pdf) for more information. 7 | 8 | Install 9 | ==== 10 | 11 | To download to your Raspberry Pi type in the terminal: 12 | 13 | ``` 14 | git clone https://github.com/abelectronicsuk/ABElectronics_NodeJS_Libraries.git 15 | ``` 16 | The IO Pi library is located in the /lib/expanderpi/ directory 17 | 18 | The example files are located in the /examples/expanderpi/ directory 19 | 20 | The Expander Pi library requires the rpio and i2c-bus libraries to run. 21 | 22 | Install rpio from https://www.npmjs.com/package/rpio with 23 | ``` 24 | npm install rpio 25 | ``` 26 | 27 | Install i2c-bus from https://www.npmjs.com/package/i2c-bus with 28 | ``` 29 | npm install i2c-bus 30 | ``` 31 | 32 | The Expander Pi library is split up into four classes, ADC, DAC, IO and RTC. 33 | 34 | # ExpanderPiADC 35 | 36 | The ExpanderPiADC class controls the MCP3208 12-bit ADC. 37 | 38 | ## Functions 39 | 40 | ``` 41 | readADCVoltage(channel, mode) 42 | ``` 43 | Read the voltage from the selected channel on the ADC 44 | **Parameters:** channel - 1 or 2; mode - 0 = single-ended, 1 = differential 45 | **Returns:** number as a float between 0 and 2.048 46 | 47 | ``` 48 | readADCRaw(channel, mode) 49 | ``` 50 | Read the raw value from the selected channel on the ADC 51 | **Parameters:** channel - 1 or 2; mode - 0 = single-ended, 1 = differential 52 | **Returns:** int 53 | ``` 54 | setADCRefVoltage(voltage) 55 | ``` 56 | Set the reference voltage for the analogue to digital converter. 57 | The ADC uses the raspberry pi 3.3V power as a voltage reference so using this method to set the reference to match the exact output voltage from the 3.3V regulator will increase the accuracy of the ADC readings. 58 | **Parameters:** voltage - float between 0.0 and 7.0 59 | **Returns:** null 60 | 61 | ## Usage 62 | 63 | To use the ExpanderPiADC class in your code you must first import the Expander Pi library: 64 | ``` 65 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 66 | ``` 67 | Next you must initialise the ExpanderPiADC object: 68 | ``` 69 | var adc = new ExpanderPiADC(); 70 | ``` 71 | Set the reference voltage. 72 | ``` 73 | adc.setADCRefVoltage(4.096); 74 | ``` 75 | Read the voltage from channel 1 and display it on the screen 76 | ``` 77 | console.log('Reading 1 Voltage: ' + adc.readADCVoltage(1, 0)); 78 | ``` 79 | 80 | # ExpanderPi 81 | 82 | The ExpanderPiDAC class controls the MCP4822 12-bit DAC. 83 | 84 | ## Functions 85 | 86 | ``` 87 | setDACVoltage(channel, voltage) 88 | ``` 89 | Set the voltage for the selected channel on the DAC. The DAC has two gain values, 1 or 2, which can be set when the ADCDAC object is created. A gain of 1 will give a voltage between 0 and 2.047 volts. A gain of 2 will give a voltage between 0 and 4.096 volts. 90 | **Parameters:** channel - 1 or 2, voltage - sets the voltage for the selected channel. 91 | **Returns:** null 92 | 93 | ``` 94 | setDACRaw(channel, value) 95 | ``` 96 | Set the raw value from the selected channel on the DAC 97 | **Parameters:** channel - 1 or 2, value - int between 0 and 4095 98 | **Returns:** null 99 | 100 | ``` 101 | setDACGain(gain) 102 | ``` 103 | Set the gain for the DAC. This is used to set the output based on the reference voltage of 2.048V. 104 | When the gain is set to 2 the maximum voltage will be approximately 4.096V. 105 | **Parameters:** gain - 1 or 2 106 | **Returns:** null 107 | 108 | ## Usage 109 | 110 | To use the ExpanderPiDAC class in your code you must first import the Expander Pi library: 111 | ``` 112 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 113 | ``` 114 | 115 | Next, you must initialise the ExpanderPiDAC object: 116 | ``` 117 | var dac = new ExpanderPiDAC(); 118 | ``` 119 | 120 | Set the DAC gain to be 1 121 | ``` 122 | dac.setDACGain(1); 123 | ``` 124 | 125 | Set the voltage on channel 1 as 0.8V and channel 2 as 1.5V. 126 | 127 | ``` 128 | dac.setDACVoltage(1, 0.8); 129 | dac.setDACVoltage(2, 1.5); 130 | ``` 131 | 132 | # ExpanderPiIO 133 | 134 | The ExpanderPiIO class controls the MCP23017 16-pin I/O controller. 135 | 136 | ## Functions 137 | 138 | ``` 139 | setPinDirection(pin, direction): 140 | ``` 141 | Sets the IO direction for an individual pin 142 | **Parameters:** pin - 1 to 16, direction - 1 = input, 0 = output 143 | **Returns:** null 144 | 145 | ``` 146 | setPortDirection(port, direction): 147 | ``` 148 | Sets the IO direction for the specified IO port 149 | **Parameters:** port - 0 = pins 1 to 8, port 1 = pins 9 to 16, direction - 1 = input, 0 = output 150 | **Returns:** null 151 | 152 | ``` 153 | setPinPullup(pin, value) 154 | ``` 155 | Set the internal 100K pull-up resistors for an individual pin 156 | **Parameters:** pin - 1 to 16, value: 1 = Enabled, 0 = Disabled 157 | **Returns:** null 158 | 159 | ``` 160 | setPortPullups(port, value) 161 | ``` 162 | Set the internal 100K pull-up resistors for the selected IO port 163 | **Parameters:** port - 0 or 1, value: 0x00 to 0xFF 164 | **Returns:** null 165 | 166 | ``` 167 | writePin(pin, value) 168 | ``` 169 | Write to an individual pin 1 - 16 170 | **Parameters:** pin - 1 to 16, value - 1 = Enabled, 0 = Disabled 171 | **Returns:** null 172 | ``` 173 | writePort(self, port, value) 174 | ``` 175 | Write to all pins on the selected port 176 | **Parameters:** port - 0 = pins 1 to 8, port 1 = pins 9 to 16, value - number between 0 and 255 or 0x00 and 0xFF 177 | **Returns:** null 178 | ``` 179 | readPin(pin) 180 | ``` 181 | Read the value of an individual pin 1 - 16 182 | **Parameters:** pin: 1 to 16 183 | **Returns:** 0 = logic level low, 1 = logic level high 184 | ``` 185 | readPort(port) 186 | ``` 187 | Read all pins on the selected port 188 | **Parameters:** port - 0 = pins 1 to 8, port 1 = pins 9 to 16 189 | **Returns:** number between 0 and 255 or 0x00 and 0xFF 190 | ``` 191 | invertPort(port, polarity) 192 | ``` 193 | Invert the polarity of the pins on a selected port 194 | **Parameters:** port - 0 = pins 1 to 8, port 1 = pins 9 to 16, polarity - 0 = same logic state of the input pin, 1 = inverted logic state of the input pin 195 | **Returns:** null 196 | 197 | ``` 198 | invertPin(pin, polarity) 199 | ``` 200 | Invert the polarity of the selected pin 201 | **Parameters:** pin - 1 to 16, polarity - 0 = same logic state of the input pin, 1 = inverted logic state of the input pin 202 | **Returns:** null 203 | 204 | ``` 205 | mirrorInterrupts(value) 206 | ``` 207 | Mirror Interrupts 208 | **Parameters:** value - 1 = The INT pins are internally connected, 0 = The INT pins are not connected. INTA is associated with PortA and INTB is associated with PortB 209 | **Returns:** null 210 | 211 | ``` 212 | setInterruptType(port, value) 213 | ``` 214 | Sets the type of interrupt for each pin on the selected port 215 | **Parameters:** port 0 = pins 1 to 8, port 1 = pins 9 to 16, value: 1 = interrupt is fired when the pin matches the default value, 0 = the interrupt is fired on state change 216 | **Returns:** null 217 | 218 | ``` 219 | setInterruptPolarity(value) 220 | ``` 221 | This sets the polarity of the INT output pins 222 | **Parameters:** 1 = Active-high, 0 = Active-low 223 | **Returns:** null 224 | 225 | ``` 226 | setInterruptDefaults(port, value) 227 | ``` 228 | These bits set the compare value for pins configured for interrupt-on-change on the selected port. 229 | If the associated pin level is the opposite of the register bit, an interrupt occurs. 230 | **Parameters:** port 0 = pins 1 to 8, port 1 = pins 9 to 16, value: compare value 231 | **Returns:** null 232 | ``` 233 | setInterruptOnPort(port, value) 234 | ``` 235 | Enable interrupts for the pins on the selected port 236 | **Parameters:** port 0 = pins 1 to 8, port 1 = pins 9 to 16, value: number between 0 and 255 or 0x00 and 0xFF 237 | **Returns:** null 238 | 239 | ``` 240 | setInterruptOnPin(pin, value) 241 | ``` 242 | Enable interrupts for the selected pin 243 | **Parameters:** pin - 1 to 16, value - 0 = interrupt disabled, 1 = interrupt enabled 244 | **Returns:** null 245 | 246 | ``` 247 | readInterruptStatus(port) 248 | ``` 249 | Read the interrupt status for the pins on the selected port 250 | **Parameters:** port 0 = pins 1 to 8, port 1 = pins 9 to 16 251 | **Returns:** status 252 | 253 | ``` 254 | readInterruptCapture(port) 255 | ``` 256 | Read the value from the selected port at the time of the last interrupt trigger 257 | **Parameters:** port 0 = pins 1 to 8, port 1 = pins 9 to 16 258 | **Returns:** status 259 | 260 | ``` 261 | resetInterrupts() 262 | ``` 263 | Set the interrupts A and B to 0 264 | **Parameters:** null 265 | **Returns:** null 266 | 267 | ## Usage 268 | 269 | To use the ExpanderPiIO class in your code you must first import the Expander Pi library: 270 | ``` 271 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 272 | ``` 273 | 274 | Next, you must initialise the ExpanderPiIO object 275 | 276 | ``` 277 | var bus = new ExpanderPiIO(); 278 | ``` 279 | 280 | We will read the inputs 1 to 8 from bus 1 so set port 0 as inputs and enable the internal pull-up resistors 281 | 282 | ``` 283 | bus.setPortDirection(0, 0xFF); 284 | bus.setPortPullups(0, 0xFF); 285 | ``` 286 | 287 | You can now read pin 1 with: 288 | ``` 289 | console.log('Pin 1: %d', bus.readPin(1)); 290 | ``` 291 | 292 | # ExpanderPiRTC 293 | 294 | The ExpanderPiRTC class controls the DS1307 real-time clock. 295 | 296 | ## Functions: 297 | 298 | ``` 299 | setDate(date) 300 | ``` 301 | Set the date and time on the RTC using a javascript Date object 302 | **Parameters:** date 303 | **Returns:** null 304 | 305 | ``` 306 | readDate() 307 | ``` 308 | Returns the date from the RTC as a javascript Date object 309 | **Returns:** date object 310 | 311 | 312 | ``` 313 | enableOutput() 314 | ``` 315 | Enable the square-wave output on the SQW pin. 316 | **Returns:** null 317 | 318 | ``` 319 | disableOutput() 320 | ``` 321 | Disable the square-wave output on the SQW pin. 322 | **Returns:** null 323 | 324 | ``` 325 | setFrequency(frequency) 326 | ``` 327 | Set the frequency for the square-wave output on the SQW pin. 328 | **Parameters:** frequency - options are: 1 = 1Hz, 2 = 4.096KHz, 3 = 8.192KHz, 4 = 32.768KHz 329 | **Returns:** null 330 | 331 | ``` 332 | writeMemory(address, valuearray) 333 | ``` 334 | Write to the memory on the DS1307. The DS1307 contains 56 - Byte, battery-backed RAM with Unlimited Writes 335 | **Parameters:** address - 0x08 to 0x3F 336 | **Parameters:** valuearray - byte array (Uint8Array) containing data to be written to memory 337 | **Returns:** null 338 | 339 | ``` 340 | readMemory(address, valuearray) 341 | ``` 342 | Read from the memory on the DS1307. The DS1307 contains 56 - Byte, battery-backed RAM with Unlimited Writes 343 | **Parameters:** address - 0x08 to 0x3F 344 | **Parameters:** length - Up to 32 bytes. length can not exceed the available address space. 345 | **Returns:** Returns a Uint8Array type array of the data read from memory 346 | 347 | ## Usage 348 | 349 | To use the ExpanderPiRTC class in your code you must first import the Expander Pi library: 350 | ``` 351 | var expanderpi = require('../../lib/expanderpi/expanderpi'); 352 | ``` 353 | 354 | Next, you must create an ExpanderPiRTC object: 355 | 356 | ``` 357 | var rtc = new ExpanderPiRTC(); 358 | ``` 359 | Set the current time using a date object: 360 | ``` 361 | var d = new Date(2016, 07, 04, 10, 23, 00, 00); 362 | rtc.setDate(d); 363 | ``` 364 | Enable the square-wave output at 8.192KHz on the SQW pin: 365 | ``` 366 | rtc.set_frequency(3) 367 | rtc.enable_output() 368 | ``` 369 | Read the current date and time from the RTC at 1-second intervals: 370 | ``` 371 | var myClock = setInterval(clockTimer, 1000); 372 | 373 | function clockTimer() { 374 | console.log(rtc.readDate().toISOString()); 375 | } 376 | ``` 377 | --------------------------------------------------------------------------------