simulator: add USB serial input field
This commit is contained in:
parent
21ee056e26
commit
22b1ac0283
@ -25,7 +25,11 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "watch.h"
|
#include "watch.h"
|
||||||
|
#include "filesystem.h"
|
||||||
#include "movement.h"
|
#include "movement.h"
|
||||||
|
|
||||||
#ifndef MOVEMENT_FIRMWARE
|
#ifndef MOVEMENT_FIRMWARE
|
||||||
@ -423,6 +427,32 @@ bool app_loop(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if we are plugged into USB, handle the file browser tasks
|
||||||
|
if (watch_is_usb_enabled()) {
|
||||||
|
char line[256] = {0};
|
||||||
|
#if __EMSCRIPTEN__
|
||||||
|
// This is a terrible hack; ideally this should be handled deeper in the watch library.
|
||||||
|
// Alas, emscripten treats read() as something that should pop up an input box, so I
|
||||||
|
// wasn't able to implement this over there. I sense that this relates to read() being
|
||||||
|
// the wrong way to read data from USB (like we should be using fgets or something), but
|
||||||
|
// until I untangle that, this will have to do.
|
||||||
|
char *received_data = (char*)EM_ASM_INT({
|
||||||
|
var len = lengthBytesUTF8(tx) + 1;
|
||||||
|
var s = _malloc(len);
|
||||||
|
stringToUTF8(tx, s, len);
|
||||||
|
return s;
|
||||||
|
});
|
||||||
|
memcpy(line, received_data, min(255, strlen(received_data)));
|
||||||
|
free(received_data);
|
||||||
|
EM_ASM({
|
||||||
|
tx = "";
|
||||||
|
});
|
||||||
|
#else
|
||||||
|
read(0, line, 256);
|
||||||
|
#endif
|
||||||
|
if (strlen(line)) printf(line);
|
||||||
|
}
|
||||||
|
|
||||||
event.subsecond = 0;
|
event.subsecond = 0;
|
||||||
|
|
||||||
return can_sleep && (movement_state.light_ticks == -1) && !movement_state.is_buzzing;
|
return can_sleep && (movement_state.light_ticks == -1) && !movement_state.is_buzzing;
|
||||||
|
@ -41,3 +41,7 @@ void SYSTEM_Handler(void) {
|
|||||||
bool watch_is_buzzer_or_led_enabled(void){
|
bool watch_is_buzzer_or_led_enabled(void){
|
||||||
return hri_mclk_get_APBCMASK_TCC0_bit(MCLK);
|
return hri_mclk_get_APBCMASK_TCC0_bit(MCLK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool watch_is_usb_enabled(void) {
|
||||||
|
return USB->DEVICE.CTRLA.bit.ENABLE;
|
||||||
|
}
|
||||||
|
@ -76,6 +76,10 @@
|
|||||||
*/
|
*/
|
||||||
bool watch_is_buzzer_or_led_enabled(void);
|
bool watch_is_buzzer_or_led_enabled(void);
|
||||||
|
|
||||||
|
/** @brief Returns true if USB is enabled.
|
||||||
|
*/
|
||||||
|
bool watch_is_usb_enabled(void);
|
||||||
|
|
||||||
/** @brief Reads up to len bytes from the USB serial.
|
/** @brief Reads up to len bytes from the USB serial.
|
||||||
* @param file ignored, you can pass in 0
|
* @param file ignored, you can pass in 0
|
||||||
* @param ptr pointer to a buffer of at least len bytes
|
* @param ptr pointer to a buffer of at least len bytes
|
||||||
|
@ -323,6 +323,9 @@
|
|||||||
|
|
||||||
<button onclick="getLocation()">Set location register (will prompt for access)</button>
|
<button onclick="getLocation()">Set location register (will prompt for access)</button>
|
||||||
<br>
|
<br>
|
||||||
|
<input id="input" style="width: 500px"></input>
|
||||||
|
<button id="submit" onclick="sendText()">Send</button>
|
||||||
|
<br>
|
||||||
<textarea id="output" rows="8" style="width: 100%"></textarea>
|
<textarea id="output" rows="8" style="width: 100%"></textarea>
|
||||||
|
|
||||||
<script type='text/javascript'>
|
<script type='text/javascript'>
|
||||||
@ -365,10 +368,16 @@
|
|||||||
};
|
};
|
||||||
lat = 0;
|
lat = 0;
|
||||||
lon = 0;
|
lon = 0;
|
||||||
|
tx = "";
|
||||||
function updateLocation(location) {
|
function updateLocation(location) {
|
||||||
lat = Math.round(location.coords.latitude * 100);
|
lat = Math.round(location.coords.latitude * 100);
|
||||||
lon = Math.round(location.coords.longitude * 100);
|
lon = Math.round(location.coords.longitude * 100);
|
||||||
}
|
}
|
||||||
|
function sendText() {
|
||||||
|
var inputElement = document.getElementById('input');
|
||||||
|
tx = inputElement.value + "\n";
|
||||||
|
inputElement.value = "";
|
||||||
|
}
|
||||||
function showError(error) {
|
function showError(error) {
|
||||||
switch(error.code) {
|
switch(error.code) {
|
||||||
case error.PERMISSION_DENIED:
|
case error.PERMISSION_DENIED:
|
||||||
|
@ -3,3 +3,7 @@
|
|||||||
bool watch_is_buzzer_or_led_enabled(void) {
|
bool watch_is_buzzer_or_led_enabled(void) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool watch_is_usb_enabled(void) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user