GithubHelp home page GithubHelp logo

Comments (4)

SubjectXXX avatar SubjectXXX commented on June 12, 2024 1

Ahh, you pushed me into the right direction.

Problem was that I was saving this array to DB for persistent settings after possible power cutout, and I was saving this gpio objects into same database, when application restarted, values were read from DB (shades position and stuff) and also an object, which wasnt gpio anymore, but some placeholder with default methods and properties.

And also, yes, I missed reconfigureDirection (direction was a bit confusing word for me in this context thus I skipped the whole part), I will fix constructors with said optional switch and will always generate new gpio objects, not gonna save them for later use.

Thank you for your help.

from onoff.

fivdi avatar fivdi commented on June 12, 2024

This is probably not onoff bug, but it only happens when using onoff functionality, so I decided to ask here.

Yes, I don't think this is an onoff bug either.

Some where in code I need to initialize new Gpio object on selected pin with 'out':
if (typeof objects[item_id].gpio_down === 'undefined') {
objects[item_id].gpio_down = new Gpio(7, 'out');
}

But then, if I do this:
objects[item_id].gpio_down.writeSync(1);

I get
TypeError: objects[item_id].gpio_up.writeSync is not a function

The TypeError here most likely means one of two things:

  • Somewhere in your code, a value that is not a Gpio object is being assigned to objects[item_id].gpio_down
  • Somewhere in your code, a value that is not a Function is being assigned to objects[item_id].gpio_down.writeSync

Problem is, it works, if I make new Gpio object on the fly and do operation on it:
var gpio_item = new Gpio(7, 'out');
gpio_item.writeSync(1);

Yes, this works because neither gpio_item nor gpio_item.writeSync is being overwritten.

but I need to have access to this objects for later use (optimization on rpi4 with latest nodejs and onoff), since
var gpio_item = new Gpio(7, 'out');
always puts pin to 0v.

Please carefully read the documentation for the Gpio constructor to discover the different possibilities here.

I need to read voltage on pin and then decide what to do later in code.

For this Gpio(<number>, 'in') is required. See the documentation for further information.

Where am I doing the wrong turn?

I can't say as I don't know your code. You will need to debug the application to see what is going wrong.

Edit: I edited this response as the original post was also edited.

from onoff.

SubjectXXX avatar SubjectXXX commented on June 12, 2024

Thank you for your quick reply.

Sorry for edit, I did edit it in first minute after posting tho :)

Anyway, regarding changing object or function somewhere else in code is not a problem, since I only make this object once and at the same time I save it to array.
Later, I only access it via that function call.. Unless there are same backstage shenanigans NodeJS does..

I have read constructor instructions and didnt find where I can instantiate GPIO object while retaining already set value on it. Thus I went direction of saving created objects into array.

By "I need to read voltage.." I meant I need to know, what is the current state of pin (1 or 0) in out mode and then act accordingly (sounds really complicated, but in my case, I need to check if shade on my window is moving (pin is active, 1) and then stop it before moving shade in different direction).

I did workaround, that I now strictly always set pins to 0 no matter what, then start moving shade in selected direction. But its ugly code..

from onoff.

fivdi avatar fivdi commented on June 12, 2024

Anyway, regarding changing object or function somewhere else in code is not a problem, since I only make this object once and at the same time I save it to array.
Later, I only access it via that function call.. Unless there are same backstage shenanigans NodeJS does..

Node doesn't do any backstage shenanigans 😄. Please post a short, complete and tested program that can be used to reproduce the TypeError problem.

I have read constructor instructions and didn't find where I can instantiate GPIO object while retaining already set value on it. Thus I went direction of saving created objects into array.

When a Raspberry Pi is powered on all GPIOs are configured as inputs by default. Because they are inputs, they are not outputs, so it's not possible to configure them as outputs which retain their current output value. They don't have an output value that could be retained when powered on, they have an input value.

If the GPIO has already been configured as an output, for example, by a previous run of your application (after the Raspberry Pi was powered on), the reconfigureDirection option described in the constructor can be set to false to leave the GPIO in it's current state.

from onoff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.