Skip to content

Commit 99e1105

Browse files
committed
chuni-io: handle incoming messages
1 parent d1f6367 commit 99e1105

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

ChuniVController/ChuniIO/chuniio.cc

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ static HANDLE chuni_io_slider_thread;
2020
static bool chuni_io_slider_stop_flag;
2121
static SOCKET chuni_socket;
2222
static USHORT chuni_port = 24864; // CHUNI on dialpad
23-
static char recv_buf[32];
2423

2524
HRESULT chuni_io_jvs_init(void)
2625
{
@@ -73,18 +72,22 @@ void chuni_io_jvs_read_coin_counter(uint16_t* out)
7372
}
7473

7574
if (chuni_coin_pending) chuni_coins++;
75+
chuni_coin_pending = false;
7676

7777
*out = chuni_coins;
7878
}
7979

8080
void chuni_io_jvs_poll(uint8_t* opbtn, uint8_t* beams)
8181
{
8282
if (chuni_test_pending) {
83-
*opbtn |= 0x01; /* Test */
83+
*opbtn |= 0x01;
84+
chuni_test_pending = false;
8485
}
8586

8687
if (chuni_service_pending) {
87-
*opbtn |= 0x02; /* Service */
88+
*opbtn |= 0x02;
89+
chuni_service_pending = false;
90+
8891
}
8992

9093
*beams = chuni_ir_sensor_map;
@@ -136,28 +139,66 @@ void chuni_io_slider_stop(void)
136139

137140
void chuni_io_slider_set_leds(const uint8_t* rgb)
138141
{
139-
/*for (uint8_t i = 0; i < 32; i++) {
140-
log_debug("SET_LED[%d]: %d\n", i, rgb[i]);
141-
}*/
142+
static uint8_t prev_rgb_status[32];
143+
144+
for (uint8_t i = 0; i < 32; i++) {
145+
if (rgb[i] != prev_rgb_status[i]) log_debug("SET_LED[%d]: %d\n", i, rgb[i]);
146+
prev_rgb_status[i] = rgb[i];
147+
}
142148
}
143149

144150
static unsigned int __stdcall chuni_io_slider_thread_proc(void* ctx)
145151
{
146152
chuni_io_slider_callback_t callback;
147-
uint8_t pressure[32];
153+
static uint8_t pressure[32];
154+
static char recv_buf[32];
148155
size_t i;
149156

150157
for (i = 0; i < _countof(pressure); i++) pressure[i] = 0;
151158

152159
callback = (chuni_io_slider_callback_t) ctx;
153160

154161
while (!chuni_io_slider_stop_flag) {
155-
int len = recv(chuni_socket, recv_buf, 32, 0);
162+
int len = recv(chuni_socket, recv_buf, 32, 0); // FIXME: discard pending data on proc start
156163
if (len == (int) sizeof(chuni_msg_t)) {
157-
// TOD: message parsing, event dispatch logic
164+
const chuni_msg_t* msg = (const chuni_msg_t*)recv_buf;
165+
if (msg->src != SRC_CONTROLLER) {
166+
log_warn("got non-controller message.\n");
167+
callback(pressure);
168+
continue;
169+
}
170+
switch (msg->type) {
171+
case COIN_INSERT:
172+
chuni_coin_pending = true;
173+
break;
174+
case SLIDER_PRESS:
175+
if (msg->target >= 32) log_error("invalid slider value %d in SLIDER_PRESS.\n", msg->target);
176+
else pressure[msg->target] = 128;
177+
break;
178+
case SLIDER_RELEASE:
179+
if (msg->target >= 32) log_error("invalid slider value %d in SLIDER_RELEASE.\n", msg->target);
180+
else pressure[msg->target] = 0;
181+
break;
182+
case CABINET_TEST:
183+
chuni_test_pending = true;
184+
break;
185+
case CABINET_SERVICE:
186+
chuni_service_pending = true;
187+
break;
188+
case IR_BLOCKED:
189+
if (msg->target >= 6) log_error("invalid slider value %d in IR_BLOCKED.\n", msg->target);
190+
else chuni_ir_sensor_map |= 1 << msg->target;
191+
break;
192+
case IR_UNBLOCKED:
193+
if (msg->target >= 6) log_error("invalid slider value %d in IR_UNBLOCKED.\n", msg->target);
194+
else chuni_ir_sensor_map &= ~(1 << msg->target);
195+
break;
196+
default:
197+
log_error("bad message type %d.\n", msg->type);
198+
}
158199
}
159200
else if (len > 0) {
160-
log_warn("got invalid packet of length %d.", len);
201+
log_warn("got invalid packet of length %d.\n", len);
161202
}
162203

163204
callback(pressure);

0 commit comments

Comments
 (0)