They actually work pretty well and are quite accurate.
The CS4122 needs a max of about 170ma of power at ~7V in to drive an aircore gauge in the 45 degree quadrants (~80ma for 0,90,180,270) - so these gauges are somewhat power hungry.
My next trick is to get the altimeters working.They have a 10 turn 10k pot geared to the internals and a 6v mini planetary gear motor to move the needles.
I going to use a polou jrk G2 21v3 to via i2c and see if it can be made to work.
Code: Select all
#include <si_message_port.hpp>
#include <SPI.h>
#define ChipSelectPinA 8
#define ChipSelectPinB 9
SiMessagePort* messagePort;
int t_delay = 50;
unsigned int gaugePosA = 0;
unsigned int gaugePosB = 0;
unsigned long millis_now = 0;
static void new_message_callback(uint16_t message_id, struct SiMessagePortPayload* payload) {
if (payload == NULL) {
messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received without payload");
}
else {
switch(payload->type) {
case SI_MESSAGE_PORT_DATA_TYPE_BYTE:
messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " bytes: " + payload->data_byte[0]);
break;
case SI_MESSAGE_PORT_DATA_TYPE_STRING:
messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received string: " + payload->data_string);
break;
case SI_MESSAGE_PORT_DATA_TYPE_INTEGER:
// messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " ints: " + payload->data_int[0]);
if (message_id == 41) {
gaugePosA = payload->data_int[0];
}
if (message_id == 42) {
gaugePosB = payload->data_int[0];
}
if (message_id == 81) {
gaugePosA = payload->data_int[0];
messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " ints: " + payload->data_int[0]);
}
if (message_id == 82) {
gaugePosB = payload->data_int[0];
messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " ints: " + payload->data_int[0]);
}
break;
case SI_MESSAGE_PORT_DATA_TYPE_FLOAT:
messagePort->DebugMessage(SI_MESSAGE_PORT_LOG_LEVEL_INFO, (String)"Received " + payload->len + " floats: " + payload->data_float[0]);
break;
}
}
}
// send a 12 bit message to one of two CS4122 chips connected via SPI. Target range is 0-1023.
void send_aircore(unsigned int channel,unsigned int target) {
unsigned int target1;
unsigned int oldval;
byte byte0;
byte byte1;
target1 = target;
if (target1 > 1023) { target1 = 0; }
byte1 = (target1 >> 8);
byte0 = (target1 & 0xFF); //0xFF = B11111111
if (channel == 0) {
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
digitalWrite (ChipSelectPinA, HIGH);
SPI.transfer(byte1);
oldval = SPI.transfer(byte0);
digitalWrite (ChipSelectPinA, LOW);
SPI.endTransaction();
} else {
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
digitalWrite (ChipSelectPinB, HIGH);
SPI.transfer(byte1);
oldval = SPI.transfer(byte0);
digitalWrite (ChipSelectPinB, LOW);
SPI.endTransaction();
}
}
// make the needles move around on power up
void aircore_init() {
send_aircore(0,0);
send_aircore(1,0);
delay(1000);
send_aircore(0,255);
send_aircore(1,255);
delay(1000);
send_aircore(0,511);
send_aircore(1,511);
delay(1000);
send_aircore(0,767);
send_aircore(1,767);
delay(1000);
send_aircore(0,0);
send_aircore(1,0);
delay(1000);
for (unsigned int i = 0; i<= 100; i++) {
send_aircore (0,i);
send_aircore (1,i);
delay(15);
}
for (unsigned int i = 100; i >= 0; i--) {
send_aircore (0,i);
send_aircore (1,i);
delay(15);
}
}
void setup() {
// Init library on channel G and Arduino type UNO
messagePort = new SiMessagePort(SI_MESSAGE_PORT_DEVICE_ARDUINO_UNO, SI_MESSAGE_PORT_CHANNEL_G, new_message_callback);
pinMode (ChipSelectPinA, OUTPUT);
pinMode (ChipSelectPinB, OUTPUT);
SPI.begin();
aircore_init();
}
void loop() {
messagePort->Tick();
if(millis() >= millis_now + t_delay){
millis_now += t_delay;
send_aircore(0,gaugePosA);
send_aircore(1,gaugePosB);
}
// You can send your own messages to Air Manager or Air Player
//messagePort->SendMessage(123);
//messagePort->SendMessage(123, "hello");
//messagePort->SendMessage(123, (int32_t)1000);
//messagePort->SendMessage(123, 2.5f);
//messagePort->SendMessage(123, (uint8_t) 0xAA);
}