{
  "format": "evn-blocks",
  "version": 3,
  "title": "UART: every block",
  "needs": "a jumper wire from TX to RX on Serial 1 (the board hears what it sends). Without the wire the program still runs and says nothing came back.",
  "description": "Every UART block once, on Serial 1 wired back to itself: sending lines and plain text, reading a line (and waiting for one that never comes), counting and reading everything that is waiting, and throwing it away. Everything it sends comes straight back, so each step shows its result.",
  "workspace": {
    "blocks": {
      "languageVersion": 0,
      "blocks": [
        {
          "type": "evn_setup_start",
          "x": 30,
          "y": 30,
          "next": {
            "block": {
              "type": "evn_uart_setup",
              "fields": {
                "PORT": "1",
                "BAUD": "115200"
              }
            }
          }
        },
        {
          "type": "evn_program_start",
          "x": 30,
          "y": 115,
          "next": {
            "block": {
              "type": "evn_uart_send",
              "fields": {
                "PORT": "1",
                "NEWLINE": true
              },
              "inputs": {
                "TEXT": {
                  "shadow": {
                    "type": "text",
                    "fields": {
                      "TEXT": "first line"
                    }
                  }
                }
              },
              "next": {
                "block": {
                  "type": "text_print",
                  "inputs": {
                    "TEXT": {
                      "block": {
                        "type": "text_join",
                        "inputs": {
                          "ADD0": {
                            "shadow": {
                              "type": "text",
                              "fields": {
                                "TEXT": "line 1: "
                              }
                            }
                          },
                          "ADD1": {
                            "block": {
                              "type": "evn_uart_line",
                              "fields": {
                                "PORT": "1"
                              },
                              "inputs": {
                                "TIMEOUT": {
                                  "shadow": {
                                    "type": "math_number",
                                    "fields": {
                                      "NUM": 1000
                                    }
                                  }
                                }
                              }
                            }
                          }
                        },
                        "extraState": {
                          "itemCount": 2
                        }
                      }
                    }
                  },
                  "next": {
                    "block": {
                      "type": "text_print",
                      "inputs": {
                        "TEXT": {
                          "block": {
                            "type": "text_join",
                            "inputs": {
                              "ADD0": {
                                "shadow": {
                                  "type": "text",
                                  "fields": {
                                    "TEXT": "line 2 (nothing more was sent: empty after 0.2 s): "
                                  }
                                }
                              },
                              "ADD1": {
                                "block": {
                                  "type": "evn_uart_line",
                                  "fields": {
                                    "PORT": "1"
                                  },
                                  "inputs": {
                                    "TIMEOUT": {
                                      "shadow": {
                                        "type": "math_number",
                                        "fields": {
                                          "NUM": 200
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            },
                            "extraState": {
                              "itemCount": 2
                            }
                          }
                        }
                      },
                      "next": {
                        "block": {
                          "type": "evn_comment",
                          "fields": {
                            "TEXT": "plain text, no newline: it waits as bytes until something reads them"
                          },
                          "next": {
                            "block": {
                              "type": "evn_uart_send",
                              "fields": {
                                "PORT": "1",
                                "NEWLINE": false
                              },
                              "inputs": {
                                "TEXT": {
                                  "shadow": {
                                    "type": "text",
                                    "fields": {
                                      "TEXT": "ABCDEFGH"
                                    }
                                  }
                                }
                              },
                              "next": {
                                "block": {
                                  "type": "evn_wait",
                                  "inputs": {
                                    "TIME": {
                                      "shadow": {
                                        "type": "math_number",
                                        "fields": {
                                          "NUM": 10
                                        }
                                      }
                                    }
                                  },
                                  "next": {
                                    "block": {
                                      "type": "text_print",
                                      "inputs": {
                                        "TEXT": {
                                          "block": {
                                            "type": "text_join",
                                            "inputs": {
                                              "ADD0": {
                                                "shadow": {
                                                  "type": "text",
                                                  "fields": {
                                                    "TEXT": "bytes waiting: "
                                                  }
                                                }
                                              },
                                              "ADD1": {
                                                "block": {
                                                  "type": "evn_uart_any",
                                                  "fields": {
                                                    "PORT": "1"
                                                  }
                                                }
                                              }
                                            },
                                            "extraState": {
                                              "itemCount": 2
                                            }
                                          }
                                        }
                                      },
                                      "next": {
                                        "block": {
                                          "type": "text_print",
                                          "inputs": {
                                            "TEXT": {
                                              "block": {
                                                "type": "text_join",
                                                "inputs": {
                                                  "ADD0": {
                                                    "shadow": {
                                                      "type": "text",
                                                      "fields": {
                                                        "TEXT": "everything waiting: "
                                                      }
                                                    }
                                                  },
                                                  "ADD1": {
                                                    "block": {
                                                      "type": "evn_uart_read",
                                                      "fields": {
                                                        "PORT": "1"
                                                      }
                                                    }
                                                  }
                                                },
                                                "extraState": {
                                                  "itemCount": 2
                                                }
                                              }
                                            }
                                          },
                                          "next": {
                                            "block": {
                                              "type": "evn_uart_send",
                                              "fields": {
                                                "PORT": "1",
                                                "NEWLINE": false
                                              },
                                              "inputs": {
                                                "TEXT": {
                                                  "shadow": {
                                                    "type": "text",
                                                    "fields": {
                                                      "TEXT": "stale data"
                                                    }
                                                  }
                                                }
                                              },
                                              "next": {
                                                "block": {
                                                  "type": "evn_wait",
                                                  "inputs": {
                                                    "TIME": {
                                                      "shadow": {
                                                        "type": "math_number",
                                                        "fields": {
                                                          "NUM": 10
                                                        }
                                                      }
                                                    }
                                                  },
                                                  "next": {
                                                    "block": {
                                                      "type": "evn_uart_clear",
                                                      "fields": {
                                                        "PORT": "1"
                                                      },
                                                      "next": {
                                                        "block": {
                                                          "type": "text_print",
                                                          "inputs": {
                                                            "TEXT": {
                                                              "block": {
                                                                "type": "text_join",
                                                                "inputs": {
                                                                  "ADD0": {
                                                                    "shadow": {
                                                                      "type": "text",
                                                                      "fields": {
                                                                        "TEXT": "after throwing it away: "
                                                                      }
                                                                    }
                                                                  },
                                                                  "ADD1": {
                                                                    "block": {
                                                                      "type": "evn_uart_any",
                                                                      "fields": {
                                                                        "PORT": "1"
                                                                      }
                                                                    }
                                                                  },
                                                                  "ADD2": {
                                                                    "shadow": {
                                                                      "type": "text",
                                                                      "fields": {
                                                                        "TEXT": " bytes waiting"
                                                                      }
                                                                    }
                                                                  }
                                                                },
                                                                "extraState": {
                                                                  "itemCount": 3
                                                                }
                                                              }
                                                            }
                                                          }
                                                        }
                                                      }
                                                    }
                                                  }
                                                }
                                              }
                                            }
                                          }
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}
