Jump to content
C4 Forums | Control4

General programming best practices questions


Mr .Flibble

Recommended Posts

Hello all. I'm a professional programmer with 10 years experience in the various mainstream languages (PERL, C#, Java, JavaScript) but I have only a couple of hours experience programming Control4 systems. I've a couple of questions that I'm sure run through the minds of most developers when they first work with a C4 system.

My main query is about code re-use. I haven't found any way that I can make basic functions/macros/scripts that can be reused. It seems like if I want to do anything twice I need to repeat myself, which isn't great for a number of reasons. Is there a way to make a script/macro/function that can executed from various places?

Secondly, so far I have only discovered the GUI drag and drop method of programming. I presume this get converted to some sort of textual programming language at some point. Is it possible to see the resulting textual code and to program in that directly? Or is the only way to make a program by using the GUI?

Thirdly, are scrips always run sequentially/single threadedly? Here is why I ask. Imagine the following program which turns off all lights but turns on light1 if it's night time for 5 minutes at night time and turns light2 for 5 minutes at any time:

- turn off all lights

- if nighttime

-- turn on light1

-- wait 5 minutes

-- turn off light1

- turn on light2

- wait 5 minutes

- turn off light2

If we can do a parallel branch then the program will work as desired. If we cannot then we have to add in more logic to workaround the extra 5 minute delay which occurs at nighttime.

Thanks in advance for any tips. If anyone can recommend any reading material please point me in the right direction.

Link to comment
Share on other sites


Hello all. I'm a professional programmer with 10 years experience in the various mainstream languages (PERL, C#, Java, JavaScript) but I have only a couple of hours experience programming Control4 systems. I've a couple of questions that I'm sure run through the minds of most developers when they first work with a C4 system.

My main query is about code re-use. I haven't found any way that I can make basic functions/macros/scripts that can be reused. It seems like if I want to do anything twice I need to repeat myself, which isn't great for a number of reasons. Is there a way to make a script/macro/function that can executed from various places?

Depends on what you mean axactly. If you want the same programming to trigger from multiple events, you can either copy and paste or have the events trigger a variable, then have the variable trigger the actual effect.

If you're talking using a piece of programming (say turn light a on to 50%) and want to do the same piece of programming on light b, then no you can't due to the way devices are tracked within composer

Secondly, so far I have only discovered the GUI drag and drop method of programming. I presume this get converted to some sort of textual programming language at some point. Is it possible to see the resulting textual code and to program in that directly? Or is the only way to make a program by using the GUI?

No you can't go deeper - at least not within the program. Technically of course nothing prevents you from developing a program to see the actual programming language, but this is not a standard or supported option

Thirdly, are scrips always run sequentially/single threadedly? Here is why I ask. Imagine the following program which turns off all lights but turns on light1 if it's night time for 5 minutes at night time and turns light2 for 5 minutes at any time:

- turn off all lights

- if nighttime

-- turn on light1

-- wait 5 minutes

-- turn off light1

- turn on light2

- wait 5 minutes

- turn off light2

If we can do a parallel branch then the program will work as desired. If we cannot then we have to add in more logic to workaround the extra 5 minute delay which occurs at nighttime.

Thanks in advance for any tips. If anyone can recommend any reading material please point me in the right direction.

Individual scripts are sequential only. The wait you're talking about (actually called delay) will "pause" that script - that's it's intended use.

Within your example, instead of putting in a delay, you would trigger a Macro that executes the "delayed" programming - essentially making this macro be a separate script running parallel.

Note that for your specific example there is a built-in timer agent - these work smoother than delays.

As for the above macro - there are other ways (triggering timers directly in the first script, triggering a boolean variable and programming under that, etc. It's partly preference, partly efficiency on which one you would use.

Most would use a timer direct, but Macro programming comes closest to running parallel scripts.

Link to comment
Share on other sites

Depends on what you mean axactly. If you want the same programming to trigger from multiple events, you can either copy and paste or have the events trigger a variable, then have the variable trigger the actual effect.

If you're talking using a piece of programming (say turn light a on to 50%) and want to do the same piece of programming on light b, then no you can't due to the way devices are tracked within composer

I meant the former. That answers my question. Thanks.

No you can't go deeper - at least not within the program. Technically of course nothing prevents you from developing a program to see the actual programming language, but this is not a standard or supported option

Pictures it is then :)

Within your example, instead of putting in a delay, you would trigger a Macro that executes the "delayed" programming - essentially making this macro be a separate script running parallel.

When you say Macro, do you mean there is the ability to make separate Macros that can be called at will from parts of your program? Or do you mean I should trigger a variable, then have the variable trigger the actual logic (as mentioned above)?

Link to comment
Share on other sites

On that last bit - either or. Under agents the Macro option can be enabled and from there set to do what you want.

Macro's are the more "proper"way to set multiple events that you may want to trigger multiple times - but not always all them at the same time.

Link to comment
Share on other sites

On that last bit - either or. Under agents the Macro option can be enabled and from there set to do what you want.

Macro's are the more "proper"way to set multiple events that you may want to trigger multiple times - but not always all them at the same time.

Excellent. I think that is what I've been looking for. Thank you.

Link to comment
Share on other sites

While this would be nice, the difficulty has always been that all DeviceCommands require the *ID* of the device, not the name.

You could certainly use the name, except they're not unique. Even within rooms, they could be the same name.

It makes it difficult to translate to a text representation.

RyanE

Link to comment
Share on other sites

While this would be nice, the difficulty has always been that all DeviceCommands require the *ID* of the device, not the name.

You could certainly use the name, except they're not unique. Even within rooms, they could be the same name.

It makes it difficult to translate to a text representation.

RyanE

Finally we agree on something!

Woohoo

Makes it difficult for those who have no clue how to write code, but as I'm sure your aware this shouldn't be an issue for those with experience.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.