Skip to content

Commit df011e8

Browse files
committed
v1.1: seprated network thread, allow S+T press.
1 parent bd325d8 commit df011e8

File tree

4 files changed

+95
-62
lines changed

4 files changed

+95
-62
lines changed

ChuniVController/ChuniIO/chuniio.cc

Lines changed: 77 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#pragma comment(lib,"ws2_32.lib")
1111

1212
static unsigned int __stdcall chuni_io_slider_thread_proc(void* ctx);
13+
static unsigned int __stdcall chuni_io_network_thread_proc(void* ctx);
1314

1415
static bool chuni_coin_pending = false;
1516
static bool chuni_service_pending = false;
@@ -22,6 +23,7 @@ static SOCKET chuni_socket;
2223
static USHORT chuni_port = 24864; // CHUNI on dialpad
2324
static struct sockaddr_in remote;
2425
static bool remote_exist = false;
26+
static uint8_t chuni_sliders[32];
2527

2628
HRESULT chuni_io_jvs_init(void)
2729
{
@@ -62,6 +64,14 @@ HRESULT chuni_io_jvs_init(void)
6264
return S_FALSE;
6365
}
6466

67+
(HANDLE)_beginthreadex(
68+
NULL,
69+
0,
70+
chuni_io_network_thread_proc,
71+
NULL,
72+
0,
73+
NULL);
74+
6575
log_info("server socket initialization completed.\n");
6676

6777
return S_OK;
@@ -167,77 +177,89 @@ void chuni_io_slider_set_leds(const uint8_t* rgb)
167177
}
168178
}
169179

