-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
1828 lines (1678 loc) · 109 KB
/
Program.cs
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.VisualBasic;
using System.Collections.Concurrent;
using System.Data.Common;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using Softata;
using Softata.Enums;
using System.Collections.Generic;
using System.Collections;
using static Softata.SoftataLib;
//using SoftataConsole;
using B = ConsoleTextFormat.Fmt.Bold;
using F = ConsoleTextFormat.Fmt;
using L = ConsoleTextFormat.Layout;
using ConsoleTextFormat;
using System.Runtime.Intrinsics.X86;
using static System.Runtime.CompilerServices.RuntimeHelpers;
using System.Diagnostics.Eventing.Reader;
using System.Transactions;
using System.Windows.Input;
namespace SoftataBasic
{
internal partial class Program
{
const double PotMin = 0;
const double PotMax = 100;
const double LightMin = 0;
const double LightMax = 61;
const double SoundMin = 0;
const double SoundMax = 100;
const int numLoops = 20;
// Set the same as Arduino:
static int port = 4242;
static string ipaddressStr = "192.168.0.12";
static bool hasRunCalibrationOnce = false;
// Configure hardware pin connections thus:
static byte LED = 16;
static byte BUTTON = 18;
static byte POTENTIOMETER = 26;//A0
static byte LIGHTSENSOR = 27; //A1
static byte SOUNDSENSOR = 28; //A2
static byte RELAY = 16;
static byte MAX_NUM_NEOPIXEL_PIXELS = 8;
const string Tab5 = "\t\t\t\t\t";
// Choose test DEFAULT type (There is now a menu to select from)
//static softatalib.ConsoleTestType Testtype = ConsoleTestType.Sensors;
//static softatalib.ConsoleTestType Testtype = ConsoleTestType.Analog_Potentiometer_Light_and_Sound;
//static softatalib.ConsoleTestType Testtype = ConsoleTestType.LCD1602Display;
//static softatalib.ConsoleTestType Testtype = ConsoleTestType.NeopixelDisplay;
//static softatalib.ConsoleTestType Testtype = ConsoleTestType.Digital_Button_and_LED;
//static softatalib.ConsoleTestType Testtype = ConsoleTestType.Serial;
static ConsoleTestType Testtype = ConsoleTestType.Digital_Button_and_LED;
//Set Serial1 or Serial2 for send and receive.
//Nb: If both true or both false then loopback on same serial port.
//static bool Send1 = true;
//static bool Recv1 = true;
// Next two are the same test
//static softatalib.ConsoleTestType Testtype = ConsoleTestType.Analog_Potentiometer_and_LED;
//static softatalib.ConsoleTestType Testtype = ConsoleTestType.PWM;
private static Softata.SoftataLib _softatalib;
static Softata.SoftataLib softatalib { get {return _softatalib; } set {_softatalib = value; } }
internal static void ShowHeading(string test="")
{
test = string.IsNullOrEmpty(test) ? "" : $": {test}";
Layout.RainbowHeading($"SOFTATA TESTS{test}");
Console.WriteLine("--------------------------");
Console.WriteLine("For details see https://davidjones.sportronics.com.au/cats/softata/");
}
public static bool connected
{
get
{
if (softatalib == null)
return false;
return softatalib.Connected;
}
}
static void Main(string[] args)
{
hasRunCalibrationOnce = false;
softatalib = new SoftataLib();
AnalogInit();
Console.Clear();
ShowHeading();
Console.WriteLine("For details see https://davidjones.sportronics.com.au/cats/softata/");
Console.WriteLine();
//SettingsManager.ClearAllSettings();
SettingsManager.ReadAllSettings();
string? _ipaddressStr = SettingsManager.ReadSetting("IpaddressStr");
if (!string.IsNullOrEmpty(_ipaddressStr))
{
if (_ipaddressStr.Count(c => c == '.') == 3)
{
if (IPAddress.TryParse(_ipaddressStr, out IPAddress? address))
{
ipaddressStr = _ipaddressStr;
}
else
Console.WriteLine("\t\t App SettingsIP Address");
}
else
Console.WriteLine("\t\tInvalid App Settings IP Address");
}
else
{
SettingsManager.AddUpdateAppSettings("IpaddressStr", ipaddressStr);
}
string _port = SettingsManager.ReadSetting("Port");
if (!string.IsNullOrEmpty(_port))
{
if (int.TryParse(_port, out int _portNo))
{
port = _portNo;
}
else
Console.WriteLine("\t\tInvalid AppSettings Port");
}
else
{
SettingsManager.AddUpdateAppSettings("Port", port.ToString());
}
string IpAddress = ipaddressStr;
int Port = port;
/*string? s_ipaddress = Environment.GetEnvironmentVariable("SOFTATA_IPADDRESS");
if(!string.IsNullOrEmpty(s_ipaddress))
{
if (s_ipaddress.Count(c => c == '.') == 3)
{
if (IPAddress.TryParse(s_ipaddress, out IPAddress? address))
{
IpAddress = s_ipaddress;
};
}
}
string? s_portstr = Environment.GetEnvironmentVariable("SOFTATA_PORT");
if (!string.IsNullOrEmpty(s_portstr))
{
if (int.TryParse(s_portstr, out int port))
{
Port = port;
}
}*/
bool quit = false;
while (!quit)
{
ShowHeading();
try
{
if (!connected)
{
Console.WriteLine($"{B.fgblu}Default Softata Server is at{B.fgYel} {ipaddressStr}:{port}{Fmt.clr}");
Layout.Info("Enter new values"," or press [Enter] to continue:");
Console.Write("Plz Enter IPAdress: ");
string? ip = Console.ReadLine();
if (!string.IsNullOrEmpty(ip))
{
if (ip.Count(c => c == '.') == 3)
{
if (IPAddress.TryParse(ip, out IPAddress? address))
{
IpAddress = ip;
SettingsManager.AddUpdateAppSettings("IpaddressStr", IpAddress);
}
else
Console.WriteLine("\t\tInvalid IP Address");
}
else
Console.WriteLine("\t\tInvalid IP Address");
}
Console.Write("Plz Enter Port: ");
string? prt = Console.ReadLine();
if (!string.IsNullOrEmpty(prt))
{
if (int.TryParse(prt, out int portNo))
{
Port = portNo;
SettingsManager.AddUpdateAppSettings("Port", Port.ToString());
}
else
Console.WriteLine("\t\tInvalid Port");
}
ShowHeading();
Console.WriteLine($"{B.fgblu}The selected Softata Server is at{B.fgYel} {ipaddressStr}:{port}{Fmt.clr}");
Console.WriteLine("Make sure the Pico has has booted ...");
Console.WriteLine(" ... and is waiting (4s slow flash) before proceeding:"); Console.WriteLine();
}
quit = false;
Layout.AddHideMenuItems("MaxType");
Layout.AddHideMenuItems("Undefined");
Testtype = Layout.SelectEnum<ConsoleTestType>((int)Testtype + 1, ref quit, true);
Layout.ClearHideMenuItems();
if (quit)
return;
ShowHeading();
Layout.Info($"Selected Test: ",$" {Testtype}");
if (!connected)
{
bool res = softatalib.Connect(IpAddress, Port);
if (!res)
{
Console.WriteLine($"Failed to connect to {IpAddress}:{Port}");
Console.WriteLine("Press [Enter] to try again or [Q] to quit");
string? key = Console.ReadLine();
if (!string.IsNullOrEmpty(key))
{
if (key.ToUpper() == "Q")
quit = true;
}
else continue;
}
else
{
//Environment.SetEnvironmentVariable("SOFTATA_IPADDRESS", IpAddress);
//Environment.SetEnvironmentVariable("SOFTATA_PORT", Port.ToString());
Console.WriteLine($"Connected to {IpAddress}:{Port}");
Console.WriteLine();
quit = YesNoQuit("Press [Enter] to continue or [Q] to quit", true);
}
if (quit)
return;
softatalib.SendMessageCmd("Begin");
Thread.Sleep(500);
string Version = softatalib.SendMessageCmd("Version");
Console.WriteLine($"Softata Version: {Version}");
Thread.Sleep(500);
string devicesCSV = softatalib.SendMessageCmd("Devices");
Console.WriteLine($"{devicesCSV}");
Thread.Sleep(500);
}
// Explicitly declare subclasses
// Instantiate where needed.
SoftataLib.Digital softatalibDigital;
//SoftataLib.Analog softatalibAnalog;
SoftataLib.PWM softatalibPWM;
SoftataLib.Serial softatalibSerial1;
SoftataLib.Serial softatalibSerial2;
SoftataLib.Sensor softatalibSensor;
SoftataLib.Actuator softatalibActuator;
SoftataLib.Display softatalibDisplay;
switch (Testtype)
{
case ConsoleTestType.Analog_Potentiometer_and_LED:
case ConsoleTestType.PWM:
case ConsoleTestType.Analog_Potentiometer_Light_and_Sound:
case ConsoleTestType.Potentiometer_and_Actuator:
case ConsoleTestType.Analog_Device_Raw:
Calibrate();
break;
}
Layout.RainbowHeading($"Softata Test: { Testtype}");
switch (Testtype)
{
case ConsoleTestType.Test_OTA_Or_WDT:
softatalibDigital = new SoftataLib.Digital(softatalib);
softatalibDigital.SetPinMode(BUTTON, SoftataLib.PinMode.DigitalInput);
softatalibDigital.SetPinMode(LED, SoftataLib.PinMode.DigitalOutput);
softatalibDigital.SetPinState(LED, SoftataLib.PinState.High);
Console.WriteLine("WDT Test: Enable WDT in softata.h, deploy and boot then press [Return]");
Console.WriteLine("OTA Test: Enable OTA (optonally disable WDT) in softata.h, deploy and boot then press [Return]");
Console.ReadLine();
Console.WriteLine("4 LED toggles then WDT.Update/OTA.handle turned off, with busy wait on device,for 100 secs");
for (int i = 0; i < 4; i++)
{
softatalibDigital.TogglePinState(LED);
Console.WriteLine($"{i} secs");
Thread.Sleep(1000);
}
Console.WriteLine($"Turning off WDT/OTA Updates with busy wait on device. Press [Enter]");
Console.ReadLine();
softatalibDigital.TurnOffWDTUpdates();
for (int i = 0; i < 100; i++)
{
Console.WriteLine($"{i} secs");
softatalibDigital.TogglePinState(LED);
Thread.Sleep(1000);
}
break;
// LED-Button test
case ConsoleTestType.Digital_Button_and_LED:
softatalibDigital = new SoftataLib.Digital(softatalib);
softatalibDigital.SetPinMode(BUTTON, SoftataLib.PinMode.DigitalInput);
softatalibDigital.SetPinMode(LED, SoftataLib.PinMode.DigitalOutput);
softatalibDigital.SetPinState(LED, SoftataLib.PinState.High);
Layout.Info("Button connected to pin", $" {BUTTON}");
Layout.Info($"LED connected to pin", $" {LED}");
Layout.Info("LED will toggle when button NOT pressed.");
Layout.Press2Continue();
int digMax = 0x10;
for (int i = 0; i < digMax; i++)
{
Console.WriteLine($"{i + 1}/{digMax}");
while (softatalibDigital.GetPinState(BUTTON))
Thread.Sleep(100);
softatalibDigital.TogglePinState(LED);
}
break;
// Potentiometer-LED Test
case ConsoleTestType.Analog_Potentiometer_and_LED:
case ConsoleTestType.PWM:
// Note no pin setup needed for analog
softatalibDigital = new SoftataLib.Digital(softatalib);
//softatalibAnalog = new SoftataLib.Analog(softatalib);
softatalibPWM = new SoftataLib.PWM(softatalib);
byte numPWMBits = 10;
Layout.Info($"Potentiometer connected to pin", $" {POTENTIOMETER}");
Layout.Info($"Light Sensor connected to pin", $" {LIGHTSENSOR}");
Layout.Info($"{numPWMBits}", $" Bit PWM being used. 10 ADC bits");
Layout.Info("LED brightness depends upon potentiometer.");
Layout.Press2Continue();
softatalibDigital.SetPinMode(LED, SoftataLib.PinMode.DigitalOutput);
softatalibPWM.SetPinModePWM(LED, numPWMBits);
for (int i = 0; i < numLoops; i++)
{
int val = softatalibAnalog!.AnalogRead(POTENTIOMETER);
if (val != int.MaxValue)
{
Console.WriteLine($"{i + 1}/{numLoops}. AnalogRead({POTENTIOMETER}) = {val}");
int pwmVal = val;
if (val > 1023)
pwmVal = 1023;
softatalibPWM.SetPWM(LED, pwmVal);
}
else
Console.WriteLine($"AnalogRead({POTENTIOMETER}) failed");
Console.WriteLine();
Thread.Sleep(500);
}
break;
case ConsoleTestType.Loopback:
softatalibSerial1 = new SoftataLib.Serial(softatalib);
softatalibSerial2 = new SoftataLib.Serial(softatalib);
byte[] txPins = new byte[] { 0, 0, 4 }; //Nb: Recv are Tx+1
List<string> SerialModes = new List<string>
{
"Serial 1 Loopback",
"Serial 2 Loopback",
"(Tx)Serial 1 -> (Rx)Serial 2",
"(Tx)Serial 2 -> (Rx)Serial 1"
};
byte iserialTxRx = 1;
L.Info("Serial Test");
int res = Layout.DisplayMenu(iserialTxRx, SerialModes.ToList<string>(), true);
if (res < 0)
break;
iserialTxRx = (byte)res;
byte comTx = 1;
byte comRx = 1;
switch (iserialTxRx)
{
case 1:
comTx = 1;
comRx = 1;
break;
case 2:
comTx = 2;
comRx = 2;
break;
case 3:
comTx = 1;
comRx = 2;
break;
case 4:
comTx = 2;
comRx = 1;
break;
case 5:
break;
}
Console.WriteLine("");
L.Info("Serial mode Selection:");
SerialModes = new List<string>
{
"ASCII",
" Byte"
};
if (iserialTxRx < 3)
{
SerialModes.Add(" GPS");
}
int iserialMode = 1;
res = Layout.DisplayMenu(iserialMode, SerialModes.ToList<string>(), true);
if (res < 0)
break;
iserialMode = 1 + res;
Console.WriteLine();
L.Info("BAUD Rate (Default 9600).");
int baudRate = 9600;
do
{
bool serialFound = false;
baudRate = 9600;
while (!serialFound)
{
Console.Write("Enter BAUD:");
string? baudStr = Console.ReadLine();
if (int.TryParse(baudStr, out int baud))
{
serialFound = true;
baudRate = baud;
}
else if (string.IsNullOrEmpty(baudStr))
{
serialFound = true;
}
}
if (!serialFound)
Console.WriteLine("Invalid");
else if (!softatalib.Baudrates.Contains(baudRate))
Console.WriteLine("Invalid");
} while (!softatalib.Baudrates.Contains(baudRate));
softatalibSerial1.serialSetup(txPins[1], baudRate, 1);
softatalibSerial2.serialSetup(txPins[2], baudRate, 2);
if (iserialMode == 1) // ASCII test
{
for (char sendCh = ' '; sendCh <= '~'; sendCh++)
{
char recvCh;
softatalibSerial1.serialWriteChar(comTx, sendCh);
Thread.Sleep(100);
recvCh = softatalibSerial1.serialGetChar(comRx);
if (recvCh == sendCh)
Console.WriteLine($"{Tab5}Serial{comTx} Sent {sendCh} Got {recvCh} on Serial{comRx},OK");
else
Console.WriteLine($"{Tab5}Serial{comTx} Sent {sendCh} Got {recvCh} on Serial{comRx},NOK!");
Thread.Sleep(200);
}
}
else if (iserialMode == 2) // Byte test
{
for (byte sendByte = 0x00; sendByte <= 0xff; sendByte++)
{
byte recvByte;
softatalibSerial1.serialWriteByte(comTx, sendByte);
recvByte = softatalibSerial1.serialGetByte(comRx);
if (recvByte == sendByte)
Console.WriteLine($"{Tab5}Serial{comTx} Sent {sendByte} Got {recvByte} on Serial{comRx},OK");
else
Console.WriteLine($"{Tab5}Serial{comTx} Sent {sendByte} Got {recvByte} on Serial{comRx},NOK!");
Thread.Sleep(200);
if (sendByte == 0xff)
break;
}
}
else if (iserialMode == 3) // GPS
{
Console.WriteLine();
Console.WriteLine("Reading GPS");
Console.WriteLine("Press [Esc] to stop");
Thread.Sleep(0500);
while (true)
{
string msg = softatalibSerial1.readLine(comRx, false);
Console.WriteLine($"{Tab5}{msg}");
if (Console.KeyAvailable)
{
var cki = Console.ReadKey();
if (cki.Key == ConsoleKey.Escape)
{
break;
}
}
}
}
break;
case ConsoleTestType.Sensors:
softatalibSensor = new SoftataLib.Sensor(softatalib);
bool debug = false;
string[] Sensors = softatalibSensor.GetSensors();
if (Sensors.Length == 0)
Console.WriteLine($"No sensors found");
else
{
L.Info($"Select Sensor:");
bool found = false;
byte isensor = 0;
res = Layout.DisplayMenu(isensor, Sensors.ToList<string>(), true);
if (res<0)
break;
isensor = (byte)res;
string sensor = Sensors[isensor];
string pins = softatalibSensor.GetPins(isensor);
if (string.IsNullOrEmpty(pins))
Console.WriteLine($"{sensor} getPins() failed");
else
{
Console.WriteLine($"{sensor} getPins OK");
Console.WriteLine($"{sensor} Pins = {pins}");
}
Console.WriteLine("Press any key to setup sensor");
Console.ReadLine();
byte sensorLinkedListIndex = (byte)softatalibSensor.SetupDefault(isensor);
if (sensorLinkedListIndex < 0)
Console.WriteLine($"Instantiated sensor {sensor} not found");
else
{
Console.WriteLine($"Instantiated {sensor} found at {sensorLinkedListIndex}");
string[] properties = softatalibSensor.GetProperties(isensor);
if (properties.Length == 0)
Console.WriteLine($"{sensor} getProperties() failed");
else
{
Console.WriteLine($"{sensor} getProperties OK");
foreach (string property in properties)
Console.WriteLine($"{sensor} property = {property}");
}
Console.WriteLine();
//////////////////////////////////
int sensorMode = 1;
/////////////////////////////////
Console.WriteLine("(START) SELECT SENSOR MODE");
List<string> sensorModes = new List<string> {
"Read Sensor Values",
"Get Telemetry",
"Start Stream Telemetry to Bluetooth",
"Start Stream Telemetry to Azure IoT Hub",
"Pause BT or IoT Telemetry Stream",
"Continue BT IoT Telemetry Stream"
};
res = Layout.DisplayMenu(sensorMode, sensorModes, true);
if (res < 0)
break;
sensorMode = (byte)(1+res);
bool showMenu = false;
bool keepRunning = true;
uint period = 2500;
switch (sensorMode)
{
case 1:
case 2:
found = false;
do
{
Console.Write($"Please enter the period btw sensor reads (Default {period}mS): ");
string? p = Console.ReadLine();
if (uint.TryParse(p, out uint _period))
{
period = _period;
found = true;
}
else if (string.IsNullOrEmpty(p))
{
found = true;
}
} while (!found);
Console.WriteLine();
break;
}
int sensorQuitModeNo = 7;
while (keepRunning)
{
if (showMenu)
{
//sensorMode = 1;
res = Layout.DisplayMenu(sensorMode, sensorModes, true);
if (res < 0)
{
sensorMode = sensorQuitModeNo;
keepRunning = false;
}
else
sensorMode = (byte)(1 + res);
showMenu = true;
switch (sensorMode)
{
case 1:
case 2:
showMenu = false;
break;
}
}
if (sensorMode == sensorQuitModeNo)
{
Console.WriteLine("Quitting app. Please wait.");
break;
}
else if (sensorMode == 2)
{
string json = softatalibSensor.GetTelemetry((byte)sensorLinkedListIndex, debug);
Console.WriteLine($"\t\t Telemetry: {json}");
Console.WriteLine("Press [Esc] to stop");
Thread.Sleep((int)period);
}
else if (sensorMode == 3)
{
string indxStr = softatalibSensor.StartSendingTelemetryBT((byte)sensorLinkedListIndex);
if (int.TryParse(indxStr, out int val))
Console.WriteLine($"Streaming to BT started. List No:{val}");
else
Console.WriteLine($"Streaming to BT failed to start.");
showMenu = true;
}
else if (sensorMode == 4)
{
string indxStr = softatalibSensor.StartSendingTelemetryToIoTHub((byte)sensorLinkedListIndex);
if (int.TryParse(indxStr, out int val))
{
Console.WriteLine($"Streaming to Azure IoT Hub started. List No:{val}");
Console.WriteLine("Nb: Observe Telemetry in Device Explorer:");
Console.WriteLine("https://github.com/Azure/azure-iot-explorer/");
Console.WriteLine("--------------------------------------------");
Console.WriteLine();
}
else
Console.WriteLine($"Streaming to Azure IoT Hub failed to start.");
showMenu = true;
}
else if (sensorMode == 5)
{
string json = softatalibSensor.PauseSendTelemetry((byte)sensorLinkedListIndex);
if (!string.IsNullOrEmpty(json))
Console.WriteLine($"json {json}");
}
else if (sensorMode == 6)
{
string json = softatalibSensor.ContinueSendTelemetry((byte)sensorLinkedListIndex);
if (!string.IsNullOrEmpty(json))
Console.WriteLine($"json {json}");
}
else if (sensorMode == 1)
{
double[]? values = softatalibSensor.ReadAll((byte)sensorLinkedListIndex, debug);
if (values == null)
Console.WriteLine($"{sensor} readAll() failed");
else
{
if (debug)
Console.WriteLine($"{sensor} readAll() OK");
else
Console.WriteLine("ReadAll():");
for (int p = 0; p < properties.Length; p++)
Console.WriteLine($"\t\t{sensor} {properties[p]} = {values[p]}");
}
Console.WriteLine("Individual Read():");
for (byte p = 0; p < properties.Length; p++)
{
double? value = softatalibSensor.Read((byte)sensorLinkedListIndex, p, debug);
if (value == null)
Console.WriteLine($"{sensor} read() failed");
else
Console.WriteLine($"\t\t{sensor} {properties[p]} = {value}");
Console.WriteLine();
}
Console.WriteLine("Press [Esc] to stop");
Thread.Sleep((int)period);
}
if (Console.KeyAvailable)
{
var cki = Console.ReadKey();
if (cki.Key == ConsoleKey.Escape)
{
showMenu = true;
}
}
}
}
}
break;
case ConsoleTestType.Displays_Suite_of_Tests:
case ConsoleTestType.Displays_Individual_Cmds:
case ConsoleTestType.Displays_Generic:
softatalibDisplay = new SoftataLib.Display(softatalib);
SoftataLib.Display.Neopixel? softataLibDisplayNeopixel=null;
SoftataLib.Display.BARGRAPHDisplay softataLibDisplayBargraphDisplay = null;
//SoftataLib.Display.BARGRAPHDisplay softataLibDisplayGBargraphDisplay;
SoftataLib.Display.LCD1602Display softataLibDisplayLCD1602Display=null;
SoftataLib.Display.Oled096 softataLibDisplayOled096=null;
byte idisplay = 1;
string display = "";
string[] Cmds = new string[0];
string[] Miscs = new string[0];
List<string> Cmds2Use = new List<string>();
List<int> Cmds2UseEnums = new List<int>();
string cmds = softatalibDisplay.GetCmds();
if (string.IsNullOrEmpty(cmds))
Console.WriteLine($"getCmds() failed");
else
{
Console.WriteLine($"getCmds OK");
Cmds = cmds.Split(',');
Console.WriteLine($"Cmds:");
for (int i = 0; i < Cmds.Count(); i++)
{
string cmd = Cmds[i];
if (cmd.Length > 2)
if (cmd[0] != 'D')
{
Cmds2Use.Add( cmd.Substring(2));
Cmds2UseEnums.Add(i);
}
}
L.Info($"Generic Display Class Cmds found");
}
L.Press2Continue();
string[] Displays = softatalibDisplay.GetDisplays();
if (Displays.Length == 0)
Console.WriteLine($"No displays found");
else
{
Console.WriteLine($"Displays found:");
res = Layout.DisplayMenu(idisplay, Displays.ToList<string>(), true);
if (res < 0)
break;
idisplay = (byte)res;
display = Displays[idisplay];
Layout.RainbowHeading($"Softata Test: {Testtype}");
Layout.Info($"Display: {display}","",F.Col.yellow,F.Col.black);
string pins = softatalibDisplay.GetPins(idisplay);
if (string.IsNullOrEmpty(pins))
L.Info($"{display} getPins() failed", "", F.Col.white, F.Col.red);
else
{
L.Info($"{display} getPins OK:");
L.Info($"{display} Pins = {pins}");
}
Miscs = softatalibDisplay.GetMiscCmds(idisplay);
if (Miscs == null)
Console.WriteLine($"{display} getMiscCmds() failed");
else
{
Console.WriteLine($"{display} getMiscCmds OK");
}
}
byte displayLinkedListIndex;
L.Press2Continue();
//////////////////////////////////////////
// NOTE: enum order of DisplayDevice must match that returned by GroveDisplayCmds.getDisplays
DisplayDevice displayDevice = (DisplayDevice)idisplay;
//////////////////////////////////////////
if ((displayDevice != DisplayDevice.NEOPIXEL)&&
(displayDevice != DisplayDevice.LCD1602) &&
(displayDevice != DisplayDevice.OLED096) &&
(displayDevice != DisplayDevice.BARGRAPH) &&
(displayDevice != DisplayDevice.GBARGRAPH))
{
L.Press2Continue("That display not yet supported in Displays_Individual_Cmds mode (7)");
break;
}
byte numPixels = 0; //// softataLibDisplayNeopixel.MaxNumPixels;
// Only do non-default setup for Neopixel
if (displayDevice == DisplayDevice.NEOPIXEL)
{
if(softataLibDisplayNeopixel==null)
softataLibDisplayNeopixel = new SoftataLib.Display.Neopixel(softatalib);
Console.WriteLine($"Select number of Pixels:");
List<string> pixelsStr = new List<string> { };
numPixels = softataLibDisplayNeopixel.MaxNumPixels;
for (byte i = 1; i <= softataLibDisplayNeopixel.MaxNumPixels; i++)
{
pixelsStr.Add($"Pixels");
}
numPixels = 8;
res = Layout.DisplayMenu(numPixels, pixelsStr, true);
if (res < 0)
break;
numPixels = (byte)(res+1);
L.Info($"{numPixels} Pixels chosen");
displayLinkedListIndex = (byte)softatalibDisplay.Setup(idisplay, 16, numPixels);
}
else if ( (displayDevice == DisplayDevice.BARGRAPH) || (displayDevice == DisplayDevice.GBARGRAPH) )
{
softataLibDisplayBargraphDisplay = new SoftataLib.Display.BARGRAPHDisplay(softatalib);
// Use default settings
displayLinkedListIndex = (byte)softatalibDisplay.SetupDefault(idisplay);
//Or use custom settings: {data,latch,clock} GPIO Pins
//List<byte> settings = new List<byte> { 20, 21 }; // Send the data pin as 16
//displayLinkedListIndex = (byte)softatalibDisplay.Setup(idisplay, 16, settings);
}
else if (displayDevice == DisplayDevice.OLED096)
{
softataLibDisplayOled096 = new SoftataLib.Display.Oled096(softatalib);
displayLinkedListIndex = (byte)softatalibDisplay.SetupDefault(idisplay);
}
else if (displayDevice == DisplayDevice.LCD1602)
{
softataLibDisplayLCD1602Display = new SoftataLib.Display.LCD1602Display(softatalib);
displayLinkedListIndex = (byte)softatalibDisplay.SetupDefault(idisplay);
}
else
displayLinkedListIndex = (byte)softatalibDisplay.SetupDefault(idisplay);
//int bargraphPin = 0;
Console.WriteLine($"displayLinkedListIndex: {displayLinkedListIndex}");
if (displayLinkedListIndex < 0)
Console.WriteLine($"Instantiated display {display} not found");
else
{
//bool bargraphResult = true;
if (Testtype == ConsoleTestType.Displays_Generic)
{
int line = 1;
int pos = 1;
while (true)
{
byte icmd = 0;
string command = "";
L.ClearHideMenuItems();
res = Layout.DisplayMenu(icmd, Cmds2Use, true);
if (res < 0)
break;
if ((res > -1) && (res < Cmds2UseEnums.Count()))
{
icmd = (byte)Cmds2UseEnums[res];
command = Cmds2Use[res].ToLower();
L.Info($"{Cmds2Use[res]} ({res}) chosen which maps {icmd}th cmommand on Arduino");
}
string? msg = "";
string response = "";
if (command.Contains("writestring"))
{
if (command.Contains("cursor"))
{
//case GroveDisplayCmds.setCursor:
L.Info("Enter line 1 or 2");
line = L.Prompt4Num(line, 2, false);
L.Info("Enter line position 1 to 40");
pos = L.Prompt4Num(pos, 40, false);
msg = Console.ReadLine();
if (msg is not null)
{
if (softatalibDisplay.WriteString(displayLinkedListIndex, pos, line, msg))
response = "OK";
else
response = "NOT OK";
}
}
else
{
//case GroveDisplayCmds.writestrngCMD:
while (string.IsNullOrEmpty(msg))
{
msg = Console.ReadLine();
}
if (msg is not null)
{
if (softatalibDisplay.WriteString(displayLinkedListIndex, msg))
response = "OK";
else
response = "NOT OK";
}
}
}
else if (command.Contains("cursor"))
{
L.Info("Enter line 1 or 2");
line = L.Prompt4Num(line, 2, false);
L.Info("Enter line position 1 to 40");
pos = L.Prompt4Num(pos, 40, false);
if (softatalibDisplay.SetCursor(displayLinkedListIndex, pos, line))
response = "OK";
else
response = "NOT OK";
}
else if (command.Contains("getListofCMDs"))
{
//GroveDisplayCmds.getListofCMDs:
response = softatalibDisplay.GetCmds();
}
else
{
response = softatalibDisplay.GenericDisplayCmd(displayDevice, (byte)icmd, displayLinkedListIndex);
}
L.Info(response);
}
}
else if (Testtype == ConsoleTestType.Displays_Individual_Cmds)
{
//SoftataLib.Display.Neopixel softataLibDisplayNeopixel = null;
Tuple<byte, byte, byte> rgb = new Tuple<byte, byte, byte>(0x40, 0, 0);
int imisc = 0xff;
while (true)
{
if (Miscs != null)
{
if (Miscs.Length > 0)
{
List<string> miscsStrs = Miscs.ToList<string>();
switch (displayDevice)
{
case DisplayDevice.NEOPIXEL:
miscsStrs.Add("Set_Indiv_Pixel_Color");
break;
case DisplayDevice.BARGRAPH:
case DisplayDevice.GBARGRAPH:
miscsStrs.Add("Clear");
miscsStrs.Add("All_On");
break;
case DisplayDevice.LCD1602:
miscsStrs.Add("Clear");
miscsStrs.Add("Enter_Text");
miscsStrs.Add("First_Line");
miscsStrs.Add("Second_Line");
break;
}
res = Layout.DisplayMenu(imisc, miscsStrs, true);
if (res < 0)
break;
imisc = (byte)(1+res);
L.Info($"{miscsStrs[imisc-1]} chosen");
}
}
switch (displayDevice)
{
case DisplayDevice.LCD1602:
if (softataLibDisplayLCD1602Display == null)//Shouldn't get here