fivdi / pigpio

Fast GPIO, PWM, servo control, state change notification and interrupt handling with Node.js on the Raspberry Pi
MIT License
943 stars 89 forks source link

Array support #116

Closed IanW6374 closed 4 years ago

IanW6374 commented 4 years ago

I have tried to address the GPIO outputs in an array in order to provide a way of controlling GPIO peripherals.

const outputDevice = []; outputDevice.push = new Gpio(23, {mode: Gpio.OUTPUT}); outputDevice.push = new Gpio(24, {mode: Gpio.OUTPUT}); outputDevice.push = new Gpio(25, {mode: Gpio.OUTPUT}); outputDevice.push = new Gpio(22, {mode: Gpio.OUTPUT});

and then using...

outputDevice[0].digitalWrite(1);

But I try this approach I get the following error "Property 'digitalWrite' does not exist on type 'never'.ts(2339)"

fivdi commented 4 years ago

The Array push() method is being used incorrectly.

In other words, this:

 outputDevice.push = new Gpio(23, {mode: Gpio.OUTPUT});

Should be:

 outputDevice.push(new Gpio(23, {mode: Gpio.OUTPUT}));
IanW6374 commented 4 years ago

Thanks for the update - Unfortunately I still get the error - Argument of type 'any' is not assignable to parameter of type 'never'.ts(2345) when using the new syntax.

outputDevice.push(new Gpio(23, {mode: Gpio.OUTPUT}));

fivdi commented 4 years ago

Are you a JavaScript / TypeScript beginner? Please post a complete but short JavaScript program that can be used to reproduce the error.

Unlike the first error message the second error message doesn't mention anything related to pigpio so perhaps it has nothing to do with pigpio.

IanW6374 commented 4 years ago

I am fairly new to Typescript \ Javascript but converting one of my programs over from Python which uses the same "Array" principle with the gpiozero Python module.

A very basic program to illustrate the issue but I get the following console output when trying to compile: -

/home/pi/garage/node_modules/ts-node/src/index.ts:434 return new TSError(diagnosticText, diagnosticCodes) ^ TSError: ⨯ Unable to compile TypeScript: src/index.ts:6:19 - error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'.

6 outputDevice.push(new Gpio(23, {mode: Gpio.OUTPUT}));

src/index.ts:7:19 - error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'.

7 outputDevice.push(new Gpio(24, {mode: Gpio.OUTPUT}));

src/index.ts:8:19 - error TS2345: Argument of type 'any' is not assignable to parameter of type 'never'.

8 outputDevice.push(new Gpio(25, {mode: Gpio.OUTPUT}));

src/index.ts:13:18 - error TS2339: Property 'digitalWrite' does not exist on type 'never'.

13 outputDevice[id].digitalWrite(1);
at createTSError (/home/pi/garage/node_modules/ts-node/src/index.ts:434:12)
at reportTSError (/home/pi/garage/node_modules/ts-node/src/index.ts:438:19)
at getOutput (/home/pi/garage/node_modules/ts-node/src/index.ts:578:36)
at Object.compile (/home/pi/garage/node_modules/ts-node/src/index.ts:775:32)
at Module.m._compile (/home/pi/garage/node_modules/ts-node/src/index.ts:858:43)
at Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Object.require.extensions.<computed> [as .ts] (/home/pi/garage/node_modules/ts-node/src/index.ts:861:12)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)

[nodemon] app crashed - waiting for file changes before starting...

index.js

// eslint-disable-next-line @typescript-eslint/no-var-requires const Gpio = require('pigpio').Gpio;

const outputDevice = []; outputDevice.push(new Gpio(23, {mode: Gpio.OUTPUT})); outputDevice.push(new Gpio(24, {mode: Gpio.OUTPUT})); outputDevice.push(new Gpio(25, {mode: Gpio.OUTPUT}));

const id = getRndInteger(0, 2); console.log(Turning on LED: ${id}); outputDevice[id].digitalWrite(1);

/* Works when using variable method

const led = new Gpio(25, {mode: Gpio.OUTPUT}); led.digitalWrite(1) */

function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min + 1) ) + min; }

fivdi commented 4 years ago

I'm not familiar with typescript and can't tell you if the sample code posted is valid TypeScript. It looks like valid JavaScript to me. This makes me wonder why you are compiling it with the TypeScript compiler and questioning if it's even necessary to do that.

Assuming that the code is in a file called index.js, what happens if you run the command sudo node index.js

IanW6374 commented 4 years ago

I really appreciate the quick response / feedback It looks like an issue with Typescript as it compiles / runs okay in Javascript. You are correct I don't need Typescript for this basic program but the larger module has some Typescript components to it.

fivdi commented 4 years ago

Good to hear that it's working now with JavaScript. I guess you'll have to take a look at some tutorials to figure out what needs to be done for TypeScript. I'll go ahead and close the issue now as it isn't related to pigpio.