Showing posts with label Make Controller. Show all posts
Showing posts with label Make Controller. Show all posts

November 16, 2008

Integer Splitting in C

For our make project we are using a single int of 9 digits to send communication to the different units. However first we needed a simple way to split the integer into separate parts. Sadly there is no split or charAt functions for integers, so I had to do some math to split it apart. This resulted in the functions below.

Just drop theses functions in...


int* protoConvertFrom(int num)
{
int *data;
data[3] = num % 100000;
data[2] = ( (num % 10000000) - data[3]) / 100000;
data[1] = ( (num % 100000000) - (data[3] + data[2]) ) / 10000000;
data[0] = ( (num % 1000000000) - (data[3] + data[2] + data[1]) ) / 100000000;
return data;
}

int protoConvertTo(int *part)
{
return (part[0] * 100000000) + (part[1] * 10000000) + (part[2] * 100000) + part[3];
}


API information...


int* protoConvertFrom(int num)


Splits an integer of 9 places (our protocol) into an array of four parts .

Parameters:

num: The integer received from the network.

Returns:

An array pointer of four integers... [0] = from, [1] = to, [2] = ID, [3] = value

Example:

int num = 131100001;
int *data = protoConvertFrom(num);
int ID = data[2];




int protoConvertTo(int *part)


Take a array pointer of 4 parts and combines then into a 9 digit integer (our protocol).

Parameters:

part: A pointer to an array.

Returns:

An a nine digit integer that is ready to send over the network

Example:

int *data = new int[4];
data[0] = 1; // from
data[1] = 3; // to
data[2] = 11; // ID
data[3] = 1; // value
int num = protoConvertTo(data);
// returns 131100001

November 9, 2008

Make Controller network communication

For our project in Computer Architecture, we are programing several Make controllers to work together as a home control system. As the programing manager, apposed to the general manager, and part of the system controller group, I have been trying to compile function and a basic API together for the other teams to drop in and use. Here is the easiest way to get networking working on the make controller, using Datagram Sockets. I must say, I hate how small the code is compare to the 7+ hours of my own time and another 2+ hours working with others it took to get working...

Drop these functions into the code...



int sendMessage(int num, int address, int port)
{
struct netconn* udpSocket = DatagramSocket( port );
int val = DatagramSocketSend( udpSocket, address, port, &num, 4 );
DatagramSocketClose( udpSocket );
return val;
}

int recieveMessage(int address, int port)
{
struct netconn* udpSocket = DatagramSocket( port );
int num = 0;
DatagramSocketReceive( udpSocket, port, &address, &port, &num, 4 );
DatagramSocketClose( udpSocket );
return num;
}


API information...


int sendMessage(int num, int address, int port)


Sends out an integer over a specified socket on the network to a network device based on its IP.

Parameters:

num: The integer to send.
address: The IP address of the network device that will be receiving the packets.
port: The port that the network device will be listening on.

Returns:

This function returns an int returned by DatagramSocketSend, which gives the number of bytes sent.

Example:

int address = IP_ADDRESS( 192, 168, 0, 210 );
int num = 1;
int port = 12345;
sendMessage(num, address, port);



int recieveMessage(int address, int port)


Receives data from the network buffer and stores it for use. Please Note: This function sits and waits for data to be written to the socket buffer before continuing, so it best used in a separate task.

Parameters:

address: The IP address of the network device that will be sending the packets.
port: The port that the network device will sending packets on.

Returns:

This function returns an integer that was written using the sendMessage function.

Example:


int address = IP_ADDRESS( 192, 168, 0, 200 );
int port = 12345;
int num = recieveMessage(address,port);