From 99117bef4a3b400476612567df5c3645cd60ca3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ho=CC=88gborg?= Date: Thu, 22 Oct 2015 23:05:09 +0200 Subject: [PATCH] Added different levels of debug output --- internal/pocsag/common.go | 4 +++- internal/pocsag/file.go | 7 ++++--- internal/pocsag/pocsag.go | 4 ++-- internal/pocsag/stream.go | 19 ++++++++++++++++--- internal/utils/util.go | 7 ++++++- main.go | 17 ++++++++++++----- 6 files changed, 43 insertions(+), 15 deletions(-) diff --git a/internal/pocsag/common.go b/internal/pocsag/common.go index 1bd56a9..0b71d63 100644 --- a/internal/pocsag/common.go +++ b/internal/pocsag/common.go @@ -6,6 +6,7 @@ import ( var ( DEBUG bool + LEVEL int ) var ( @@ -15,6 +16,7 @@ var ( ) // Tell the package to print debug data -func SetDebug(d bool) { +func SetDebug(d bool, verbosity int) { DEBUG = d + LEVEL = verbosity } diff --git a/internal/pocsag/file.go b/internal/pocsag/file.go index 9eaba9b..0fc3231 100644 --- a/internal/pocsag/file.go +++ b/internal/pocsag/file.go @@ -18,10 +18,11 @@ func ReadWav(path string) *bytes.Buffer { return nil } - samplecount := int(wavdata.Subchunk2Size / uint32(wavdata.BitsPerSample/8)) - seconds := float32(samplecount) / float32(wavdata.SampleRate) - if DEBUG { + + samplecount := int(wavdata.Subchunk2Size / uint32(wavdata.BitsPerSample/8)) + seconds := float32(samplecount) / float32(wavdata.SampleRate) + fmt.Printf("Samplerate: %d\n", wavdata.SampleRate) fmt.Printf("Samples: %d\n", samplecount) fmt.Printf("Seconds: %0.3f\n", seconds) } diff --git a/internal/pocsag/pocsag.go b/internal/pocsag/pocsag.go index 8b72fd8..4c91fea 100644 --- a/internal/pocsag/pocsag.go +++ b/internal/pocsag/pocsag.go @@ -46,7 +46,7 @@ func ParsePOCSAG(bits []datatypes.Bit, messagetype MessageType) { return } - if DEBUG { + if DEBUG && LEVEL > 1 { for i, batch := range batches { println("") println("Batch: ", i) @@ -96,7 +96,7 @@ func (p *POCSAG) ParseBatches(bits []datatypes.Bit) ([]*Batch, error) { batchbits := bits[a : a+POCSAG_BATCH_LEN+32] stream := utils.MSBBitsToBytes(batchbits, 8) - if DEBUG { + if DEBUG && LEVEL > 2 { out, err := os.Create(fmt.Sprintf("batches/batch-%d.bin", batchno)) if err != nil { return nil, err diff --git a/internal/pocsag/stream.go b/internal/pocsag/stream.go index 9e40f78..4dc0d3e 100644 --- a/internal/pocsag/stream.go +++ b/internal/pocsag/stream.go @@ -55,11 +55,14 @@ func (s *StreamReader) StartScan(bitstream chan []datatypes.Bit) { transmission := s.ReadTransmission(stream[start:]) bits := utils.StreamToBits(transmission, bitlength) + + if DEBUG && LEVEL > 2 { + utils.PrintBitstream(bits) + } + bitstream <- bits } - time.Sleep(3 * time.Millisecond) - } } @@ -81,6 +84,10 @@ func (s *StreamReader) ReadTransmission(beginning []int16) []int16 { stream = append(stream, bstr...) if s.isNoise(bstr) { + if DEBUG && LEVEL > 1 { + print("\n") + println("Transmission end (high noise level)") + } break } } @@ -223,7 +230,13 @@ func (s *StreamReader) isNoise(stream []int16) bool { prevsamp = sample } - return switches > 100 + switchrate := float32(switches) / float32(len(stream)) + + if DEBUG && LEVEL > 1 { + fmt.Printf("%0.0f ", switchrate*100) + } + + return switchrate > 0.15 } // bToInt16 converts bytes to int16 diff --git a/internal/utils/util.go b/internal/utils/util.go index 7402f9c..bf6bcb8 100644 --- a/internal/utils/util.go +++ b/internal/utils/util.go @@ -10,6 +10,7 @@ import ( var ( DEBUG bool + LEVEL int ) var ( @@ -18,8 +19,10 @@ var ( blue = color.New(color.FgBlue) ) -func SetDebug(d bool) { +// Tell the package to print debug data +func SetDebug(d bool, verbosity int) { DEBUG = d + LEVEL = verbosity } // StreamToBits converts samples to bits using the bitlength specified. @@ -182,6 +185,7 @@ func PrintStream(samples []int16) { for _, sample := range samples { PrintSample(sample) } + println("") } // PrintBitstream, used for debugging of streams @@ -189,6 +193,7 @@ func PrintBitstream(bits []datatypes.Bit) { for _, b := range bits { PrintSample(int16(b.Int())) } + println("") } // PrintSample, used for debugging of streams diff --git a/main.go b/main.go index 5a120cb..61e01f0 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ type Config struct { baud int debug bool messagetype pocsag.MessageType + verbosity int } func main() { @@ -39,7 +40,12 @@ func main() { cli.StringFlag{ Name: "input,i", Value: "", - Usage: "wav file or data dump with signed 16 bit ints", + Usage: "wav file with signed 16 bit ints, - for sttdin", + }, + cli.IntFlag{ + Name: "verbosity", + Value: 0, + Usage: "Verbosity level, 0 lowest level", }, cli.IntFlag{ Name: "baud,b", @@ -48,7 +54,7 @@ func main() { }, cli.BoolFlag{ Name: "debug", - Usage: "Debug mode", + Usage: "Output debug information", }, cli.StringFlag{ Name: "type,t", @@ -62,11 +68,12 @@ func main() { input: c.String("input"), baud: c.Int("baud"), debug: c.Bool("debug"), + verbosity: c.Int("verbosity"), messagetype: pocsag.MessageType(c.String("type")), } - utils.SetDebug(config.debug) - pocsag.SetDebug(config.debug) + utils.SetDebug(config.debug, config.verbosity) + pocsag.SetDebug(config.debug, config.verbosity) Run() @@ -79,7 +86,7 @@ func Run() { var source io.Reader - if config.input == "-" { + if config.input == "-" || config.input == "" { source = os.Stdin } else { // file reading source = pocsag.ReadWav(config.input)