Skip to content

Instantly share code, notes, and snippets.

@bitm0de
Last active October 18, 2018 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitm0de/af41e7a76684d55b386dedd2a89efd91 to your computer and use it in GitHub Desktop.
Save bitm0de/af41e7a76684d55b386dedd2a89efd91 to your computer and use it in GitHub Desktop.
/*******************************************************************************************
SIMPL+ Module Information
*******************************************************************************************/
/*
Programmer: Troy Garner
Comments: Decoder for the connections string on an ECROSS or CCROSS
Note: The maximum number of connections that can be printed on a line are 125 connections.
Therefore, if an ID has more than 125 connections, <Connection$> will be issued on
multiple passes of the logic processor.
# This module only holds itself responsible for parsing a single line from the connections
serial; line \x01 specifically.
*/
#SYMBOL_NAME "Crosspoint Connections Decoder"
#CATEGORY "46" "Signal Routing" /* Custom Category */
/*******************************************************************************************
Compiler Directives
*******************************************************************************************/
#DEFAULT_VOLATILE
#ENABLE_TRACE
#DEFINE_CONSTANT CONNECTION_CHANGED_TIMEOUT 10
#DEFINE_CONSTANT MAX_CONNECTIONS 125 // 255 max (but requires a second serial line - not supported here)
#DEFINE_CONSTANT MAX_CONNECTIONS_STRLEN 515 // 5 + (255 * 2)
/*******************************************************************************************
DIGITAL, ANALOG and SERIAL INPUTS and OUTPUTS
*******************************************************************************************/
DIGITAL_INPUT evaluate;
STRING_INPUT connections$[MAX_CONNECTIONS_STRLEN];
DIGITAL_OUTPUT changed, _SKIP_, in_use;
ANALOG_OUTPUT this_id, connection_count, connection_id[MAX_CONNECTIONS];
/*******************************************************************************************
Globals
*******************************************************************************************/
INTEGER __lock__;
INTEGER _max_connections;
/*******************************************************************************************
Functions
*******************************************************************************************/
// 16-bit SIMPL+ INTEGER (2-bytes - big endian)
INTEGER_FUNCTION GetInt16(STRING input$, INTEGER offset)
{
return ((Byte(input$, offset) << 8) | Byte(connections$, offset + 1));
}
FUNCTION Recalculate()
{
INTEGER i;
if (__lock__ = 1) { return; }
__lock__ = 1;
// \x01\x{1} - parses first line only
if ((Len(connections$) >= 5) && (Byte(connections$, 1) = 0x01))
{
// acknowledge the fact that there may be more than one line
in_use = ((Byte(connections$, 2) = 0x01) || (Byte(connections$, 2) = 0x02));
this_id = GetInt16(connections$, 3);
connection_count = Byte(connections$, 5);
if (Len(connections$) = (5 + (connection_count * 2)))
{
for (i = 1 to _max_connections)
{
if (i <= connection_count)
{
connection_id[i] = GetInt16(connections$, 6 + ((i - 1) * 2));
}
else
{
connection_id[i] = 0;
}
}
Pulse(1, changed);
}
}
__lock__ = 0;
}
/*******************************************************************************************
Event Handlers
*******************************************************************************************/
PUSH evaluate
{
Recalculate();
}
CHANGE connections$
{
// helps with fast changing connections$ serial
// to avoid re-entrancy issues (using THREADSAFE would
// just throw the evaluation away, but it also disregards
// 2-series compatibility...)
WAIT(15, __connections_changed__)
{
Recalculate();
}
RetimeWait(15, __connections_changed__);
}
/*******************************************************************************************
Main
*******************************************************************************************/
FUNCTION Main()
{
INTEGER i;
__lock__ = 0;
for (i = MAX_CONNECTIONS to 1 step -1)
{
if (IsSignalDefined(connection_id[i]))
{
_max_connections = i;
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment