Not pretty but got the job done for me:
// Arduino digital and analog pins
uint64_t digitalPins = 0;//
const uint8_t pins[] = { 22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47 };
const uint8_t nPins = sizeof pins / sizeof pins[0];
void setup()
{
// init serial
Serial.begin(115200);
delay(10);
analogReference (DEFAULT);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
pinMode(A3, INPUT_PULLUP);
pinMode(A4, INPUT_PULLUP);
pinMode(A5, INPUT_PULLUP);
pinMode(A6, INPUT_PULLUP);
pinMode(A7, INPUT_PULLUP);
}
void loop()
{
ReadDigitalStatuses();
ReadAnalogStatuses();
SendCANFramesToSerial();
}
void ReadDigitalStatuses()
{
// read status of digital pins (22-53)
digitalPins = 0;
int bitposition = 0;
for (int i=0; i<nPins; i++)
{
if (digitalRead(pins[i]) == HIGH) digitalPins |= (1ULL << bitposition);
bitposition++;
}
}
void ReadAnalogStatuses()
{
}
void SendCANFramesToSerial()
{
byte buf[8];
int TODf3 = analogRead(1);
int TODf2 = analogRead(2);
int TODf1 = analogRead(3);
int TODauto = analogRead(4);
int TODcheck = analogRead(5);
int TODrr = analogRead(6);
int TODlr = analogRead(7);
int fuel = analogRead(8);
fuel = map(fuel, 1023, 0, 0, 1023);
int spare = analogRead(9);
int LeftLane = analogRead(10);
int RightLane = analogRead(11);
int DashDimmer = analogRead(12);
// build 3200 CAN frame
memcpy(buf, &TODcheck, 2);
memcpy(buf + 2, &TODrr, 2);
memcpy(buf + 4, &TODlr, 2);
memcpy(buf + 6, &fuel, 2);
// write 3200 CAN frame to serial
SendCANFrameToSerial(3200, buf);
// build 3201 CAN frame
memcpy(buf, &digitalPins, sizeof(digitalPins));//memcpy(buf, &digitalPins, 2);
// write 3201 CAN frame to serial
SendCANFrameToSerial(3201, buf);
// build 3rd CAN frame
memcpy(buf, &TODf3, 2);
memcpy(buf + 2, &TODf2, 2);
memcpy(buf + 4, &TODf1, 2);
memcpy(buf + 6, &TODauto, 2);
// write 3rd CAN frame to serial
SendCANFrameToSerial(3202, buf);
// build 4th CAN frame
memcpy(buf, &spare, 2);
memcpy(buf + 2, &LeftLane, 2);
memcpy(buf + 4, &RightLane, 2);
memcpy(buf + 6, &DashDimmer, 2);
// write 4th CAN frame to serial
SendCANFrameToSerial(3203, buf);
}
void SendCANFrameToSerial(unsigned long canFrameId, const byte* frameData)
{
// the 4 byte identifier at the beginning of each CAN frame
// this is required for RealDash to 'catch-up' on ongoing stream of CAN frames
const byte serialBlockTag[4] = { 0x44, 0x33, 0x22, 0x11 };
Serial.write(serialBlockTag, 4);
// the CAN frame id number (as 32bit little endian value)
Serial.write((const byte*)&canFrameId, 4);
// CAN frame payload
Serial.write(frameData, 8);
}