Ei kuvausta

main.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/BurntSushi/toml"
  7. "github.com/naleek/gortty"
  8. "log"
  9. "os"
  10. "strconv"
  11. )
  12. const (
  13. verMajor = 0
  14. verMinor = 1
  15. verPatch = 0
  16. )
  17. type config struct {
  18. Baud float64 `toml:"baud"`
  19. StopBits float64 `toml:"stopbits"`
  20. DataBits int `toml:"databits"`
  21. SampleRate int `toml:"samplerate"`
  22. MarkFrequency int `toml:"markfrequency"`
  23. SpaceFrequency int `toml:"spacefrequency"`
  24. LocalEcho bool `toml:"localecho"`
  25. CodeSet string `toml:"codeset"`
  26. Registers map[string]string `toml:"registers"`
  27. }
  28. func main() {
  29. var configFile string
  30. flag.StringVar(&configFile, "c", "rttymodem.toml", "Configuration file path")
  31. showHelp := flag.Bool("h", false, "Show usage and exit")
  32. showVersion := flag.Bool("v", false, "Show version and exit")
  33. flag.Parse()
  34. if *showHelp {
  35. flag.PrintDefaults()
  36. os.Exit(0)
  37. }
  38. if *showVersion {
  39. fmt.Printf("%d.%d.%d\n", verMajor, verMinor, verPatch)
  40. os.Exit(0)
  41. }
  42. log.Printf("starting up with config from %s")
  43. conf := &config{}
  44. if _, err := toml.DecodeFile(configFile, &conf); err != nil {
  45. log.Fatal(err)
  46. }
  47. s := &gortty.ModemSettings{
  48. Baud: conf.Baud,
  49. StopBits: conf.StopBits,
  50. DataBits: conf.DataBits,
  51. SampleRate: conf.SampleRate,
  52. OneFreq: conf.MarkFrequency,
  53. ZeroFreq: conf.SpaceFrequency,
  54. }
  55. if conf.CodeSet == "" {
  56. conf.CodeSet = "USTTY"
  57. }
  58. c, err := gortty.LoadCodeSet(conf.CodeSet)
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. ctx, cancel := context.WithCancel(context.Background())
  63. m, err := NewModem(ctx, s, c)
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. m.SetLocalEcho(conf.LocalEcho)
  68. for i, r := range conf.Registers {
  69. n, err := strconv.Atoi(i)
  70. if err != nil {
  71. continue
  72. }
  73. m.SetRegister(n, r)
  74. }
  75. defer m.Close()
  76. m.run()
  77. cancel()
  78. }