Wednesday, July 26, 2006

Crestron Analog RAM 101

In an earlier post, I summed up some guidelines for the use of NVRAM symbols in a Crestron program. I have received some feedback that indicates that some programmers have questions about how to go about storing a variable through a reboot.

This is easy to do. Insert an Analog RAM (ARAM) symbol in your Crestron program (remember to read the earlier post on memory allocation for information about the impact of where in your program you insert the symbol)



By placing a 1 on the store, recall, and select1 inputs, you can set up the Analog RAM to constantly store all of the analog values that are connected to it. When your Crestron processor reboots, these values will be recalled because there is a 1 on recall. Any analog signal can now be stored by adding it to both the input and output side of the symbol.

As mentioned in the memory allocation article, it is a good practice to add a few lines for expansion and comment them out. This way, you won't impact memory usage if you need to add a few more signals later on.

Thursday, July 20, 2006

Serial Buffer vs. Analog Buffer

You may have noticed that when you first create an Analog Buffer (ABUF) or Serial Buffer (SBUF), the input and output lines are not red or black, but rather green. Green lines in Crestron SIMPL Windows indicate an ambiguous signal. An ambiguous signal is a signal that has not yet been declared as digital (blue), analog (red) or serial (black).


These signals are ambiguous because, contrary to the names of the symbols, either analog or serial signals can be run through EITHER the analog OR serial buffers. There are notable differences between these buffers, but the names of the symbols do not capture these differences well.

The analog buffer propagates the inputs to the outputs on the rising edge of the enable line. Analog values propagate immediately, and when the enable goes low, the output remains at the last value. Any serial signal that has been tied to a Make String Permanent (MSP) or otherwise made permanent (such as being tied to a Telephone Dialing Keypad) will also propagate on the rising edge of the enable line. Transient serial signals, which is the term for any serial signal that has not been "made permanent," will not propagate at the rising edge.

The serial buffer propagates the inputs to the outputs while the enable line is high. The rising edge of the enable line will not propagate any signals. The inputs will only propagate to the outputs if new data is sent to the input while the enable line is already high. This is the difference between the serial buffer and the analog buffer. If an analog signal is run through a serial buffer, it will not propagate the value on the rising edge of enable, but rather wait until the analog input changes or re-asserts. When the enable goes low, the output remains at the last value. If a serial signal (transient or permanent) is run through a serial buffer, the value will only be propagated when the input changes once the enable is already held high.

A common question is "Why doesn't my serial buffer pass my data when I pulse the enable?" The solution is to use an Analog Buffer if you want the data to be passed from input to output on the rising edge of enable, even if the data is serial in nature.

Tuesday, July 11, 2006

Crestron Memory Allocation

It is important to understand the way that memory is allocated in a SIMPL Windows program. vdonr posted a message asking about this on the Crestron Yahoo! message boards on June 22nd, and a lengthy thread developed. This post contains a summary of the advice given about the use of NVRAM (Non Volatile Random Access Memory) on the Crestron processor. Credit is given at the end to the many contributors.

NVRAM is used to store data in the Crestron processor in the event of a processor reboot (such as a power outage, a crash or a program upload). The common symbols used for this are Analog RAM, Digital RAM, Serial RAM, and SIMPL+ variables that have been flagged as non-volatile. There are other symbols as well (such as Analog Non-Volatile Ramp), but these are the basics.

In SIMPL+, only variables that have been flagged as non-volatile are stored when the processor reboots. By default, SIMPL+ inserts a #DEFAULT_NONVOLATILE (on line 25 if you are using the default template). This directive will store all variables that are not specifically flagged volatile in NVRAM. If you replace this directive with #DEFAULT_VOLATILE, all variables that are not specifically flagged non-volatile will not be stored in NVRAM, freeing up memory space. There is a decrease in speed when accessing non-volatile variable in SIMPL+, so this should be avoided unless necessary.

One common mis-conception is that the symbol Make String Permanent (MSP) has something to do with NVRAM. It does not. The MSP is used to make "transient" serial strings permanent. The primary reason to do this is to ensure that the strings are sent to a touchpanel when it issues an update request. There are also cases in SIMPL logic where a "permanent" string is desired.

Another important thing to remember is that NVRAM and NVRAMDISK do not necessarily mean the same thing. NVRAM is the non-volatile memory of the Crestron processor. NVRAMDISK refers to the practice of partioning a certain percentage of this memory for use as disk space. This allows a programmer to write data to disk without a compact flash card present. The use of compact flash or NVRAMDISK can avoid a lot of the problems typically associated with the use of NVRAM symbols like the Analog RAM.

NVRAM is addressed from the top of the program to the bottom. While it is not necessary to know exactly how much memory is consumed by each symbol during normal programming (as the compiler handles it for you), it will make it easier to understand the allocation process if we spell out the memory consumption in bytes of each symbol.

The Analog and Digital RAM symbols use 4 bytes of memory per input/output pair, multiplied by the number of selects declared. So, an Analog RAM with five inputs and outputs and two selects will use 40 bytes of NVRAM ((4 x 5) x 2).

The Serial RAM symbol uses 1 byte of memory, plus 1 byte of memory per character used, all multiplied by the number of selects declared. So a Serial RAM with 24 characters specified and three selects will use 75 bytes of NVRAM ((1 + 24) x 3).

