Skip to content

Instantly share code, notes, and snippets.

@tschak909
Created January 2, 2020 01:00
Show Gist options
  • Save tschak909/dd5e02c1aa24aed43c19a6a4f6374277 to your computer and use it in GitHub Desktop.
Save tschak909/dd5e02c1aa24aed43c19a6a4f6374277 to your computer and use it in GitHub Desktop.
re-think sync
/**
Test #30 - Re-think SIO sync
*/
#define TEST_NAME "#FujiNet Rethink Sync(tm)"
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif
// Uncomment for Debug on 2nd UART (GPIO 2)
// #define DEBUG_S
// Uncomment for Debug on TCP/6502 to DEBUG_HOST
// Run: `nc -vk -l 6502` on DEBUG_HOST
// #define DEBUG_N
// #define DEBUG_HOST "192.168.1.7"
#ifdef ESP8266
#define SIO_UART Serial
#define BUG_UART Serial1
#define PIN_LED 2
#define PIN_INT 5
#define PIN_PROC 4
#define PIN_MTR 16
#define PIN_CMD 12
#endif
#ifdef ESP32
#define SIO_UART Serial2
#define BUG_UART Serial
#define PIN_LED1 2
#define PIN_LED2 4
#define PIN_INT 26
#define PIN_PROC 22
#define PIN_MTR 33
#define PIN_CMD 21
#endif
/**
A Single command frame, both in structured and unstructured
form.
*/
union
{
struct
{
unsigned char devic;
unsigned char comnd;
unsigned char aux1;
unsigned char aux2;
unsigned char cksum;
};
byte cmdFrameData[5];
} cmdFrame;
void setup()
{
#ifdef DEBUG_S
BUG_UART.begin(115200);
Debug_println();
Debug_println(TEST_NAME);
#endif
// Set up pins
pinMode(PIN_INT, OUTPUT); // thanks AtariGeezer
digitalWrite(PIN_INT, HIGH);
pinMode(PIN_PROC, OUTPUT); // thanks AtariGeezer
digitalWrite(PIN_PROC, HIGH);
pinMode(PIN_MTR, INPUT);
pinMode(PIN_CMD, INPUT);
#ifdef ESP8266
pinMode(PIN_LED, INPUT);
digitalWrite(PIN_LED, HIGH); // off
#elif defined(ESP32)
pinMode(PIN_LED1, OUTPUT);
pinMode(PIN_LED2, OUTPUT);
digitalWrite(PIN_LED1, HIGH); // off
digitalWrite(PIN_LED2, HIGH); // off
#endif
#ifdef DEBUG_N
/* Get WiFi started, but don't wait for it otherwise SIO
powered FujiNet fails to boot
*/
WiFi.begin(DEBUG_SSID, DEBUG_PASSWORD);
#endif
// Set up serial
SIO_UART.begin(19200);
#ifdef ESP8266
SIO_UART.swap();
#endif
}
/**
* SIO processing loop
*/
void sio_loop()
{
while (digitalRead(PIN_CMD)==HIGH)
yield();
SIO_UART.setTimeout(2); // 2ms
SIO_UART.readBytes(cmdFrame.cmdFrameData,5);
while (digitalRead(PIN_CMD)==LOW)
yield();
#ifdef DEBUG
Debug_printf("DEVIC: %02x\nCOMND: %02x\nAUX1: %02x\nAUX2: %02x\nCKSUM: %02x\n\n",
cmdFrame.devic,
cmdFrame.comnd,
cmdFrame.aux1,
cmdFrame.aux2,
cmdFrame.cksum);
#endif
}
void loop()
{
#ifdef DEBUG_N
/* Connect to debug server if we aren't and WiFi is connected */
if ( !wificlient.connected() && WiFi.status() == WL_CONNECTED )
{
wificlient.connect(DEBUG_HOST, 6502);
wificlient.println(TEST_NAME);
}
#endif
sio_loop();
#ifdef ESP32
if (cmdState == WAIT && digitalRead(PIN_LED2) == LOW)
{
digitalWrite(PIN_LED2, HIGH); // Turn off SIO LED
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment