Skip to content

Commit b3df128

Browse files
committed
!ci maybe dont change termios
1 parent 1aa70b0 commit b3df128

File tree

2 files changed

+61
-68
lines changed

2 files changed

+61
-68
lines changed

lib/pbio/drv/bluetooth/bluetooth_simulation.c

Lines changed: 11 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
#if PBDRV_CONFIG_BLUETOOTH_SIMULATION
77

88
#include <errno.h>
9-
#include <fcntl.h>
109
#include <signal.h>
1110
#include <stdio.h>
1211
#include <stdlib.h>
13-
#include <termios.h>
1412
#include <unistd.h>
1513

1614
#include "bluetooth.h"
@@ -131,17 +129,17 @@ pbio_error_t pbdrv_bluetooth_controller_initialize(pbio_os_state_t *state, pbio_
131129
#define STDIN_HEADER_SIZE (1)
132130

133131
static void pbdrv_bluetooth_simulation_tick_handler() {
134-
uint8_t buf[256 + STDIN_HEADER_SIZE];
135-
ssize_t r = read(STDIN_FILENO, buf + STDIN_HEADER_SIZE, sizeof(buf) - STDIN_HEADER_SIZE);
136-
137-
if (r > 0) {
138-
buf[0] = PBIO_PYBRICKS_COMMAND_WRITE_STDIN;
139-
pbdrv_bluetooth_receive_handler(buf, r + STDIN_HEADER_SIZE);
140-
} else if (r == 0) {
141-
// EOF.
142-
} else if (errno != EAGAIN && errno != EWOULDBLOCK) {
143-
// No data available.
144-
}
132+
// uint8_t buf[256 + STDIN_HEADER_SIZE];
133+
// ssize_t r = read(STDIN_FILENO, buf + STDIN_HEADER_SIZE, sizeof(buf) - STDIN_HEADER_SIZE);
134+
135+
// if (r > 0) {
136+
// buf[0] = PBIO_PYBRICKS_COMMAND_WRITE_STDIN;
137+
// pbdrv_bluetooth_receive_handler(buf, r + STDIN_HEADER_SIZE);
138+
// } else if (r == 0) {
139+
// // EOF.
140+
// } else if (errno != EAGAIN && errno != EWOULDBLOCK) {
141+
// // No data available.
142+
// }
145143
}
146144

147145
static pbio_os_process_t pbdrv_bluetooth_simulation_process;
@@ -170,59 +168,7 @@ static pbio_error_t pbdrv_bluetooth_simulation_process_thread(pbio_os_state_t *s
170168
return bluetooth_thread_err;
171169
}
172170

173-
static struct termios oldt;
174-
175-
static void restore_terminal_settings(void) {
176-
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
177-
}
178-
179-
static void handle_signal(int sig) {
180-
restore_terminal_settings();
181-
signal(sig, SIG_DFL);
182-
raise(sig);
183-
}
184-
185171
void pbdrv_bluetooth_init_hci(void) {
186-
struct termios newt;
187-
188-
// Save the original terminal settings
189-
if (tcgetattr(STDIN_FILENO, &oldt) != 0) {
190-
printf("DEBUG: Failed to get terminal attributes\n");
191-
return;
192-
}
193-
194-
// Register the cleanup function to restore terminal settings on exit
195-
atexit(restore_terminal_settings);
196-
signal(SIGINT, handle_signal);
197-
signal(SIGTERM, handle_signal);
198-
199-
newt = oldt;
200-
201-
// Get one char at a time instead of newline and disable CTRL+C for exit.
202-
newt.c_lflag &= ~(ICANON | ECHO | ISIG);
203-
204-
// MicroPython REPL expects \r for newline.
205-
newt.c_iflag |= INLCR;
206-
newt.c_iflag &= ~ICRNL;
207-
208-
if (tcsetattr(STDIN_FILENO, TCSANOW, &newt) != 0) {
209-
printf("Failed to set terminal attributes\n");
210-
return;
211-
}
212-
213-
// Set stdin non-blocking so we can service it in the runloop like on
214-
// embedded hubs.
215-
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
216-
if (flags == -1) {
217-
printf("Failed to get fcntl flags\n");
218-
return;
219-
}
220-
221-
if (fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) == -1) {
222-
printf("Failed to set non-blocking\n");
223-
return;
224-
}
225-
226172
bluetooth_thread_err = PBIO_ERROR_AGAIN;
227173
bluetooth_thread_state = 0;
228174
pbio_os_process_start(&pbdrv_bluetooth_simulation_process, pbdrv_bluetooth_simulation_process_thread, NULL);

lib/pbio/platform/sim_hub/platform.c

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
// SPDX-License-Identifier: MIT
22
// Copyright (c) 2025 The Pybricks Authors
33

4+
#include <errno.h>
5+
#include <fcntl.h>
6+
#include <signal.h>
47
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <termios.h>
10+
#include <unistd.h>
511

612
#include "../../drv/motor_driver/motor_driver_virtual_simulation.h"
713

@@ -133,9 +139,6 @@ const pbdrv_motor_driver_virtual_simulation_platform_data_t
133139
},
134140
};
135141

