pull/1/head
ozarchie 2018-01-03 09:47:01 +10:00 zatwierdzone przez GitHub
rodzic bd03afdf10
commit 20fccbb347
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
1 zmienionych plików z 58 dodań i 150 usunięć

Wyświetl plik

@ -2,12 +2,12 @@
* Copyright 2017, 2018 John Archbold
*/
// Pin definitions for HBX interface
// Pin definitions for H2X interface
// =================================
#define HDA1 8 // Pin2, 4, 6 on HBX interface
#define HDA1 8 // Pin2, 4, 6 on H2X interface
#define HDA2 10 // Not used
#define HCL1 2 // Pin3 on HBX interface
#define HCL2 3 // Pin5 on HBX interface
#define HCL1 2 // Pin3 on H2X interface
#define HCL2 3 // Pin5 on H2X interface
#define H2X_INPUTPU INPUT_PULLUP // Set pin data input mode
#define H2X_INPUT INPUT // Set pin data input mode
@ -15,8 +15,8 @@
// Jumpers to run monitor or test
// ==============================
#define TESTHBX 9 // Mega2560 D2
#define MONITORHBX 11 // Mega2560 D3
#define TESTH2X 9 // Mega2560 D2
#define MONITORH2X 11 // Mega2560 D3
#define CR 0x0d
@ -24,6 +24,9 @@
#define H2XLEN 256
#define H2XMASK H2XLEN - 1
#define MOTORAZ 0
#define MOTORALT 1
// ETX ISR States
#define START 0x01
#define ACK 0x02
@ -50,14 +53,15 @@ char BitCountArray[16] =
{ 24,24,16,8,0,0,0,0,25,8,8,0,0,0,0,0 };
volatile unsigned char isr_state;
volatile unsigned long isr_timeout;
volatile unsigned long isr_timeout[2];
volatile unsigned char sFlag;
volatile unsigned char BitCountIndex;
volatile unsigned char H2XMotor;
volatile unsigned char HexData;
volatile unsigned char HBXCmnd;
volatile unsigned char HBXData;
volatile unsigned char HBXByteCount;
volatile unsigned char HBXBitCount;
volatile unsigned char H2XCmnd;
volatile unsigned char H2XData;
volatile unsigned char H2XByteCount;
volatile unsigned char H2XBitCount;
volatile unsigned char H2XRxBuffer[H2XLEN]; // Hold data from H2X
volatile unsigned char H2XRxiPtr = 0; // Pointer for input from H2X
@ -68,17 +72,18 @@ void setup() {
Serial.begin(115200);
Serial.println("H2X-Monitor");
attachInterrupt(digitalPinToInterrupt(HCL1), hcl1_isr, FALLING);
attachInterrupt(digitalPinToInterrupt(HCL1), hcl_isr, FALLING);
Serial.print ("Az on Pin ");
Serial.println(HCL1);
attachInterrupt(digitalPinToInterrupt(HCL2), hcl2_isr, FALLING);
attachInterrupt(digitalPinToInterrupt(HCL2), hcl_isr, FALLING);
Serial.print ("Alt on Pin ");
Serial.println(HCL2);
HBXMonitorMode();
H2XMonitorMode();
sFlag = 0;
isr_state = START;
isr_timeout = micros();
isr_timeout[MOTORAZ] = micros();
isr_timeout[MOTORALT] = micros();
interrupts();
}
@ -91,42 +96,49 @@ void loop() {
}
}
void HBXMonitorMode(void) {
void H2XMonitorMode(void) {
HDAListen(); // HDA as input
HCL1Listen(); // HCLs as inputs
HCL2Listen();
}
void hcl1_isr() {
if ((micros() - isr_timeout) > 5000)
void hcl_isr() {
if ((digitalRead(HCL1) == LOW))
H2XMotor = MOTORAZ;
else
H2XMotor = MOTORALT;
// Check if something went wrong
// If it has been more than 2mS (each data bit is ~200us)
// then force it to start state
if ((micros() - isr_timeout[H2XMotor]) > 2000) // 2 mS
isr_state = START;
isr_timeout = micros();
isr_timeout[H2XMotor] = micros();
if (isr_state == START) {
HBXBitCount = 8;
H2XBitCount = 8;
isr_state = COMMAND;
H2XRxBuffer[H2XRxiPtr] = '1'; // Finish off with a CRLF
H2XRxBuffer[H2XRxiPtr] = H2XMotor + '1'; // Identify motor
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
H2XRxBuffer[H2XRxiPtr] = ','; // Finish off with a CRLF
H2XRxBuffer[H2XRxiPtr] = ','; // for csv
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
}
else if (isr_state == COMMAND) {
if (HBXBitCount > 0) { // Read the command
HBXCmnd = (HBXCmnd << 1);
HBXBitCount -= 1;
if (H2XBitCount > 0) { // Read the command
H2XCmnd = (H2XCmnd << 1);
H2XBitCount -= 1;
if (digitalRead(HDA1) == HIGH)
HBXCmnd |= 0x01;
H2XCmnd |= 0x01;
}
else {
HexData = HBXCmnd >> 4; // Get high nibble
HexData = H2XCmnd >> 4; // Get high nibble
if (HexData <= 9) HexData += '0'; // convert to hex
else HexData += '7';
H2XRxBuffer[H2XRxiPtr] = HexData; // Put the (hex) data in the buffer
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
HexData = HBXCmnd & 0x0F; // Get low nibble
HexData = H2XCmnd & 0x0F; // Get low nibble
if (HexData <= 9) HexData += '0'; // convert to hex
else HexData += '7';
H2XRxBuffer[H2XRxiPtr] = HexData; // Put the (hex) data in the buffer
@ -137,14 +149,14 @@ void hcl1_isr() {
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
BitCountIndex = HBXCmnd & 0x0F; // Only 11 known commands
HBXBitCount = BitCountArray[BitCountIndex]; // Get number of data bits
if (HBXBitCount > 0) { // Check bitcount associated with command
BitCountIndex = H2XCmnd & 0x0F; // Only 11 known commands
H2XBitCount = BitCountArray[BitCountIndex]; // Get number of data bits
if (H2XBitCount > 0) { // Check bitcount associated with command
isr_state = DATA;
HBXBitCount--; // This is the first data bit
HBXByteCount = 1; // ditto
H2XBitCount--; // This is the first data bit
H2XByteCount = 1; // ditto
if (digitalRead(HDA1) == HIGH) // Read the first data bit
HBXData |= 0x01;
H2XData |= 0x01;
}
else { // Nothing to do - all finished - back to start
isr_state = START;
@ -158,143 +170,39 @@ void hcl1_isr() {
}
}
else if (isr_state == DATA) {
if (HBXBitCount > 0) { // Read the data
HBXData = (HBXData << 1);
HBXBitCount -= 1;
HBXByteCount += 1;
if (H2XBitCount > 0) { // Read the data
H2XData = (H2XData << 1);
H2XBitCount -= 1;
H2XByteCount += 1;
if (digitalRead(HDA1) == HIGH)
HBXData |= 0x01;
if ((HBXByteCount == 8) || (HBXBitCount == 0)) { // Full byte or last bit
H2XData |= 0x01;
if ((H2XByteCount == 8) || (H2XBitCount == 0)) { // Full byte or last bit
HexData = HBXData >> 4; // Get high nibble
HexData = H2XData >> 4; // Get high nibble
if (HexData <= 9) HexData += '0'; // convert to hex
else HexData += '7';
H2XRxBuffer[H2XRxiPtr] = HexData; // Put the (hex) data in the buffer
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
HexData = HBXData & 0x0F; // Get low nibble
HexData = H2XData & 0x0F; // Get low nibble
if (HexData <= 9) HexData += '0'; // convert to hex
else HexData += '7';
H2XRxBuffer[H2XRxiPtr] = HexData; // Put the (hex) data in the buffer
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
if (HBXBitCount != 0) {
if (H2XBitCount != 0) {
H2XRxBuffer[H2XRxiPtr] = ','; // Finish off with a CRLF
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
}
HBXData = 0; // For the last data bit
HBXByteCount = 0;
H2XData = 0; // For the last data bit
H2XByteCount = 0;
}
}
if (HBXBitCount == 0) { // All data received - back to start
isr_state = START;
H2XRxBuffer[H2XRxiPtr] = CR; // Finish off with a CRLF
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
H2XRxBuffer[H2XRxiPtr] = LF; // Finish off with a CRLF
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
}
}
}
void hcl2_isr() {
if ((micros() - isr_timeout) > 5000)
isr_state = START;
isr_timeout = micros();
if (isr_state == START) {
HBXBitCount = 8;
isr_state = COMMAND;
H2XRxBuffer[H2XRxiPtr] = '2'; // Finish off with a CRLF
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
H2XRxBuffer[H2XRxiPtr] = ','; // Finish off with a CRLF
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
}
else if (isr_state == COMMAND) {
if (HBXBitCount > 0) { // Read the command
HBXCmnd = (HBXCmnd << 1);
HBXBitCount -= 1;
if (digitalRead(HDA1) == HIGH)
HBXCmnd |= 0x01;
}
else {
HexData = HBXCmnd >> 4; // Get high nibble
if (HexData <= 9) HexData += '0'; // convert to hex
else HexData += '7';
H2XRxBuffer[H2XRxiPtr] = HexData; // Put the (hex) data in the buffer
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
HexData = HBXCmnd & 0x0F; // Get low nibble
if (HexData <= 9) HexData += '0'; // convert to hex
else HexData += '7';
H2XRxBuffer[H2XRxiPtr] = HexData; // Put the (hex) data in the buffer
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
BitCountIndex = HBXCmnd & 0x0F; // Only 11 known commands
HBXBitCount = BitCountArray[BitCountIndex]; // Get number of data bits
if (HBXBitCount > 0) { // Check bitcount associated with command
isr_state = DATA;
HBXBitCount--; // This is the first data bit
HBXByteCount = 1; // ditto
if (digitalRead(HDA1) == HIGH) // Read the first data bit
HBXData |= 0x01;
H2XRxBuffer[H2XRxiPtr] = ','; // Data to follow
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
}
else { // Nothing to do - all finished - back to start
isr_state = START;
H2XRxBuffer[H2XRxiPtr] = CR; // Finish off with a CRLF
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
H2XRxBuffer[H2XRxiPtr] = LF; // Finish off with a CRLF
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
}
}
}
else if (isr_state == DATA) {
if (HBXBitCount > 0) { // Read the data
HBXData = (HBXData << 1);
HBXBitCount -= 1;
HBXByteCount += 1;
if (digitalRead(HDA1) == HIGH)
HBXData |= 0x01;
if ((HBXByteCount == 8) || (HBXBitCount == 0)) { // Full byte or last bit
HexData = HBXData >> 4; // Get high nibble
if (HexData <= 9) HexData += '0'; // convert to hex
else HexData += '7';
H2XRxBuffer[H2XRxiPtr] = HexData; // Put the (hex) data in the buffer
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
HexData = HBXData & 0x0F; // Get low nibble
if (HexData <= 9) HexData += '0'; // convert to hex
else HexData += '7';
H2XRxBuffer[H2XRxiPtr] = HexData; // Put the (hex) data in the buffer
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
if (HBXBitCount != 0) {
H2XRxBuffer[H2XRxiPtr] = ','; // Finish off with a CRLF
H2XRxiPtr += 1;
H2XRxiPtr &= H2XMASK;
}
HBXData = 0; // For the last data bit
HBXByteCount = 0;
}
}
if (HBXBitCount == 0) { // All data received - back to start
if (H2XBitCount == 0) { // All data received - back to start
isr_state = START;
H2XRxBuffer[H2XRxiPtr] = CR; // Finish off with a CRLF
H2XRxiPtr += 1;