/*
BK-PS2-MARS-Mouse-host by TheGWBV, 29/08/2020, Step 2.
Free for exUSSR users and all others ;-)
*/
const int ResetPin = 4; // номер вывода с Reset
int ResetState = 0; // переменная для чтения состояния Reset
int MX_B = 0; // переменная для чтения и буферизации перемещения по оси Х
int MY_B = 0; // переменная для чтения и буферизации перемещения по оси Y
bool XY = LOW; // переменная, для чередования диагональных перемещений
#include <ps2.h>
#include <SoftwareSerial.h>
// Джойстик-шар мыши Марсианки
#define marsUP 7
#define marsR 9
#define marsDn 8
#define marsL 10
// Кнопки мыши Марсианки
#define marsLB 2
#define marsRB 11
/*
*
Пины для UART/ТЛГ:
RX is digital pin 1 (connect to TX of other device)
TX is digital pin 3 (connect to RX of other device)
*/
SoftwareSerial TLG_soft_Serial(0, 1); // RX, TX
/*
Used an arduino sketch to interface with a ps/2 mouse.
Also uses serial protocol to talk back to the host
and report what it finds.
*/
/*
Pin 13 is the mouse data pin, pin 15 is the clock pin
Feel free to use whatever pins are convenient.
*/
PS2 mouse(6, 5); // 6 - clock, 5 - data
void setup()
{
/*
Аппаратный UART подключаем к ПЭВМ... с эмулятором БК-шки, и/или прочим софтом для связи по UART
*/
Serial.begin(38400); while (!Serial); // wait for serial port to connect. Needed for native USB port only
/* Пины для UART/ТЛГ:
RX is digital pin 2 (connect to TX of other device)
TX is digital pin 4 (connect to RX of other device)
Можно подключать к ТЛГ БК0010 или к псевдо-ТЛГ на БК0011М (для этой модели ТЛГ заводом не предусмотрен!)
*/
TLG_soft_Serial.begin(38400); // Open software serial communications and wait for port to open
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH);
// BK Marsianka output: 2 - marsLB, 3 - marsRB, 7 - marsUP, 9 - marsR, 8 - marsDn, 10 - marsL.
// Джойстик-шар мыши Марсианки
pinMode(marsUP, OUTPUT); digitalWrite(marsUP, HIGH); // Вверх
pinMode(marsR, OUTPUT); digitalWrite(marsR, HIGH); // Вправо
pinMode(marsDn, OUTPUT); digitalWrite(marsDn, HIGH); // Вниз
pinMode(marsL, OUTPUT); digitalWrite(marsL, HIGH); // Влево
// Кнопки мыши Марсианки
pinMode(marsLB, OUTPUT); digitalWrite(marsLB, HIGH); // Левая кнопка
pinMode(marsRB, OUTPUT); digitalWrite(marsRB, HIGH); // Правая кнопка
mouse_init(); // Инициировать мышь PS/2
}
/*
Initialize the mouse. Reset it, and place it into remote
mode, so we can get the encoder data on demand.
*/
void mouse_init()
{
mouse.write(0xff); // reset
mouse.read(); // ack byte
mouse.read(); // blank */
mouse.read(); // blank */
mouse.write(0xf0); // remote mode
mouse.read(); // ack
delayMicroseconds(50);
}
/*
Обнуляем координатные переменные контроллера.
*/
void reset_XY()
{
//Serial.print("\t+-------+");
digitalWrite(marsR, HIGH);
digitalWrite(marsL, HIGH);
digitalWrite(marsUP, HIGH);
digitalWrite(marsDn, HIGH);
//MY_B = 0;
//MX_B = 0;
//digitalWrite(marsLB, HIGH);
//digitalWrite(marsRB, HIGH);
//Serial.println();
}
/*
считываем данные с мышки и выводим в COM-порт
*/
void loop()
{
char mStat;
char mX;
char mY;
uint16_t reg_177714;
/* запрашиваем данные от мышки */
mouse.write(0xeb); // команда на чтение данных
mouse.read(); // игнорируем ack
mStat = mouse.read();
mX = mouse.read();
mY = mouse.read();
ResetState = digitalRead(ResetPin);
/* if (ResetState == LOW)
{
reset_XY();
} */
// Просмотр битов направлений на предмет наличия диагональных
if (mX==0&&mY==0)
{
reset_XY();
goto end_loop;
}
if (mX > 0&&mY > 0)
{
Serial.print("\t+Вправо+Вверх");
if (XY==LOW)
{
mX=1;
mY=0;
}
else
{
mX=0;
mY=1;
}
XY=!XY;
}
else if (mX > 0&&mY < 0)
{
Serial.print("\t+Вправо+Вниз");
if (XY==LOW)
{
mX=1;
mY=0;
}
else
{
mX=0;
mY=-1;
}
XY=!XY;
}
else if (mX < 0&&mY > 0)
{
Serial.print("\t+Влево+Вверх");
if (XY==LOW)
{
mX=-1;
mY=0;
}
else
{
mX=0;
mY=1;
}
XY=!XY;
}
else if (mX < 0&&mY < 0)
{
Serial.print("\t+Влево+Вниз");
if (XY==LOW)
{
mX=-1;
mY=0;
}
else
{
mX=0;
mY=-1;
}
XY=!XY;
}
// Влево
if ( mX < 0 ) // -1
{
Serial.print("\t+Влево");
digitalWrite(marsL, LOW);
//mX=-1;
}
else
{
Serial.print("\t-");
digitalWrite(marsL, HIGH);
}
// Вправо
if ( mX > 0 ) // 1
{
Serial.print("\t+Вправо");
digitalWrite(marsR, LOW);
//mX=1;
}
else
{
Serial.print("\t-");
digitalWrite(marsR, HIGH);
}
// Вверх
if ( mY > 0 ) // 1
{
Serial.print("\t+Вверх");
digitalWrite(marsUP, LOW);
//mY=1;
}
else
{
Serial.print("\t-");
digitalWrite(marsUP, HIGH);
}
// Вниз
if ( mY < 0 ) // -1
{
Serial.print("\t+Вниз");
digitalWrite(marsDn, LOW);
//mY=-1;
}
else
{
Serial.print("\t-");
digitalWrite(marsDn, HIGH);
}
end_loop:
if ( mStat & 0x01 )
{
Serial.print("\t+ЛевКн");
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(marsLB, LOW);
}
else
{
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("\t-");
digitalWrite(marsLB, HIGH);
}
if ( mStat & 0x02 )
{
Serial.print("\t+ПравКн");
digitalWrite(marsRB, LOW);
}
else
{
Serial.print("\t-");
digitalWrite(marsRB, HIGH);
}
/* выводим данные на аппаратный UART */
Serial.println();
Serial.print(mStat, BIN);
Serial.print("\tX=");
Serial.print(mX, DEC);
Serial.print("\tY=");
Serial.print(mY, DEC);
Serial.println();
//delay(50);
// BK TLG UART run over and over: for USB-Com <-[data]-> BK-TLG-UART
if ( TLG_soft_Serial.available() ) {
Serial.write(TLG_soft_Serial.read());
digitalWrite(LED_BUILTIN, HIGH);
}
if ( Serial.available() ) {
TLG_soft_Serial.write(Serial.read());
}
}
[свернуть]