aboutsummaryrefslogtreecommitdiffstats
path: root/libs/pixtend/v1/ppl/pixtendlib/__init__.py
blob: 33c22d6334de9b23ecd8ad878376006bffc578c4 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
#!/usr/bin/python
# coding=utf-8
import RPi.GPIO as GPIO
import time
import spidev
import ctypes
import subprocess
import shlex

# This file is part of the PiXtend(R) Project.
#
# For more information about PiXtend(R) and this program,
# see <https://www.pixtend.de> or <https://www.pixtend.com>
#
# Copyright (C) 2018 Robin Turner
# Qube Solutions UG (haftungsbeschränkt), Arbachtalstr. 6
# 72800 Eningen, Germany
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

__author__ = "Robin Turner"
__version__ = "0.1.1"

# <editor-fold desc="Region: cTypes definitions into short names">

"""
Use the UINT8 cType to create a one (1) byte length variable, short name c_uint8
"""
c_uint8 = ctypes.c_uint8


"""
Use the UINT16 cType to create a two (2) byte length variable, short version c_uint16
"""
c_uint16 = ctypes.c_uint16

"""
Use the FLOAT type from cTypes and make a short version c_float
"""
c_float = ctypes.c_float

# </editor-fold>

# <editor-fold desc="Region: cType style data fields from structures and unions">


class FlagsBits(ctypes.LittleEndianStructure):
    """
    This class has a field with named bits within one single byte, they will be used later to control/change
    individual bits in one single byte.
    
    :type bit0 : c_uint8
    :type bit1 : c_uint8
    :type bit2 : c_uint8
    :type bit3 : c_uint8
    :type bit4 : c_uint8
    :type bit5 : c_uint8
    :type bit6 : c_uint8
    :type bit7 : c_uint8
    """

    def __init__(self, *args, **kwargs):
        super(FlagsBits, self).__init__(*args, **kwargs)
        self.bit0 = 0
        self.bit1 = 0
        self.bit2 = 0
        self.bit3 = 0
        self.bit4 = 0
        self.bit5 = 0
        self.bit6 = 0
        self.bit7 = 0

    _fields_ = [
        ("bit0", c_uint8, 1),  # asByte & 1
        ("bit1", c_uint8, 1),  # asByte & 2
        ("bit2", c_uint8, 1),  # asByte & 4
        ("bit3", c_uint8, 1),  # asByte & 8
        ("bit4", c_uint8, 1),  # asByte & 16
        ("bit5", c_uint8, 1),  # asByte & 32
        ("bit6", c_uint8, 1),  # asByte & 64
        ("bit7", c_uint8, 1)  # asByte & 128
    ]


class FlagsBits16(ctypes.LittleEndianStructure):
    """
    This class has a field with named bits within one 2 bytes structure (c_uint16 type), they will be used later
    to control/change individual bits in two bytes.
    
    :type bit0 : ctypes.c_ushort
    :type bit1 : ctypes.c_ushort
    :type bit2 : ctypes.c_ushort
    :type bit3 : ctypes.c_ushort
    :type bit4 : ctypes.c_ushort
    :type bit5 : ctypes.c_ushort
    :type bit6 : ctypes.c_ushort
    :type bit7 : ctypes.c_ushort
    :type bit8 : ctypes.c_ushort
    :type bit9 : ctypes.c_ushort
    :type bit10 : ctypes.c_ushort
    :type bit11 : ctypes.c_ushort
    :type bit12 : ctypes.c_ushort
    :type bit13 : ctypes.c_ushort
    :type bit14 : ctypes.c_ushort
    :type bit15 : ctypes.c_ushort
    """

    def __init__(self, *args, **kwargs):
        super(FlagsBits16, self).__init__(*args, **kwargs)
        self.bit0 = 0
        self.bit1 = 0
        self.bit2 = 0
        self.bit3 = 0
        self.bit4 = 0
        self.bit5 = 0
        self.bit6 = 0
        self.bit7 = 0
        self.bit8 = 0
        self.bit9 = 0
        self.bit10 = 0
        self.bit11 = 0
        self.bit12 = 0
        self.bit13 = 0
        self.bit14 = 0
        self.bit15 = 0

    _fields_ = [
        ("bit0", c_uint16, 1),  # asByte & 1
        ("bit1", c_uint16, 1),  # asByte & 2
        ("bit2", c_uint16, 1),  # asByte & 4
        ("bit3", c_uint16, 1),  # asByte & 8
        ("bit4", c_uint16, 1),  # asByte & 16
        ("bit5", c_uint16, 1),  # asByte & 32
        ("bit6", c_uint16, 1),  # asByte & 64
        ("bit7", c_uint16, 1),  # asByte & 128
        ("bit8", c_uint16, 1),  # asByte & 256
        ("bit9", c_uint16, 1),  # asByte & 512
        ("bit10", c_uint16, 1),  # asByte & 1024
        ("bit11", c_uint16, 1),  # asByte & 2048
        ("bit12", c_uint16, 1),  # asByte & 4096
        ("bit13", c_uint16, 1),  # asByte & 8192
        ("bit14", c_uint16, 1),  # asByte & 16384
        ("bit15", c_uint16, 1)  # asByte & 32768
    ]


class Flags(ctypes.Union):
    """
    A class using the ctypes Union to combine the 8 bits from the FlagsBits class (field) and one byte (type c_uint8)
    into one memory space overlaying each other. This way each single bit within a byte can be accessed directly by
    its individual name. Using the asBytes field element allows to set all 8 bits at once.   
    
    :type b : FlagsBits
    :type asByte : c_uint8
    """

    def __init__(self, *args, **kwargs):
        super(Flags, self).__init__(*args, **kwargs)
        self.b = FlagsBits()
        self.asByte = 0

    _fields_ = [
        ("b", FlagsBits),
        ("asByte", c_uint8)
    ]


class AnalogValueBytes(ctypes.Structure):
    """
    This is a structure for two (2) single bytes to split up a 16 bit / 2 bytes data type. This class can be used
    in a Union.
    
    :type byte0 : c_uint8
    :type byte1 : c_uint8
    """

    def __init__(self, *args, **kwargs):
        super(AnalogValueBytes, self).__init__(*args, **kwargs)
        self.byte0 = c_uint8(0)
        self.byte1 = c_uint8(0)

    _fields_ = [
        ("byte0", c_uint8),
        ("byte1", c_uint8)
    ]


class AnalogValue(ctypes.Union):
    """
    Union from ctypes to combine the AnalogValueBytes structure with a C_UINT16 type to make a double byte
    memory area. This memory can then be accessed via the C_UINT16 (asUint16) field or via 2 individual named
    bytes from the structure AnalogValueBytes.
    
    :type bytes : AnalogValueBytes
    :type asUint16 : c_uint16
    """

    def __init__(self, *args, **kwargs):
        super(AnalogValue, self).__init__(*args, **kwargs)
        self.bytes = AnalogValueBytes()
        self.asUint16 = c_uint16(0)

    _fields_ = [
        ("bytes", AnalogValueBytes),
        ("asUint16", c_uint16)
    ]


class Flags16(ctypes.Union):
    """
    Union from ctypes to combine a 16 bits structure with a C_UINT16 type to make a two byte memory space
    for the SPI data which will be sent to the DAC via SPI Master 0 and Chip Select 1.

    :type bits : FlagsBits16
    :type asUint16 : c_uint16
    """

    def __init__(self, *args, **kwargs):
        super(Flags16, self).__init__(*args, **kwargs)
        self.bits = FlagsBits16()
        self.asUint16 = c_uint16(0)

    _fields_ = [
        ("bits", FlagsBits16),
        ("asUint16", c_uint16)
    ]


class UcVersionBytes(ctypes.Structure):
    """
    This is a structure for two (2) single bytes to store the firmware version and the board version reported by the
    microcontroller on the PiXtend board.
    
    Example:
    UC_VERSIONH = 13 means board version is 1.3.x
    UC_VERSIONL = 2 means firmware version 2.x is installed on the  microcontroller
    
    :type UC_VERSIONL : c_uint8
    :type UC_VERSIONH : c_uint8
    """

    def __init__(self, *args, **kwargs):
        super(UcVersionBytes, self).__init__(*args, **kwargs)
        self.UC_VERSIONL = c_uint8(0)
        self.UC_VERSIONH = c_uint8(0)

    _fields_ = [
        ("UC_VERSIONL", c_uint8),
        ("UC_VERSIONH", c_uint8)
    ]

# </editor-fold>