136-
extern uint8_t pbsys_hmi_native_program_buf[PBDRV_CONFIG_BLOCK_DEVICE_RAM_SIZE];
137-
extern uint32_t pbsys_hmi_native_program_size;
138-
139142
int main(int argc, char **argv) {
140143

141144
if (argc > 1) {
@@ -150,6 +153,8 @@ int main(int argc, char **argv) {
150153
}
151154

152155
// Read the multi-mpy file from pipe.
156+
extern uint8_t pbsys_hmi_native_program_buf[PBDRV_CONFIG_BLOCK_DEVICE_RAM_SIZE];
157+
extern uint32_t pbsys_hmi_native_program_size;
153158
pbsys_hmi_native_program_size = fread(pbsys_hmi_native_program_buf, 1, sizeof(pbsys_hmi_native_program_buf), pipe);
154159
pclose(pipe);
155160

@@ -159,6 +164,48 @@ int main(int argc, char **argv) {
159164
}
160165
}
161166

167+
// struct termios term_old, term_new;
168+
169+
// // Save the original terminal settings
170+
// if (tcgetattr(STDIN_FILENO, &term_old) != 0) {
171+
// printf("DEBUG: Failed to get terminal attributes\n");
172+
// return 0;
173+
// }
174+
// term_new = term_old;
175+
176+
// // Get one char at a time instead of newline and disable CTRL+C for exit.
177+
// term_new.c_lflag &= ~(ICANON | ECHO | ISIG);
178+
179+
// // MicroPython REPL expects \r for newline.
180+
// term_new.c_iflag |= INLCR;
181+
// term_new.c_iflag &= ~ICRNL;
182+
183+
// if (tcsetattr(STDIN_FILENO, TCSANOW, &term_new) != 0) {
184+
// printf("Failed to set terminal attributes\n");
185+
// return 0;
186+
// }
187+
188+
// // Set stdin non-blocking so we can service it in the runloop like on
189+
// // embedded hubs.
190+
// int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
191+
// if (flags == -1) {
192+
// printf("Failed to get fcntl flags\n");
193+
// return 0;
194+
// }
195+
196+
// if (fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) == -1) {
197+
// printf("Failed to set non-blocking\n");
198+
// return 0;
199+
// }
200+
162201
extern void _main(void);
163202
_main();
203+
204+
// // Restore terminal settings.
205+
// if (tcsetattr(STDIN_FILENO, TCSANOW, &term_old) != 0) {
206+
// printf("Failed to restore terminal attributes\n");
207+
// return 0;
208+
// }
209+
210+
return 0;
164211
}

0 commit comments

Comments
 (0)