forked from chilipeppr/serial-port-json-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufferflow_tinyg_v2.go
792 lines (650 loc) · 26.7 KB
/
bufferflow_tinyg_v2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
package main
import (
"encoding/json"
"log"
"regexp"
//"strconv"
"strings"
"sync"
//"time"
//"errors"
"fmt"
"runtime/debug"
"time"
)
type BufferflowTinygV2 struct {
Name string
Port string
Paused bool
ManualPaused bool // indicates user hard paused the buffer on their own, i.e. not from flow control
//StopSending int
//StartSending int
//PauseOnEachSend time.Duration // Amount of milliseconds to pause on each send to give TinyG time to send us a qr report
sem chan int // semaphore to wait on until given release
LatestData string // this holds the latest data across multiple serial reads so we can analyze it for qr responses
//BypassMode bool // this means don't actually watch for qr responses until we know tinyg is in qr response mode
//wg sync.WaitGroup
quit chan int
parent_serport *serport
re *regexp.Regexp
reNewLine *regexp.Regexp
reQrOff *regexp.Regexp
reQrOn *regexp.Regexp
reNoResponse *regexp.Regexp
reComment *regexp.Regexp
reComment2 *regexp.Regexp
rePutBackInJsonMode *regexp.Regexp
reJsonVerbositySetTo0 *regexp.Regexp
reCrLfSetTo1 *regexp.Regexp
reRxResponse *regexp.Regexp
reFlowChar *regexp.Regexp
// slot counter approach
reSlotDone *regexp.Regexp // the r:null cmd to look for back from tinyg indicating line processed
//reCmdsWithNoRResponse *regexp.Regexp // since we're using slot approach, we expect an r:{} response, but some commands don't give that so just don't expect it
//SlotMax int // queue into tinyg using slot approach
//SlotCtr int // queue into tinyg using slot approach
//lock *sync.Mutex // use a lock/unlock instead of sem chan int
// do buffer size counting approach instead
BufferMax int
//BufferSize int
//BufferSizeArray []int
//BufferCmdArray []string
q *Queue
// use thread locking for b.Paused
lock *sync.Mutex
// use thread locking for b.ManualPaused
manualLock *sync.Mutex
// use more thread locking for b.semLock
semLock *sync.Mutex
// additional lock for BlockUntilReady vs OnIncomingData method
inOutLock *sync.Mutex
}
func (b *BufferflowTinygV2) Init() {
b.Paused = false
b.ManualPaused = false
b.lock = &sync.Mutex{}
b.manualLock = &sync.Mutex{}
b.semLock = &sync.Mutex{}
b.inOutLock = &sync.Mutex{}
// when we get an r:{} response we know a line was processed
b.reSlotDone, _ = regexp.Compile("{\"r\":{")
// when we see the response to an rx query so we know how many chars
// are sitting in the serial buffer
b.reRxResponse, _ = regexp.Compile("{\"rx\":")
b.reFlowChar, _ = regexp.Compile("\u0011|\u0013")
//b.reCmdsWithNoRResponse, _ = regexp.Compile("[!~%]")
/* Start Buffer Size Approach Items */
b.BufferMax = 200 //max buffer size 254 bytes available
//b.BufferSize = 0 //initialize buffer at zero bytes
b.q = NewQueue()
// make buffered channel big enough we won't overflow it
// meaning we get told b.sem on incoming data, so at most this could
// be the size of 1 character and the TinyG only allows 255, so just
// go high to make sure it's high enough to never block
// buffered
b.sem = make(chan int, 1000)
// the regular expression to find the qr value
// this regexp will find qr when in json mode or non-json mode on tinyg
b.re, _ = regexp.Compile("\"{0,1}qr\"{0,1}:(\\d+)")
// we split the incoming data on newline using this regexp
// tinyg seems to only send \n but look for \n\r optionally just in case
b.reNewLine, _ = regexp.Compile("\\r{0,1}\\n")
// Look for qr's being turned off by user to auto turn-on BypassMode
b.reQrOn, _ = regexp.Compile("{\"qv\":[1-9]}|\\[qv\\]\\s+queue report verbosity\\s+[1-9]")
// this regexp catches !, ~, %, \n, $ by itself, or $$ by itself and indicates
// no r:{} response will come back so don't expect it
b.reNoResponse, _ = regexp.Compile("^[!~%\n$?]")
// if we get a cmd with a $ at the start or a ? at start, append
// a new command that will put tinyg back in json mode
b.rePutBackInJsonMode, _ = regexp.Compile("^[$?]")
// see if they tried to turn off json verbosity, which will break things
b.reJsonVerbositySetTo0, _ = regexp.Compile("(\\$jv\\=0|\\{\"jv\"\\:0\\})")
// see if they tried to turn on CRLF, which will break things
b.reCrLfSetTo1, _ = regexp.Compile("(\\$ec\\=1|\\{\"ec\"\\:1\\})")
b.reComment, _ = regexp.Compile("\\(.*?\\)")
b.reComment2, _ = regexp.Compile(";.*")
//initialize query loop
//b.rxQueryLoop(b.parent_serport)
go func() {
time.Sleep(1500 * time.Millisecond)
//spWriteJson("sendjson {\"P\":\"" + b.parent_serport.portConf.Name + "\",\"Data\":[{\"D\":\"" + "{\\\"rxm\\\":0}\\n\", \"Id\":\"internalInit0\", \"Pause\":50}]}")
// get feed rate override from get go
spFeedRateOverride("fro " + b.parent_serport.portConf.Name + "\n")
log.Println("Just forcibly asked for the fro status")
}()
}
func (b *BufferflowTinygV2) RewriteSerialData(cmd string, id string) string {
return ""
}
// Serial buffer size approach
func (b *BufferflowTinygV2) BlockUntilReady(cmd string, id string) (bool, bool, string) {
// Lock for this ENTIRE method
b.inOutLock.Lock()
log.Printf("BlockUntilReady() Start\n")
log.Printf("\tid:%v, txt:%v\n", id, strings.Replace(cmd, "\n", "\\n", -1))
// keep track of whether we need to unlock at end of method or not
// i.e. we unlock if we have to pause, thus we won't have to doubly unlock at end of method
isNeedToUnlock := true
// if we rewrite the gcode
newCmd := ""
// Only increment if cmd is something we'll get an r:{} response to
isReturnsNoResponse := b.SeeIfSpecificCommandsReturnNoResponse(cmd)
if isReturnsNoResponse == false {
// Normal Command - i.e. it returns response
newCmd = b.RewriteSerialData(cmd, id)
b.q.Push(cmd, id)
log.Printf("\tWe have cmd that returns response. cmd:%v\n", strings.Replace(cmd, "\n", "\\n", -1))
} else {
// this is sketchy. could we overrun the buffer by not counting !~%\n
// so to give extra room don't actually allow full serial buffer to
// be used in b.BufferMax
log.Printf("\tWe have cmd that returns no response, so not incrementing buffer size for cmd:%v\n", strings.Replace(cmd, "\n", "\\n", -1))
}
log.Printf("\tLen of cmd: %v, new len of local queue:%v, debugStr:%v\n", len(cmd), b.q.LenOfCmds(), b.q.DebugStr())
if b.q.LenOfCmds() >= b.BufferMax {
b.SetPaused(true, 0) // b.Paused = true
log.Printf("\tIt looks like the local queue is over the allowed size, so we are going to pause. Then when some incoming responses come in a check will occur to see if there's room to send this command. Pausing...")
}
if b.GetPaused() {
//log.Println("It appears we are being asked to pause, so we will wait on b.sem")
// We are being asked to pause our sending of commands
// clear all b.sem signals so when we block below, we truly block
b.ClearOutSemaphore()
// since we need other code to run while we're blocking, we better release the packet ctr lock
b.inOutLock.Unlock()
// since we already unlocked this thread, note it so we don't doubly unlock
isNeedToUnlock = false
log.Println("\tBlocking on b.sem until told from OnIncomingData to go")
unblockType, ok := <-b.sem // will block until told from OnIncomingData to go
log.Printf("\tDone blocking cuz got b.sem semaphore release. ok:%v, unblockType:%v\n", ok, unblockType)
// we get an unblockType of 1 for normal unblocks
// we get an unblockType of 2 when we're being asked to wipe the buffer, i.e. from a % cmd
if unblockType == 2 {
log.Println("\tThis was an unblock of type 2, which means we're being asked to wipe internal buffer. so return false.")
// returning false asks the calling method to wipe the serial send once
// this function returns
return false, false, ""
}
}
// we will get here when we're done blocking and if we weren't cancelled
// if this cmd returns no response, we need to generate a fake "Complete"
// so do it now
willHandleCompleteResponse := true
if isReturnsNoResponse == true {
willHandleCompleteResponse = false
}
//log.Printf("BlockUntilReady(cmd:%v, id:%v) end\n", cmd, id)
log.Printf("BlockUntilReady() End\n")
// we are done with using the packet ctr data, so can unlock
if isNeedToUnlock {
b.inOutLock.Unlock()
}
return true, willHandleCompleteResponse, newCmd
}
type LocalRemoteBuffers struct {
LocalBufSize int `json:"Lbs"`
CmdCnt int `json:"-"`
Cmds string `json:"-"`
}
type BufferStats struct {
P string
D string
}
// Serial buffer size approach
func (b *BufferflowTinygV2) OnIncomingData(data string) {
// we are given data piecemeal, not a line at a time, so add to a string
b.LatestData += data
arrLines := b.reNewLine.Split(b.LatestData, -1)
if len(arrLines) > 1 {
// that means we found a newline and have 2 or greater array values
// so we need to analyze our arrLines[] lines but keep last line
// for next trip into OnIncomingData
//log.Printf("We have data lines to analyze. numLines:%v\n", len(arrLines))
} else {
// we don't have a newline yet, so just exit and move on
// we don't have to reset b.LatestData because we ended up
// without any newlines so maybe we will next time into this method
//log.Printf("Did not find newline yet, so nothing to analyze\n")
return
}
// Lock the packet ctr at start and then end
b.inOutLock.Lock()
defer b.inOutLock.Unlock()
log.Printf("OnIncomingData() Start.")
// if we made it here we have lines to analyze
// so analyze all of them except the last line
for _, element := range arrLines[:len(arrLines)-1] {
log.Printf("< %v", element)
// check for r:{} response indicating a gcode line has been processed
if b.reSlotDone.MatchString(element) {
// ok, a line has been processed
if b.q.Len() > 0 {
doneCmd, id := b.q.Poll()
//doneCmd := b.BufferCmdArray[0]
// Send cmd:"Complete" back
m := DataCmdComplete{"Complete", id, b.Port, b.q.LenOfCmds(), doneCmd}
bm, err := json.Marshal(m)
if err == nil {
h.broadcastSys <- bm
}
// For debug
mLocalQueue := LocalRemoteBuffers{}
mLocalQueue.LocalBufSize = b.q.LenOfCmds()
mLocalQueue.CmdCnt = b.q.Len()
//mLocalQueue.Cmds = b.q.DebugStr()
bmLocalQueue, errlq := json.Marshal(mLocalQueue)
if errlq != nil {
log.Printf("\tCould not marshal localBufSize debug stmt:%v", mLocalQueue)
} else {
log.Printf("\tHere is our localBufSize debug stmt:%v", string(bmLocalQueue))
}
// we need to send a buffer size update
mbs := BufferStats{}
mbs.P = b.Port
//mbs.D = "{\"LocalBufSize\":" + strconv.Itoa(b.q.LenOfCmds()) + ",\"Cmds\":\"" + b.q.DebugStr() + "\"}\n"
mbs.D = string(bmLocalQueue) + "\n"
bmbs, err2 := json.Marshal(mbs)
if err2 == nil {
h.broadcastSys <- bmbs
//log.Printf("\tFinal buf size update:%v", string(bmbs))
}
//log.Printf("\tLocal buffer decreased to itemCnt:%v, lenOfBuf:%v\n", b.q.Len(), b.q.LenOfCmds())
if *bufFlowDebugType == "on" {
// let's report on how our buffer is doing
// we need to unmarshall this r:{} response
// do some initial cleanup to remove \u0011 or \u0013
// that we're getting likely for flow control that is
// throwing off the unmarshal call
element2 := b.reFlowChar.ReplaceAllString(element, "")
// unmarshall r:{} json
var rm RespMsg
err2 := json.Unmarshal([]byte(element2), &rm)
bfc := BufFlowCmd{}
bfc.Cmd = "BufFlowDebug"
bfc.Gcode = doneCmd
bfc.Resp = element
bfc.Id = id
bfc.HowMuchWeThinkWeShouldRemove = len(doneCmd)
bfc.IsErr = false
bfc.IsMatchOnBufDecreaseCnt = false
if err2 != nil {
log.Printf("Problem decoding json on r:{} response. giving up. json:%v, err:%v\n", element, err2)
spErr(fmt.Sprintf("Problem decoding json on r:{} response. giving up. json:%v, err:%v", element, err2))
bfc.IsErr = true
bfc.Err = "Problem unmarshalling json which likely means we had dropped characters on the serial buffer. Giving up."
//return
} else {
log.Printf("RespMsg:%v\n", rm)
if len(rm.F) > 2 {
bfc.HowMuchTinyTellsUsToRemove = rm.F[2]
if rm.F[2] == len(doneCmd) {
bfc.IsMatchOnBufDecreaseCnt = true
} else {
bfc.IsMatchOnBufDecreaseCnt = false
}
}
}
bfcm, err3 := json.Marshal(bfc)
if err3 == nil {
h.broadcastSys <- bfcm
} else {
log.Fatal(fmt.Sprintf("Could not marshal the buffer flow debug json response. We should never get here and since we did we are exiting so you can debug me. Giving up. json:%v, err:%v", element, err3))
}
// also check for rx value being returned so we can decide
// if our serial buffer value is the same as what TinyG thinks
// it should be.
if b.reRxResponse.MatchString(element) {
var rrxm RespRxMsg
err4 := json.Unmarshal([]byte(element2), &rrxm)
bfrx := BufFlowRx{}
bfrx.Cmd = "BufFlowRxDebug"
bfrx.Resp = element
bfrx.IsErr = false
bfrx.IsMatchOnTotalBuf = false
bfrx.TotalInBufPerSpjs = b.q.LenOfCmds()
if err4 != nil {
bfrx.IsErr = true
bfrx.Err = "Could not unmarshall the r:rx json string? huh?"
} else {
bfrx.TotalInBufPerTinyG = 254 - rrxm.R.Rx
// do they match?
if bfrx.TotalInBufPerSpjs == bfrx.TotalInBufPerTinyG {
bfrx.IsMatchOnTotalBuf = true
} else {
bfrx.IsMatchOnTotalBuf = false
}
}
bfrxm, err5 := json.Marshal(bfrx)
if err5 == nil {
h.broadcastSys <- bfrxm
} else {
log.Fatal(fmt.Sprintf("Could not marshal the buffer flow debug RX json response. We should never get here and since we did we are exiting so you can debug me. Giving up. json:%v, err:%v", element, err5))
}
}
}
} else {
log.Printf("\tWe should RARELY get here cuz we should have a command in the queue to dequeue when we get the r:{} response. If you see this debug stmt this is one of those few instances where TinyG sent us a r:{} not in response to a command we sent.")
}
//if b.BufferSize < b.BufferMax {
// We should have our queue dequeued so lets see if we are now below
// the allowed buffer room. If so go ahead and release the block on send
// This if stmt still may not be true here because we could have had a tiny
// cmd just get completed like "G0 X0" and the next cmd is long like "G2 X23.32342 Y23.535355 Z1.04345 I0.243242 J-0.232455"
// So we'll have to wait until the next time in here for this test to pass
if b.q.LenOfCmds() < b.BufferMax {
//log.Printf("tinyg just completed a line of gcode and there is room in buffer so setPaused(false)\n")
// if we are paused, tell us to unpause cuz we have clean buffer room now
if b.GetPaused() {
// we are paused, but we can't just go unpause ourself, because we may
// be manually paused. this means we have to do a double-check here
// and not just go unpausing ourself just cuz we think there's room in the buffer.
// this is because we could have just sent a ! to the tinyg. we may still
// get back some random r:{} after the ! was sent, and that would mean we think
// we can go sending more data, but really we can't cuz we were HARD Manually paused
if b.GetManualPaused() == false {
// we are not in a manual pause state, that means we can go ahead
// and unpause ourselves
b.SetPaused(false, 1) //set paused to false first, then release the hold on the buffer
} else {
log.Println("\tWe just got incoming r:{} so we could unpause, but since manual paused we will ignore until next time a r:{} comes in to unpause")
}
}
}
}
// handle communication back to client
// for base serial data (this is not the cmd:"Write" or cmd:"Complete")
m := DataPerLine{b.Port, element + "\n"}
bm, err := json.Marshal(m)
if err == nil {
h.broadcastSys <- bm
}
} // for loop
// now wipe the LatestData to only have the last line that we did not analyze
// because we didn't know/think that was a full command yet
b.LatestData = arrLines[len(arrLines)-1]
// we are losing incoming serial data because of garbageCollection()
// doing a "stop the world" and all this data queues up back on the
// tinyg and we miss stuff coming in, which gets our serial counter off
// and then causes stalling, so we're going to attempt to force garbageCollection
// each time we get data so that we don't have pauses as long as we were having
if *gcType == "max" {
debug.FreeOSMemory()
}
//time.Sleep(3000 * time.Millisecond)
log.Printf("OnIncomingData() end.\n")
}
// Clean out b.sem so it can truly block
func (b *BufferflowTinygV2) ClearOutSemaphore() {
ctr := 0
keepLooping := true
for keepLooping {
select {
case _, ok := <-b.sem: // case d, ok :=
//log.Printf("Consuming b.sem queue to clear it before we block. ok:%v, d:%v\n", ok, string(d))
ctr++
if ok == false {
keepLooping = false
}
default:
keepLooping = false
//log.Println("Hit default in select clause")
}
}
//log.Printf("Done consuming b.sem queue so we're good to block on it now. ctr:%v\n", ctr)
// ok, all b.sem signals are now consumed into la-la land
}
// break commands into individual commands
// so, for example, break on newlines to separate commands
// or, in the case of ~% break those onto separate commands
func (b *BufferflowTinygV2) BreakApartCommands(cmd string) []string {
// add newline after !~%
reSingle := regexp.MustCompile("([!~%])")
cmd = reSingle.ReplaceAllString(cmd, "$1\n")
cmds := strings.Split(cmd, "\n")
//log.Printf("Len of cmds array after split:%v\n", len(cmds))
//json, _ := json.Marshal(cmds)
//log.Printf("cmds after split:%v\n", json)
finalCmds := []string{}
if len(cmds) == 1 {
item := cmds[0]
// just put cmd back in with newline
if reSingle.MatchString(item) {
//log.Printf("len1. Added cmd back. Not re-adding newline cuz artificially added one earlier. item:'%v'\n", item)
finalCmds = append(finalCmds, item)
} else {
item = item + "\n"
//log.Printf("len1. Re-adding item to finalCmds with newline:'%v'\n", item)
finalCmds = append(finalCmds, item)
}
} else {
for index, item := range cmds {
// since more than 1 cmd, loop thru
if reSingle.MatchString(item) {
//log.Printf("Added cmd back. Not re-adding newline cuz artificially added one earlier. item:'%v'\n", item)
finalCmds = append(finalCmds, item)
} else {
// should we add back our newline? do this if there are elements after us
if index < len(cmds)-1 {
// there are cmds after me, so add newline
//log.Printf("Re-adding newline to item:%v\n", item)
s := item + "\n"
finalCmds = append(finalCmds, s)
//log.Printf("Added cmd back with newline. New cmd item:'%v'\n", s)
} else {
//log.Printf("Skipping adding cmd back cuz just empty newline. item:'%v'\n", item)
//log.Printf("Re-adding item to finalCmds without adding newline:%v\n", item)
//finalCmds = append(finalCmds, item)
}
}
}
}
// loop 1 more time to do some rewriting
newFinalCmds := []string{}
for _, item := range finalCmds {
// remove comments
//item = b.reComment.ReplaceAllString(item, "")
//item = b.reComment2.ReplaceAllString(item, "")
// see if we need to override a cmd to not screw stuff up for us
// if user sets json verbosity to 0, reset it back
if match := b.reJsonVerbositySetTo0.MatchString(item); match {
// they turned off json verbosity, shame on them, override it
// by setting back
newFinalCmds = append(newFinalCmds, "{\"jv\":1}\n")
} else if match := b.reCrLfSetTo1.MatchString(item); match {
// they turned off json verbosity, shame on them, override it
// by setting back
newFinalCmds = append(newFinalCmds, "{\"ec\":0}\n")
} else {
// just put the command back into the array without modifying
newFinalCmds = append(newFinalCmds, item)
}
// see if need to put back in json mode
if match := b.rePutBackInJsonMode.MatchString(item); match {
// yes, this cmd needs to have us put tinyg back in json mode
newFinalCmds = append(newFinalCmds, "{\"ej\":\"\"}\n")
}
}
//log.Printf("Final array of cmds after BreakApartCommands(). newFinalCmds:%v\n", newFinalCmds)
return newFinalCmds
}
func (b *BufferflowTinygV2) Pause() {
// Since we're tweaking b.Paused lock all threads
//b.lock.Lock()
//defer b.lock.Unlock()
b.SetPaused(true, 0) //b.Paused = true
//b.BypassMode = false // turn off bypassmode in case it's on
//log.Println("Paused buffer on next BlockUntilReady() call")
log.Println("Paused buffer")
}
func (b *BufferflowTinygV2) Unpause() {
// Since we're tweaking b.Paused lock all threads
//b.lock.Lock()
//defer b.lock.Unlock()
b.SetPaused(false, 1) //b.Paused = false
//log.Println("Unpause(), so we will send signal of 1 to b.sem to unpause the BlockUntilReady() thread")
// do this as go-routine so we don't block on the b.sem <- 1 write
/*
go func() {
log.Printf("Unpause() Semaphore goroutine created.\n")
// this is an unbuffered channel, so we will
// block here which is why this is a goroutine
// sending a 1 asks BlockUntilReady() to move forward
b.sem <- 1
// when we get here that means a BlockUntilReady()
// method consumed the signal, meaning we unblocked them
// which is good because they're allowed to start sending
// again
defer func() {
log.Printf("Unpause() Semaphore just got consumed by the BlockUntilReady()\n")
}()
}()
*/
log.Println("Unpaused buffer") // inside BlockUntilReady() call")
}
func (b *BufferflowTinygV2) SeeIfSpecificCommandsShouldSkipBuffer(cmd string) bool {
// remove comments
cmd = b.reComment.ReplaceAllString(cmd, "")
cmd = b.reComment2.ReplaceAllString(cmd, "")
if match, _ := regexp.MatchString("[!~%]", cmd); match {
log.Printf("Found cmd that should skip buffer. cmd:%v\n", cmd)
return true
}
return false
}
func (b *BufferflowTinygV2) SeeIfSpecificCommandsShouldPauseBuffer(cmd string) bool {
// remove comments
cmd = b.reComment.ReplaceAllString(cmd, "")
cmd = b.reComment2.ReplaceAllString(cmd, "")
if match, _ := regexp.MatchString("[!]", cmd); match {
//log.Printf("Found cmd that should pause buffer. cmd:%v\n", cmd)
return true
}
return false
}
func (b *BufferflowTinygV2) SeeIfSpecificCommandsShouldUnpauseBuffer(cmd string) bool {
// remove comments
cmd = b.reComment.ReplaceAllString(cmd, "")
cmd = b.reComment2.ReplaceAllString(cmd, "")
if match, _ := regexp.MatchString("[~%]", cmd); match {
//log.Printf("Found cmd that should unpause buffer. cmd:%v\n", cmd)
return true
}
return false
}
func (b *BufferflowTinygV2) SeeIfSpecificCommandsShouldWipeBuffer(cmd string) bool {
// remove comments
cmd = b.reComment.ReplaceAllString(cmd, "")
cmd = b.reComment2.ReplaceAllString(cmd, "")
if match, _ := regexp.MatchString("[%]", cmd); match {
//log.Printf("Found cmd that should wipe out and reset buffer. cmd:%v\n", cmd)
// Since we're tweaking b.Paused lock all threads
//b.lock.Lock()
//defer b.lock.Unlock()
//b.BufferSize = 0
//b.BufferSizeArray = nil
//b.BufferCmdArray = nil
//b.q.Delete()
return true
}
return false
}
func (b *BufferflowTinygV2) SeeIfSpecificCommandsReturnNoResponse(cmd string) bool {
// remove comments
//cmd = b.reComment.ReplaceAllString(cmd, "")
//cmd = b.reComment2.ReplaceAllString(cmd, "")
//log.Printf("Checking cmd:%v for no response?", cmd)
if match := b.reNoResponse.MatchString(cmd); match {
//log.Printf("Found cmd that does not get a response from TinyG. cmd:%v\n", cmd)
return true
}
return false
}
// This is called if user wiped entire buffer of gcode commands queued up
// which is up to 25,000 of them. So, we need to release the OnBlockUntilReady()
// in a way where the command will not get executed, so send unblockType of 2
func (b *BufferflowTinygV2) ReleaseLock() {
log.Println("Lock being released in TinyG buffer")
b.q.Delete()
b.SetPaused(false, 2)
}
func (b *BufferflowTinygV2) IsBufferGloballySendingBackIncomingData() bool {
// we want to send back incoming data as per line data
// rather than having the default spjs implemenation that sends back data
// as it sees it. the reason is that we were getting packets out of order
// on the browser on bad internet connections. that will still happen with us
// sending back per line data, but at least it will allow the browser to parse
// correct json now.
// TODO: The right way to solve this is to watch for an acknowledgement
// from the browser and queue stuff up until the acknowledgement and then
// send the full blast of ganged up data
return true
}
//Use this function to open a connection, write directly to serial port and close connection.
//This is used for sending query requests outside of the normal buffered operations that will pause to wait for room in the grbl buffer
//'?' is asynchronous to the normal buffer load and does not need to be paused when buffer full
func (b *BufferflowTinygV2) rxQueryLoop(p *serport) {
b.parent_serport = p //make note of this port for use in clearing the buffer later, on error.
ticker := time.NewTicker(5000 * time.Millisecond)
b.quit = make(chan int)
go func() {
for {
select {
case <-ticker.C:
// we'll write a lazy formatted version of json to reduce the amt of chars
// chewed up since we're doing this outside the scope of the serial buffer counter
n2, err := p.portIo.Write([]byte("{rx:n}\n"))
log.Print("Just wrote ", n2, " bytes to serial: {rx:n}")
if err != nil {
errstr := "Error writing to " + p.portConf.Name + " " + err.Error() + " Closing port."
log.Print(errstr)
h.broadcastSys <- []byte(errstr)
ticker.Stop() //stop query loop if we can't write to the port
break
}
case <-b.quit:
ticker.Stop()
return
}
}
}()
}
func (b *BufferflowTinygV2) Close() {
//stop the rx query loop when the serial port is closed off.
log.Println("Stopping the RX query loop")
b.ReleaseLock()
b.Unpause()
go func() {
b.quit <- 1
}()
}
// Gets the paused state of this buffer
// go-routine safe.
func (b *BufferflowTinygV2) GetPaused() bool {
b.lock.Lock()
defer b.lock.Unlock()
return b.Paused
}
// Sets the paused state of this buffer
// go-routine safe.
func (b *BufferflowTinygV2) SetPaused(isPaused bool, semRelease int) {
b.lock.Lock()
defer b.lock.Unlock()
b.Paused = isPaused
// only release semaphore if we are being told to unpause
if b.Paused == false {
// the BlockUntilReady thread should be sitting waiting
// so when we send this should trigger it
b.sem <- semRelease
log.Println("Just sent release to b.sem so we will not block the sending to serial port anymore.")
}
}
func (b *BufferflowTinygV2) GetManualPaused() bool {
b.manualLock.Lock()
defer b.manualLock.Unlock()
return b.ManualPaused
}
func (b *BufferflowTinygV2) SetManualPaused(isPaused bool) {
b.manualLock.Lock()
defer b.manualLock.Unlock()
b.ManualPaused = isPaused
}