|
| 1 | +#' Arduino Plotter |
| 2 | +#' |
| 3 | +#' @param names Labels for variables |
| 4 | +#' @param sep_fun A function that separates the inline string into different |
| 5 | +#' variables. By default, it is `ar_sep_comma`, which splits the string by |
| 6 | +#' comma. You can easily write your own function, which can even do additional |
| 7 | +#' calculation. |
| 8 | +#' @param reduce_freq T/F. It might be the case that plotly streaming API can't |
| 9 | +#' process frequency > 25 Hz (40ms delay time) or it might be the case that |
| 10 | +#' my computer doesn't have enough power. Anyway, I set this option here to |
| 11 | +#' add 40ms delay time to reduce the sampling frequency. |
| 12 | +#' @inheritParams ar_monitor |
| 13 | +#' |
| 14 | +#' @export |
| 15 | +ar_plotter <- function(fd, names = NULL, sep_fun = ar_sep_comma, |
| 16 | + reduce_freq = TRUE, flush_time = 0.05, |
| 17 | + eolchar = "\n", buf_max = 256, timeout = 5000) { |
| 18 | + shiny::runApp( |
| 19 | + ar_app(con = fd, names = names, sep_fun = sep_fun, |
| 20 | + flush_time = flush_time, reduce_freq = reduce_freq, |
| 21 | + eolchar = eolchar, buf_max = buf_max, timeout = timeout), |
| 22 | + launch.browser = rstudioapi::viewer |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +# con <- ar_init("/dev/cu.SLAB_USBtoUART", baud = 57600) |
| 27 | + |
| 28 | +ar_app <- function(con, names = NULL, sep_fun = ar_sep_comma, |
| 29 | + flush_time = 0.05, reduce_freq = TRUE, |
| 30 | + eolchar = "\n", buf_max = 256, timeout = 5000) { |
| 31 | + message("Flushing Port...") |
| 32 | + ar_flush_hard(con, flush_time) |
| 33 | + first_dot <- ar_read(con, eolchar, buf_max, timeout) |
| 34 | + if (first_dot == "") { |
| 35 | + stop("Your connection is probably dead. Please use ar_init and start", |
| 36 | + " a new connection") |
| 37 | + } |
| 38 | + first_dot <- sep_fun(first_dot) |
| 39 | + signal_vars <- seq(length(first_dot)) |
| 40 | + |
| 41 | + if (is.null(names)) { |
| 42 | + names(signal_vars) <- paste("Var", signal_vars) |
| 43 | + names(first_dot) <- paste("Var", signal_vars) |
| 44 | + } else { |
| 45 | + if (length(names) != length(first_dot)) { |
| 46 | + stop( |
| 47 | + "The amount of names provided is different from the amount of values." |
| 48 | + ) |
| 49 | + } |
| 50 | + names(signal_vars) <- names |
| 51 | + names(first_dot) <- names |
| 52 | + } |
| 53 | + |
| 54 | + save_file_default <- glue("arduino_{format(Sys.time(), '%Y%m%d_%H%M%S')}.csv") |
| 55 | + |
| 56 | + ui <- fluidPage( |
| 57 | + br(), |
| 58 | + actionButton("power", label = NULL, icon = icon("power-off", "text-danger"), |
| 59 | + width = NULL, |
| 60 | + style = "border-radius: 25px; position: absolute; top: 15px; right: 15px;z-index: 20;"), |
| 61 | + inline_widget(actionButton("start", icon = icon("play"), |
| 62 | + label = NULL, width = "100%"), "50px"), |
| 63 | + inline_widget(actionButton("reset", icon = icon("undo"), |
| 64 | + label = NULL, width = "100%"), "50px"), |
| 65 | + inline_widget(h5("Vars:"), "35px"), |
| 66 | + inline_widget(selectInput( |
| 67 | + "y_var", label = NULL, choices = signal_vars, |
| 68 | + selected = signal_vars, multiple = T |
| 69 | + ), "calc(95% - 180px);z-index: 15;"), |
| 70 | + plotlyOutput("plot", height = "250px"), |
| 71 | + inline_widget(checkboxInput("save", strong("Save to file?")), "30%"), |
| 72 | + inline_widget(textInput("file", NULL, save_file_default), "65%") |
| 73 | + ) |
| 74 | + |
| 75 | + server <- function(input, output, session) { |
| 76 | + rv <- reactiveValues() |
| 77 | + rv$state <- 0 |
| 78 | + |
| 79 | + # first_xy <- separateXY(first_dot) |
| 80 | + |
| 81 | + output$plot <- renderPlotly({ |
| 82 | + req(input$y_var) |
| 83 | + input$reset |
| 84 | + p <- plot_ly(type = 'scatter', mode = 'lines', line = list(width = 3)) |
| 85 | + for (y_i in sort(as.integer(input$y_var))) { |
| 86 | + p <- add_trace(p, y = first_dot[y_i], name = names(first_dot[y_i])) |
| 87 | + } |
| 88 | + return(p) |
| 89 | + }) |
| 90 | + |
| 91 | + observeEvent(input$start, { |
| 92 | + rv$state <- 1 - rv$state |
| 93 | + start_icon <- icon(c("play", "pause")[rv$state + 1]) |
| 94 | + updateActionButton(session, "start", icon = start_icon) |
| 95 | + ar_flush_hard(con, flush_time) |
| 96 | + }) |
| 97 | + |
| 98 | + observeEvent(input$reset, { |
| 99 | + rv$state <- 0 |
| 100 | + updateActionButton(session, "start", icon = icon("play")) |
| 101 | + ar_flush_hard(con, flush_time) |
| 102 | + }) |
| 103 | + |
| 104 | + observeEvent(input$save, { |
| 105 | + if (!file.exists(input$file)) { |
| 106 | + file.create(input$file) |
| 107 | + cat(csv_newline(names(first_dot)), file = input$file) |
| 108 | + } |
| 109 | + }, ignoreInit = TRUE) |
| 110 | + |
| 111 | + observe({ |
| 112 | + invalidateLater(1) |
| 113 | + if (rv$state) { |
| 114 | + ar_flush_hard(con, 0.04, FALSE) |
| 115 | + realtime <- sep_fun(ar_read(con, eolchar, buf_max, timeout)) |
| 116 | + if (input$save) { |
| 117 | + cat(csv_newline(realtime), file = input$file, append = TRUE) |
| 118 | + } |
| 119 | + realtime_y <- lapply(realtime[sort(as.integer(input$y_var))], list) |
| 120 | + realtime_list <- list(y = realtime_y) |
| 121 | + to_traces <- as.list(seq(length(input$y_var))) |
| 122 | + plotlyProxy("plot", session) %>% |
| 123 | + plotlyProxyInvoke("extendTraces", realtime_list, to_traces) |
| 124 | + } |
| 125 | + }) |
| 126 | + |
| 127 | + observeEvent(input$power, { |
| 128 | + invisible(stopApp()) |
| 129 | + }) |
| 130 | + } |
| 131 | + |
| 132 | + shinyApp(ui, server) |
| 133 | +} |
| 134 | + |
0 commit comments