|
Re: replace WPILib part
Header:
PHP Code:
#ifndef __DASHBOARD_H__
#define __DASHBOARD_H__
#include "ErrorBase.h"
#include "NetworkCommunication/FRCComm.h"
#include <VxWorks.h>
#include <stack>
#include <vector>
using namespace std;
typedef unsigned int uint;
/**
* Pack data into the "user data" field that gets sent to the dashboard laptop
* via the driver station.
*/
class Dashboard : public ErrorBase
{
// Can only be constructed by the DriverStation class.
friend class DriverStation;
public:
inline void Add(string name, string value);
inline void Add(string name, int value);
inline void Add(string name, float value);
inline void Add(string name, double value);
inline void Add(const char* name, int value);
inline void Add(const char* name, float value);
inline void Add(const char* name, double value);
inline void Add(const char* name, const char* value);
void AddDebugVariable(const char* name, const char* value);
inline void AddDebugVariable(string name, string value);
inline void AddDebugVariable(string name, int value);
inline void AddDebugVariable(string name, float value);
inline void AddDebugVariable(string name, double value);
inline void AddDebugVariable(const char* name, int value);
inline void AddDebugVariable(const char* name, float value);
inline void AddDebugVariable(const char* name, double value);
//AddDebugVariable renamed for convenience
inline void var(const char* name, const char* value);
inline void var(string name, string value);
inline void var(string name, int value);
inline void var(string name, float value);
inline void var(string name, double value);
inline void var(const char* name, int value);
inline void var(const char* name, float value);
inline void var(const char* name, double value);
void AddRaw(const char *raw, uint length);
INT32 Send();
private:
Dashboard(char **userStatus);
virtual ~Dashboard();
static const INT32 kMaxDashboardDataSize= USER_STATUS_DATA_SIZE - sizeof(UINT32) * 3 - sizeof(UINT8); // 13 bytes needed for 3 size parameters and the sequence number
// Usage Guidelines...
DISALLOW_COPY_AND_ASSIGN(Dashboard)
;
int location;
char **m_userStatus;
char *m_localBuffer;
char *raw;
uint rawLength;
SEM_ID m_printSemaphore;
UINT8 m_sequence;
};
#endif
Source:
PHP Code:
//include my dashboard heade
#include "Dashboard.h"
#include "Synchronized.h"
#include "Utility.h"
#include "WPIStatus.h"
#include <strLib.h>
#include <sstream>
using namespace std;
/**
* Dashboard constructor.
*
* This is only called once when the DriverStation constructor is called.
*/
Dashboard::Dashboard(char **userStatus):
m_userStatus(userStatus), m_localBuffer(NULL), m_printSemaphore(0), m_sequence(0)
{
//Syntax: @@@451:|name=value|dbg=name: value|name=value|:451@@@
m_localBuffer = new char[kMaxDashboardDataSize * 2];
m_localBuffer[0] = '@';
m_localBuffer[1] = '@';
m_localBuffer[2] = '@';
m_localBuffer[3] = '4';
m_localBuffer[4] = '5';
m_localBuffer[5] = '1';
m_localBuffer[6] = ':';
m_localBuffer[7] = '|';
m_localBuffer[8] = 0;
raw = new char[kMaxDashboardDataSize * 2];
raw[0]=4;
raw[1]=5;
raw[2]=1;
raw[3]=32;
m_printSemaphore = semMCreate(SEM_DELETE_SAFE | SEM_INVERSION_SAFE); // synchronize access to multi-value registers
}
/**
* Dashboard destructor.
*
* Called only when the DriverStation class is destroyed.
*/
Dashboard::~Dashboard()
{
m_userStatus = NULL;
delete [] m_localBuffer;
m_localBuffer = NULL;
}
/**
* Print a string to the UserData text on the Dashboard.
*
* This will add text to the buffer to send to the dashboard.
* You must call Finalize() periodically to actually send the buffer to the dashboard if you are not using the packed dashboard data.
*/
void Dashboard::Add(string name, string value)
{
this->Add(name.c_str(), value.c_str());
}
void Dashboard::Add(string name, int value)
{
stringstream out;
out << value;
this->Add(name.c_str(), out.str().c_str());
}
//Add clones removed...
void Dashboard::AddDebugVariable(string name, string value)
{
this->AddDebugVariable(name.c_str(), value.c_str());
}
void Dashboard::AddDebugVariable(string name, int value)
{
stringstream out;
out << value;
this->AddDebugVariable(name.c_str(), out.str().c_str());
}
//AddDebugVariable clones removed...
//AddDebugVariable renamed for convenience
void Dashboard::var(const char* name, const char* value)
{
this->AddDebugVariable(name, value);
}
void Dashboard::var(string name, string value)
{
this->AddDebugVariable(name, value);
}
//var clones removed...
void Dashboard::AddRaw(const char *raw, uint length)
{
{
Synchronized sync(m_printSemaphore);
memcpy(this->raw+4, raw, length);//keep intact the beginning
this->rawLength=length;
}
if (length+(uint)strlen(m_localBuffer) > (uint)kMaxDashboardDataSize)
{
wpi_fatal(DashboardDataOverflow);
}
}
void Dashboard::AddDebugVariable(const char* name, const char* value)
{
string fv=name;
fv+=": ";
fv+=value;
this->Add("dbg",fv.c_str());//TEST:this might fail the cast
}
void Dashboard::Add(const char* name, const char* value)
{
INT32 size;
{
Synchronized sync(m_printSemaphore);
memcpy(m_localBuffer + strlen(m_localBuffer), name, strlen(name));
m_localBuffer[strlen(m_localBuffer)+1]=0;//keep null to allow strlen to function
m_localBuffer[strlen(m_localBuffer)]='=';
memcpy(m_localBuffer + strlen(m_localBuffer), value, strlen(value));
m_localBuffer[strlen(m_localBuffer)+1]=0;//keep null to allow strlen to function
m_localBuffer[strlen(m_localBuffer)]='|';
//m_localBuffer + strlen(m_localBuffer), writeFmt, args);
size = strlen(m_localBuffer);
}
if (size > kMaxDashboardDataSize)
{
wpi_fatal(DashboardDataOverflow);
}
}
/**
* Indicate that the packing is complete and commit the buffer to the DriverStation.
*
* The packing of the dashboard packet is complete.
* If you are not using the packed dashboard data, you can call Finalize() to commit the Printf() buffer and the error string buffer.
* In effect, you are packing an empty structure.
* Prepares a packet to go to the dashboard...
* Pack the sequence number, Printf() buffer, the errors messages (not implemented yet), and packed dashboard data buffer.
* @return The total size of the data packed into the userData field of the status packet.
*/
INT32 Dashboard::Send()
{
if (*m_userStatus == NULL)
{
wpi_fatal(NullParameter);
return 0;
}
INT32 size = 0;
// QUE: Is this necessary?
// Sequence number
memcpy(*m_userStatus + size, &m_sequence, sizeof(m_sequence));
size += sizeof(m_sequence);
m_sequence++;
INT32 printSize;
{
//Raw data (images, etc)
Synchronized sync(m_printSemaphore);
memcpy(*m_userStatus + size, &rawLength, sizeof(rawLength));
size += sizeof(rawLength);
memcpy(*m_userStatus + size, raw, rawLength);
size += printSize;
// raw[0]=4;
// raw[1]=5;
// raw[2]=1;
// raw[3]=32;
//skip this because don't ever overwrite it
// User printed strings
printSize = strlen(m_localBuffer);
memcpy(*m_userStatus + size, m_localBuffer, printSize);
size += printSize;
string end="|:451@@@";
memcpy(*m_userStatus + size, end.c_str(), end.length());
size += end.length();
m_localBuffer[0] = '@';
m_localBuffer[1] = '@';
m_localBuffer[2] = '@';
m_localBuffer[3] = '4';
m_localBuffer[4] = '5';
m_localBuffer[5] = '1';
m_localBuffer[6] = ':';
m_localBuffer[7] = '|';
m_localBuffer[8] = 0;
}
return size;
}
main code:
PHP Code:
//...
DriverStation::GetInstance()->ds.GetDashboardPacker().Add("gsgs","fsdfds");
DriverStation::GetInstance()->ds.GetDashboardPacker().Send();
//...
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
|