170-
static unsigned int __stdcall chuni_io_slider_thread_proc(void* ctx)
171-
{
172-
chuni_io_slider_callback_t callback;
173-
static uint8_t pressure[32];
174-
static char recv_buf[32];
175-
size_t i;
176-
177-
for (i = 0; i < _countof(pressure); i++) pressure[i] = 0;
180+
static void chuni_io_ir(uint8_t sensor, bool set) {
181+
if (sensor % 2 == 0) sensor++;
182+
else sensor--;
183+
if (set) chuni_ir_sensor_map |= 1 << sensor;
184+
else chuni_ir_sensor_map &= ~(1 << sensor);
185+
}
178186

179-
callback = (chuni_io_slider_callback_t) ctx;
187+
static unsigned int __stdcall chuni_io_network_thread_proc(void* ctx) {
188+
log_info("spinning up network event handler...\n");
180189

181-
while (!chuni_io_slider_stop_flag) {
182-
int addr_sz = sizeof(struct sockaddr_in);
183-
// FIXME: discard pending data on proc start?
190+
static char recv_buf[32];
191+
int addr_sz = sizeof(struct sockaddr_in);
192+
while (true) {
184193
int len = recvfrom(chuni_socket, recv_buf, 32, 0, (sockaddr*)&remote, &addr_sz);
185194
remote_exist = true;
186-
if (len == (int) sizeof(chuni_msg_t)) {
195+
196+
if (len == (int)sizeof(chuni_msg_t)) {
187197
const chuni_msg_t* msg = (const chuni_msg_t*)recv_buf;
188198
if (msg->src != SRC_CONTROLLER) {
189199
log_warn("got non-controller message.\n");
190-
callback(pressure);
191200
continue;
192201
}
193202
switch (msg->type) {
194-
case COIN_INSERT:
195-
log_debug("adding coin.\n");
196-
chuni_coin_pending = true;
197-
break;
198-
case SLIDER_PRESS:
199-
log_debug("slider_press at %d.\n", msg->target);
200-
if (msg->target >= 16) log_error("invalid slider value %d in SLIDER_PRESS.\n", msg->target);
201-
else {
202-
pressure[(msg->target) * 2] = 128;
203-
pressure[(msg->target) * 2 + 1] = 128;
204-
}
205-
break;
206-
case SLIDER_RELEASE:
207-
log_debug("slider released on %d.\n", msg->target);
208-
if (msg->target >= 16) log_error("invalid slider value %d in SLIDER_RELEASE.\n", msg->target);
209-
else {
210-
pressure[(msg->target) * 2] = 0;
211-
pressure[(msg->target) * 2 + 1] = 0;
212-
}
213-
break;
214-
case CABINET_TEST:
215-
log_debug("setting cabinet_test.\n");
216-
chuni_test_pending = true;
217-
break;
218-
case CABINET_SERVICE:
219-
log_debug("setting cabinet_service.\n");
220-
chuni_service_pending = true;
221-
break;
222-
case IR_BLOCKED:
223-
log_debug("ir %d blokced.\n", msg->target);
224-
if (msg->target >= 6) log_error("invalid slider value %d in IR_BLOCKED.\n", msg->target);
225-
else chuni_ir_sensor_map |= 1 << msg->target;
226-
break;
227-
case IR_UNBLOCKED:
228-
log_debug("ir %d unblokced.\n", msg->target);
229-
if (msg->target >= 6) log_error("invalid slider value %d in IR_UNBLOCKED.\n", msg->target);
230-
else chuni_ir_sensor_map &= ~(1 << msg->target);
231-
break;
232-
default:
233-
log_error("bad message type %d.\n", msg->type);
203+
case COIN_INSERT:
204+
log_debug("adding coin.\n");
205+
chuni_coin_pending = true;
206+
break;
207+
case SLIDER_PRESS:
208+
log_debug("slider_press at %d.\n", msg->target);
209+
if (msg->target >= 16) log_error("invalid slider value %d in SLIDER_PRESS.\n", msg->target);
210+
else {
211+
chuni_sliders[(msg->target) * 2] = 128;
212+
chuni_sliders[(msg->target) * 2 + 1] = 128;
213+
}
214+
break;
215+
case SLIDER_RELEASE:
216+
log_debug("slider released on %d.\n", msg->target);
217+
if (msg->target >= 16) log_error("invalid slider value %d in SLIDER_RELEASE.\n", msg->target);
218+
else {
219+
chuni_sliders[(msg->target) * 2] = 0;
220+
chuni_sliders[(msg->target) * 2 + 1] = 0;
221+
}
222+
break;
223+
case CABINET_TEST:
224+
log_debug("setting cabinet_test.\n");
225+
chuni_test_pending = true;
226+
break;
227+
case CABINET_SERVICE:
228+
log_debug("setting cabinet_service.\n");
229+
chuni_service_pending = true;
230+
break;
231+
case IR_BLOCKED:
232+
log_debug("ir %d blokced.\n", msg->target);
233+
if (msg->target >= 6) log_error("invalid slider value %d in IR_BLOCKED.\n", msg->target);
234+
else chuni_io_ir(msg->target, true);
235+
break;
236+
case IR_UNBLOCKED:
237+
log_debug("ir %d unblokced.\n", msg->target);
238+
if (msg->target >= 6) log_error("invalid slider value %d in IR_UNBLOCKED.\n", msg->target);
239+
else chuni_io_ir(msg->target, false);
240+
break;
241+
default:
242+
log_error("bad message type %d.\n", msg->type);
234243
}
235244
}
236245
else if (len > 0) {
237246
log_warn("got invalid packet of length %d.\n", len);
238247
}
248+
}
249+
}
250+
251+
static unsigned int __stdcall chuni_io_slider_thread_proc(void* ctx) {
252+
chuni_io_slider_callback_t callback;
253+
254+
size_t i;
255+
256+
for (i = 0; i < _countof(chuni_sliders); i++) chuni_sliders[i] = 0;
239257

240-
callback(pressure);
258+
callback = (chuni_io_slider_callback_t) ctx;
259+
260+
while (!chuni_io_slider_stop_flag) {
261+
callback(chuni_sliders);
262+
Sleep(1);
241263
}
242264

243265
return 0;

ChuniVController/ChuniVController/MainWindow.xaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:local="clr-namespace:ChuniVController"
77
mc:Ignorable="d"
88
Loaded="OnLoad"
9-
Title="ChuniController" Topmost="True" ShowActivated="False" ResizeMode="NoResize" SizeToContent="WidthAndHeight" Opacity="{Binding Value, ElementName=OpacitySlider}" AllowsTransparency="True" WindowStyle="None" BorderBrush="Black" BorderThickness="2" MouseDown="DoMove">
9+
Title="ChuniController" Topmost="True" ShowActivated="False" ResizeMode="NoResize" SizeToContent="WidthAndHeight" Opacity="{Binding Value, ElementName=OpacitySlider}" AllowsTransparency="True" WindowStyle="None" BorderBrush="#ccc" BorderThickness="2" MouseDown="DoMove">
1010
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
1111

1212

@@ -46,18 +46,19 @@
4646
<TextBox x:Name="KeyHeight" PreviewTextInput="NumericValidation" HorizontalAlignment="Left" Text="200" Width="60" Height="17" />
4747
<Label Margin="3,0,0,0" Content="Sensor height:" />
4848
<TextBox x:Name="AirHeight" PreviewTextInput="NumericValidation" Text="30" Margin="3,0,0,0" Width="60" Height="17" />
49-
<Button Margin="3" Content="Apply" HorizontalAlignment="Left" Width="75" Click="DoApply" IsDefault="True" />
49+
<Button Margin="3" Content="Apply" HorizontalAlignment="Left" Width="50" Click="DoApply" IsDefault="True" />
5050
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
5151
<Label Content="Opacity" />
5252
<Slider Margin="3" x:Name="OpacitySlider" Width="100" Maximum="1" Minimum="0.1" Value="1" TickFrequency="0.01" SmallChange="0.01" LargeChange="0.1"/>
5353
<CheckBox Margin="3" Content="Allow Mouse" Checked="SetAllowMouse" Unchecked="UnsetAllowMouse" Height="17" />
5454
<CheckBox Margin="3" Content="Lock Window" Checked="SetLockWindow" Unchecked="UnsetLockWindow" Height="17" />
5555
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
56-
<Button Margin="3" Content="Coin" HorizontalAlignment="Left" Width="75" Click="DoCoin" />
57-
<Button Margin="3" Content="Service" HorizontalAlignment="Left" Width="75" Click="DoService" />
58-
<Button Margin="3" Content="Test" HorizontalAlignment="Left" Width="75" Click="DoTest" />
56+
<Button Margin="3" Content="Coin" HorizontalAlignment="Left" Width="50" Click="DoCoin" />
57+
<Button Margin="3" Content="Service" HorizontalAlignment="Left" Width="50" Click="DoService" />
58+
<Button Margin="3" Content="Test" HorizontalAlignment="Left" Width="50" Click="DoTest" />
59+
<Button Margin="3" Content="S+T" HorizontalAlignment="Left" Width="50" Click="DoServiceTest" />
5960
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
60-
<Button Margin="3" Content="Exit" Width="75" Click="DoExit" IsCancel="True" />
61+
<Button Margin="3" Content="Exit" Width="50" Click="DoExit" IsCancel="True" />
6162
</StackPanel>
6263

6364
</StackPanel>

ChuniVController/ChuniVController/MainWindow.xaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ private void DoTest(object sender, RoutedEventArgs e)
159159
cio.Send(msg);
160160
}
161161

162+
private void DoServiceTest(object sender, RoutedEventArgs e)
163+
{
164+
ChuniIoMessage msg = new ChuniIoMessage();
165+
msg.Source = (byte)ChuniMessageSources.Controller;
166+
msg.Type = (byte)ChuniMessageTypes.CabinetTest;
167+
cio.Send(msg);
168+
msg.Type = (byte)ChuniMessageTypes.CabinetService;
169+
cio.Send(msg);
170+
}
171+
162172
private void DoService(object sender, RoutedEventArgs e)
163173
{
164174
ChuniIoMessage msg = new ChuniIoMessage();

ChuniVController/ChuniVController/TouchPad.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
mc:Ignorable="d"
88
d:DesignHeight="450" d:DesignWidth="800">
99
<Grid>
10-
<Rectangle x:Name="LedStrip" Stroke="#fff" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Fill="#000" />
10+
<Rectangle x:Name="LedStrip" Stroke="#333" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Fill="#000" />
1111

1212
</Grid>
1313
</UserControl>

0 commit comments

Comments
 (0)