In the following example, we have an Analog RAM with two inputs and outputs and a single select, a SIMPL+ module that uses some NVRAM, and a Serial RAM with 24 characters and two selects.

The first Analog RAM symbol would use the first eight bytes of memory (two inputs/outputs x 4 bytes per input/output = 8 bytes.) The SIMPL+ module will use the next memory slots (the number of bytes of NVRAM that are indicated at compile time.) The Serial RAM will use the next 50 bytes ( (1 + 24 characters) x 2 selects = 50).



If we insert an extra input/output pair at the end of the Analog RAM, the next time we upload every symbol after that (the SIMPL+ module and the Serial RAM) will have their data "corrupted" by the changes in memory addresses. The Analog RAM will now take up the first 12 bytes of NVRAM, not just 8 like it did before. So, the SIMPL+ module will be starting at the 13th byte of NVRAM, not the 9th. The first four bytes of the Serial RAM will now be in the addresses that the SIMPL+ is looking at, the each byte of the Serial RAM will be "shifted up" four bytes, probably rendering all of the data useless. All of this data will have to be re-stored at the touchpanel. If the program were larger, this could be a big headache.

Another way to jumble up the memory would be to insert an NVRAM symbol somewhere above any other NVRAM symbol, or to change the order of the NVRAM symbols. This would have the same effect, as memory is addressed from the top down.

I consider the following to be "best practices" when dealing with NVRAM in a Crestron program:

1. Change the compiler directive in SIMPL+ modules that you write to #DEFAULT_VOLATILE, unless you know that you need almost all of the variables to be stored after a reboot for some reason. Flag individual variables as non-volatile as needed. (example: "NONVOLATILE INTEGER x;")

2. When you're writing a program from scratch, put all of the NVRAM symbols (including SIMPL+ modules with non-volatile variables) at the top in a dedicated sub folder. When you add NVRAM as you are working, put it at the bottom of this sub folder. Do not change the order of these symbols. This way, whenever anyone modifies your program in the future, none of your data will be corrupted.

3. When you're modifying someone else's program, place any new NVRAM symbols at the very bottom. This way, you know you will not be corrupting any of the original data.

4. Avoid NVRAM use in modules, as this encourages the NVRAM to fall elsewhere in the program.

5. Store large arrays of data (such as camera location presets, phone numbers, user names, etc.) to NVRAMDISK or Compact Flash. This avoids problems with memory addressing altogether. Things like volume are usually OK in NVRAM, because it doesn't take a long time to re-enter the data in the event that the value is corrupted. Using Compact Flash is ultimately the best choice, as it can be accessed by multiple SIMPL+ modules simultaneously.

6. If your top folder contains multiple NVRAM symbols, and you're storing a lot of information in a single Analog RAM, add a few extra lines with dummy signals at the end. This way, if you need to expand the Analog RAM, you can just comment one of these back in and rename it, without corrupting the memory allocation.

This is a summary of a number of posts on the Yahoo! group by: al_avt, alan_audiblesolutions, andyross94928, bfschroe, bryn4ne, chipmoodydcs, ckangis, ipwrangler, lkingcliby, m_ras, mjrtoo2000, richard_leek, skaudle, thatkirk

Monday, July 10, 2006

Using Crestron to Select Sources for Multiple Destinations

fonze_scm asked for a suggestion on a method to allow users to select from multiple sources, select one or more outputs for a source, and then press a "Take" button to send the patches to all of the different display devices. Here is one suggestion, as always using only Crestron SIMPL Windows, without SIMPL+.

The first step is to provide a set of buttons on the touchpanels to allow the user to select the source they are going to assign. I recommend using an Interlock (IL) for this function. Name the inputs after your sources, and the outputs the same name with "_fb" at the end. These should be tied to the presses and feedbacks for the buttons on the touchpanel for each source.



The second step is to draw buttons on the touchpanel for each of the outputs. Name the press and feedback for each of these buttons something like "screen_1_select." This press should be routed to the enable of a buffer. The inputs of the buffer should be the feedback outputs of the interlock, and the outputs should be signals such as "source_3_to_screen_1." This will pulse an output for the selected source, unique for the display, when the display is selected.

The user can easily route the same source to multiple displays by selecting the source, and then selecting each desired display.



Next, insert an Analog Initialize (INIT) for each display device. This will convert the digital pulses from the buffers into an analog value for the "preview" source for each display device. Remember, fonze_scm doesn't want the Crestron system to route the source to the display device until the user presses a "TAKE" button.

Take the outputs from each buffer and route to the inputs of an analog initialize. The output should be named something to the effect of "screen_1_preview." This output will contain a value indicating the source that has been selected for preview.



Finally, draw a "TAKE" button on the touchpanel. This will be assigned the signal name "execute" which will trigger an Analog Buffer (ABUF). The ABUF will take the sources that have been selected for each display, and send them to an analog variable containing the current source for each display device. This can then be processed using an Analog Equate (EQU) or other analog logic for sending the commands to the switchers and display devices.



If you want the inputs cleared after a patch is made, insert a stepper that clears the Interlock and/or Initiliazes after the TAKE button is pressed.