Standard CAN Create C Functions
CANcreate includes several unique C functions that are part of its API:
void Graphics.DrawBitmap(int X, int Y, char* File)
Graphics.DrawBitmap(20, 10, GETIMAGE(“User\\SWOFF”));
This will draw the bitmap SWOFF.BMP at the screen location 20 pixels from the left and 10 pixels from the top. In order for a given image (In this case “SwOff”) to be used, it has to be published to the device. If the image is already used in your schematic design (e.g. by a BITMAP object) then it will automatically be published. If not, you need to include a PUBLISH object in your schematic and select the SwOff image. Please note, if a bitmap is published with transparency information, it will have the extension BMT instead of BMP.
void Text.Format(char* Str, char* Format)
Format month day and year into string. A maximum of 12 parameters can be used in the Text.Format function. In this example dateString must be an array of characters capable of containing the entire formatted string.
Text.Format(dateString,”%02d/%02d/%4d”,month,day,year);
void Graphics.TextOut(int X, int Y, char* Text)
Write char array tempString to location 10,10
Graphics.TextOut(10,10,tempString);
void Graphics.TextOut(int X, int Y, char* Text, int Foreground, int Background)
Write char array tempString to location 10,10 using foreground color white and background color black. Other colors can be used and follow 24 bit color convention (Red = 0xFF0000, Green = 0x00FF00, Blue = 0x0000FF).
Graphics.TextOut(10,10,tempString,0xFFFFFF,0);
void Graphics.SetBackgroundMode(int Mode)
Mode = 1 for Opaque, 2 for Transparent and 3 for Background
Graphics.SetBackgroundMode(1);
void Graphics.DrawBackground(char* File)
Draw BKGND1.BMP as a background.
Graphics.DrawBackground(GETIMAGE(“User\\BKGND1”));
void CAN.TransmitMessage(int Port, int ID, int Bytes, char* Data)
Port = 1 for 516CANDI only, could also be 2 if using 2nd port of 550MDI or 700MDI, ID is the 29 bit extended CAN identifier, Bytes is the number of bytes to transmit (maximum of 8) and txData is an array of characters where each character is a transmitted byte in the message.
CAN.TransmitMessage(1, 0x18E00000, 8, txData);
int CAN.ReceiveMessage(int Port, int* ID, int* Bytes, char* Data)
Returns 1 if any CAN messages has been received on the specified port, otherwise 0. If a message is received, the data is placed in the variables specified in the parameters.
int ID, Bytes;
char Data[8];
if (CAN.ReceiveMessage(1, &ID, &Bytes, Data)) {
Text.Format(tempString, “%08X %d”, ID, Bytes);
Graphics.TextOut(10, 10, tempString);
Text.Format(tempString, “%02X”, Data[0]);
Graphics.TextOut(10, 35, tempString);
}
void CAN.SetSpeed(int Port, int Bps)
Independently changes the speed of either of the CAN ports.
CAN.SetSpeed(1,250000); /* Set port 1 speed to 250kbps */
CAN.SetSpeed(2,125000); /* Set port 2 speed to 125kbps */
void Graphics.DrawLine(int X1, int Y1, int X2, int Y2, int Color)
Draws a line of the screen from one point to another in the specified color.
Draw white line from 10,10 to 20,20
Graphics.DrawLine(10,10,20,20,0xFFFFFF);
void Graphics.FillRegion(int X, int Y, int Width, int Height, int Color)
Draw red square 50×50 at location 10,10
Graphics.FillRegion(10,10,50,50,0xFF0000);
void Graphics.SetFont(int Font)
Set default font size to small (1=small, 2=medium, 3=large)
Graphics.SetFont(1);
int File.Open(char* File, int Flags)
Open history.txt for reading (flags: 1=read, 2=write, 3=append, 4=write remain open) Returns -1 if file cannot be opened.
int handler = File.Open(“history.txt”,1);
int File.Close(int Handle)
Close handler that contains open file
File.Close(handler);
int File.Read(int Handle, char* Buffer, int Count)
Read 20 characters into tempBuf char array from open file handle handler
File.Read(handler,tempBuf,20);
int File.Write(int Handle, char* Buffer, int Count)
Write 20 characters of tempBuf char array into open file handle handler
File.Write(handler,tempBuf,20);
int File.GetPosition(int Handle)
Return current position of open file handle handler to variable pos
int pos = File.GetPosition(handler);
int File.SetPosition(int Handle, int Position)
Set current position of open file handle handler to 20
File.SetPosition(handler,20);
int File.GetSize(int Handle)
Return total size (in chars) of open file handle handler to variable size
int size = File.GetSize(handler);
void Graphics.EraseRegion(int X, int Y, int Width, int Height)
Clear region 50×50 pixels at location 10,10 to background
Graphics.EraseRegion(10,10,50,50);
void Video.SetOptions(int Options)
Sets video options to enable cross hair overlay. Set options = 0 to disable cross hair overlay
Video.SetOptions(1);
int File.ReadLine(int Handle, char* Buffer, int Size)
Reads entire line or up to Size of open file handle handler and stores in char array tempBuf
File.ReadLine(handler,tempBuf,20);
int Touch.Read(int *X, int *Y)
Reads the current touch coordinates. Returns 1 if touch is actively being pressed, otherwise 0.
if (Touch.Read(&X, &Y)) {
/* Screen is being touched at coordinate (X, Y) */
}
void System.SetTime(int Year, int Month, int Day, int Hour, int Minute, int Second)
Sets the local system time of the real time clock.
System.SetTime(2012, 16, 4, 12, 30 15);
int Math.Sqrt(int Value)
Returns the integer square root of the value.
result = Math.Sqrt(16);
int Graphics.DrawBitmap(int X, int Y, int Width, int Height, char* FileName, int SX, int SY);
Draws a bitmap on the screen of a source image. Only draws a subset of the image specified by the origin coordinates (SX,SY).
Graphics.DrawBitmap(x, y, 8, 16, GETIMAGE(“User\\Font”), u * 8, v * 16);
int Bluetooth.WriteValue(int Index, char* Buffer, int Count);
Writes a Bluetooth LE value to the given characteristic index. Returns the number of bytes successfully written.
result = Bluetooth.WriteValue(0, dataBuffer, dataCount);
int Bluetooth.ReadValue(int Index, char* Buffer, int Count);
Reads a Bluetooth LE value frp, the given characteristic index. Returns the number of bytes successfully read.
result = Bluetooth.ReadValue(0, dataBuffer, dataCount);
int System.WriteEEPROM(int Address, char* Buffer, int Count);
Writes to the system EEPROM if it is available on the hardware. Address is the location to write to, Buffer is where the bytes will be read from, and Count is the number of bytes to write. Returns 0 if successful.
result = System.WriteEEPROM(5, dataBuffer, 4);
int System.ReadEEPROM(int Address, char* Buffer, int Count);
Reads from the system EEPROM if it is available on the hardware. Address it the location to read from, Buffer is where the placed, and Count is the number of bytes to read. Returns 0 if successful.
result = System.ReadEEPROM(5, dataBuffer, 4);
void System.Trace(char* Text);
Writes a debug trace to the trace output. Can be viewed in the CANcreate simulator under Simulator->Trace Output…
System.Trace(“Code Executed”);
int Math.Abs(int Value);
Returns the absolute value of an integer.
result = Math.Abs(-123);
void Text.Copy(char* Target, char* Source);
Copies the source string to the target string.
Text.Copy(tempString, “Initialize a new string”);
int Text.Concat(char* Target, char* Source);
Appends the source string to the end of the target string.
Text.Concat(tempString, “New line of text\n”);
int Text.Length(char* Text);
Returns the length of a string.
result = Text.Length(tempString);
int Text.Compare(char* Str1, char* Str2);
Compares two strings. Returns zero if they are equal. Returns <0 if the first character that does not match has a lower value in Str1 than in Str2, and >0 if the value is higher.
result = Bluetooth.ReadValue(0, dataBuffer, dataCount);
int Text.ToInt(char* Text);
Returns the integer value of the ASCII string stored in Text.
result = Text.ToInt(“12345”);
int System.GetElapsedMS();
Returns the number of milliseconds that have elapsed since the last time this function has been called.
elapsed = System.GetElapsedMS();
void Module.SetLEDColor(int position, int color);
Sets the color of the specified LED. Each switch may have two or three LEDs so this function may need to be called multiple times to change all the LEDs associated with a given switch. Call Module.WriteLEDs() to commit the changes.
0x000000FF = Red
0x0000FF00 = Green
0x00FF0000 = Blue
0xFF000000 = White (if equipped)
Module.SetLEDColor(5, 0x0000FF00);
void Module.WriteLEDs();
Commits the changes made by Module.SetLEDColor() to the hardware. Minimize the number of calls to this function to improve performance.
Module.WriteLEDs();
int Module.GetLEDColor(int position);
Reads the color of the specified as last set by Module.SetLEDColor().
previousColor = Module.GetLEDColor(6);
int CAN.GetStatus(int port);
Returns the status of the specified CAN message object.
status = CAN.GetStatus(0);
void Module.WriteRelays(int state);
Writes the relay outputs to the given state, with each bit corresponding to a single relay.
Module.WriteRelays(0x05); /* Turn on relays #1 and #3 */
int Module.ReadFeedback();
Reads the relay voltage feedback status, with each bit corresponding to a single relay.
if (Module.ReadFeedback() & 0x08) { /* Relay #4 has voltage on its output pin */
int Module.ReadVoltage(int channel);
Reads the voltage on the specified channel.
Channels 0 to 7 correspond to the current readings of the eight relay channels.
Channel 16 is the module supply voltage reading, scaled linearly with 255 counts being approximately equal to 50V.
voltage = Module.ReadVoltage(16);
int System.ReadRegister(int address);
Reads a value from an application specific register.
PKR Registers:
0 – Module Temperature (Celsius)
1 – Light Sensor Status
2 – Relay PWM Value
3 – Add-on Keys
4 – Backlight (PKU1000 and PKU1400 only, 255=100%)
temperature = System.ReadRegister(0);
int System.WriteRegister(int address, int value);
Writes a value to an application specific register.
System.WriteRegister(0, 128); // Set backlight to 50%
0 – Backlight (PKU1000 and PKU1400 only, 255=100%)