class Pixtend(object):
    """
    The PiXtend class derived from Python object can be used to control and manipulate states of inputs and outputs 
    (analog and digital) in the microcontroller on the PiXtend board via the Raspberry Pi's SPI bus.

    Import the pixtend file:           from pixtendlib import Pixtend
    Creating an instance:              p = Pixtend()
    Activate the SPI bus:              p.open()
    Read relay 0 state:                mystate = p.relay0
    Set relay 1 to on:                 p.relay1 = p.ON
    """

    # <editor-fold desc="Global class defines">

    # Global class defines
    PIXTEND_MAX_RELAYS = 4
    PIXTEND_MC_RESET_PIN = 23
    PIXTEND_SPI_ENABLE_PIN = 24
    PIXTEND_SERIAL_PIN = 18
    PIXTEND_SPI_NOT_FOUND = -1
    PIXTEND_SPI_NULL_BYTE = 0b00000000
    PIXTEND_SPI_HANDSHAKE = 0b10101010

    # --------------------------------------
    # 4-Byte Command length
    # --------------------------------------
    PIXTEND_SPI_SET_DOUT = 0b00000001
    PIXTEND_SPI_GET_DIN = 0b00000010
    PIXTEND_SPI_SET_RELAY = 0b00000111
    PIXTEND_SPI_SET_GPIO = 0b00001000
    PIXTEND_SPI_GET_GPIO = 0b00001001
    PIXTEND_SPI_GET_DOUT = 0b00010010
    PIXTEND_SPI_GET_RELAY = 0b00010011
    PIXTEND_SPI_SET_SERVO0 = 0b10000000
    PIXTEND_SPI_SET_SERVO1 = 0b10000001
    PIXTEND_SPI_SET_GPIO_CTRL = 0b10000101
    PIXTEND_SPI_SET_UC_CTRL = 0b10000110
    PIXTEND_SPI_SET_RASPSTAT = 0b10001000
    PIXTEND_SPI_GET_UC_STAT = 0b10001010

    # --------------------------------------
    # 5-Byte Command length
    # --------------------------------------
    PIXTEND_SPI_GET_AIN0 = 0b00000011
    PIXTEND_SPI_GET_AIN1 = 0b00000100
    PIXTEND_SPI_GET_AIN2 = 0b00000101
    PIXTEND_SPI_GET_AIN3 = 0b00000110
    PIXTEND_SPI_GET_TEMP0 = 0b00001010
    PIXTEND_SPI_GET_TEMP1 = 0b00001011
    PIXTEND_SPI_GET_TEMP2 = 0b00001100
    PIXTEND_SPI_GET_TEMP3 = 0b00001101
    PIXTEND_SPI_GET_HUM0 = 0b00001110
    PIXTEND_SPI_GET_HUM1 = 0b00001111
    PIXTEND_SPI_GET_HUM2 = 0b00010000
    PIXTEND_SPI_GET_HUM3 = 0b00010001
    PIXTEND_SPI_SET_PWM0 = 0b10000010
    PIXTEND_SPI_SET_PWM1 = 0b10000011
    PIXTEND_SPI_SET_AI_CTRL = 0b10000111
    PIXTEND_SPI_GET_UC_VER = 0b10001001

    # --------------------------------------
    # 6-Byte Command length
    # --------------------------------------
    PIXTEND_SPI_SET_PWM_CTRL = 0b10000100

    # --------------------------------------
    # Auto Mode - 34 bytes Command length
    # --------------------------------------
    PIXTEND_SPI_AUTO_MODE = 0b11100111

    # --------------------------------------
    # Define types, states and selections
    # --------------------------------------
    ON = 1
    OFF = 0
    RS232 = False
    RS485 = True
    DAC_A = 0
    DAC_B = 1
    SERVO_MODE = 0
    PWM_MODE = 1
    GPIO_OUTPUT = 1
    GPIO_INPUT = 0
    JUMPER_5V = 0
    JUMPER_10V = 1
    BIT_0 = 0
    BIT_1 = 1
    BIT_2 = 2
    BIT_3 = 3
    BIT_4 = 4
    BIT_5 = 5
    BIT_6 = 6
    BIT_7 = 7
    BIT_8 = 8
    BIT_9 = 9
    BIT_10 = 10
    BIT_11 = 11
    BIT_12 = 12
    BIT_13 = 13
    BIT_14 = 14
    BIT_15 = 15

    # </editor-fold>

    def __init__(self):
        """
        Constructor of the Pixtend class.
        Create all objects and variables needed, set defaults for the RPi SPI bus, the GPIOs and activate
        the BCM GPIO layout on the Raspberry Pi. The GPIO 24 needs to be an output and set to 'on' to enable
        the communication with the microcontroller on the PiXtend board via SPI Master 0 and Chip Select 0.
        """

        # Default SPI frequency is 100kHz
        self.__spi_speed = 100000
        # The microcontroller is on the SPI Master 0 with CS 0
        self.__spi_channel = 0
        self.__spi_cs = 0
        self.__spi = None
        self.__spi_dac = None

        # Initialize variables
        self.__use_fahrenheit = False
        self.__is_automatic_mode_active = False
        self.__is_spi_open = False
        self.__is_spi_dac_open = False
        self.__max_relay_bits = self.PIXTEND_MAX_RELAYS
        self.__relays_states = 0
        self.__digital_inputs_states = 0
        self.__digital_outputs_states = 0
        self.__gpio_states_manual = 0
        self.__gpio_states_auto_in = 0
        self.__gpio_states_auto_out = 0
        self.__gpio_ctrl = 0
        self.__servo0_value = c_uint8(0)
        self.__servo1_value = c_uint8(0)
        self.__pwm0_value = AnalogValue()
        self.__pwm1_value = AnalogValue()
        self.__pwm_ctrl0 = Flags()
        self.__pwm_ctrl1 = Flags()
        self.__pwm_ctrl2 = Flags()
        self.__pwm_period = AnalogValue()
        self.__analog_dac_value = Flags16()
        self.__uc_version = UcVersionBytes()
        self.__uc_status = c_uint8(0)
        self.__uc_ctrl = Flags()
        self.__ai_ctrl0 = Flags()
        self.__ai_ctrl1 = Flags()
        self.__pi_status = Flags()
        self.__analog_value = AnalogValue()
        self.__ai0_jumper_setting_10_volts = 0
        self.__ai1_jumper_setting_10_volts = 0
        self.__ai0_raw_value = AnalogValue()
        self.__ai1_raw_value = AnalogValue()
        self.__ai2_raw_value = AnalogValue()
        self.__ai3_raw_value = AnalogValue()
        self.__temp0_raw_value = AnalogValue()
        self.__temp1_raw_value = AnalogValue()
        self.__temp2_raw_value = AnalogValue()
        self.__temp3_raw_value = AnalogValue()
        self.__humid0_raw_value = AnalogValue()
        self.__humid1_raw_value = AnalogValue()
        self.__humid2_raw_value = AnalogValue()
        self.__humid3_raw_value = AnalogValue()
        self.__ai0_value = c_float(0.0)
        self.__ai1_value = c_float(0.0)
        self.__ai2_value = c_float(0.0)
        self.__ai3_value = c_float(0.0)
        self.__temp0_value = c_float(0.0)
        self.__temp1_value = c_float(0.0)
        self.__temp2_value = c_float(0.0)
        self.__temp3_value = c_float(0.0)
        self.__humid0_value = c_float(0.0)
        self.__humid1_value = c_float(0.0)
        self.__humid2_value = c_float(0.0)
        self.__humid3_value = c_float(0.0)
        self.__analog0_dac_value = Flags16()
        self.__analog1_dac_value = Flags16()
        
        # Turn RPi GPIO warnings off in case GPIOs are still/already in use
        GPIO.setwarnings(False)
        # Change layout to BCM
        GPIO.setmode(GPIO.BCM)
        # Set SPI Enable pin to output
        GPIO.setup(self.PIXTEND_SPI_ENABLE_PIN, GPIO.OUT)
        GPIO.setup(self.PIXTEND_MC_RESET_PIN, GPIO.OUT)
        GPIO.setup(self.PIXTEND_SERIAL_PIN, GPIO.OUT)
        # Activate SPI Enable, allow communication
        GPIO.output(self.PIXTEND_SPI_ENABLE_PIN, True)
        # Turn microcontroller reset pin off
        GPIO.output(self.PIXTEND_MC_RESET_PIN, False)
        # Set serial mode to RS232
        GPIO.output(self.PIXTEND_SERIAL_PIN, False)

    @staticmethod
    def __dump(obj):
        for attr in dir(obj):
            if hasattr(obj, attr):
                print("obj.%s = %s" % (attr, getattr(obj, attr)))

    def __del__(self):
        """
        Destructor of the Pixtend class.
        Delete all objects, clean up GPIOs and close the SPI bus when the Pixtend instance is destroyed.
        """

        self.__use_fahrenheit = False
        self.__is_automatic_mode_active = False
        try:
            del self.__relays_states
        except:
            pass
        try:
            del self.__digital_inputs_states
        except:
            pass
        try:
            del self.__digital_outputs_states
        except:
            pass
        try:
            del self.__analog_value
        except:
            pass
        try:
            del self.__gpio_states_manual
        except:
            pass
        try:
            del self.__gpio_states_auto_in
        except:
            pass
        try:
            del self.__gpio_states_auto_out
        except:
            pass
        try:
            del self.__servo0_value
        except:
            pass
        try:
            del self.__servo1_value
        except:
            pass
        try:
            del self.__pwm0_value
        except:
            pass
        try:
            del self.__pwm1_value
        except:
            pass
        try:
            del self.__pwm_ctrl0
        except:
            pass
        try:
            del self.__pwm_ctrl1
        except:
            pass
        try:
            del self.__pwm_ctrl2
        except:
            pass
        try:
            del self.__pwm_period
        except:
            pass
        try:
            del self.__analog_dac_value
        except:
            pass
        try:
            del self.__uc_ctrl
        except:
            pass
        try:
            del self.__ai_ctrl0
        except:
            pass
        try:
            del self.__ai_ctrl1
        except:
            pass
        try:
            del self.__pi_status
        except:
            pass
        try:
            del self.__ai0_jumper_setting_10_volts
        except:
            pass
        try:
            del self.__ai1_jumper_setting_10_volts
        except:
            pass
        try:
            del self.__ai0_raw_value
        except:
            pass
        try:
            del self.__ai1_raw_value
        except:
            pass
        try:
            del self.__ai2_raw_value
        except:
            pass
        try:
            del self.__ai3_raw_value
        except:
            pass
        try:
            del self.__temp0_raw_value
        except:
            pass
        try:
            del self.__temp1_raw_value
        except:
            pass
        try:
            del self.__temp2_raw_value
        except:
            pass
        try:
            del self.__temp3_raw_value
        except:
            pass
        try:
            del self.__humid0_raw_value
        except:
            pass
        try:
            del self.__humid1_raw_value
        except:
            pass
        try:
            del self.__humid2_raw_value
        except:
            pass
        try:
            del self.__humid3_raw_value
        except:
            pass
        try:
            del self.__ai0_value
        except:
            pass
        try:
            del self.__ai1_value
        except:
            pass
        try:
            del self.__ai2_value
        except:
            pass
        try:
            del self.__ai3_value
        except:
            pass
        try:
            del self.__temp0_value
        except:
            pass
        try:
            del self.__temp1_value
        except:
            pass
        try:
            del self.__temp2_value
        except:
            pass
        try:
            del self.__temp3_value
        except:
            pass
        try:
            del self.__humid0_value
        except:
            pass
        try:
            del self.__humid1_value
        except:
            pass
        try:
            del self.__humid2_value
        except:
            pass
        try:
            del self.__humid3_value
        except:
            pass
        try:
            del self.__analog0_dac_value
        except:
            pass
        try:
            del self.__analog1_dac_value
        except:
            pass

        try:
            GPIO.cleanup()
        except:
            pass
        
        if self.__is_spi_open:
            try:
                self.__spi.close()
            except:
                pass
            self.__spi = None
            self.__is_spi_open = False

        if self.__is_spi_dac_open:
            try:
                self.__spi_dac.close()
            except:
                pass
            self.__spi_dac = None
            self.__is_spi_dac_open = False

        try:
            del self.__spi
        except:
            pass
        try:
            del self.__spi_dac
        except:
            pass
        try:
            del self.__is_spi_open
        except:
            pass
        try:
            del self.__is_spi_dac_open
        except:
            pass


    def __transfer_spi_data(self, command, value0=0b00000000, value1=0b00000000, value2=0b00000000, auto_data=None):
        """
        Transfer data to microcontroller all in one block, data needs to be passed
        as byte array, the return value is also a byte array / list of the same byte count as was sent
        to the microcontroller.
        
        :param int command: Command for the microcontroller to execute, must be of one of the PIXTEND_SPI_xxx constants
        :param int value0: First byte value needed for 4 byte commands, optional depending on command
        :param int value1: Second byte value needed for 5 byte commands, optional depending on command
        :param int value2: Third byte value needed for 6 byte commands, optional depending on command 
        :param auto_data: Array of bytes to send to the microcontroller in Auto Mode
        :type auto_data: int[] or None
        :return: Array of bytes received from the microcontroller on the PiXtend board
        :rtype: int[]
        :raises Exception: If command does not have one of the allowed PIXTEND_SPI_xxx constants values
        :raises IOError: If SPI bus was not initialized (opened) before use
        """

        # auto_data is mutable, None is used as default, if no parameter gets passed, it is inited as an array.
        if auto_data is None:
            auto_data = [0]

        # <editor-fold desc="Region: Command length decision --> 4, 5 or 6 bytes to transfer">
        if (command == self.PIXTEND_SPI_SET_DOUT) or (command == self.PIXTEND_SPI_GET_DIN) or (
                    command == self.PIXTEND_SPI_SET_RELAY) or (command == self.PIXTEND_SPI_SET_GPIO) or (
                    command == self.PIXTEND_SPI_GET_GPIO) or (command == self.PIXTEND_SPI_GET_DOUT) or (
                    command == self.PIXTEND_SPI_GET_RELAY) or (command == self.PIXTEND_SPI_SET_SERVO0) or (
                    command == self.PIXTEND_SPI_SET_SERVO1) or (command == self.PIXTEND_SPI_SET_GPIO_CTRL) or (
                    command == self.PIXTEND_SPI_SET_UC_CTRL) or (command == self.PIXTEND_SPI_SET_RASPSTAT) or (
                    command == self.PIXTEND_SPI_GET_UC_STAT):
            to_send = [self.PIXTEND_SPI_HANDSHAKE, command, value0, self.PIXTEND_SPI_HANDSHAKE]
        elif (command == self.PIXTEND_SPI_GET_AIN0) or (command == self.PIXTEND_SPI_GET_AIN1) or (
                    command == self.PIXTEND_SPI_GET_AIN2) or (command == self.PIXTEND_SPI_GET_AIN3) or (
                    command == self.PIXTEND_SPI_GET_TEMP0) or (command == self.PIXTEND_SPI_GET_TEMP1) or (
                    command == self.PIXTEND_SPI_GET_TEMP2) or (command == self.PIXTEND_SPI_GET_TEMP3) or (
                    command == self.PIXTEND_SPI_GET_HUM0) or (command == self.PIXTEND_SPI_GET_HUM1) or (
                    command == self.PIXTEND_SPI_GET_HUM2) or (command == self.PIXTEND_SPI_GET_HUM3) or (
                    command == self.PIXTEND_SPI_SET_PWM0) or (command == self.PIXTEND_SPI_SET_PWM1) or (
                    command == self.PIXTEND_SPI_SET_AI_CTRL) or (command == self.PIXTEND_SPI_GET_UC_VER):
            to_send = [self.PIXTEND_SPI_HANDSHAKE, command, value0, value1, self.PIXTEND_SPI_NULL_BYTE]
        elif command == self.PIXTEND_SPI_SET_PWM_CTRL:
            to_send = [self.PIXTEND_SPI_HANDSHAKE, command, value0, value1, value2, self.PIXTEND_SPI_NULL_BYTE]
        elif command == self.PIXTEND_SPI_AUTO_MODE:
            to_send = auto_data
        else:
            raise Exception("__transfer_spi_data --> PIXTEND SPI command unknown!!!")
        # </editor-fold>

        if self.__is_spi_open:
            resp = self.__spi.xfer2(to_send)  # transfer byte data in one block with cs always active during transfer
        else:
            raise IOError("SPI not initialized!!! Use open method first!", "Method __transfer_spi_data was called!")

        return resp

    def __transfer_spi_dac_data(self, value0=0, value1=0):
        """
        Transfer data to the DAC on the PiXtend board all in one block, the DAC does not return anything.
        The DAC expects 2 bytes in a special format, see MCP4812 manual for more details.
        
        :param int value0: First byte for the DAC
        :param int value1: Second byte for the DAC
        """

        # Build data array to send to the DAC.
        to_send = [value0, value1]

        if self.__is_spi_dac_open:
            # transfer byte data in one block with cs always active during transfer
            resp = self.__spi_dac.xfer2(to_send)
        else:
            raise IOError("SPI for DAC not initialized!!! Use open_dac method first!",
                          "Method __transfer_spi_dac_data was called!")

        return resp

    def __reset_microcontroller(self):
        """
        DO NOT USE DURING NORMAL OPERATION - Internal function to reset the MC for testing
        """

        GPIO.output(self.PIXTEND_MC_RESET_PIN, True)
        time.sleep(1)
        GPIO.output(self.PIXTEND_MC_RESET_PIN, False)
        time.sleep(1)

    def open(self, spi_channel=0, spi_cs=0, spi_speed=100000):
        """
        Open SPI Master 0 with Chip Select 0 on the Raspberry Pi to start the communication with the microcontroller
        on the PiXtend board.
        
        :param int spi_channel: Number of the SPI master, default is 0, optional parameter
        :param int spi_cs:  Chip Select (CS) for the SPI master, default is 0, optional parameter
        :param int spi_speed:  SPI frequency, default 100 kHz, optional parameter
        :raises IOError: If SPI bus has already been opened
        """

        self.__spi_channel = spi_channel
        self.__spi_cs = spi_cs
        self.__spi_speed = spi_speed

        # Open SPI bus
        if not self.__is_spi_open:
            self.__spi = spidev.SpiDev(self.__spi_channel, self.__spi_cs)
            self.__spi.open(self.__spi_channel, self.__spi_cs)
            self.__spi.max_speed_hz = self.__spi_speed
            self.__is_spi_open = True
            # Get the current board and firmware version right away
            self.__uc_version_get()
        else:
            raise IOError("Error: SPI 0 CS 0 already opened!")

    def open_dac(self, spi_channel=0, spi_cs=1, spi_speed=100000):
        """
        Open SPI Master 0 with Chip Select 1 on the Raspberry Pi to start the communication
        with the DAC on the PiXtend board.
        
        :param int spi_channel: Number of the SPI master, default is 0, optional parameter
        :param int spi_cs: Chip Select (CS) for the SPI master for the DAC, default is 1, optional parameter
        :param int spi_speed: SPI frequency, default 100 kHz, optional parameter
        :raises IOError: If SPI bus has already been opened
        """

        self.__spi_channel = spi_channel
        self.__spi_cs = spi_cs
        self.__spi_speed = spi_speed

        # Set the dac gain permanently to 0
        # 0 = 2x (VOUT = 2 * VREF * D/4096),  where internal VREF = 2.048V.
        self.__analog_dac_value.bits.bit1 = 0

        # Set the dac output shutdown control bit permanently to 1
        # 1 = Active mode operation. VOUT is available.
        self.__analog_dac_value.bits.bit2 = 1

        # Open SPI bus
        if not self.__is_spi_dac_open:
            self.__spi_dac = spidev.SpiDev(self.__spi_channel, self.__spi_cs)
            self.__spi_dac.open(self.__spi_channel, self.__spi_cs)
            self.__spi_dac.max_speed_hz = self.__spi_speed
            self.__is_spi_dac_open = True
        else:
            raise IOError("SPI 0 CS 1 already opened!!!")

    def close(self):
        """
        Close SPI device, clean up Raspberry Pi GPIO device and set all variables to None.
        """
        try:
            self.__is_automatic_mode_active = None
            self.__use_fahrenheit = None
            self.__relays_states = None
            self.__digital_inputs_states = None
            self.__digital_outputs_states = None
            self.__analog_value = None
            self.__gpio_states_manual = None
            self.__gpio_states_auto_in = None
            self.__gpio_states_auto_out = None
            self.__servo0_value = None
            self.__servo1_value = None
            self.__pwm0_value = None
            self.__pwm1_value = None
            self.__pwm_ctrl0 = None
            self.__pwm_ctrl1 = None
            self.__pwm_ctrl2 = None
            self.__pwm_period = None
            self.__analog_dac_value = None
            self.__uc_ctrl = None
            self.__ai_ctrl0 = None
            self.__ai_ctrl1 = None
            self.__pi_status = None
            self.__ai0_jumper_setting_10_volts = None
            self.__ai1_jumper_setting_10_volts = None
            self.__ai0_raw_value = None
            self.__ai1_raw_value = None
            self.__ai2_raw_value = None
            self.__ai3_raw_value = None
            self.__temp0_raw_value = None
            self.__temp1_raw_value = None
            self.__temp2_raw_value = None
            self.__temp3_raw_value = None
            self.__humid0_raw_value = None
            self.__humid1_raw_value = None
            self.__humid2_raw_value = None
            self.__humid3_raw_value = None
            self.__ai0_value = None
            self.__ai1_value = None
            self.__ai2_value = None
            self.__ai3_value = None
            self.__temp0_value = None
            self.__temp1_value = None
            self.__temp2_value = None
            self.__temp3_value = None
            self.__humid0_value = None
            self.__humid1_value = None
            self.__humid2_value = None
            self.__humid3_value = None
            self.__analog0_dac_value = None
            self.__analog1_dac_value = None
        except:
            pass

        try:
            GPIO.cleanup()
        except:
            pass
        
        if self.__is_spi_open:
            try:
                self.__spi.close()
            except:
                pass
            self.__spi = None
            self.__is_spi_open = None

        if self.__is_spi_dac_open:
            try:
                self.__spi_dac.close()
            except:
                pass
            self.__spi_dac = None
            self.__is_spi_dac_open = None

    # <editor-fold desc="Region: Microcontroller Control Register, Version and Status">

    # **************************************************************************
    # Microcontroller Control Register, Version and Status
    # **************************************************************************

    def __uc_control_register_set(self, value):
        """
        Write one byte of data to the microcontroller's control register. See PiXtend manuals and AppNotes
        for more information on this topic. http://www.pixtend.de/files/manuals/AppNote_PiXtend_Control_Status_Bytes.pdf
        can be a good start.

        :param c_uint8 value: data to write to the microcontroller's control register
        """

        if not self.__is_automatic_mode_active:
            self.__transfer_spi_data(self.PIXTEND_SPI_SET_UC_CTRL, value)

    def __uc_status_register_get(self):
        """
        Get the microcontroller's status register via SPI.

        :return: The value received from the microcontroller 
        :rtype: c_uint8
        """

        # If the automatic mode is active, the data transfer is all done in the auto_mode method.
        if not self.__is_automatic_mode_active:
            value = self.__transfer_spi_data(self.PIXTEND_SPI_GET_UC_STAT)
            if len(value) >= 3:
                self.__uc_status = value[3]
            else:
                self.__uc_status = 0

        return self.__uc_status

    def __uc_version_get(self):
        """
        Get the board and firmware version from the microcontroller on the PiXtend board.
        """

        # Only do the transfer if automatic mode is off
        if not self.__is_automatic_mode_active:
            value = self.__transfer_spi_data(self.PIXTEND_SPI_GET_UC_VER)

            if len(value) >= 4:
                self.__uc_version.UC_VERSIONL = value[3]
                self.__uc_version.UC_VERSIONH = value[4]

    @property
    def uc_status(self):
        """
        Get the microcontroller status byte.
        
        :return: Status byte of the microcontroller on the PiXtend board
        :rtype: c_uint8
        """

        return self.__uc_status_register_get()

    @property
    def uc_board_version(self):
        """
        Get the PiXtend board version.
        
        Example:
        A value of 13 means board version 1.3.x

        :return: Board version of the PiXtend board
        :rtype: c_uint8
        """

        # If the automatic mode is off, the board version will be fetched via SPI.
        if not self.__is_automatic_mode_active:
            self.__uc_version_get()
        return self.__uc_version.UC_VERSIONH

    @property
    def uc_fw_version(self):
        """
        Get the microcontroller firmware version on the PiXtend board.
        
        Example:
        A value of 2 means firmware version 2.x is installed on the microcontroller

        :return: Firmware version of the microcontroller on the PiXtend board
        :rtype: c_uint8
        """

        # If the automatic mode is off, the firmware version will be fetched via SPI.
        if not self.__is_automatic_mode_active:
            self.__uc_version_get()
        return self.__uc_version.UC_VERSIONL

    @property
    def uc_control(self):
        """
        Get or Set the microcontroller's control register. If the automatic mode is off, setting
        a new value will have immediate effect.

        :return: Current value of the control byte in the microcontroller
        :rtype: c_uint8
        :raises ValueError: If the passed value is smaller then 0 or larger then 255
        """

        return self.__uc_ctrl.asByte

    @uc_control.setter
    def uc_control(self, value):
        if value >= 0 or value <= 255:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 to 255")
        self.__uc_ctrl.asByte = value
        self.__uc_control_register_set(value)

    # </editor-fold>

    # <editor-fold desc="Region: GPIO Control, GPIO configuration, Settings for DHT11/22 on/off, GPIO value In/Out">

    # **************************************************************************
    # GPIO Control - GPIO In/Out - DHT11/22 Settings
    # **************************************************************************

    def __gpio_ctrl_set(self, value):
        """
        Set the PiXtend GPIO control register to the value of 'value'.
        """

        # If the automatic mode is active, the data transfer is all done in the auto_mode method.
        if not self.__is_automatic_mode_active:
            self.__transfer_spi_data(self.PIXTEND_SPI_SET_GPIO_CTRL, value)

    def __gpio_ctrl_change_value(self, value, bit_num):
        """
        Change one bit within the INT variable __gpio_ctrl based on value and bit_num.
        If value == 0 the bit at position bit_num is cleared, if value == 1 then the bit at position
        bit_num is set.
        
        :param int value: Value of the bit, 0 = off and 1 = on
        :param int bit_num: Bit to set or to clear, parameter is zero based 
        """

        if value == 0:
            self.__gpio_ctrl = self.__clear_bit(self.__gpio_ctrl, bit_num)
        if value == 1:
            self.__gpio_ctrl = self.__set_bit(self.__gpio_ctrl, bit_num)

        self.__gpio_ctrl_set(self.__gpio_ctrl)

    @property
    def gpio0_direction(self):
        """
        Get or Set the direction of GPIO 0, input or output is possible.
          
        Example:
        p.gpio0_direction = p.GPIO_INPUT
        p.gpio0_direction = p.GPIO_OUTPUT
        
        or
        
        p.gpio0_direction = 0 # Input
        p.gpio0_direction = 1 # Output
        """

        return self.__test_bit(self.__gpio_ctrl, self.BIT_0)

    @gpio0_direction.setter
    def gpio0_direction(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = Input, 1 = Output")
        self.__gpio_ctrl_change_value(value, self.BIT_0)

    @property
    def gpio1_direction(self):
        """
        Get or Set the direction of GPIO 1, input or output is possible.

        Example:
        p.gpio1_direction = p.GPIO_INPUT
        p.gpio1_direction = p.GPIO_OUTPUT

        or

        p.gpio1_direction = 0 # Input
        p.gpio1_direction = 1 # Output
        """

        return self.__test_bit(self.__gpio_ctrl, self.BIT_1)

    @gpio1_direction.setter
    def gpio1_direction(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = Input, 1 = Output")
        self.__gpio_ctrl_change_value(value, self.BIT_1)

    @property
    def gpio2_direction(self):
        """
        Get or Set the direction of GPIO 2, input or output is possible.

        Example:
        p.gpio2_direction = p.GPIO_INPUT
        p.gpio2_direction = p.GPIO_OUTPUT

        or

        p.gpio2_direction = 0 # Input
        p.gpio2_direction = 1 # Output
        """

        return self.__test_bit(self.__gpio_ctrl, self.BIT_2)

    @gpio2_direction.setter
    def gpio2_direction(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = Input, 1 = Output")
        self.__gpio_ctrl_change_value(value, self.BIT_2)

    @property
    def gpio3_direction(self):
        """
        Get or Set the direction of GPIO 3, input or output is possible.

        Example:
        p.gpio3_direction = p.GPIO_INPUT
        p.gpio3_direction = p.GPIO_OUTPUT

        or

        p.gpio3_direction = 0 # Input
        p.gpio3_direction = 1 # Output
        """

        return self.__test_bit(self.__gpio_ctrl, self.BIT_3)

    @gpio3_direction.setter
    def gpio3_direction(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = Input, 1 = Output")
        self.__gpio_ctrl_change_value(value, self.BIT_3)

    @property
    def dht0(self):
        """
        Get or Set the 1-Wire setting for GPIO 0. Default is 'off' (0), if set to 'on' (1) means 1-wire sensors
        like DHT11, DHT22 and AM2302 can be used at this GPIO. The direction bit (input/output) will be ignored if
        this property is set to 'on' (1).
        """

        return self.__test_bit(self.__gpio_ctrl, self.BIT_4)

    @dht0.setter
    def dht0(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__gpio_ctrl_change_value(value, self.BIT_4)

    @property
    def dht1(self):
        """
        Get or Set the 1-Wire setting for GPIO 1. Default is 'off' (0), if set to 'on' (1) means 1-wire sensors
        like DHT11, DHT22 and AM2302 can be used at this GPIO. The direction bit (input/output) will be ignored if
        this property is set to 'on' (1).
        """

        return self.__test_bit(self.__gpio_ctrl, self.BIT_5)

    @dht1.setter
    def dht1(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__gpio_ctrl_change_value(value, self.BIT_5)

    @property
    def dht2(self):
        """
        Get or Set the 1-Wire setting for GPIO 2. Default is 'off' (0), if set to 'on' (1) means 1-wire sensors
        like DHT11, DHT22 and AM2302 can be used at this GPIO. The direction bit (input/output) will be ignored if
        this property is set to 'on' (1).
        """

        return self.__test_bit(self.__gpio_ctrl, self.BIT_6)

    @dht2.setter
    def dht2(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__gpio_ctrl_change_value(value, self.BIT_6)

    @property
    def dht3(self):
        """
        Get or Set the 1-Wire setting for GPIO 3. Default is 'off' (0), if set to 'on' (1) means 1-wire sensors
        like DHT11, DHT22 and AM2302 can be used at this GPIO. The direction bit (input/output) will be ignored if
        this property is set to 'on' (1).
        """

        return self.__test_bit(self.__gpio_ctrl, self.BIT_7)

    @dht3.setter
    def dht3(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__gpio_ctrl_change_value(value, self.BIT_7)

    def __gpio_states_get(self):
        """
        Get the current GPIO state from the microcontroller on the PiXtend board.
        """

        # Only do SPI data transfer if the Auto Mode is not active.
        if not self.__is_automatic_mode_active:
            value = self.__transfer_spi_data(self.PIXTEND_SPI_GET_GPIO)
            if len(value) >= 3:
                self.__gpio_states_manual = value[3]
            else:
                self.__gpio_states_manual = 0

            self.__gpio_states_auto_in = self.__gpio_states_manual

            return self.__gpio_states_manual
        else:
            return self.__gpio_states_auto_in

    def __gpio_states_set(self, value):
        """
        Set the states of the GPIOs on the PiXtend board.
        """

        resp = self.__transfer_spi_data(self.PIXTEND_SPI_SET_GPIO, value)
        return resp

    def __gpio_states_change_value(self, value, bit_num):
        """
        Change the value of a single bit in the INT variable __gpio_states_manual given by bit_num to the
        value given by value. In Auto Mode the variable __gpio_states_auto_out is used instead.
        
        :param int value: Value of the bit, 0 = off and 1 = on 
        :param int bit_num: Bit to set or to clear, parameter is zero based 
        """

        if not self.__is_automatic_mode_active:
            if value == 0:
                self.__gpio_states_manual = self.__clear_bit(self.__gpio_states_manual, bit_num)
            if value == 1:
                self.__gpio_states_manual = self.__set_bit(self.__gpio_states_manual, bit_num)

            self.__gpio_states_auto_out = self.__gpio_states_manual

            self.__gpio_states_set(self.__gpio_states_manual)
        else:
            if value == 0:
                self.__gpio_states_auto_out = self.__clear_bit(self.__gpio_states_auto_out, bit_num)
            if value == 1:
                self.__gpio_states_auto_out = self.__set_bit(self.__gpio_states_auto_out, bit_num)

    @property
    def gpio0(self):
        """
        Get or Set the state of GPIO 0. The value 0 means 'off' and a value of 1 means 'on'.
        
        Example:
        p.gpio0 = p.ON # Turns the GPIO on
        p.gpio0 = p.OFF # Turns the GPIO off
        or use
        p.gpio0 = 1 # Turns the GPIO on
        p.gpio0 = 0 # Turns the GPIO off
        """

        return self.__test_bit(self.__gpio_states_get(), self.BIT_0)

    @gpio0.setter
    def gpio0(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        bit_num = self.BIT_0
        if self.__test_bit(self.__gpio_ctrl, bit_num) == 1:
            self.__gpio_states_change_value(value, bit_num)
        else:
            raise IOError("IOError: GPIO 0 configured as INPUT! Cannot use as OUTPUT!",
                          "- DirectionBit=" + str(self.__test_bit(self.__gpio_ctrl, bit_num)))

    @property
    def gpio1(self):
        """
        Get or Set the state of GPIO 1. The value 0 means 'off' and a value of 1 means 'on'.
        
        Example:
        p.gpio1 = p.ON # Turns the GPIO on
        p.gpio1 = p.OFF # Turns the GPIO off
        or use
        p.gpio1 = 1 # Turns the GPIO on
        p.gpio1 = 0 # Turns the GPIO off
        """

        return self.__test_bit(self.__gpio_states_get(), self.BIT_1)

    @gpio1.setter
    def gpio1(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        bit_num = self.BIT_1
        if self.__test_bit(self.__gpio_ctrl, bit_num) == 1:
            self.__gpio_states_change_value(value, bit_num)
        else:
            raise IOError("IOError: GPIO 1 configured as INPUT! Cannot use as OUTPUT!",
                          "- DirectionBit=" + str(self.__test_bit(self.__gpio_ctrl, bit_num)))

    @property
    def gpio2(self):
        """
        Get or Set the state of GPIO 2. The value 0 means 'off' and a value of 1 means 'on'.
        
        Example:
        p.gpio2 = p.ON # Turns the GPIO on
        p.gpio2 = p.OFF # Turns the GPIO off
        or use
        p.gpio2 = 1 # Turns the GPIO on
        p.gpio2 = 0 # Turns the GPIO off
        """

        return self.__test_bit(self.__gpio_states_get(), self.BIT_2)

    @gpio2.setter
    def gpio2(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        bit_num = self.BIT_2
        if self.__test_bit(self.__gpio_ctrl, bit_num) == 1:
            self.__gpio_states_change_value(value, bit_num)
        else:
            raise IOError("IOError: GPIO 2 configured as INPUT! Cannot use as OUTPUT!",
                          "- DirectionBit=" + str(self.__test_bit(self.__gpio_ctrl, bit_num)))

    @property
    def gpio3(self):
        """
        Get or Set the state of GPIO 3. The value 0 means 'off' and a value of 1 means 'on'.
        
        Example:
        p.gpio3 = p.ON # Turns the GPIO on
        p.gpio3 = p.OFF # Turns the GPIO off
        or use
        p.gpio3 = 1 # Turns the GPIO on
        p.gpio3 = 0 # Turns the GPIO off
        """

        return self.__test_bit(self.__gpio_states_get(), self.BIT_3)

    @gpio3.setter
    def gpio3(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        bit_num = self.BIT_3
        if self.__test_bit(self.__gpio_ctrl, bit_num) == 1:
            self.__gpio_states_change_value(value, bit_num)
        else:
            raise IOError("IOError: GPIO 3 configured as INPUT! Cannot use as OUTPUT!",
                          "- DirectionBit=" + str(self.__test_bit(self.__gpio_ctrl, bit_num)))

    # </editor-fold>

    # <editor-fold desc="Region: Servo Control ">

    # **************************************************************************
    # Servo Control 
    # **************************************************************************

    @property
    def servo0(self):
        """
        Get or Set the value for PWM 0 in servo mode. Possible values are 0 to 250.
        """

        return self.__servo0_value.value

    @servo0.setter
    def servo0(self, value):
        if 0 <= value <= 250:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use a value from 0 to 250")
        self.__servo0_value.value = value
        if not self.__is_automatic_mode_active:
            self.__transfer_spi_data(self.PIXTEND_SPI_SET_SERVO0, self.__servo0_value.value)
        else:
            if self.pwm_ctrl_mode == 0:
                self.__pwm0_value.bytes.byte0 = value
                self.__pwm0_value.bytes.byte1 = 0
            else:
                raise ValueError("Mode error! Servo mode was used, but outputs are set to PWM output.")

    @property
    def servo1(self):
        """
        Get or Set the value for PWM 1 in servo mode. Possible values are 0 to 250.
        """

        return self.__servo1_value.value

    @servo1.setter
    def servo1(self, value):
        if 0 <= value <= 250:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use a value from 0 to 250")
        self.__servo1_value.value = value
        if not self.__is_automatic_mode_active:
            self.__transfer_spi_data(self.PIXTEND_SPI_SET_SERVO1, self.__servo1_value.value)
        else:
            if self.pwm_ctrl_mode == 0:
                self.__pwm1_value.bytes.byte0 = value
                self.__pwm1_value.bytes.byte1 = 0
            else:
                raise ValueError("Mode error! Servo mode was used, but outputs are set to PWM output.")

    # </editor-fold>

    # <editor-fold desc="Region: PWM Control">

    # **************************************************************************
    # PWM Control 
    # **************************************************************************
    # Refer to the PiXtend's documentation for the 3 PWM Control registers
    # set_pwm_control: value0 = PWM_CTRL0, value1 = PWM_CTRL1, value2 = PWM_CTRL2

    def __pwm_control_set(self, value0, value1, value2):
        """
        Set the PWM control registers in the microcontroller on the PiXtend board to the values of value0,
        value1 and value2.
        """

        resp = self.__transfer_spi_data(self.PIXTEND_SPI_SET_PWM_CTRL, value0, value1, value2)
        return resp

    def pwm_ctrl_configure(self):
        """
        Configures the PWM control of the microcontroller on the PiXtend board. The data transferred includes
        all PWM settings like the Mode, OD setting and the Clock Select for the PWMs as well as the frequency.
        """

        self.__pwm_ctrl1.asByte = self.__pwm_period.bytes.byte0
        self.__pwm_ctrl2.asByte = self.__pwm_period.bytes.byte1

        if not self.__is_automatic_mode_active:
            self.__pwm_control_set(self.__pwm_ctrl0.asByte, self.__pwm_ctrl1.asByte, self.__pwm_ctrl2.asByte)

    @property
    def pwm_ctrl_mode(self):
        """
        Get or Set the PWM mode. A value of 0 means the PWMs are in servo mode, a value of 1 means both PWMs are
        in PWM mode.
        """

        return self.__pwm_ctrl0.b.bit0

    @pwm_ctrl_mode.setter
    def pwm_ctrl_mode(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = servo or 1 = pwm")
        self.__pwm_ctrl0.b.bit0 = value

    @property
    def pwm_ctrl_od0(self):
        """
        Get or Set the Over Drive (OD) value for PWM 0. A value of 0 means 'off' and 1 means 'on'.
        """

        return self.__pwm_ctrl0.b.bit1

    @pwm_ctrl_od0.setter
    def pwm_ctrl_od0(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 or 1")
        self.__pwm_ctrl0.b.bit1 = value

    @property
    def pwm_ctrl_od1(self):
        """
        Get or Set the Over Drive (OD) value for PWM 1. A value of 0 means 'off' and 1 means 'on'.
        """

        return self.__pwm_ctrl0.b.bit2

    @pwm_ctrl_od1.setter
    def pwm_ctrl_od1(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 or 1")
        self.__pwm_ctrl0.b.bit2 = value

    @property
    def pwm_ctrl_cs0(self):
        """
        Get or Set the Clock Select bit 0 (CS0) for both PWMs. This setting will be ignored if PWMs are in servo mode.
        A value of 1 means this CS is 'on' (selected) and 0 means 'off' (not selected).
        """

        return self.__pwm_ctrl0.b.bit5

    @pwm_ctrl_cs0.setter
    def pwm_ctrl_cs0(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 or 1")
        self.__pwm_ctrl0.b.bit5 = value

    @property
    def pwm_ctrl_cs1(self):
        """
        Get or Set the Clock Select bit 1 (CS1) for both PWMs. This setting will be ignored if PWMs are in servo mode.
        A value of 1 means this CS is 'on' (selected) and 0 means 'off' (not selected).
        """

        return self.__pwm_ctrl0.b.bit6

    @pwm_ctrl_cs1.setter
    def pwm_ctrl_cs1(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 or 1")
        self.__pwm_ctrl0.b.bit6 = value

    @property
    def pwm_ctrl_cs2(self):
        """
        Get or Set the Clock Select bit 2 (CS2) for both PWMs. This setting will be ignored if PWMs are in servo mode.
        A value of 1 means this CS is 'on' (selected) and 0 means 'off' (not selected).
        """

        return self.__pwm_ctrl0.b.bit7

    @pwm_ctrl_cs2.setter
    def pwm_ctrl_cs2(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 or 1")
        self.__pwm_ctrl0.b.bit7 = value

    @property
    def pwm_ctrl_period(self):
        """
        Get or Set the frequency/signal period length of the PWMs. Values from 0 to 65000 are allowed.
        
        Example:
            pwm_ctrl_cs0 = 1, pwm_ctrl_cs1 = 1, pwm_ctrl_cs2 = 0, pwm_ctrl_mode = 1 and pwm_ctrl_period = 1000
            PWM Period length = uMC Freq / Prescaler / PWM_CTRL1..2
            PWM Period length = 16 MHz   /    64     /  1000        = 250 Hz
        """

        return self.__pwm_period.asUint16

    @pwm_ctrl_period.setter
    def pwm_ctrl_period(self, value):
        if 0 <= value <= 65000:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use a value from 0 to 65000")
        self.__pwm_period.asUint16 = value

    @property
    def pwm0(self):
        """
        Get or Set the PWM 0 duty cycle. This value has to correspond to the PWM freq/signal period configuration.
        The allowed values for PWM 0 are 0 to 65000.
        
        Example:
            pwm_ctrl_cs0 = 1, pwm_ctrl_cs1 = 1, pwm_ctrl_cs2 = 0, pwm_ctrl_mode = 1 and pwm_ctrl_period = 5000
            * If pwm0 is set to 2500 the duty cycle will be 50%
            * If pwm0 > pwm_ctrl_period the PWM channel will be continuously logical 1
            * If pwm0 = 0 the PWM channel will be continuously logical 0
        """

        return self.__pwm0_value.asUint16

    @pwm0.setter
    def pwm0(self, value):
        if 0 <= value <= 65000:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use a value from 0 to 65000")
        self.__pwm0_value.asUint16 = value
        if not self.__is_automatic_mode_active:
            self.__transfer_spi_data(self.PIXTEND_SPI_SET_PWM0, self.__pwm0_value.bytes.byte0,
                                     self.__pwm0_value.bytes.byte1)

    @property
    def pwm1(self):
        """
        Get or Set the PWM 1 duty cycle. This value has to correspond to the PWM freq/signal period configuration.
        The allowed values for PWM 1 are 0 to 65000.

        Example:
            pwm_ctrl_cs0 = 1, pwm_ctrl_cs1 = 1, pwm_ctrl_cs2 = 0, pwm_ctrl_mode = 1 and pwm_ctrl_period = 5000
            * If pwm1 is set to 2500 the duty cycle will be 50%.
            * If pwm1 > pwm_ctrl_period the PWM channel will be continuously logical 1
            * If pwm1 = 0 the PWM channel will be continuously logical 0
        """

        return self.__pwm1_value.asUint16

    @pwm1.setter
    def pwm1(self, value):
        if 0 <= value <= 65000:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use a value from 0 to 65000")
        self.__pwm1_value.asUint16 = value
        if not self.__is_automatic_mode_active:
            self.__transfer_spi_data(self.PIXTEND_SPI_SET_PWM1, self.__pwm1_value.bytes.byte0,
                                     self.__pwm1_value.bytes.byte1)

    # </editor-fold>

    # <editor-fold desc="Region: Analog Inputs 0 - 3">

    # **************************************************************************
    # Analog Inputs 0 - 3
    # **************************************************************************
    def __analog_input_register_set(self):
        """
        Set the analog control register of the microcontroller on the PiXtend board. 
        """

        self.__transfer_spi_data(self.PIXTEND_SPI_SET_AI_CTRL, self.__ai_ctrl0.asByte, self.__ai_ctrl1.asByte)

    def __analog_input_get(self, command):
        """
        Get the analog value from one of the 4 analog inputs on the PiXtend board.
        
        :param int command: One of the 4 PIXTEND_SPI_GET_AINx constants values specifying AI0-3
        :return: A 16 bit value representing the analog value of the requested AIx
        :rtype: c_uint16
        """

        # Only transfer data is Auto Mode is not active.
        if not self.__is_automatic_mode_active:
            # Wait, this method might have been called before. The microcontroller needs time for processing.
            time.sleep(0.05)
            self.__transfer_spi_data(command)
            # Wait again after the first transfer, the microcontroller now needs time to start and complete its A/D
            # conversion. Then the 'real' data can be requested.
            time.sleep(0.05)
            resp = self.__transfer_spi_data(command)
            # Wait for security in case this or another method with SPI data transfer is called right after this one.
            time.sleep(0.05)

            if len(resp) >= 4:
                self.__analog_value.bytes.byte0 = resp[3]
                self.__analog_value.bytes.byte1 = resp[4]
            else:
                self.__analog_value.asUint16 = 0
        else:
            if command == self.PIXTEND_SPI_GET_AIN0:
                self.__analog_value.asUint16 = self.__ai0_raw_value.asUint16
            elif command == self.PIXTEND_SPI_GET_AIN1:
                self.__analog_value.asUint16 = self.__ai1_raw_value.asUint16
            elif command == self.PIXTEND_SPI_GET_AIN2:
                self.__analog_value.asUint16 = self.__ai2_raw_value.asUint16
            elif command == self.PIXTEND_SPI_GET_AIN3:
                self.__analog_value.asUint16 = self.__ai3_raw_value.asUint16
            else:
                self.__analog_value.asUint16 = 0

        return self.__analog_value.asUint16

    @property
    def analog_input0_raw(self):
        """
        Get the raw value of analog input 0. 
        """

        return self.__analog_input_get(self.PIXTEND_SPI_GET_AIN0)

    @property
    def analog_input1_raw(self):
        """
        Get the raw value of analog input 1. 
        """

        return self.__analog_input_get(self.PIXTEND_SPI_GET_AIN1)

    @property
    def analog_input2_raw(self):
        """
        Get the raw value of analog input 2. 
        """

        return self.__analog_input_get(self.PIXTEND_SPI_GET_AIN2)

    @property
    def analog_input3_raw(self):
        """
        Get the raw value of analog input 3. 
        """

        return self.__analog_input_get(self.PIXTEND_SPI_GET_AIN3)

    @property
    def analog_input0_10volts_jumper(self):
        """
        Get or Set the 10 volts jumper setting, depending if the jumper was physically set on the PiXtend board.
        The library needs to know this setting to perform correct calculations of the raw analog
        values of the analog inputs when they are converted their final float value. Applies only to AI0 and AI1
        as these 2 inputs measure voltage.
        """

        return self.__ai0_jumper_setting_10_volts

    @analog_input0_10volts_jumper.setter
    def analog_input0_10volts_jumper(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use: 0 = 5 volts, 1 = 10 volts")
        self.__ai0_jumper_setting_10_volts = value

    @property
    def analog_input1_10volts_jumper(self):
        """
        Get or Set the 10 volts jumper setting, depending if the jumper was physically set on the PiXtend board.
        The library needs to know this setting to perform correct calculations of the raw analog
        values of the analog inputs when they are converted their final float value. Applies only to AI0 and AI1
        as these 2 inputs measure voltage.
        """

        return self.__ai1_jumper_setting_10_volts

    @analog_input1_10volts_jumper.setter
    def analog_input1_10volts_jumper(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use: 0 = 5 volts, 1 = 10 volts")
        self.__ai1_jumper_setting_10_volts = value

    @property
    def analog_input0_nos(self):
        """
        Get or Set the Number of Samples (NoS) the analog input 0 should take from the incoming analog signal.
        Possible NoS values are 1, 5, 10 (default) and 50. If something is wrong -1 is returned.
        
        :return: Decimal value of the Number of Samples (NoS) 
        :rtype: int
        :raises ValueError: If passed value is not 1, 5, 10, 50
        """

        if self.__ai_ctrl0.b.bit0 == 0 and self.__ai_ctrl0.b.bit1 == 0:
            return 10
        elif self.__ai_ctrl0.b.bit0 == 1 and self.__ai_ctrl0.b.bit1 == 0:
            return 1
        elif self.__ai_ctrl0.b.bit0 == 0 and self.__ai_ctrl0.b.bit1 == 1:
            return 5
        elif self.__ai_ctrl0.b.bit0 == 1 and self.__ai_ctrl0.b.bit1 == 1:
            return 50
        else:
            return -1

    @analog_input0_nos.setter
    def analog_input0_nos(self, value):
        if value == 1 or value == 5 or value == 10 or value == 50:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use: 1, 5, 10 or 50 NoS")

        if value == 1:
            self.__ai_ctrl0.b.bit0 = 1
            self.__ai_ctrl0.b.bit1 = 0
        elif value == 5:
            self.__ai_ctrl0.b.bit0 = 0
            self.__ai_ctrl0.b.bit1 = 1
        elif value == 10:
            self.__ai_ctrl0.b.bit0 = 0
            self.__ai_ctrl0.b.bit1 = 0
        elif value == 50:
            self.__ai_ctrl0.b.bit0 = 1
            self.__ai_ctrl0.b.bit1 = 1
        else:
            self.__ai_ctrl0.b.bit0 = 0
            self.__ai_ctrl0.b.bit1 = 0

        if not self.__is_automatic_mode_active:
            self.__analog_input_register_set()

    @property
    def analog_input1_nos(self):
        """
        Get or Set the Number of Samples (NoS) the analog input 1 should take from the incoming analog signal.
        Possible NoS values are 1, 5, 10 (default) and 50. If something is wrong -1 is returned.

        :return: Decimal value of the Number of Samples (NoS) 
        :rtype: int
        :raises ValueError: If passed value is not 1, 5, 10, 50
        """

        if self.__ai_ctrl0.b.bit2 == 0 and self.__ai_ctrl0.b.bit3 == 0:
            return 10
        elif self.__ai_ctrl0.b.bit2 == 1 and self.__ai_ctrl0.b.bit3 == 0:
            return 1
        elif self.__ai_ctrl0.b.bit2 == 0 and self.__ai_ctrl0.b.bit3 == 1:
            return 5
        elif self.__ai_ctrl0.b.bit2 == 1 and self.__ai_ctrl0.b.bit3 == 1:
            return 50
        else:
            return -1

    @analog_input1_nos.setter
    def analog_input1_nos(self, value):
        if value == 1 or value == 5 or value == 10 or value == 50:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use: 1, 5, 10 or 50 NoS")

        if value == 1:
            self.__ai_ctrl0.b.bit2 = 1
            self.__ai_ctrl0.b.bit3 = 0
        elif value == 5:
            self.__ai_ctrl0.b.bit2 = 0
            self.__ai_ctrl0.b.bit3 = 1
        elif value == 10:
            self.__ai_ctrl0.b.bit2 = 0
            self.__ai_ctrl0.b.bit3 = 0
        elif value == 50:
            self.__ai_ctrl0.b.bit2 = 1
            self.__ai_ctrl0.b.bit3 = 1
        else:
            self.__ai_ctrl0.b.bit2 = 0
            self.__ai_ctrl0.b.bit3 = 0

        if not self.__is_automatic_mode_active:
            self.__analog_input_register_set()

    @property
    def analog_input2_nos(self):
        """
        Get or Set the Number of Samples (NoS) the analog input 2 should take from the incoming analog signal.
        Possible NoS values are 1, 5, 10 (default) and 50. If something is wrong -1 is returned.

        :return: Decimal value of the Number of Samples (NoS) 
        :rtype: int
        :raises ValueError: If passed value is not 1, 5, 10, 50
        """

        if self.__ai_ctrl0.b.bit4 == 0 and self.__ai_ctrl0.b.bit5 == 0:
            return 10
        elif self.__ai_ctrl0.b.bit4 == 1 and self.__ai_ctrl0.b.bit5 == 0:
            return 1
        elif self.__ai_ctrl0.b.bit4 == 0 and self.__ai_ctrl0.b.bit5 == 1:
            return 5
        elif self.__ai_ctrl0.b.bit4 == 1 and self.__ai_ctrl0.b.bit5 == 1:
            return 50
        else:
            return -1

    @analog_input2_nos.setter
    def analog_input2_nos(self, value):
        if value == 1 or value == 5 or value == 10 or value == 50:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use: 1, 5, 10 or 50 NoS")

        if value == 1:
            self.__ai_ctrl0.b.bit4 = 1
            self.__ai_ctrl0.b.bit5 = 0
        elif value == 5:
            self.__ai_ctrl0.b.bit4 = 0
            self.__ai_ctrl0.b.bit5 = 1
        elif value == 10:
            self.__ai_ctrl0.b.bit4 = 0
            self.__ai_ctrl0.b.bit5 = 0
        elif value == 50:
            self.__ai_ctrl0.b.bit4 = 1
            self.__ai_ctrl0.b.bit5 = 1
        else:
            self.__ai_ctrl0.b.bit4 = 0
            self.__ai_ctrl0.b.bit5 = 0

        if not self.__is_automatic_mode_active:
            self.__analog_input_register_set()

    @property
    def analog_input3_nos(self):
        """
        Get or Set the Number of Samples (NoS) the analog input 3 should take from the incoming analog signal.
        Possible NoS values are 1, 5, 10 (default) and 50. If something is wrong -1 is returned.

        :return: Decimal value of the Number of Samples (NoS) 
        :rtype: int
        :raises ValueError: If passed value is not 1, 5, 10, 50
        """

        if self.__ai_ctrl0.b.bit6 == 0 and self.__ai_ctrl0.b.bit7 == 0:
            return 10
        elif self.__ai_ctrl0.b.bit6 == 1 and self.__ai_ctrl0.b.bit7 == 0:
            return 1
        elif self.__ai_ctrl0.b.bit6 == 0 and self.__ai_ctrl0.b.bit7 == 1:
            return 5
        elif self.__ai_ctrl0.b.bit6 == 1 and self.__ai_ctrl0.b.bit7 == 1:
            return 50
        else:
            return -1

    @analog_input3_nos.setter
    def analog_input3_nos(self, value):
        if value == 1 or value == 5 or value == 10 or value == 50:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use: 1, 5, 10 or 50 NoS")

        if value == 1:
            self.__ai_ctrl0.b.bit6 = 1
            self.__ai_ctrl0.b.bit7 = 0
        elif value == 5:
            self.__ai_ctrl0.b.bit6 = 0
            self.__ai_ctrl0.b.bit7 = 1
        elif value == 10:
            self.__ai_ctrl0.b.bit6 = 0
            self.__ai_ctrl0.b.bit7 = 0
        elif value == 50:
            self.__ai_ctrl0.b.bit6 = 1
            self.__ai_ctrl0.b.bit7 = 1
        else:
            self.__ai_ctrl0.b.bit6 = 0
            self.__ai_ctrl0.b.bit7 = 0

        if not self.__is_automatic_mode_active:
            self.__analog_input_register_set()

    @property
    def analog_input_nos_freq(self):
        """
        Get or Set the Clock Select of the A/D converter of the microcontroller on the PiXtend board.
        Possible float values are 0.125, 0.250, 0.500, 1.0, 2.0, 4.0 and 8.0. The unit is Mhz.
        
        :return: Float value of the currently set A/D converter frequency
        :rtype: float
        """

        if self.__ai_ctrl1.b.bit5 == 0 and self.__ai_ctrl1.b.bit6 == 0 and self.__ai_ctrl1.b.bit7 == 0:
            return 0.125
        elif self.__ai_ctrl1.b.bit5 == 0 and self.__ai_ctrl1.b.bit6 == 1 and self.__ai_ctrl1.b.bit7 == 1:
            return 0.250
        elif self.__ai_ctrl1.b.bit5 == 1 and self.__ai_ctrl1.b.bit6 == 0 and self.__ai_ctrl1.b.bit7 == 1:
            return 0.500
        elif self.__ai_ctrl1.b.bit5 == 0 and self.__ai_ctrl1.b.bit6 == 0 and self.__ai_ctrl1.b.bit7 == 1:
            return 1.0
        elif self.__ai_ctrl1.b.bit5 == 1 and self.__ai_ctrl1.b.bit6 == 1 and self.__ai_ctrl1.b.bit7 == 0:
            return 2.0
        elif self.__ai_ctrl1.b.bit5 == 0 and self.__ai_ctrl1.b.bit6 == 1 and self.__ai_ctrl1.b.bit7 == 0:
            return 4.0
        elif self.__ai_ctrl1.b.bit5 == 1 and self.__ai_ctrl1.b.bit6 == 0 and self.__ai_ctrl1.b.bit7 == 0:
            return 8.0
        else:
            return -1.0

    @analog_input_nos_freq.setter
    def analog_input_nos_freq(self, value):
        if value == 0.125 or value == 0.250 or value == 0.500 or value == 1.0 or \
                        value == 2.0 or value == 4.0 or value == 8.0:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) +
                             " not allowed! - Use (MHz): 0.125, 0.250, 0.500, 1.0, 2.0, 4.0 or 8.0")

        if value == 0.125:
            self.__ai_ctrl1.b.bit5 = 0
            self.__ai_ctrl1.b.bit6 = 0
            self.__ai_ctrl1.b.bit7 = 0
        elif value == 0.250:
            self.__ai_ctrl1.b.bit5 = 0
            self.__ai_ctrl1.b.bit6 = 1
            self.__ai_ctrl1.b.bit7 = 1
        elif value == 0.500:
            self.__ai_ctrl1.b.bit5 = 1
            self.__ai_ctrl1.b.bit6 = 0
            self.__ai_ctrl1.b.bit7 = 1
        elif value == 1.0:
            self.__ai_ctrl1.b.bit5 = 0
            self.__ai_ctrl1.b.bit6 = 0
            self.__ai_ctrl1.b.bit7 = 1
        elif value == 2.0:
            self.__ai_ctrl1.b.bit5 = 1
            self.__ai_ctrl1.b.bit6 = 1
            self.__ai_ctrl1.b.bit7 = 0
        elif value == 4.0:
            self.__ai_ctrl1.b.bit5 = 0
            self.__ai_ctrl1.b.bit6 = 1
            self.__ai_ctrl1.b.bit7 = 0
        elif value == 8.0:
            self.__ai_ctrl1.b.bit5 = 1
            self.__ai_ctrl1.b.bit6 = 0
            self.__ai_ctrl1.b.bit7 = 0
        else:
            self.__ai_ctrl1.b.bit5 = 0
            self.__ai_ctrl1.b.bit6 = 0
            self.__ai_ctrl1.b.bit7 = 0

        if not self.__is_automatic_mode_active:
            self.__analog_input_register_set()

    @property
    def analog_input0(self):
        """
        Get analog input 0 value as float in Volts. The returned value is based on the 10 volts jumper setting. 
        """

        # Only get data directly if Auto Mode is not active.
        if not self.__is_automatic_mode_active:
            if self.__ai0_jumper_setting_10_volts == 1:
                self.__ai0_value = c_float(self.analog_input0_raw * (10.0 / 1024))
            else:
                self.__ai0_value = c_float(self.analog_input0_raw * (5.0 / 1024))

        return self.__ai0_value.value

    @property
    def analog_input1(self):
        """
        Get analog input 1 value as float in Volts. The returned value is based on the 10 volts jumper setting. 
        """

        # Only get data directly if Auto Mode is not active.
        if not self.__is_automatic_mode_active:
            if self.__ai1_jumper_setting_10_volts == 1:
                self.__ai1_value = c_float(self.analog_input1_raw * (10.0 / 1024))
            else:
                self.__ai1_value = c_float(self.analog_input1_raw * (5.0 / 1024))

        return self.__ai1_value.value

    @property
    def analog_input2(self):
        """
        Get analog input 2 value as float in Ampere (mA).
        """

        # Only get data directly if Auto Mode is not active.
        if not self.__is_automatic_mode_active:
            self.__ai2_value = c_float(self.analog_input2_raw * 0.024194115990990990990990990991)

        return self.__ai2_value.value

    @property
    def analog_input3(self):
        """
        Get analog input 3 value as float in Ampere (mA).
        """

        # Only get data directly if Auto Mode is not active.
        if not self.__is_automatic_mode_active:
            self.__ai3_value = c_float(self.analog_input3_raw * 0.024194115990990990990990990991)

        return self.__ai3_value.value

    # </editor-fold>

    # <editor-fold desc="Region: Temperature Inputs 0 - 3">

    # **************************************************************************
    # Temperature Inputs 0 - 3
    # **************************************************************************

    def __temperature_get(self, command):
        """
        Get the current temperature from the GPIO specified in 'command'. This is only possible if the corresponding
        GPIO is set to DHT mode.
        
        :param int command: One of 4 possible constant values defined by PIXTEND_SPI_GET_TEMPx
        :return: The 16 bit raw temperature value from the sensor attached to the GPIO
        :rtype: c_uint16
        """

        if not self.__is_automatic_mode_active:
            # Wait, this method might have been called before. The microcontroller needs time for processing.
            time.sleep(0.1)
            self.__transfer_spi_data(command)
            # Wait again after the first transfer/request, the microcontroller now needs time to start and complete
            # its 1-wire communication with the sensor attached to the requested GPIO.
            # Then the 'real' data can be requested.
            time.sleep(0.2)
            resp = self.__transfer_spi_data(command)
            # Wait for security in case this or another method with SPI data transfer is called right after this one.
            time.sleep(0.1)

            if len(resp) >= 4:
                self.__analog_value.bytes.byte0 = resp[3]
                self.__analog_value.bytes.byte1 = resp[4]
            else:
                self.__analog_value.asUint16 = 0
        else:
            if command == self.PIXTEND_SPI_GET_TEMP0:
                self.__analog_value.asUint16 = self.__temp0_raw_value.asUint16
            elif command == self.PIXTEND_SPI_GET_TEMP1:
                self.__analog_value.asUint16 = self.__temp1_raw_value.asUint16
            elif command == self.PIXTEND_SPI_GET_TEMP2:
                self.__analog_value.asUint16 = self.__temp2_raw_value.asUint16
            elif command == self.PIXTEND_SPI_GET_TEMP3:
                self.__analog_value.asUint16 = self.__temp3_raw_value.asUint16
            else:
                self.__analog_value.asUint16 = 0

        return self.__analog_value.asUint16

    @property
    def use_fahrenheit(self):
        """
        Get or Set if the conversion of the temperature raw value should be done in Fahrenheit.
        Default is 'False', meaning by getting the temperature from t0_dht22 to t3_dht22 or from t0_dht11 to t3_dht11
        the value will be in degrees Celsius, if set to 'True' the values will be in Fahrenheit.
        
        :returns: Bool value, 'False' for Celsius and 'True' for Fahrenheit
        :rtype: bool
        """

        return self.__use_fahrenheit

    @use_fahrenheit.setter
    def use_fahrenheit(self, value):
        self.__use_fahrenheit = value

    @property
    def temp_input0_raw(self):
        """
        Get the temperature raw value from temperature input 0. 
        """

        return self.__temperature_get(self.PIXTEND_SPI_GET_TEMP0)

    @property
    def temp_input1_raw(self):
        """
        Get the temperature raw value from temperature input 1. 
        """

        return self.__temperature_get(self.PIXTEND_SPI_GET_TEMP1)

    @property
    def temp_input2_raw(self):
        """
        Get the temperature raw value from temperature input 2. 
        """

        return self.__temperature_get(self.PIXTEND_SPI_GET_TEMP2)

    @property
    def temp_input3_raw(self):
        """
        Get the temperature raw value from temperature input 3. 
        """

        return self.__temperature_get(self.PIXTEND_SPI_GET_TEMP3)

    @property
    def t0_dht22(self):
        """
        Get the converted temperature value from temperature input 0 from a DHT22 sensor. 
        """

        factor = 1.0
        value = self.temp_input0_raw
        if self.__test_bit(value, self.BIT_15) == 1:
            value = self.__clear_bit(value, self.BIT_15)
            factor = -1.0

        if self.__use_fahrenheit:
            return ((float(value) * 1.8) + 32) * factor
        else:
            return (float(value) / 10.0) * factor

    @property
    def t1_dht22(self):
        """
        Get the converted temperature value from temperature input 1 from a DHT22 sensor. 
        """

        factor = 1.0
        value = self.temp_input1_raw
        if self.__test_bit(value, self.BIT_15) == 1:
            value = self.__clear_bit(value, self.BIT_15)
            factor = -1.0

        if self.__use_fahrenheit:
            return ((float(value) * 1.8) + 32) * factor
        else:
            return (float(value) / 10.0) * factor

    @property
    def t2_dht22(self):
        """
        Get the converted temperature value from temperature input 2 from a DHT22 sensor. 
        """

        factor = 1.0
        value = self.temp_input2_raw
        if self.__test_bit(value, self.BIT_15) == 1:
            value = self.__clear_bit(value, self.BIT_15)
            factor = -1.0

        if self.__use_fahrenheit:
            return ((float(value) * 1.8) + 32) * factor
        else:
            return (float(value) / 10.0) * factor

    @property
    def t3_dht22(self):
        """
        Get the converted temperature value from temperature input 3 from a DHT22 sensor. 
        """

        factor = 1.0
        value = self.temp_input3_raw
        if self.__test_bit(value, self.BIT_15) == 1:
            value = self.__clear_bit(value, self.BIT_15)
            factor = -1.0

        if self.__use_fahrenheit:
            return ((float(value) * 1.8) + 32) * factor
        else:
            return (float(value) / 10.0) * factor

    @property
    def t0_dht11(self):
        """
        Get the converted temperature value from temperature input 0 from a DHT11 sensor. 
        """

        if self.__use_fahrenheit:
            return (float(self.temp_input0_raw / 256) * 1.8) + 32
        else:
            return float(self.temp_input0_raw / 256)

    @property
    def t1_dht11(self):
        """
        Get the converted temperature value from temperature input 1 from a DHT11 sensor. 
        """

        if self.__use_fahrenheit:
            return (float(self.temp_input1_raw / 256) * 1.8) + 32
        else:
            return float(self.temp_input1_raw / 256)

    @property
    def t2_dht11(self):
        """
        Get the converted temperature value from temperature input 2 from a DHT11 sensor. 
        """

        if self.__use_fahrenheit:
            return (float(self.temp_input2_raw / 256) * 1.8) + 32
        else:
            return float(self.temp_input2_raw / 256)

    @property
    def t3_dht11(self):
        """
        Get the converted temperature value from temperature input 3 from a DHT11 sensor. 
        """

        if self.__use_fahrenheit:
            return (float(self.temp_input3_raw / 256) * 1.8) + 32
        else:
            return float(self.temp_input3_raw / 256)

    # </editor-fold>

    # <editor-fold desc="Region: Humidity Inputs 0 - 3">

    # **************************************************************************
    # Humidity Inputs 0 - 3
    # **************************************************************************

    def __humidity_get(self, command):
        """
        Get the current humidity from the GPIO specified in 'command'. This is only possible if the corresponding
        GPIO is set to DHT (1-wire) mode.

        :param int command: One of 4 possible constant values defined by PIXTEND_SPI_GET_HUMx
        :return: The 16 bit raw humidity value from the sensor attached to the GPIO
        :rtype: c_uint16
        """

        if not self.__is_automatic_mode_active:
            # Wait, this method might have been called before. The microcontroller needs time for processing.
            time.sleep(0.1)
            self.__transfer_spi_data(command)
            # Wait again after the first transfer/request, the microcontroller now needs time to start and complete
            # its 1-wire communication with the sensor attached to the requested GPIO.
            # Then the 'real' data can be requested.
            time.sleep(0.2)
            value = self.__transfer_spi_data(command)
            # Wait for security in case this or another method with SPI data transfer is called right after this one.
            time.sleep(0.1)

            if len(value) >= 4:
                self.__analog_value.bytes.byte0 = value[3]
                self.__analog_value.bytes.byte1 = value[4]
            else:
                self.__analog_value.asUint16 = 0
        else:
            if command == self.PIXTEND_SPI_GET_HUM0:
                self.__analog_value.asUint16 = self.__humid0_raw_value.asUint16
            elif command == self.PIXTEND_SPI_GET_HUM1:
                self.__analog_value.asUint16 = self.__humid1_raw_value.asUint16
            elif command == self.PIXTEND_SPI_GET_HUM2:
                self.__analog_value.asUint16 = self.__humid2_raw_value.asUint16
            elif command == self.PIXTEND_SPI_GET_HUM3:
                self.__analog_value.asUint16 = self.__humid3_raw_value.asUint16
            else:
                self.__analog_value.asUint16 = 0

        return self.__analog_value.asUint16

    @property
    def hum_input0_raw(self):
        """
        Get the humidity raw value from humidity input 0. 
        """

        return self.__humidity_get(self.PIXTEND_SPI_GET_HUM0)

    @property
    def hum_input1_raw(self):
        """
        Get the humidity raw value from humidity input 1. 
        """

        return self.__humidity_get(self.PIXTEND_SPI_GET_HUM1)

    @property
    def hum_input2_raw(self):
        """
        Get the humidity raw value from humidity input 2. 
        """

        return self.__humidity_get(self.PIXTEND_SPI_GET_HUM2)

    @property
    def hum_input3_raw(self):
        """
        Get the humidity raw value from humidity input 3. 
        """

        return self.__humidity_get(self.PIXTEND_SPI_GET_HUM3)

    @property
    def h0_dht22(self):
        """
        Get the converted humidity value from humidity input 0 if a DHT22 sensor is physically attached. 
        """

        return float(self.hum_input0_raw) / 10.0

    @property
    def h1_dht22(self):
        """
        Get the converted humidity value from humidity input 1 if a DHT22 sensor is physically attached. 
        """

        return float(self.hum_input1_raw) / 10.0

    @property
    def h2_dht22(self):
        """
        Get the converted humidity value from humidity input 2 if a DHT22 sensor is physically attached. 
        """

        return float(self.hum_input2_raw) / 10.0

    @property
    def h3_dht22(self):
        """
        Get the converted humidity value from humidity input 3 if a DHT22 sensor is physically attached. 
        """

        return float(self.hum_input3_raw) / 10.0

    @property
    def h0_dht11(self):
        """
        Get the converted humidity value from humidity input 0 if a DHT11 sensor is physically attached.  
        """

        return float(self.hum_input0_raw / 256)

    @property
    def h1_dht11(self):
        """
        Get the converted humidity value from humidity input 1 if a DHT11 sensor is physically attached.  
        """

        return float(self.hum_input1_raw / 256)

    @property
    def h2_dht11(self):
        """
        Get the converted humidity value from humidity input 2 if a DHT11 sensor is physically attached.  
        """

        return float(self.hum_input2_raw / 256)

    @property
    def h3_dht11(self):
        """
        Get the converted humidity value from humidity input 3 if a DHT11 sensor is physically attached. 
        """

        return float(self.hum_input3_raw / 256)

    # </editor-fold>

    # <editor-fold desc="Region: Digital Outputs">

    # **************************************************************************
    # Digital Outputs
    # **************************************************************************

    def __digital_outputs_states_get(self):
        """
        Get the states of the digital outputs. Since PiXtend board version 1.3.x the digital outputs have the
        capability to read back their own state. 
        """

        # Only do an SPI data transfer if Auto Mode is not active and the PiXtend board version is higher
        # then 12, meaning version 1.2.x. Only boards starting from version 1.3.0 can read back their own outputs.
        if not self.__is_automatic_mode_active and self.__uc_version.UC_VERSIONH > 12:
            resp = self.__transfer_spi_data(self.PIXTEND_SPI_GET_DOUT)

            if len(resp) >= 3:
                self.__digital_outputs_states = resp[3]
            else:
                self.__digital_outputs_states = 0

        return self.__digital_outputs_states

    def __digital_outputs_states_set(self, value, bit_num):

        if value == 0:
            self.__digital_outputs_states = self.__clear_bit(self.__digital_outputs_states, bit_num)
        if value == 1:
            self.__digital_outputs_states = self.__set_bit(self.__digital_outputs_states, bit_num)

        # Only do an SPI data transfer if Auto Mode is not active.
        if not self.__is_automatic_mode_active:
            self.__transfer_spi_data(self.PIXTEND_SPI_SET_DOUT, self.__digital_outputs_states)

    @property
    def digital_output0(self):
        """
        Get or Set the state of digital output 0. A value 0 means 'off' and a value of 1 means 'on'. 
        """

        return self.__test_bit(self.__digital_outputs_states_get(), self.BIT_0)

    @digital_output0.setter
    def digital_output0(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__digital_outputs_states_set(value, self.BIT_0)

    @property
    def do0(self):
        """
        Get or Set the state of digital output 0. A value 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_output0 <--> do0
        """

        return self.digital_output0

    @do0.setter
    def do0(self, value):
        self.digital_output0 = value

    @property
    def digital_output1(self):
        """
        Get or Set the state of digital output 1. A value 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_outputs_states_get(), self.BIT_1)

    @digital_output1.setter
    def digital_output1(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__digital_outputs_states_set(value, self.BIT_1)

    @property
    def do1(self):
        """
        Get or Set the state of digital output 1. A value 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_output1 <--> do1
        """

        return self.digital_output1

    @do1.setter
    def do1(self, value):
        self.digital_output1 = value

    @property
    def digital_output2(self):
        """
        Get or Set the state of digital output 2. A value 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_outputs_states_get(), self.BIT_2)

    @digital_output2.setter
    def digital_output2(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__digital_outputs_states_set(value, self.BIT_2)

    @property
    def do2(self):
        """
        Get or Set the state of digital output 2. A value 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_output2 <--> do2
        """

        return self.digital_output2

    @do2.setter
    def do2(self, value):
        self.digital_output2 = value

    @property
    def digital_output3(self):
        """
        Get or Set the state of digital output 3. A value 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_outputs_states_get(), self.BIT_3)

    @digital_output3.setter
    def digital_output3(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__digital_outputs_states_set(value, self.BIT_3)

    @property
    def do3(self):
        """
        Get or Set the state of digital output 3. A value 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_output3 <--> do3
        """

        return self.digital_output3

    @do3.setter
    def do3(self, value):
        self.digital_output3 = value

    @property
    def digital_output4(self):
        """
        Get or Set the state of digital output 4. A value 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_outputs_states_get(), self.BIT_4)

    @digital_output4.setter
    def digital_output4(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__digital_outputs_states_set(value, self.BIT_4)

    @property
    def do4(self):
        """
        Get or Set the state of digital output 4. A value 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_output4 <--> do4
        """

        return self.digital_output4

    @do4.setter
    def do4(self, value):
        self.digital_output4 = value

    @property
    def digital_output5(self):
        """
        Get or Set the state of digital output 5. A value 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_outputs_states_get(), self.BIT_5)

    @digital_output5.setter
    def digital_output5(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__digital_outputs_states_set(value, self.BIT_5)

    @property
    def do5(self):
        """
        Get or Set the state of digital output 5. A value 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_output5 <--> do5
        """

        return self.digital_output5

    @do5.setter
    def do5(self, value):
        self.digital_output5 = value

    # </editor-fold>

    # <editor-fold desc="Region: Relay Outputs">

    # **************************************************************************
    # Relay Outputs
    # **************************************************************************

    def __relays_states_get(self):
        """
        Get the current states of the 4 relays on the PiXtend board.
        """

        # Only do an SPI data transfer if Auto Mode is not active.
        if not self.__is_automatic_mode_active:
            resp = self.__transfer_spi_data(self.PIXTEND_SPI_GET_RELAY)
            if len(resp) >= 3:
                self.__relays_states = resp[3]
            else:
                self.__relays_states = 0

        return self.__relays_states

    def __relays_states_set(self, value, bit_num):
        """
        Set the current states of the 4 relays on the PiXtend board.
        
        :param int value: Value the specified bit should have, 0 or 1 
        :param int bit_num: Number of the bit to change 
        """

        if value == 0:
            self.__relays_states = self.__clear_bit(self.__relays_states, bit_num)
        if value == 1:
            self.__relays_states = self.__set_bit(self.__relays_states, bit_num)

        # Only do an SPI data transfer if Auto Mode is not active.
        if not self.__is_automatic_mode_active:
            self.__transfer_spi_data(self.PIXTEND_SPI_SET_RELAY, self.__relays_states)

    @property
    def relay0(self):
        """
        Get or Set the state of relay 0. A value of 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__relays_states_get(), self.BIT_0)

    @relay0.setter
    def relay0(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__relays_states_set(value, self.BIT_0)

    @property
    def relay1(self):
        """
        Get or Set the state of relay 1. A value of 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__relays_states_get(), self.BIT_1)

    @relay1.setter
    def relay1(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__relays_states_set(value,  self.BIT_1)

    @property
    def relay2(self):
        """
        Get or Set the state of relay 2. A value of 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__relays_states_get(), self.BIT_2)

    @relay2.setter
    def relay2(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__relays_states_set(value, self.BIT_2)

    @property
    def relay3(self):
        """
        Get or Set the state of relay 3. A value of 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__relays_states_get(), self.BIT_3)

    @relay3.setter
    def relay3(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = off, 1 = on")
        self.__relays_states_set(value, self.BIT_3)

    # </editor-fold>

    # <editor-fold desc="Region: Digital Inputs">

    # **************************************************************************
    # Digital Inputs
    # **************************************************************************

    def __digital_inputs_get(self):
        """
        Get the current states of the 8 digital inputs on the PiXtend board.
        """

        # In manual mode (__is_automatic_mode_active == False), get the current states of the
        # digital inputs directly via SPI request. In Auto Mode, simply return the int with the states.
        if not self.__is_automatic_mode_active:
            value = self.__transfer_spi_data(self.PIXTEND_SPI_GET_DIN)
            if len(value) >= 3:
                self.__digital_inputs_states = value[3]
            else:
                self.__digital_inputs_states = 0

        return self.__digital_inputs_states

    @property
    def digital_input0(self):
        """
        Get the state of digital input 0. A value of 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_inputs_get(), self.BIT_0)

    @property
    def digital_input1(self):
        """
        Get the state of digital input 1. A value pf 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_inputs_get(), self.BIT_1)

    @property
    def digital_input2(self):
        """
        Get the state of digital input 2. A value of 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_inputs_get(), self.BIT_2)

    @property
    def digital_input3(self):
        """
        Get the state of digital input 3. A value of 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_inputs_get(), self.BIT_3)

    @property
    def digital_input4(self):
        """
        Get the state of digital input 4. A value of 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_inputs_get(), self.BIT_4)

    @property
    def digital_input5(self):
        """
        Get the state of digital input 5. A value of 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_inputs_get(), self.BIT_5)

    @property
    def digital_input6(self):
        """
        Get the state of digital input 6. A value of 0 means 'off' and a value of 1 means 'on'. 
        """

        return self.__test_bit(self.__digital_inputs_get(), self.BIT_6)

    @property
    def digital_input7(self):
        """
        Get the state of digital input 7. A value of 0 means 'off' and a value of 1 means 'on'.
        """

        return self.__test_bit(self.__digital_inputs_get(), self.BIT_7)

    # </editor-fold>

    # <editor-fold desc="Region: Digital Inputs short names for shorter programs">

    # **************************************************************************
    # Digital Inputs short form
    # **************************************************************************

    @property
    def di0(self):
        """
        Get the state of digital input 0. A value of 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_input0 <--> di0
        """

        return self.digital_input0

    @property
    def di1(self):
        """
        Get the state of digital input 1. A value of 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_input1 <--> di1
        """

        return self.digital_input1

    @property
    def di2(self):
        """
        Get the state of digital input 2. A value of 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_input2 <--> di2
        """

        return self.digital_input2

    @property
    def di3(self):
        """
        Get the state of digital input 3. A value of 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_input3 <--> di3
        """

        return self.digital_input3

    @property
    def di4(self):
        """
        Get the state of digital input 4. A value of 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_input4 <--> di4
        """

        return self.digital_input4

    @property
    def di5(self):
        """
        Get the state of digital input 5. A value of 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_input5 <--> di5
        """

        return self.digital_input5

    @property
    def di6(self):
        """
        Get the state of digital input 6. A value of 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_input6 <--> di6
        """

        return self.digital_input6

    @property
    def di7(self):
        """
        Get the state of digital input 7. A value of 0 means 'off' and a value of 1 means 'on'.
        This property is just a shorter version in terms of wording: digital_input7 <--> di7
        """

        return self.digital_input7

    # </editor-fold>

    # <editor-fold desc="Region: Digital Analog Converter DAC">

    # **************************************************************************
    # Digital Analog Converter DAC
    # **************************************************************************
    @property
    def dac_selection(self):
        """
        Get or Set the DAC selection. There are 2 DAC's on the PiXtend board. DAC A = 0 and DAC B = 1.
        
        :return: selected DAC, 0 = DAC A and 1 = DAC B
        :rtype: int
        """
        return self.__analog_dac_value.bits.bit0

    @dac_selection.setter
    def dac_selection(self, value):
        if value == 0 or value == 1:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 0 = DAC A or 1 = DAC B")
        self.__analog_dac_value.bits.bit0 = value

    def set_dac_output(self, value):
        """
        Set the analog output value for the chosen DAC. The active DAC can be chosen with the property 'dac_selection'.
        The value 0 or constant DAC_A selects DAC A and the value 1 or constant DAC_B selects DAC B.
        Example:
        Selecting and setting DAC A:
        p.dac_selection = p.DAC_A
        p.set_dac_output (512)
        
        Selecting and setting DAC B:
        p.dac_selection = p.DAC_B
        p.set_dac_output (256)
        
        :param int value: Output value for the chosen DAC.
        :raises ValueError: If value is smaller then 0 or larger then 1023
        """

        if 0 <= value <= 1023:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: 10bit values max. 1023")

        analog_value = Flags16()
        temp_value = Flags16()
        dac_value = AnalogValue()

        analog_value.asUint16 = value

        # Copy the 10 relevant bits from the user var to the DAC MCP4812 data format.
        # The first 2 bits in the DAC 16 bit data format are unused! Therefore we start with bit2.
        # See the DAC manual for more details.
        temp_value.bits.bit2 = analog_value.bits.bit0
        temp_value.bits.bit3 = analog_value.bits.bit1
        temp_value.bits.bit4 = analog_value.bits.bit2
        temp_value.bits.bit5 = analog_value.bits.bit3
        temp_value.bits.bit6 = analog_value.bits.bit4
        temp_value.bits.bit7 = analog_value.bits.bit5
        temp_value.bits.bit8 = analog_value.bits.bit6
        temp_value.bits.bit9 = analog_value.bits.bit7
        temp_value.bits.bit10 = analog_value.bits.bit8
        temp_value.bits.bit11 = analog_value.bits.bit9

        # bit 15 A/B: DAC A or DAC B Selection bit
        temp_value.bits.bit15 = self.__analog_dac_value.bits.bit0
        # bit 14 Don't care
        temp_value.bits.bit14 = 0
        # bit 13 Output Gain Selection bit
        temp_value.bits.bit13 = self.__analog_dac_value.bits.bit1
        # bit 12 Output Shutdown Control bit
        temp_value.bits.bit12 = self.__analog_dac_value.bits.bit2

        # Assign the 16 bit value to another union to get the individual bytes to send via SPI
        dac_value.asUint16 = temp_value.asUint16

        # Send the 2 bytes to the DAC via SPI
        self.__transfer_spi_dac_data(dac_value.bytes.byte1, dac_value.bytes.byte0)

    # </editor-fold>

    # <editor-fold desc="Region: Auto Mode">

    # **************************************************************************
    # Automatic Mode for PiXtend
    # **************************************************************************

    def auto_mode(self):
        """
        Method for the auto(matic) mode data transfer. The settings and values of all applicable
        properties like outputs, GPIO and PWM configuration for the PiXtend board are sent to the microcontroller
        in one block and states and values of all digital and analog inputs and outputs are received as response.
        This is the most efficient way to work with the microcontroller on the PiXtend board.
        
        In the beginning the auto_mode method should be called until the return value is 0 and the microcontroller's
        uc_state is 1, meaning the communication is working and the microcontroller has entered the 'Run' state.
        After that the auto_mode method does not need to be called on a regular basis, but when new values are needed
        or outputs have to be turned on or off.
        
        Example:
        if p.auto_mode() == 0 and p.uc_status == 1:
            p.relay0 = p.ON
        
        :return: 0 means communication is ok and running, -1 means crc error and/or a problem with the received data
        :rtype: int
        """

        # Set internal automatic mode to "on" (True)
        self.__is_automatic_mode_active = True

        # Set the microcontroller's control register to run in automatic mode. This value has to remain on 16 as
        # long as the automatic mode is used.
        self.__uc_ctrl.asByte = 16  # 0b00010000

        # Prepare the 34 bytes array with the data to be sent to the microcontroller
        spi_output = [0] * 34

        spi_output[0] = 128
        spi_output[1] = 255
        spi_output[2] = self.__digital_outputs_states
        spi_output[3] = self.__relays_states
        spi_output[4] = self.__gpio_states_auto_out
        spi_output[5] = self.__pwm0_value.bytes.byte0
        spi_output[6] = self.__pwm0_value.bytes.byte1
        spi_output[7] = self.__pwm1_value.bytes.byte0
        spi_output[8] = self.__pwm1_value.bytes.byte1
        spi_output[9] = self.__pwm_ctrl0.asByte
        spi_output[10] = self.__pwm_ctrl1.asByte
        spi_output[11] = self.__pwm_ctrl2.asByte
        spi_output[12] = self.__gpio_ctrl
        spi_output[13] = self.__uc_ctrl.asByte
        spi_output[14] = self.__ai_ctrl0.asByte
        spi_output[15] = self.__ai_ctrl1.asByte
        spi_output[16] = self.__pi_status.asByte

        # Calculate CRC16 Transmit Checksum
        crc_sum = 0xFFFF

        for i in range(2, 31, 1):
            crc_sum = self.__calc_crc16(crc_sum, spi_output[i])

        spi_output[31] = crc_sum & 0xFF  # CRC Low Byte
        spi_output[32] = (crc_sum >> 8) & 0xFF  # CRC High Byte
        spi_output[33] = 128  # Termination

        # Initialize SPI Data Transfer with spi_output data
        spi_input = self.__transfer_spi_data(self.PIXTEND_SPI_AUTO_MODE, 0, 0, 0, spi_output)

        # ------------------------------------------------------------------------
        # "spi_input" now contains all returned data, assign values to variables
        # ------------------------------------------------------------------------
        # Calculate CRC16 receive checksum
        crc_sum = 0xFFFF
        for i in range(2, 31, 1):
            crc_sum = self.__calc_crc16(crc_sum, spi_input[i])

        crc_sum_rx = (spi_input[32] << 8) + spi_input[31]

        if crc_sum_rx != crc_sum:
            # Error crc of received data and from controller are not the same
            return -1
        else:
            # CRC values received and calculated -> OK
            self.__digital_inputs_states = spi_input[2]
            self.__ai0_raw_value.bytes.byte0 = spi_input[3]
            self.__ai0_raw_value.bytes.byte1 = spi_input[4]
            self.__ai1_raw_value.bytes.byte0 = spi_input[5]
            self.__ai1_raw_value.bytes.byte1 = spi_input[6]
            self.__ai2_raw_value.bytes.byte0 = spi_input[7]
            self.__ai2_raw_value.bytes.byte1 = spi_input[8]
            self.__ai3_raw_value.bytes.byte0 = spi_input[9]
            self.__ai3_raw_value.bytes.byte1 = spi_input[10]
            self.__gpio_states_auto_in = spi_input[11]
            self.__temp0_raw_value.bytes.byte0 = spi_input[12]
            self.__temp0_raw_value.bytes.byte1 = spi_input[13]
            self.__temp1_raw_value.bytes.byte0 = spi_input[14]
            self.__temp1_raw_value.bytes.byte1 = spi_input[15]
            self.__temp2_raw_value.bytes.byte0 = spi_input[16]
            self.__temp2_raw_value.bytes.byte1 = spi_input[17]
            self.__temp3_raw_value.bytes.byte0 = spi_input[18]
            self.__temp3_raw_value.bytes.byte1 = spi_input[19]
            self.__humid0_raw_value.bytes.byte0 = spi_input[20]
            self.__humid0_raw_value.bytes.byte1 = spi_input[21]
            self.__humid1_raw_value.bytes.byte0 = spi_input[22]
            self.__humid1_raw_value.bytes.byte1 = spi_input[23]
            self.__humid2_raw_value.bytes.byte0 = spi_input[24]
            self.__humid2_raw_value.bytes.byte1 = spi_input[25]
            self.__humid3_raw_value.bytes.byte0 = spi_input[26]
            self.__humid3_raw_value.bytes.byte1 = spi_input[27]
            self.__uc_version.UC_VERSIONL = spi_input[28]
            self.__uc_version.UC_VERSIONH = spi_input[29]
            self.__uc_status = spi_input[30]

            # Calculate the measured voltage from raw value to volts based on the 10 volts jumper setting
            if self.__ai0_jumper_setting_10_volts == 1:
                self.__ai0_value = c_float(self.__ai0_raw_value.asUint16 * (10.0 / 1024))
            else:
                self.__ai0_value = c_float(self.__ai0_raw_value.asUint16 * (5.0 / 1024))

            if self.__ai1_jumper_setting_10_volts == 1:
                self.__ai1_value = c_float(self.__ai1_raw_value.asUint16 * (10.0 / 1024))
            else:
                self.__ai1_value = c_float(self.__ai1_raw_value.asUint16 * (5.0 / 1024))

            # Calculate the milli-amps from raw value of analog inputs 2 and 3
            self.__ai2_value = c_float(self.__ai2_raw_value.asUint16 * 0.024194115990990990990990990991)
            self.__ai3_value = c_float(self.__ai3_raw_value.asUint16 * 0.024194115990990990990990990991)
            self.__temp0_value = c_float(self.__temp0_raw_value.asUint16 / 10.0)
            self.__temp1_value = c_float(self.__temp1_raw_value.asUint16 / 10.0)
            self.__temp2_value = c_float(self.__temp2_raw_value.asUint16 / 10.0)
            self.__temp3_value = c_float(self.__temp3_raw_value.asUint16 / 10.0)
            self.__humid0_value = c_float(self.__humid0_raw_value.asUint16 / 10.0)
            self.__humid1_value = c_float(self.__humid1_raw_value.asUint16 / 10.0)
            self.__humid2_value = c_float(self.__humid2_raw_value.asUint16 / 10.0)
            self.__humid3_value = c_float(self.__humid3_raw_value.asUint16 / 10.0)

            return 0

    @staticmethod
    def __calc_crc16(bycrc, bydata):
        """
        Calculates a 16 bit CRC value.

        :param int bycrc: CRC value
        :param int bydata: data byte to be added to the CRC value
        :return: Calculated CRC value
        :rtype: int
        """

        bycrc = bycrc ^ bydata

        for i in range(0, 8, 1):
            if bycrc & 1:
                bycrc = (bycrc >> 1) ^ 0xA001
            else:
                bycrc = bycrc >> 1

        return bycrc

    # </editor-fold>

    # <editor-fold desc="Region: Serial Operations">

    @property
    def serial_mode(self):
        """
        Get or Set the serial mode of the PiXtend board. Use boolean values: False = RS232 and True = RS485
        
        Example:
        p.serial_mode = p.RS232 # or p.serial_mode = False
        or
        p.serial_mode = p.RS485 # or p.serial_mode = True
        
        :return: Value of the serial mode
        :rtype: bool
        """

        return GPIO.input(self.PIXTEND_SERIAL_PIN)

    @serial_mode.setter
    def serial_mode(self, value):
        if value is True or value is False:
            pass
        else:
            raise ValueError("Value error!, Value " + str(value) + " not allowed! - Use only: " +
                             "False = RS232 or True = RS485")
        GPIO.output(self.PIXTEND_SERIAL_PIN, value)

    # </editor-fold>

    # <editor-fold desc="Region: Real Time Clock - RTC">

    @staticmethod
    def update_rtc():
        """
        Update the hardware real time clock (RTC) with the current Linux system time.
        The system time is updated by the NTP service, which is active by default, if it is able to connect to
        the Internet and get the current time and date.
        """

        subprocess.call(shlex.split("hwclock -w"))

    # </editor-fold>

    # <editor-fold desc="Region: Bit Operation">

    @staticmethod
    def __test_bit(int_type, offset):
        """
        __test_bit() returns 1, if the bit at 'offset' is one else 0 if the bit is not set.
        
        :param int int_type: Integer value to test 
        :param int offset: Offset value which bit to test
        :return: Integer based value of 0 if bit at 'offset' is not set or 1 if bit is set
        :rtype: int
        """

        mask = 1 << offset
        bit_value = int_type & mask
        if bit_value > 0:
            res = 1
        else:
            res = 0

        return res

    @staticmethod
    def __set_bit(int_type, offset):
        """
        __set_bit() returns an integer with the bit at 'offset' set to 1.

        :param int int_type: Integer value in which to set one bit
        :param int offset: Offset value which bit to set
        :return: Integer with bit set at 'offset'
        :rtype: int
        """

        mask = 1 << offset
        return int_type | mask

    @staticmethod
    def __clear_bit(int_type, offset):
        """
        __clear_bit() returns an integer with the bit at 'offset' cleared, set to 0.

        :param int int_type: Integer value in which to clear one bit
        :param int offset: Offset value which bit to clear
        :return: Integer with bit cleared a 'offset'
        :rtype: int
        """

        mask = ~(1 << offset)
        return int_type & mask

    @staticmethod
    def __toggle_bit(int_type, offset):
        """
        __toggle_bit() returns an integer with the bit at 'offset' inverted, 0 -> 1 and 1 -> 0.

        :param int int_type: Integer value to toggle one bit
        :param int offset: Offset value which bit to toggle
        :return: integer with bit set a 'offset'
        :rtype: int
        """

        mask = 1 << offset
        return int_type ^ mask

    # </editor-fold>
bues.ch cgit interface