Skip to content

Commit

Permalink
#24 PS2 input repeating
Browse files Browse the repository at this point in the history
  • Loading branch information
No0ne committed Jun 14, 2024
1 parent eead2c4 commit 27c9bce
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if (MS_RATE_HOST_CONTROL)
endif()

pico_set_program_name(ps2x2pico "ps2x2pico")
pico_set_program_version(ps2x2pico "1.1")
pico_set_program_version(ps2x2pico "1.2")

pico_enable_stdio_uart(ps2x2pico 1)
pico_enable_stdio_usb(ps2x2pico 0)
Expand Down
86 changes: 69 additions & 17 deletions src/ps2in.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "ps2in.pio.h"

s8 ps2in_prog = -1;
u8 ps2in_msi = 0;
u8 ps2in_msb[] = { 0, 0, 0, 0 };

void ps2in_init(ps2in* this, PIO pio, u8 data_pin) {
if(ps2in_prog == -1) {
Expand All @@ -37,38 +39,88 @@ void ps2in_init(ps2in* this, PIO pio, u8 data_pin) {
this->sm = pio_claim_unused_sm(pio, true);
ps2in_program_init(pio, this->sm, ps2in_prog, data_pin);
this->pio = pio;
this->state = 0;
}

void ps2in_task(ps2in* this, ps2out* out) {
if(!pio_sm_is_rx_fifo_empty(this->pio, this->sm)) {
u8 byte = pio_sm_get(this->pio, this->sm) >> 23;
u32 fifo = pio_sm_get(this->pio, this->sm) >> 23;

printf("%02x %02x\n", this->sm, byte);
bool parity = 1;
for(u8 i = 0; i < 8; i++) {
parity = parity ^ (fifo >> i & 1);
}

/* if(byte == 0x00 && this->byte_next == 0xaa) {
pio_sm_put(this->pio, this->sm, ps2_frame(0xf4));
} else { //if(byte != 0xfa) {
queue_try_add(&out->qbytes, &byte);
} */
if(parity != fifo >> 8) {
pio_sm_put(this->pio, this->sm, ps2_frame(0xfe));
return;
}

this->byte_next = byte;
u8 byte = fifo;
//printf("** ps2in sm %02x byte %02x\n", this->sm, byte);

/*if(byte < 0x80 || byte == 0x83 || byte == 0x84 || byte == 0xe0 || byte == 0xf0) {
queue_try_add(&out->qbytes, &byte);
//if((fifo & 0xff) == 0xfe) {
// pio_sm_put(this->pio, this->sm, ps2_frame(this->last_tx));
// return;
//}

if(byte == 0xaa && this->state) {
this->state = this->sm ? 2 : 10;
printf("** ps2in sm %02x reset successful!\n", this->sm);
}

if(this->sm == 0) {
if(byte == 0xfa && this->state == 9) {
this->state = 10;
pio_sm_put(this->pio, this->sm, ps2_frame(this->byte));
}

} else if(byte == 0xfa && this->send_next) {
this->send_next = false;
pio_sm_put(this->pio, this->sm, ps2_frame(this->byte_next));
}*/
if(byte != 0xfa && this->state == 10) {
queue_try_add(&out->qbytes, &byte);
}
}

if(this->sm == 1) {
if(this->state == 10) {

ps2in_msb[ps2in_msi] = byte;
ps2in_msi++;

if(ps2in_msi == 4) {
ps2in_msi = 0;
ms_send_movement(ps2in_msb[0] & 0x7, ps2in_msb[1], 0x100 - ps2in_msb[2], 0x100 - ps2in_msb[3]);
}

} else {

if(byte == 0xfa && this->state == 9) {
ps2in_msi = 0;
this->state = 10;
}

u8 init[] = { 0xaa, 0x00, 0xf3, 0xc8, 0xf3, 0x64, 0xf3, 0x50, 0xf4 };

if(byte == init[1] && this->state == 2 || byte == 0xfa && this->state > 2 && this->state < 9) {
pio_sm_put(this->pio, this->sm, ps2_frame(init[this->state]));
this->state++;
}

}
}
}
}

void ps2in_reset(ps2in* this) {
this->state = 1;
printf("** ps2in reset sm %02x\n", this->sm);
pio_sm_put(this->pio, this->sm, ps2_frame(0xff));
}

void ps2in_set(ps2in* this, u8 command, u8 byte) {
pio_sm_put(this->pio, this->sm, ps2_frame(command));
this->byte_next = byte;
this->send_next = true;
if(this->state == 10) {
printf("** ps2in cmd %02x byte %02x\n", command, byte);
pio_sm_put(this->pio, this->sm, ps2_frame(command));
this->byte = byte;
this->state = 9;
}
}
5 changes: 3 additions & 2 deletions src/ps2in.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
typedef struct {
PIO pio;
uint sm;
bool send_next;
u8 byte_next;
u8 state;
u8 byte;
} ps2in;

void ps2in_init(ps2in* this, PIO pio, u8 data_pin);
void ps2in_task(ps2in* this, ps2out* out);
void ps2in_reset(ps2in* this);
void ps2in_set(ps2in* this, u8 command, u8 byte);
void ms_send_movement(u8 buttons, s8 x, s8 y, s8 z);
1 change: 1 addition & 0 deletions src/ps2ms.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void ms_send(u8 byte) {
s64 ms_reset_callback() {
ms_send(0xaa);
ms_send(ms_type);
ps2in_reset(&ms_in);
return 0;
}

Expand Down

0 comments on commit 27c9bce

Please sign in to comment.