File size: 163,147 Bytes
a2a15a2 |
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 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 |
/*
* Stefan's basic interpreter - Arduino runtime environment runtime.cpp.
*
* This is the Arduino runtime environment for BASIC. It maps the functions
* needed for the various subsystems to the MCU specific implementations.
*
* Author: Stefan Lenz, [email protected]
*
* Configure the hardware settings and parameters in hardware.h.
*/
#include "Arduino.h"
#include "hardware.h"
#include "runtime.h"
/* If the BASIC interpreter provides a loop function it will superseed this one */
void __attribute__((weak)) bloop() {};
/*
* Defining the bssystype variable which informs BASIC about the platform at runtime.
* bssystype is not used by the interpreter, but can be used by BASIC programs to
* determine the platform they are running on.
*/
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
uint8_t bsystype = SYSTYPE_AVR;
#elif defined(ARDUINO_ARCH_ESP8266)
uint8_t bsystype = SYSTYPE_ESP8266;
#elif defined(ARDUINO_ARCH_ESP32)
uint8_t bsystype = SYSTYPE_ESP32;
#elif defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_MBED_RP2040)
uint8_t bsystype = SYSTYPE_RP2040;
#elif defined(ARDUINO_ARCH_SAM) && defined(ARDUINO_ARCH_SAMD)
uint8_t bsystype = SYSTYPE_SAM;
#elif defined(ARDUINO_ARCH_XMC)
uint8_t bsystype = SYSTYPE_XMC;
#elif defined(ARDUINO_ARCH_SMT32)
uint8_t bsystype = SYSTYPE_SMT32;
#elif defined(ARDUINO_ARCH_RENESAS)
uint8_t bsystype = SYSTYPE_NRENESA;
#elif defined(ARDUINO_ARCH_MBED_GIGA)
uint8_t bsystype = SYSTYPE_GIGA;
#else
uint8_t bsystype = SYSTYPE_UNKNOWN;
#endif
/*
* Global variables of the runtime env.
*
* id: the active input stream
* od: the active output stream
* idd: the default input stream in interactive mode
* odd: the default output stream in interactive mode
* ioer: the io error variable, always or-ed with ert in BASIC
*/
int8_t id;
int8_t od;
int8_t idd = ISERIAL;
int8_t odd = OSERIAL;
int8_t ioer = 0;
/*
* MS Basic and many terminals have a real TAB function advanceing to
* the next tab stop. This is emulated by counting the characters on
* the output streams 0-4. The feature is activated with the HASMSTAB.
* The code cannot processes composed characters like Germin Umlauts
* correctly.
*/
#ifdef HASMSTAB
uint8_t charcount[5];
#endif
/*
* The pointer to the buffer used for the &0 device.
* The BASIC input buffer is used. With this feature, a line can be printed
* to the buffer and then accessed as a string @$ in BASIC. In systems where
* CHR() is not available, this can be used to convert a number to a string.
*/
char* nullbuffer = ibuffer;
uint16_t nullbufsize = BUFSIZE;
uint8_t bufferstat(uint8_t ch) { return 1; }
/*
* External libraries of the runtime environment for the peripherals.
* When the runtime environment is brocken down to modules this will
* got into the modules. Summerized here for the sake of clarity.
*/
/*
* OS specific headers.
*/
/*
* ESPy stuff, pgmspace has changed location in some ESP32 cores.
*/
#ifdef ARDUINOPROGMEM
#ifdef ARDUINO_ARCH_ESP32
#include <pgmspace.h>
#else
#include <avr/pgmspace.h>
#endif
#endif
/*
* MBED OS type includes summarized here - tested for GIGA boards.
*/
#ifdef ARDUINO_ARCH_MBED_GIGA
#include "mbed.h"
#include <mbed_mktime.h>
#endif
/*
* EEPROM code. Some of the boards bring their own EEPROM classes.
*
* These are:
* - AVR boards of all kinds
* - ESP8266 and ESP32
* - the LGT8F boards with the newer cores
*
* For XMC my homebrew is now part of the interpreter.
* See https://github.com/slviajero/XMCEEPROMLib
* For more information on this or if you want to use it on other platforms.
*
* For SAMD the FlashStorage library is used. It was orginally written by
* Cristian Maglie. I use the version of Khoi Huang from
* https://github.com/khoih-prog/FlashStorage_SAMD
* This is now bundled with the interpreter. The emulation buffers the
* entire EEPROM in RAM, so 2k should be ok but not more. If the
* dual buffer strategy of EepromFS or XMC would be used, larger
* EEPROMs could be emulated - this is work to be done.
*
* RP2040 standard cores and the DUE do not have an EEPROM emulation.
* This is trapped in hardware.h (EEPROM is disabled).
*/
#ifdef ARDUINOEEPROM
#if defined(ARDUINO_ARCH_XMC)
#include "src/XMCEEPROMLib/XMCEEPROMLib.h"
#elif defined(ARDUINO_ARCH_SAMD)
#define EEPROM_EMULATION_SIZE 2048
#include "src/FlashStorage_SAMD/FlashStorage_SAMD.h"
#else
#include <EEPROM.h>
#endif
#endif
/* Standard SPI coming with the board is used */
#ifdef ARDUINOSPI
#include <SPI.h>
#endif
/*
* Standard wire - triggered by the HASWIRE or HASSIMPLEWIR macro.
* These macros are set in the hardware.h file depending on the subsystems.
*
* If ARDUINODIRECTI2C is set the the Wire library is bypassed. This saves
* 200 bytes of RAM and 1kB of program space on an AVR 8bit. It
* also removed the blocking of Wire if the slave does not respond.
*
* In this case Wire.h should not be included in the sketch because
* it allocates the buffers and the Wire object.
*/
#if defined(HASWIRE)
#include <Wire.h>
#endif
#if defined(HASSIMPLEWIRE) && !defined(ARDUINODIRECTI2C)
#include <Wire.h>
#endif
/*
* IO channel 2 - Keyboards
*
* This is Paul Stoffregen's PS2Keyboard library for PS2 keyboards
* patched for use with non AVR boards. Please download the patched
* version from github: https://github.com/slviajero/PS2Keyboard
*/
#ifdef ARDUINOPS2
#include <PS2Keyboard.h>
#endif
/*
* The USB keyboard code of the Arduino DUE. Keymapping and timing
* need improvement. Currently not a priority.
*/
#ifdef ARDUINOUSBKBD
#include <KeyboardController.h>
#endif
/*
* The ZX81 keyboard code - tested on AVR MEGA256. Please download
* the library from github: https://github.com/slviajero/ZX81Keyboard
*/
#ifdef ARDUINOZX81KBD
#include <ZX81Keyboard.h>
#endif
/*
* This is for the USB keyboard code on a GIGA board
* https://docs.arduino.cc/tutorials/giga-r1-wifi/giga-usb/#usb-host-keyboard
* like the DUE keyboard, this code is not recommended.
*/
#ifdef GIGAUSBKBD
#include "USBHostGiga.h"
#endif
/*
* IO channel 2 - Displays
*
* The display library for the parallel LCD, this is a standard
* Arduino library.
*/
#ifdef LCDSHIELD
#include <LiquidCrystal.h>
#endif
/*
* I2C LCD displays, this library works almost universally despite
* a nasty warning message it sometimes gives.
*/
#ifdef ARDUINOLCDI2C
#include <LiquidCrystal_I2C.h>
#endif
/*
* This is the monochrome library of Oli Kraus used for Nokia and
* SSD1306 displays.
* https://github.com/olikraus/u8g2/wiki/u8g2reference
*
* It can harware scroll, but this is not yet implemented.
*/
#if defined(ARDUINONOKIA51) || defined(ARDUINOSSD1306)
#include <U8g2lib.h>
#endif
/*
* This is the (old) ILI9488 library originally created by Jarett Burket
* for SPI control if the very common ILI9488 displays.
* I created a fork as the original is no longer maintained (last commits
* from 2017). Patches in my version allow use from RP2040.
* https://github.com/slviajero/ILI9488
*
* It can hardware scroll (not yet used).
*/
#ifdef ARDUINOILI9488
#include <Adafruit_GFX.h>
#include <ILI9488.h>
#endif
/*
* This is the MCUFRIED library originally for parallel TFTs
* https://github.com/prenticedavid/MCUFRIEND_kbv
*
* For R4 boards, use my patched version
* https://github.com/slviajero/MCUFRIEND_kbv
*
* The original library is board hardware depended as it tries
* to make use of the port macros. This makes it fast but
* it cannot used on non supported boards.
*
* This library can drive the TFT from any board as it has a
* generic section using the Arduino GPIOs. This is slower but
* works. For R4 it uses the port macros.
*/
#ifdef ARDUINOMCUFRIEND
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#endif
/*
* For TFT we use the UTFT library
* http://www.rinkydinkelectronics.com/library.php?id=51
* Please note the License, it is not GPL but NON COMMERCIAL
* Creative Commons.
*/
#ifdef ARDUINOTFT
#include <memorysaver.h>
#include <UTFT.h>
#endif
/*
* Lilygo EDP47 displays, 4.7 inch epapers using the respective library
* from Lilygo https://github.com/Xinyuan-LilyGO/LilyGo-EPD47
*/
#ifdef ARDUINOEDP47
#include "epd_driver.h"
#include "font/firasans.h"
#endif
/*
* VGA is only implemented on one platform - TTGO VGA 1.4
* Needs https://github.com/slviajero/FabGL
*
* There is a side effect to the network code. The Wifi library
* has to be sourced before FabGL. Reason is unknown.
*/
#if defined(ARDUINOVGA) && defined(ARDUINO_TTGO_T7_V14_Mini32)
#include <WiFi.h>
#include <fabgl.h>
#endif
/*
* IO channel 9 - Networking.
*
* Currently the standard Ethernet shield, ESP Wifi
* MKW Wifi, RP2040 Wifi, R4 Wifi and GIGA is supported.
* All of them with the standard library.
*
* In addition to this Pubsub is used
* https://github.com/slviajero/pubsubclient
* for MQTT. This is the standard library for MQTT on Arduino.
*
* The ARDUINOMQTT macro is set in hardware.h while
* the ARDUINOWIFI is just generated by hardware.h if
* Ethernet is not set but MQTT is set. We assume that
* mostly it is Wifi we are dealing with.
*/
/* the network transport layer, either Ethernet or Wifi */
#ifdef ARDUINOETH
#include <Ethernet.h>
#else
#ifdef ARDUINOWIFI
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#elif defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_SAMD)
#include <WiFiNINA.h>
#elif defined(ARDUINO_UNOR4_WIFI)
#include <WiFiS3.h>
#elif defined(ARDUINO_ARCH_MBED_GIGA)
#include <WiFi.h>
#endif
#endif
#endif
/* the protocall */
#if defined(ARDUINOMQTT)
#include <PubSubClient.h>
#endif
/*
* IO channel 16 - the various filesystems.
*
* SD filesystems with the standard SD driver.
*
* For MEGA 256 a soft SPI solution is needed
* if standard shields are used, this done in is a
* patched SD library https://github.com/slviajero/SoftSD
*/
#ifdef ARDUINOSD
#define FILESYSTEMDRIVER
#if defined(SOFTWARE_SPI_FOR_SD)
#include <SoftSD.h>
#else
#include <SD.h>
#endif
#endif
/*
* ESPSPIFFS tested on ESP8266 and ESP32 supports formating in BASIC.
* This is standard on all ESP boards and autoconfigured.
*/
#ifdef ESPSPIFFS
#define FILESYSTEMDRIVER
#ifdef ARDUINO_ARCH_ESP8266
#include <FS.h>
#endif
#ifdef ARDUINO_ARCH_ESP32
#include <FS.h>
#include <SPIFFS.h>
#endif
#endif
/*
* ESP32FAT tested on ESP32 supports formating in BASIC. Only tested
* on the T-Deck. Partition at upload must be ffat. This is available
* on ESP32S3 but not on older boards.
*/
#ifdef ESP32FAT
#define FILESYSTEMDRIVER
#ifdef ARDUINO_ARCH_ESP32
#include <FS.h>
#include <FFat.h>
#endif
#endif
/*
* ESPSDMMC code
*/
#ifdef ESPSDMMC
#include "FS.h"
#include "SD_MMC.h"
#endif
/*
* ESPSDMMC code, this is an SD card solution with direct access
* only available on ES32 and ESP32-S3
*
* https://github.com/espressif/arduino-esp32/tree/master/libraries/SD_MMC
*
* Needed for the ESP32 CAM
*
*/
/*
* The USB filesystem for the GIGA board.
* USB device has to be connected at boot time. No remount is possible.
*/
#ifdef GIGAUSBFS
#include <DigitalOut.h>
#include <FATFileSystem.h>
#include <Arduino_USBHostMbed5.h>
#endif
/*
* RP2040 internal filesystem.
* This is test code from https://github.com/slviajero/littlefs
* and the main branch is actively developed.
*/
#ifdef RP2040LITTLEFS
#define FILESYSTEMDRIVER
#define LFS_MBED_RP2040_VERSION_MIN_TARGET "LittleFS_Mbed_RP2040 v1.1.0"
#define LFS_MBED_RP2040_VERSION_MIN 1001000
#define _LFS_LOGLEVEL_ 1
#define RP2040_FS_SIZE_KB 1024
#define FORCE_REFORMAT false
#include <LittleFS_Mbed_RP2040.h>
#endif
/*
* STM32 SDIO driver for he SD card slot of the STM32F4 boards (and others).
*/
#ifdef STM32SDIO
#define FILESYSTEMDRIVER
#include <STM32SD.h>
#ifndef SD_DETECT_PIN
#define SD_DETECT_PIN SD_DETECT_NONE
#endif
#endif
/*
* External flash file systems on an SD override internal filesystems
* (currently BASIC can only have one filesystem).
*/
#if defined(ARDUINOSD) ||defined(ESPSDMMC)
#undef ESPSPIFFS
#undef RP2040LITTLEFS
#undef ESP32FAT
#undef GIGAUSBFS
#define FILESYSTEMDRIVER
#endif
/*
* Support for external EEPROMs as filesystem overriding all
* other filessystems. This is a minimalistic filesystem meant
* for very small systems with not enough memory for real
* filesystems
* https://github.com/slviajero/EepromFS
*/
#ifdef ARDUINOEFS
#undef ESPSPIFFS
#undef ESP32FAT
#undef RP2040LITTLEFS
#undef ARDUINOSD
#undef STM32SDIO
#undef GIGAUSBFS
#define FILESYSTEMDRIVER
#endif
/* The EFS object is used for filesystems and raw EEPROM access. */
#if (defined(ARDUINOI2CEEPROM) && defined(ARDUINOI2CEEPROM_BUFFERED)) || defined(ARDUINOEFS)
#include <EepromFS.h
#endif
/*
* Software SPI only on Mega2560. This only used for the SD shield typically
* but not necessarily with a TFT display.
*/
#ifndef ARDUINO_AVR_MEGA2560
#undef SOFTWARE_SPI_FOR_SD
#endif
/*
* TFT_eSPI is a library for the T-Deck from Lilygo.
*
* #include "utilities.h" provided by Lilygo is coded directly into hardware.h
* For some reason this has to be included after FS and FFat. Not yet fully
* understood why.
*/
#ifdef TFTESPI
#include <TFT_eSPI.h>
#endif
/*
* Global variables of the runtime environment.
*/
/* If there is an unbuffered I2C EEPROM, use an autodetect mechanism. */
#if defined(ARDUINOI2CEEPROM)
unsigned int i2ceepromsize = 0;
#endif
/*
* Arduino default serial baudrate and serial flags for the
* two supported serial interfaces. Serial is always active and
* connected to channel &1 with 9600 baud.
*
* Channel 4 (ARDUINOPRT) can be either in character or block
* mode. Blockmode is set as default here. This means that all
* available characters are always loaded to a string -> inb().
* This is typically needed if you want to receive AT commands from
* a modem or a GPS device.
*
* Serial baudrate of the second channel is changeable in the BASIC
* program.
*/
const uint16_t serial_baudrate = 9600;
uint8_t sendcr = 0;
#ifdef ARDUINOPRT
uint32_t serial1_baudrate = 9600;
uint8_t blockmode = 1;
#else
const int serial1_baudrate = 0;
uint8_t blockmode = 0;
#endif
/*
* Implementation of the global IO function. These are the
* functions that are called from the BASIC interpreter. To access
* a device.
*
* ioinit(): called at setup to initialize what ever io is needed.
* outch(): prints one ascii character to the output channel od.
* inch(): gets one character (and waits for it) from the input channel id.
* checkch(): checks for one character (non blocking) on the input channel id.
* ins(): reads an entire line (uses inch except for pioserial) from the input channel id.
* availch(): checks for available characters on the input channel id.
* inb(): reads a block of characters from the input channel id.
* outs(): prints a string of characters to the output channel od.
*
*/
void ioinit() {
/* a standalone system runs from keyboard and display, i.e channel 2 */
#ifdef STANDALONE
idd = IKEYBOARD;
odd = ODSP;
#endif
/* run standalone on second serial, set the right parameters, this is
needed for a few boars where the first serial port is not usable
for BASIC */
#ifdef STANDALONESECONDSERIAL
idd = ISERIAL1;
odd = OPRT;
blockmode = 0;
sendcr = 0;
#endif
/*
* Signal handling - by default SIGINT which is ^C is always caught and
* leads to program stop. Side effect: the interpreter cannot be stopped
* with ^C, it has to be left with CALL 0, works on Linux, Mac and MINGW
* but not on DOSBOX MSDOS as DOSBOS does not handle CTRL BREAK correctly
* DOS can be interrupted with the CONIO mechanism using BREAKCHAR.
* Here on Arduino signalon() is currentl unused.
*/
signalon();
/* This is only for RASPBERRY - wiring has to be started explicitly. */
wiringbegin();
/* Start all serial protocolls, ttl channels, SPI and Wire. */
serialbegin();
/* start the second serial port */
#ifdef ARDUINOPRT
prtbegin();
#endif
/* start SPI (before displays and filesystems)*/
#ifdef ARDUINOSPI
spibegin();
#endif
/* start wire */
#if defined(HASWIRE) || defined(HASSIMPLEWIRE)
wirebegin();
#endif
/* Filesystems */
fsbegin();
/* networks */
#ifdef ARDUINOMQTT
netbegin();
mqttbegin();
#endif
/* the keyboards */
#if defined(HASKEYBOARD) || defined(HASKEYPAD)
kbdbegin();
#endif
/* the displays */
#if defined(DISPLAYDRIVER) || defined(GRAPHDISPLAYDRIVER)
dspbegin();
#endif
/* fablib code (and framebuffer on POSIX) do not use the graphics driver startup
they use vgabegin(), we also do not use the display driver */
#if defined(ARDUINOVGA)
vgabegin();
#endif
/* sensor startup */
#ifdef ARDUINOSENSORS
sensorbegin();
#endif
/* clocks and time */
#if defined(HASCLOCK)
rtcbegin();
#endif
/* the eeprom dummy needs a begin method */
ebegin();
/* activate the iodefaults */
iodefaults();
}
/* The status of the io streams just on/off on that why (0) for all the stat function */
int iostat(int channel) {
switch(channel) {
/* channel 0, the buffer */
case ONULL:
return bufferstat(0);
break;
/* channel 1, the serial port */
case ISERIAL:
return serialstat(0);
break;
/* channel 2, the display */
case ODSP:
return dspstat(0);
break;
/* channel 4, the second serial device */
#ifdef ARDUINOPRT
case ISERIAL1:
return prtstat(0);
break;
#endif
/* channel 7 wire */
#if defined(HASWIRE)
case IWIRE:
return wirestat(0);
break;
#endif
/* channel 8 radio adaptors */
#ifdef HASRF24
case IRADIO:
return radiostat(0);
break;
#endif
/* channel 9 mqtt */
#ifdef ARDUINOMQTT
case IMQTT:
return mqttstat(0);
break;
#endif
/* channel 16 file system */
#ifdef FILESYSTEMDRIVER
case IFILE:
return fsstat(0);
break;
#endif
}
return 0;
}
/* set the iodefaults at startup and in runtime status change to interactive */
void iodefaults() {
od=odd;
id=idd;
}
/* char is sometimes signed and sometimes not */
int cheof(int c) { if ((c == -1) || (c == 255)) return 1; else return 0; }
/* the generic inch code reading one character from a stream, blocking! */
char inch() {
switch(id) {
case ONULL:
return bufferread();
case ISERIAL:
return serialread();
#ifdef ARDUINOPRT
case ISERIAL1:
return prtread();
#endif
#if defined(HASKEYBOARD) || defined(HASKEYPAD) || defined(HASVT52)
case IKEYBOARD:
#if defined(HASVT52) /* if the display has a message, read it, it acts as a keyboard */
if (vt52avail()) return vt52read();
#endif
#if defined(HASKEYBOARD) || defined(HASKEYPAD)
return kbdread();
#endif
#endif
#if defined(HASWIRE)
case IWIRE:
return wireread();
#endif
#ifdef HASRF24
case IRADIO:
return radioread();
#endif
#ifdef ARDUINOMQTT
case IMQTT:
return mqttread();
#endif
#ifdef FILESYSTEMDRIVER
case IFILE:
return fileread();
#endif
}
return 0;
}
/*
* checking on a character in the stream, this is normally only used
* for interrupting a program, for many streams this is just mapped
* to avail. This is currently inconsistent.
*/
char checkch(){
switch (id) {
case ONULL:
return buffercheckch();
case ISERIAL:
return serialcheckch();
#ifdef FILESYSTEMDRIVER
case IFILE:
return fileavailable();
#endif
#ifdef HASRF24
case IRADIO:
return radioavailable();
#endif
#ifdef ARDUINOMQTT
case IMQTT:
return mqttcheckch();
#endif
#ifdef HASWIRE
case IWIRE:
return 0;
#endif
#ifdef ARDUINOPRT
case ISERIAL1:
return prtcheckch();
#endif
case IKEYBOARD:
#if defined(HASKEYBOARD) || defined(HASKEYPAD)
return kbdcheckch(); /* here no display read as this is only for break and scroll control */
#endif
break;
}
return 0;
}
/* character availability */
uint16_t availch(){
switch (id) {
case ONULL:
return bufferavailable();
case ISERIAL:
return serialavailable();
#ifdef FILESYSTEMDRIVER
case IFILE:
return fileavailable();
#endif
#ifdef HASRF24
case IRADIO:
return radioavailable();
#endif
#ifdef ARDUINOMQTT
case IMQTT:
return mqttavailable();
#endif
#if defined(HASWIRE)
case IWIRE:
return wireavailable();
#endif
#ifdef ARDUINOPRT
case ISERIAL1:
return prtavailable();
#endif
case IKEYBOARD:
#if defined(HASKEYBOARD) || defined(HASKEYPAD) || defined(HASVT52)
#if defined(HASVT52) /* if the display has a message, read it */
if (vt52avail()) return vt52avail();
#endif
#if defined(HASKEYBOARD) || defined(HASKEYPAD)
return kbdavailable();
#endif
#endif
break;
}
return 0;
}
/*
* The block mode reader for esp and sensor modules
* on a serial interface, it tries to read as many
* characters as possible into a buffer.
*
* blockmode = 1 reads once availch() bytes
* blockmode > 1 implements a timeout mechanism and tries
* to read until blockmode milliseconds have expired
* this is needed for esps and other sensors without
* flow control and volatile timing to receive more
* then 64 bytes.
*
* The function only exists if ARDUINOPRT is defined to
* activate the second serial port.
*/
uint16_t inb(char *b, uint16_t nb) {
long m;
uint16_t z;
int16_t i = 0; // check this
if (blockmode == 1) {
i=availch();
if (i>nb-1) i=nb-1;
b[0]=(unsigned char)i;
z=i;
b[i+1]=0;
b++;
while (i--) {*b++=inch();}
} else if (blockmode > 1) {
m=millis();
while (i < nb-1) {
if (availch()) b[++i]=inch();
if (millis() > m+blockmode) break;
}
b[0]=(unsigned char)i;
z=i;
b[i+1]=0;
} else {
b[0]=0;
z=0;
b[1]=0;
}
return z;
}
/*
* Reading one line from the console with inch().
* consins() is used for all channels where individual
* characters are read. Others which provide entire strings
* are handled by their respective string methods.
* (Example: picoserial, radio, wire)
*/
uint16_t consins(char *b, uint16_t nb) {
char c;
uint16_t z;
z=1;
while(z < nb) {
c=inch();
if (id == ISERIAL || id == IKEYBOARD) {
outch(c); /* this is local echo */
}
if (c == '\r') c=inch(); /* skip carriage return */
if (c == '\n' || c == -1 || c == 255) { /* terminal character is either newline or EOF */
break;
} else if (c == 127 || c == 8) { /* backspace or delete */
if (z>1) z--;
} else {
b[z++]=c;
}
}
b[z]=0;
z--;
b[0]=z; /* cast to signed char is not good */
return z;
}
/*
* ins() is the generic reader into a string, by default
* it works in line mode and ends reading after newline.
*
* The first element of the buffer is the lower byte of the length.
*
* For streams providing entire strings as an input the
* respective string method is called.
*
* All other streams are read using consins() for character by character
* input until a terminal character is reached. For almost all channel
* this is done in a *ins() method. The advantage is that one can plug in
* line oriented input here.
*/
uint16_t ins(char *b, uint16_t nb) {
switch(id) {
case ONULL:
return bufferins(b, nb);
case ISERIAL:
return serialins(b, nb);
#if defined(HASKEYBOARD) || defined(HASKEYPAD)
case IKEYBOARD:
return kbdins(b, nb);
#endif
#ifdef ARDUINOPRT
case ISERIAL1:
return prtins(b, nb);
#endif
#if defined(HASWIRE)
case IWIRE:
return wireins(b, nb);
#endif
#ifdef HASRF24
case IRADIO:
return radioins(b, nb);
#endif
#ifdef ARDUINOMQTT
case IMQTT:
return mqttins(b, nb);
#endif
#ifdef FILESYSTEMDRIVER
case IFILE:
return consins(b, nb);
#endif
default:
b[0]=0; b[1]=0;
return 0;
}
}
/*
* outch() outputs one character to a stream.
* some block oriented i/o like in radio not implemented here.
* Exception is mqtt where we buffer in the code for the PRINT command.
*
* If BASIC needs a MS style tab command, then count characters on stream
* 0-4 this does not work for control characters.
*/
void outch(char c) {
#ifdef HASMSTAB
if (od <= OPRT) {
if (c > 31) charcount[od]+=1;
if (c == 10) charcount[od]=0;
}
#endif
switch(od) {
case ONULL:
bufferwrite(c);
break;
case OSERIAL:
serialwrite(c);
break;
#ifdef ARDUINOPRT
case OPRT:
prtwrite(c);
break;
#endif
#ifdef FILESYSTEMDRIVER
case OFILE:
filewrite(c);
break;
#endif
#ifdef ARDUINOVGA
case ODSP:
vgawrite(c);
break;
#elif defined(DISPLAYDRIVER) || defined(GRAPHDISPLAYDRIVER)
case ODSP:
dspwrite(c);
break;
#endif
#ifdef ARDUINOMQTT
case OMQTT:
mqttwrite(c); /* buffering for the PRINT command */
break;
#endif
default:
break;
}
byield(); /* yield after every character for ESP8266 */
}
/*
* outs() outputs a string of length x at index ir - basic style
* default is a character by character operation, block
* oriented write needs special functions
*/
void outs(char *b, uint16_t l){
uint16_t i;
switch (od) {
#ifdef HASRF24
case ORADIO:
radioouts(b, l);
break;
#endif
#if (defined(HASWIRE))
case OWIRE:
wireouts(b, l);
break;
#endif
#ifdef ARDUINOMQTT
case OMQTT:
mqttouts(b, l);
break;
#endif
#ifdef GRAPHDISPLAYDRIVER
case ODSP:
dspouts(b, l);
break;
#endif
default:
for(i=0; i<l; i++) outch(b[i]);
}
byield(); /* triggers yield after each character output for ESP8266 stability */
}
/* handling time - part of the Arduino core - only needed on POSIX OSes */
void timeinit() {}
/* starting wiring is only needed on raspberry */
void wiringbegin() {}
/* POSIX signals - not needed here */
void signalon() {}
/*
* helper functions OS, heuristic on how much memory is
* available in BASIC
* Arduino information from
* data from https://docs.arduino.cc/learn/programming/memory-guide
*/
#if defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_XMC) || defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_RENESAS)
extern "C" char* sbrk(int incr);
long freeRam() {
char top;
return &top - reinterpret_cast<char*>(sbrk(0));
}
#elif defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) || defined(ARDUINO_ARCH_LGT8F)
long freeRam() {
extern int __heap_start,*__brkval;
int v;
return (int)&v - (__brkval == 0
? (int)&__heap_start : (int) __brkval);
}
#elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
long freeRam() {
return ESP.getFreeHeap();
}
#else
long freeRam() {
return 0;
}
#endif
/*
* Heuristic Wifi systems reserve 4k by default, small 8 bit AVR try to guess sizes conservatively
* RP2040 cannot measure, we set to 16 bit full address space
*/
long freememorysize() {
#if defined(ARDUINO_ARCH_RENESAS)
return freeRam() - 2000;
#endif
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_STM32)
return freeRam() - 4000;
#endif
#if defined(ARDUINO_ARCH_ESP32)
#if defined(ARDUINO_TTGO_T7_V14_Mini32)
return freeRam() - 4000;
#else
return freeRam() - 4000;
#endif
#endif
#if defined(ARDUINO_ARCH_XMC)
return freeRam() - 2000;
#endif
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) || defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_LGT8F)
int overhead=256;
#ifdef ARDUINO_ARCH_LGT8F
overhead+=0;
#endif
#ifdef ARDUINO_AVR_MEGA2560
overhead+=96;
#endif
#if (defined(ARDUINOWIRE) || defined(ARDUINOSIMPLEWIRE)) && !defined(ARDUINODIRECTI2C)
overhead+=200;
#endif
#ifdef ARDUINORF24
overhead+=128;
#endif
#if defined(ARDUINOSD)
overhead+=512;
#endif
#ifdef ARDUINOZX81KBD
overhead+=64;
#endif
#ifdef ARDUINOETH
overhead+=256;
#endif
return freeRam() - overhead;
#endif
#if defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ARCH_MBED_GIGA)
return 65536;
#endif
return 0;
}
/*
* the sleep and restart functions - only implemented for some controllers
*/
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
void(* callzero)() = 0;
#endif
void restartsystem() {
eflush(); /* if there is a I2C eeprom dummy, flush the buffer */
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
ESP.restart();
#endif
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
callzero();
#endif
#if defined(ARDUINO_ARCH_LGT8F)
#endif
}
/*
* I used these two articles
* https://randomnerdtutorials.com/esp8266-deep-sleep-with-arduino-ide/
* https://randomnerdtutorials.com/esp32-deep-sleep-arduino-ide-wake-up-sources/
* for this very simple implementation - needs to be improved (pass data from sleep
* state to sleep state via EEPROM)
*/
#if defined(ARDUINO_ARCH_SAMD)
#include "RTCZero.h"
#include "ArduinoLowPower.h"
RTCZero rtc;
#endif
/* STM32duino have the same structure */
#if defined(ARDUINO_ARCH_STM32)
#include "STM32RTC.h"
#include "STM32LowPower.h"
STM32RTC& rtc = STM32RTC::getInstance();
#endif
/* the NRENESA board have a buildin RTC as well */
#if defined(ARDUINO_ARCH_RENESAS)
#include "RTC.h"
RTCTime rtc;
#endif
/* for ESP32 we also include the time stuctures and offer a POSIX style clock*/
#if defined(ARDUINO_ARCH_ESP32)
#include "time.h"
#include <sys/time.h>
#endif
/* this is unfinished, don't use */
void rtcsqw();
#define LOWPOWERINTPIN 2
void aftersleepinterrupt(void) { }
void activatesleep(long t) {
eflush(); /* if there is a I2C eeprom dummy, flush the buffer */
#if defined(ARDUINO_ARCH_ESP8266)
ESP.deepSleep(t*1000);
#endif
#if defined(ARDUINO_ARCH_ESP32)
esp_sleep_enable_timer_wakeup(t*1000);
esp_deep_sleep_start();
#endif
#if defined(ARDUINO_ARCH_SAMD)
LowPower.sleep((int) t);
#endif
#if defined(ARDUINO_AVR_ATmega644)
/* unfinished, don't use, just test code
rtcsqw();
pinMode(LOWPOWERINTPIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(LOWPOWERINTPIN), aftersleepinterrupt, CHANGE);
sleepMode(SLEEP_POWER_SAVE);
sleep();
detachInterrupt(digitalPinToInterrupt(LOWPOWERINTPIN));
noSleep();
*/
#endif
}
/*
* Start the SPI bus - this is a little mean as some libraries also
* try to start the SPI which may lead to on override of the PIN settings
* if the library code is not clean - currenty no conflict known.
*
* The startup on the T-Deck seems to be tricky and require precautions.
* The startup sequence used here is from the HelloWorld example of the
* TFT_eSPI library.
*/
void spibegin() {
#ifdef ARDUINOSPI
#ifdef ARDUINO_TTGO_T7_V14_Mini32
/* this fixes the wrong board definition in the ESP32 core for this board */
SPI.begin(14, 2, 12, 13);
#else
#ifdef TFTESPI
//! The board peripheral power control pin needs to be set to HIGH when using the peripheral
pinMode(BOARD_POWERON, OUTPUT);
digitalWrite(BOARD_POWERON, HIGH);
//! Set CS on all SPI buses to high level during initialization
pinMode(BOARD_SDCARD_CS, OUTPUT);
pinMode(RADIO_CS_PIN, OUTPUT);
pinMode(BOARD_TFT_CS, OUTPUT);
digitalWrite(BOARD_SDCARD_CS, HIGH);
digitalWrite(RADIO_CS_PIN, HIGH);
digitalWrite(BOARD_TFT_CS, HIGH);
pinMode(BOARD_SPI_MISO, INPUT_PULLUP);
SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI);
#else
SPI.begin();
#endif
#endif
#endif
}
/*
* DISPLAY driver code section, the hardware models define a set of
* of functions and definitions needed for the display driver.
*
* See runtime.h for the definitions of the display driver functions.
*/
/* generate a 4 bit vga color from a given rgb color */
uint8_t rgbtovga(uint8_t r, uint8_t g, uint8_t b) {
short vga;
if (r>191 || g>191 || b>191) vga=8; else vga=0;
vga=vga+r/128+g/128*2+b/128*4;
return vga;
}
/*
* global variables for a standard LCD shield.
* Includes the standard Arduino LiquidCrystal library
*/
#ifdef LCDSHIELD
#define DISPLAYDRIVER
#undef DISPLAYHASCOLOR
#undef DISPLAYHASGRAPH
/* LCD shield pins to Arduino
* RS, EN, d4, d5, d6, d7;
* backlight on pin 10;
*/
#ifndef LCDSHIELDPINS
#ifdef ARDUINO_ESP8266_WEMOS_D1R1
#define LCDSHIELDPINS 0,2,4,14,12,13
#define LCD3VOLTS
#else
#define LCDSHIELDPINS 8,9,4,5,6,7
#endif
#endif
const int dsp_rows=2;
const int dsp_columns=16;
LiquidCrystal lcd(LCDSHIELDPINS);
void dspbegin() { lcd.begin(dsp_columns, dsp_rows); dspsetscrollmode(1, 1); }
void dspprintchar(char c, uint8_t col, uint8_t row) { lcd.setCursor(col, row); if (c) lcd.write(c);}
void dspclear() { lcd.clear(); }
void dspupdate() {}
void dspsetcursor(uint8_t c) { if (c) lcd.blink(); else lcd.noBlink(); }
void dspsetfgcolor(uint8_t c) {}
void dspsetbgcolor(uint8_t c) {}
void dspsetreverse(uint8_t c) {}
uint8_t dspident() {return 0; }
#define HASKEYPAD
/* elementary keypad reader left=1, right=2, up=3, down=4, select=<lf> */
char keypadread(){
int a=analogRead(A0);
#ifndef LCD3VOLTS
if (a >= 850) return 0;
else if (a>=600 && a<850) return 10;
else if (a>=400 && a<600) return '1';
else if (a>=200 && a<400) return '3';
else if (a>=60 && a<200) return '4';
else return '2';
#else
if (a >= 1000) return 0;
else if (a>=900 && a<1000) return 10;
else if (a>=700 && a<900) return '1';
else if (a>=400 && a<700) return '3';
else if (a>=100 && a<400) return '4';
else return '2';
#endif
}
/* repeat mode of the keypad - off means block, on means return immediately */
uint8_t kbdrepeat=0;
#endif
/*
* A LCD display connnected via I2C, uses the standard
* Arduino I2C display library.
*/
#ifdef ARDUINOLCDI2C
#define DISPLAYDRIVER
#undef DISPLAYHASCOLOR
#undef DISPLAYHASGRAPH
const int dsp_rows=4;
const int dsp_columns=20;
LiquidCrystal_I2C lcd(0x27, dsp_columns, dsp_rows);
void dspbegin() { lcd.init(); lcd.backlight(); dspsetscrollmode(1, 1); }
void dspprintchar(char c, uint8_t col, uint8_t row) { lcd.setCursor(col, row); if (c) lcd.write(c); }
void dspclear() { lcd.clear(); }
void dspupdate() {}
void dspsetcursor(uint8_t c) { if (c) lcd.blink(); else lcd.noBlink(); }
void dspsetfgcolor(uint8_t c) {}
void dspsetbgcolor(uint8_t c) {}
void dspsetreverse(uint8_t c) {}
uint8_t dspident() {return 0; }
#endif
/*
* A Nokia 5110 with ug8lib2 - can scroll quite well
* https://github.com/olikraus/u8g2/wiki/u8g2reference
* This is a buffered display it has a dspupdate() function
* it also needs to call dspgraphupdate() after each graphic
* operation
*
* default PIN settings here are for ESP8266, using the standard
* SPI SS for 15 for CS/CE, and 0 for DC, 2 for reset
*
*/
#ifdef ARDUINONOKIA51
#define DISPLAYDRIVER
#define DISPLAYPAGEMODE
#undef DISPLAYHASCOLOR /* display driver not color aware for this display */
#define DISPLAYHASGRAPH
#ifndef NOKIA_CS
#define NOKIA_CS 15
#endif
#ifndef NOKIA_DC
#define NOKIA_DC 0
#endif
#ifndef NOKIA_RST
#define NOKIA_RST 2
#endif
U8G2_PCD8544_84X48_F_4W_HW_SPI u8g2(U8G2_R0, NOKIA_CS, NOKIA_DC, NOKIA_RST);
const int dsp_rows=6;
const int dsp_columns=10;
typedef uint8_t dspcolor_t;
dspcolor_t dspfgcolor = 1;
dspcolor_t dspbgcolor = 0;
char dspfontsize = 8;
void dspbegin() { u8g2.begin(); u8g2.setFont(u8g2_font_amstrad_cpc_extended_8r); }
void dspprintchar(char c, uint8_t col, uint8_t row) { char b[] = { 0, 0 }; b[0]=c; if (c) u8g2.drawStr(col*dspfontsize+2, (row+1)*dspfontsize, b); }
void dspclear() { u8g2.clearBuffer(); u8g2.sendBuffer(); dspfgcolor=1; }
void dspupdate() { u8g2.sendBuffer(); }
void dspsetcursor(uint8_t c) {}
void dspsetfgcolor(uint8_t c) {}
void dspsetbgcolor(uint8_t c) {}
void dspsetreverse(uint8_t c) {}
uint8_t dspident() {return 0;}
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) {}
void vgacolor(uint8_t c) { dspfgcolor=c%3; u8g2.setDrawColor(dspfgcolor); }
void plot(int x, int y) { u8g2.setDrawColor(dspfgcolor); u8g2.drawPixel(x, y); dspgraphupdate(); }
void line(int x0, int y0, int x1, int y1) { u8g2.drawLine(x0, y0, x1, y1); dspgraphupdate(); }
void rect(int x0, int y0, int x1, int y1) { u8g2.drawFrame(x0, y0, x1-x0, y1-y0); dspgraphupdate(); }
void frect(int x0, int y0, int x1, int y1) { u8g2.drawBox(x0, y0, x1-x0, y1-y0); dspgraphupdate(); }
void circle(int x0, int y0, int r) { u8g2.drawCircle(x0, y0, r); dspgraphupdate(); }
void fcircle(int x0, int y0, int r) { u8g2.drawDisc(x0, y0, r); dspgraphupdate(); }
#endif
/*
* 4.7 inch epaper displays are derived from the NOKIA51 code, no grayscales
* at the moment. Forcing the font into rectangles and hoping this works.
*
* Epapers bypass the display driver here and use a graphics based display
* mode instead
*/
#ifdef ARDUINOEDP47
#define GRAPHDISPLAYDRIVER
#define DISPLAYPAGEMODE
#undef DISPLAYHASCOLOR /* display driver not color aware for this display */
#define DISPLAYHASGRAPH
const int dsp_width=960;
const int dsp_height=540;
const int dsp_rows=0;
const int dsp_columns=0;
typedef uint8_t dspcolor_t;
dspcolor_t dspfgcolor = 1;
dspcolor_t dspbgcolor = 0;
char dspfontsize = 24;
int dspgraphcursor_x = 0;
int dspgraphcursor_y = dspfontsize;
void dspbegin() { epd_init(); dspclear(); }
void dspprintstring(char* s) {
epd_poweron();
writeln((GFXfont *)&FiraSans, s, &dspgraphcursor_x, &dspgraphcursor_y, NULL);
epd_poweroff();
}
void dspclear() { epd_poweron(); epd_clear(); epd_poweroff(); dspfgcolor=1; }
void dspupdate() { }
void dspsetcursor(uint8_t c) {}
void dspsetfgcolor(uint8_t c) {}
void dspsetbgcolor(uint8_t c) {}
void dspsetreverse(uint8_t c) {}
uint8_t dspident() {return 0;}
void rgbcolor(uint8_t r, uint_8 g, uint8_t b) {}
void vgacolor(uint8_t c) { dspfgcolor=c%3; }
void plot(int x, int y) { }
void line(int x0, int y0, int x1, int y1) { }
void rect(int x0, int y0, int x1, int y1) { }
void frect(int x0, int y0, int x1, int y1) { }
void circle(int x0, int y0, int r) { }
void fcircle(int x0, int y0, int r) { }
#endif
/*
* Small SSD1306 OLED displays with I2C interface
* This is a buffered display it has a dspupdate() function
* it also needs to call dspgraphupdate() after each graphic
* operation
*/
#ifdef ARDUINOSSD1306
#define DISPLAYDRIVER
#define DISPLAYPAGEMODE
#undef DISLAYHASCOLOR /* display driver not color aware for this display */
#define DISPLAYHASGRAPH
#define SSD1306WIDTH 32
#define SSD1306HEIGHT 128
/* constructors may look like this, last argument is the reset pin
* //U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
* //U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, SCL, SDA, U8X8_PIN_NONE);
*/
#if SSD1306WIDTH == 32
/* U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, SCL, SDA, U8X8_PIN_NONE);
* use hardware I2C instead of software. Tested (only) on ESP32C3 and ESP8266 so far.
*/
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);
#endif
#if SSD1306WIDTH == 64
/* the Heltec board has an internal software I2C on pins 4=SDA and 15=SCL */
#ifdef ARDUINO_heltec_wifi_lora_32_V2
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, 15, 4, 16);
#else
/* use hardware I2C instead of software. Tested (only) on ESP32C3 and ESP8266 so far.
*/
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);
#endif
#endif
const char dspfontsize = 8;
const int dsp_rows=SSD1306WIDTH/dspfontsize;
const int dsp_columns=SSD1306HEIGHT/dspfontsize;
typedef uint8_t dspcolor_t;
dspcolor_t dspfgcolor = 1;
dspcolor_t dspbgcolor = 0;
void dspbegin() { u8g2.begin(); u8g2.setFont(u8g2_font_amstrad_cpc_extended_8r); }
void dspprintchar(char c, uint8_t col, uint8_t row) { char b[] = { 0, 0 }; b[0]=c; if (c) u8g2.drawStr(col*dspfontsize+2, (row+1)*dspfontsize, b); }
void dspclear() { u8g2.clearBuffer(); u8g2.sendBuffer(); dspfgcolor=1; }
void dspupdate() { u8g2.sendBuffer(); }
void dspsetcursor(uint8_t c) {}
void dspsetfgcolor(uint8_t c) {}
void dspsetbgcolor(uint8_t c) {}
void dspsetreverse(uint8_t c) {}
uint8_t dspident() {return 0;}
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) {}
void vgacolor(uint8_t c) { dspfgcolor=c%3; u8g2.setDrawColor(dspfgcolor); }
void plot(int x, int y) { u8g2.setDrawColor(dspfgcolor); u8g2.drawPixel(x, y); dspgraphupdate(); }
void line(int x0, int y0, int x1, int y1) { u8g2.drawLine(x0, y0, x1, y1); dspgraphupdate(); }
void rect(int x0, int y0, int x1, int y1) { u8g2.drawFrame(x0, y0, x1-x0, y1-y0); dspgraphupdate(); }
void frect(int x0, int y0, int x1, int y1) { u8g2.drawBox(x0, y0, x1-x0, y1-y0); dspgraphupdate(); }
void circle(int x0, int y0, int r) { u8g2.drawCircle(x0, y0, r); dspgraphupdate(); }
void fcircle(int x0, int y0, int r) { u8g2.drawDisc(x0, y0, r); dspgraphupdate(); }
#endif
/*
* A ILI9488 with Jarett Burkets version of Adafruit GFX and patches
* by Stefan Lenz
* currently only slow software scrolling implemented in BASIC
*
* https://github.com/slviajero/ILI9488
*
* we use 9, 8, 7 as CS, CE, RST by default and A7 for the led brightness control
*/
#ifdef ARDUINOILI9488
#define DISPLAYDRIVER
#define DISPLAYHASCOLOR
#define DISPLAYHASGRAPH
#ifndef ILI_CS
#define ILI_CS 9
#endif
#ifndef ILI_DC
#define ILI_DC 8
#endif
#ifndef ILI_RST
#define ILI_RST 7
#endif
#ifndef ILI_LED
#define ILI_LED A3
#endif
ILI9488 tft = ILI9488(ILI_CS, ILI_DC, ILI_RST);
/* ILI in landscape */
const int dsp_rows=20;
const int dsp_columns=30;
char dspfontsize = 16;
typedef uint16_t dspcolor_t;
const uint16_t dspdefaultfgcolor = 0xFFFF;
const uint8_t dspdefaultfgvgacolor = 0x0F;
dspcolor_t dspfgcolor = dspdefaultfgcolor;
dspcolor_t dspbgcolor = 0;
dspcolor_t dsptmpcolor = 0;
uint8_t dspfgvgacolor = dspdefaultfgvgacolor;
uint8_t dsptmpvgacolor = 0;
void dspbegin() {
tft.begin();
tft.setRotation(3); /* ILI in landscape, SD slot up */
tft.setTextColor(dspfgcolor);
tft.setTextSize(2);
tft.fillScreen(dspbgcolor);
pinMode(ILI_LED, OUTPUT);
analogWrite(ILI_LED, 255);
dspsetscrollmode(1, 4);
}
void dspprintchar(char c, uint8_t col, uint8_t row) { if (c) tft.drawChar(col*dspfontsize, row*dspfontsize, c, dspfgcolor, dspbgcolor, 2); }
void dspclear() {
tft.fillScreen(dspbgcolor);
dspfgcolor = dspdefaultfgcolor;
dspfgvgacolor = dspdefaultfgvgacolor;
}
void dspupdate() {}
void dspsetcursor(uint8_t c) {}
void dspsavepen() { dsptmpcolor=dspfgcolor; dsptmpvgacolor=dspfgvgacolor; }
void dsprestorepen() { dspfgcolor=dsptmpcolor; dspfgvgacolor=dsptmpvgacolor; }
void dspsetfgcolor(uint8_t c) { vgacolor(c); }
void dspsetbgcolor(uint8_t c) { }
void dspsetreverse(uint8_t c) {}
uint8_t dspident() {return 0; }
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) { dspfgvgacolor=rgbtovga(r, g, b); dspfgcolor=tft.color565(r, g, b);}
void vgacolor(uint8_t c) {
short base=128;
dspfgvgacolor=c;
if (c==8) { dspfgcolor=tft.color565(64, 64, 64); return; }
if (c>8) base=255;
dspfgcolor=tft.color565(base*(c&1), base*((c&2)/2), base*((c&4)/4));
}
void plot(int x, int y) { tft.drawPixel(x, y, dspfgcolor); }
void line(int x0, int y0, int x1, int y1) { tft.drawLine(x0, y0, x1, y1, dspfgcolor); }
void rect(int x0, int y0, int x1, int y1) { tft.drawRect(x0, y0, x1, y1, dspfgcolor);}
void frect(int x0, int y0, int x1, int y1) { tft.fillRect(x0, y0, x1, y1, dspfgcolor); }
void circle(int x0, int y0, int r) { tft.drawCircle(x0, y0, r, dspfgcolor); }
void fcircle(int x0, int y0, int r) { tft.fillCircle(x0, y0, r, dspfgcolor); }
#endif
/*
* A MCUFRIEND parallel port display for the various tft shields
* This implementation is mainly for Arduino MEGA
*
* currently only slow software scrolling implemented in BASIC
*
*/
#ifdef ARDUINOMCUFRIEND
#define DISPLAYDRIVER
#define DISPLAYHASCOLOR
#define DISPLAYHASGRAPH
#ifndef LCD_CS
#define LCD_CS A3
#endif
#ifndef LCD_CD
#define LCD_CD A2
#endif
#ifndef LCD_WR
#define LCD_WR A1
#endif
#ifndef LCD_RD
#define LCD_RD A0
#endif
#ifndef LCD_RESET
#define LCD_RESET A4
#endif
MCUFRIEND_kbv tft;
/* ILI in landscape */
const int dsp_rows=20;
const int dsp_columns=30;
char dspfontsize = 16;
typedef uint16_t dspcolor_t;
const uint16_t dspdefaultfgcolor = 0xFFFF;
const uint8_t dspdefaultfgvgacolor = 0x0F;
dspcolor_t dspfgcolor = dspdefaultfgcolor;
dspcolor_t dspbgcolor = 0;
dspcolor_t dsptmpcolor = 0;
uint8_t dspfgvgacolor = dspdefaultfgvgacolor;
uint8_t dsptmpvgacolor = 0;
void dspbegin() {
uint16_t ID = tft.readID();
if (ID == 0xD3D3) ID = 0x9481; /* write-only shield - taken from the MCDFRIEND demo */
tft.begin(ID);
tft.setRotation(1); /* ILI in landscape, 3: SD slot on right the side */
tft.setTextColor(dspfgcolor);
tft.setTextSize(2);
tft.fillScreen(dspbgcolor);
dspsetscrollmode(1, 4); /* scrolling is on, scroll 4 lines at once */
}
void dspprintchar(char c, uint8_t col, uint8_t row) { if (c) tft.drawChar(col*dspfontsize, row*dspfontsize, c, dspfgcolor, dspbgcolor, 2); }
void dspclear() {
tft.fillScreen(dspbgcolor);
dspfgcolor = dspdefaultfgcolor;
dspfgvgacolor = dspdefaultfgvgacolor;
}
void dspupdate() {}
void dspsetcursor(uint8_t c) {}
void dspsavepen() { dsptmpcolor=dspfgcolor; dsptmpvgacolor=dspfgvgacolor; }
void dsprestorepen() { dspfgcolor=dsptmpcolor; dspfgvgacolor=dsptmpvgacolor; }
void dspsetfgcolor(uint8_t c) { vgacolor(c); }
void dspsetbgcolor(uint8_t c) { }
void dspsetreverse(uint8_t c) {}
uint8_t dspident() {return 0; }
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) { dspfgvgacolor=rgbtovga(r, g, b); dspfgcolor=tft.color565(r, g, b);}
void vgacolor(uint8_t c) {
short base=128;
dspfgvgacolor=c;
if (c==8) { dspfgcolor=tft.color565(64, 64, 64); return; }
if (c>8) base=255;
dspfgcolor=tft.color565(base*(c&1), base*((c&2)/2), base*((c&4)/4));
}
void plot(int x, int y) { tft.drawPixel(x, y, dspfgcolor); }
void line(int x0, int y0, int x1, int y1) { tft.drawLine(x0, y0, x1, y1, dspfgcolor); }
void rect(int x0, int y0, int x1, int y1) { tft.drawRect(x0, y0, x1, y1, dspfgcolor);}
void frect(int x0, int y0, int x1, int y1) { tft.fillRect(x0, y0, x1, y1, dspfgcolor); }
void circle(int x0, int y0, int r) { tft.drawCircle(x0, y0, r, dspfgcolor); }
void fcircle(int x0, int y0, int r) { tft.fillCircle(x0, y0, r, dspfgcolor); }
#endif
#ifdef TFTESPI
#define DISPLAYDRIVER
#define DISPLAYHASCOLOR
#define DISPLAYHASGRAPH
/*
* first draft od the code taken from the hello world demo
* of Lilygo. This code only works on the 2.0.14 version of the ESP32 core.
* Physical display dimensions is 320x240. We use the 12pf font for a first
* test. With this font we can display 26 columns and 20 rows. This is set
* statically in the code at the moment.
*/
/* constants for 16 bit fonts */
const int dsp_rows=15;
const int dsp_columns=20;
char dspfontsize = 16;
const char dspfontsizeindex = 2;
/* constants for 8 bit fonts */
/*
const int dsp_rows=30;
const int dsp_columns=40;
char dspfontsize = 8;
const char dspfontsizeindex = 1;
*/
/* unchanged from the ILI code */
typedef uint16_t dspcolor_t;
const uint16_t dspdefaultfgcolor = 0xFFFF;
const uint8_t dspdefaultfgvgacolor = 0x0F;
dspcolor_t dspfgcolor = dspdefaultfgcolor;
dspcolor_t dspbgcolor = 0;
dspcolor_t dsptmpcolor = 0;
uint8_t dspfgvgacolor = dspdefaultfgvgacolor;
uint8_t dsptmpvgacolor = 0;
/* this is new for TFT_eSPI*/
TFT_eSPI tft;
/* change the brightness, taken from HelloWorld as well */
void tftespisetBrightness(uint8_t value)
{
static uint8_t level = 0;
static uint8_t steps = 16;
if (value == 0) {
digitalWrite(BOARD_BL_PIN, 0);
delay(3);
level = 0;
return;
}
if (level == 0) {
digitalWrite(BOARD_BL_PIN, 1);
level = steps;
delayMicroseconds(30);
}
int from = steps - level;
int to = steps - value;
int num = (steps + to - from) % steps;
for (int i = 0; i < num; i++) {
digitalWrite(BOARD_BL_PIN, 0);
digitalWrite(BOARD_BL_PIN, 1);
}
level = value;
}
void dspbegin() {
tft.begin();
tft.setRotation(1);
tft.setTextDatum(6); /* upper left*/
tft.setTextColor(dspfgcolor);
tft.fillScreen(dspbgcolor);
dspsetscrollmode(1, 4); /* scrolling is on, scroll 4 lines at once */
pinMode(BOARD_BL_PIN, OUTPUT);
tftespisetBrightness(16); /* maximum brightness */
}
void dspprintchar(char c, uint8_t col, uint8_t row) {
if (c) tft.drawChar(col*dspfontsize, row*dspfontsize, c, dspfgcolor, dspbgcolor, dspfontsizeindex);
}
void dspclear() {
tft.fillScreen(dspbgcolor);
dspfgcolor = dspdefaultfgcolor;
dspfgvgacolor = dspdefaultfgvgacolor;
}
void dspupdate() {}
void dspsetcursor(uint8_t c) {}
void dspsavepen() { dsptmpcolor=dspfgcolor; dsptmpvgacolor=dspfgvgacolor; }
void dsprestorepen() { dspfgcolor=dsptmpcolor; dspfgvgacolor=dsptmpvgacolor; }
void dspsetfgcolor(uint8_t c) { vgacolor(c); }
void dspsetbgcolor(uint8_t c) { }
void dspsetreverse(uint8_t c) {}
uint8_t dspident() {return 0; }
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) { dspfgvgacolor=rgbtovga(r, g, b); dspfgcolor=tft.color565(r, g, b);}
void vgacolor(uint8_t c) {
short base=128;
dspfgvgacolor=c;
if (c==8) { dspfgcolor=tft.color565(64, 64, 64); return; }
if (c>8) base=255;
dspfgcolor=tft.color565(base*(c&1), base*((c&2)/2), base*((c&4)/4));
}
void plot(int x, int y) { tft.drawPixel(x, y, dspfgcolor); }
void line(int x0, int y0, int x1, int y1) { tft.drawLine(x0, y0, x1, y1, dspfgcolor); }
void rect(int x0, int y0, int x1, int y1) { tft.drawRect(x0, y0, x1, y1, dspfgcolor);}
void frect(int x0, int y0, int x1, int y1) { tft.fillRect(x0, y0, x1, y1, dspfgcolor); }
void circle(int x0, int y0, int r) { tft.drawCircle(x0, y0, r, dspfgcolor); }
void fcircle(int x0, int y0, int r) { tft.fillCircle(x0, y0, r, dspfgcolor); }
#endif
/*
* A no operations graphics dummy
* Tests the BASIC side of the graphics code without triggering
* any output
*/
#ifdef ARDUINOGRAPHDUMMY
#define DISPLAYDRIVER
#undef DISPLAYHASCOLOR
#define DISPLAYHASGRAPH
const int dsp_rows=20;
const int dsp_columns=30;
const uint16_t dspdefaultfgcolor = 1;
char dspfontsize = 16;
typedef uint16_t dspcolor_t;
dspcolor_t dspfgcolor = 0xFFFF;
dspcolor_t dspbgcolor = 0x0000;
void dspbegin() { dspsetscrollmode(1, 4); }
void dspprintchar(char c, uint8_t col, uint8_t row) {}
void dspclear() {}
void dspupdate() {}
void dspsetcursor(uint8_t c) {}
void dspsetfgcolor(uint8_t c) {}
void dspsetbgcolor(uint8_t c) {}
void dspsetreverse(uint8_t c) {}
uint8_t dspident() {return 0; }
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) { dspfgcolor=0; }
void vgacolor(uint8_t c) {
short base=128;
if (c==8) { rgbcolor(64, 64, 64); return; }
if (c>8) base=255;
rgbcolor(base*(c&1), base*((c&2)/2), base*((c&4)/4));
}
void plot(int x, int y) {}
void line(int x0, int y0, int x1, int y1) {}
void rect(int x0, int y0, int x1, int y1) {}
void frect(int x0, int y0, int x1, int y1) {}
void circle(int x0, int y0, int r) {}
void fcircle(int x0, int y0, int r) {}
#endif
/*
* SD1963 TFT display code with UTFT.
* Tested witth SD1963 800*480 board.
* it is mainly intended for a MEGA or DUE as a all in one system
* this is for a MEGA shield and the CTE DUE shield, for the due
* you need to read the comment in Arduino/libraries/UTFT/hardware/arm
* HW_ARM_defines.h -> uncomment the DUE shield
* See also
* https://github.com/slviajero/tinybasic/wiki/Projects:-4.-A-standalone-computer-with-a-TFT-screen-based-on-a-DUE
*/
#ifdef ARDUINOTFT
#define DISPLAYDRIVER
#define DISPLAYHASCOLOR
#define DISPLAYHASGRAPH
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
#ifdef ARDUINO_SAM_DUE
UTFT tft(CTE70,25,26,27,28);
#else
UTFT tft(CTE70,38,39,40,41);
#endif
const int dsp_rows=30;
const int dsp_columns=50;
char dspfontsize = 16;
const uint32_t dspdefaultfgcolor = 0x00FFFFFF;
const uint8_t dspdefaultfgvgacolor = 0x0F;
typedef uint32_t dspcolor_t;
dspcolor_t dspfgcolor = dspdefaultfgcolor;
dspcolor_t dspbgcolor = 0;
dspcolor_t dsptmpcolor = 0;
uint8_t dspfgvgacolor = dspdefaultfgvgacolor;
uint8_t dsptmpvgacolor = 0;
void dspbegin() { tft.InitLCD(); tft.setFont(BigFont); tft.clrScr(); dspsetscrollmode(1, 4); }
void dspprintchar(char c, uint8_t col, uint8_t row) { if (c) tft.printChar(c, col*dspfontsize, row*dspfontsize); }
void dspclear() {
tft.clrScr();
dspfgcolor = dspdefaultfgcolor;
dspfgvgacolor = dspdefaultfgvgacolor;
vgacolor(dspfgvgacolor);
}
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) {
tft.setColor(r,g,b);
dspfgcolor=(r << 16) + (g << 8) + b;
dspfgvgacolor=rgbtovga(r, g, b);
}
void vgacolor(uint8_t c) {
short base=128;
dspfgvgacolor=c;
if (c==8) { tft.setColor(64, 64, 64); return; }
if (c>8) base=255;
tft.setColor(base*(c&1), base*((c&2)/2), base*((c&4)/4));
}
void dspupdate() {}
void dspsetcursor(uint8_t c) {}
void dspsavepen() { dsptmpcolor=dspfgcolor; dsptmpvgacolor=dspfgvgacolor; }
void dsprestorepen() { dspfgcolor=dsptmpcolor; dspfgvgacolor=dsptmpvgacolor; }
void dspsetfgcolor(uint8_t c) { vgacolor(c); }
void dspsetbgcolor(uint8_t c) { }
void dspsetreverse(uint8_t c) {}
uint8_t dspident() {return 0;}
void plot(int x, int y) { tft.drawPixel(x, y); }
void line(int x0, int y0, int x1, int y1) { tft.drawLine(x0, y0, x1, y1); }
void rect(int x0, int y0, int x1, int y1) { tft.drawRect(x0, y0, x1, y1); }
void frect(int x0, int y0, int x1, int y1) { tft.fillRect(x0, y0, x1, y1); }
void circle(int x0, int y0, int r) { tft.drawCircle(x0, y0, r); }
void fcircle(int x0, int y0, int r) { tft.fillCircle(x0, y0, r); }
#endif
/*
* This is the VGA code for fablib.
*
* Not all modes and possibilities explored. It leaves enough memory for the
* interpreter. Networking is not possible an a TTGO T7 V1.4.
*
* This code overrides the display driver logic as fabgl brings an own
* terminal emulation.
*
* Default settings is a 640*480 VGA screen with 16 colors
* and default fonts for the Terminal
*/
#if defined(ARDUINOVGA) && defined(ARDUINO_TTGO_T7_V14_Mini32)
#ifndef TTGOVGACONTROLLER
#define TTGOVGACONTROLLER VGA16Controller
#endif
#ifndef TTGOVGACOLUMNS
#define TTGOVGACOLUMNS -1
#endif
#ifndef TTGOVGAROWS
#define TTGOVGAROWS -1
#endif
#ifndef TTGOVGARESOLUTION
#define TTGOVGARESOLUTION VGA_640x480_60Hz
#endif
fabgl::TTGOVGACONTROLLER VGAController; /* 16 color object with less memory */
static fabgl::Terminal Terminal;
static Canvas cv(&VGAController);
TerminalController tc(&Terminal);
Color vga_graph_pen = Color::BrightWhite;
Color vga_graph_brush = Color::Black;
Color vga_txt_pen = Color::BrightGreen;
Color vga_txt_background = Color::Black;
fabgl::SoundGenerator soundGenerator;
/* this starts the vga controller and the terminal right now */
void vgabegin() {
VGAController.begin(GPIO_NUM_22, GPIO_NUM_21, GPIO_NUM_19, GPIO_NUM_18, GPIO_NUM_5, GPIO_NUM_4, GPIO_NUM_23, GPIO_NUM_15);
VGAController.setResolution(TTGOVGARESOLUTION);
Terminal.begin(&VGAController, TTGOVGACOLUMNS, TTGOVGAROWS);
Terminal.setBackgroundColor(vga_txt_background);
Terminal.setForegroundColor(vga_txt_pen);
#ifdef TTGOVGAFONT
Terminal.loadFont(&fabgl::TTGOVGAFONT);
#endif
Terminal.connectLocally();
Terminal.clear();
Terminal.enableCursor(1);
Terminal.setTerminalType(TermType::VT52);
}
/* for Arduino just a dummy, needed in the POSIX branch of the code */
void vgaend() {}
/* this is a dummy function, we do not have a status line */
int vgastat(uint8_t c) {return 0; }
/* scale the screen size to make a circle round, unneeded if the right resulution is set */
void vgascale(int* x, int* y) {
/* *y=*y*10/16; */
}
void rgbcolor(uint8_t r, uint8_t g, uint8_t b) {
short vga;
if (r>191 || g>191 || b>191) vga=8; else vga=0;
vga=vga+r/128+g/128*2+b/128*4;
vga_graph_pen=fabgl::Color(vga);
}
void vgacolor(uint8_t c) { vga_graph_pen = fabgl::Color(c%16); }
void plot(int x, int y) {
vgascale(&x, &y);
cv.setPenColor(vga_graph_pen);
cv.setPixel(x,y);
cv.setPenColor(vga_txt_pen);
}
void line(int x0, int y0, int x1, int y1) {
vgascale(&x0, &y0);
vgascale(&x1, &y1);
cv.setPenColor(vga_graph_pen);
cv.setPenWidth(1);
cv.drawLine(x0, y0, x1, y1);
cv.setPenColor(vga_txt_pen);
}
void rect(int x0, int y0, int x1, int y1) {
vgascale(&x0, &y0);
vgascale(&x1, &y1);
cv.setPenColor(vga_graph_pen);
cv.setPenWidth(1);
cv.drawRectangle(x0, y0, x1, y1);
cv.setPenColor(vga_txt_pen);
}
void frect(int x0, int y0, int x1, int y1) {
vgascale(&x0, &y0);
vgascale(&x1, &y1);
cv.setBrushColor(vga_graph_pen);
cv.fillRectangle(x0, y0, x1, y1);
cv.setBrushColor(vga_txt_background);
}
void circle(int x0, int y0, int r) {
int rx = r;
int ry = r;
vgascale(&x0, &y0);
vgascale(&rx, &ry);
cv.setPenColor(vga_graph_pen);
cv.setPenWidth(1);
cv.drawEllipse(x0, y0, rx, ry);
cv.setPenColor(vga_txt_pen);
}
void fcircle(int x0, int y0, int r) {
int rx = r;
int ry = r;
vgascale(&x0, &y0);
vgascale(&rx, &ry);
cv.setBrushColor(vga_graph_pen);
cv.fillEllipse(x0, y0, rx, ry);
cv.setBrushColor(vga_txt_background);
}
void vgawrite(char c){
switch(c) {
case 12: /* form feed is clear screen */
Terminal.write(27); Terminal.write('H');
Terminal.write(27); Terminal.write('J');
return;
case 10: /* this is LF Unix style doing also a CR */
Terminal.write(10); Terminal.write(13);
return;
}
Terminal.write(c);
}
#else
void vgabegin(){}
int vgastat(uint8_t c) {return 0; }
void vgawrite(char c){}
void vgaend() {}
#endif
/*
* Keyboard code for either the Fablib Terminal class or
* PS2Keyboard - please note that you need the ESP patched
* version here as mentioned above
*
* sets HASKEYBOARD to inform basic about this capability
*
* keyboards can implement
* kbdbegin()
* they need to provide
* kbdavailable(), kbdread(), kbdcheckch()
* the later is for interrupting running BASIC code
*/
#if defined(ARDUINO_TTGO_T7_V14_Mini32) && defined(ARDUINOVGA)
fabgl::PS2Controller PS2Controller;
char fabgllastchar = 0;
#else
#if defined(ARDUINO) && defined(ARDUINOPS2)
PS2Keyboard keyboard;
#else
#if defined(ARDUINO) && defined(ARDUINOUSBKBD)
USBHost usb;
KeyboardController keyboard(usb);
char usbkey=0;
#else
#if defined(ARDUINOZX81KBD)
ZX81Keyboard keyboard;
#else
#if defined(ARDUINOI2CKBD)
/* with an I2C keyboard we remeber the last key, this is similar to the USB keyboard */
char i2ckey=0;
#else
#if defined(GIGAUSBKBD)
Keyboard keyb;
#endif
#endif
#endif
#endif
#endif
#endif
/*
* Experimental, unfinished, rudimentary
*/
#if defined(ARDUINOUSBKBD)
/* not really needed, only here for reference */
char usbkeymapUS[] =
{' ', '"', '!', '#', '$', '%', '&', '\'', '(', ')', '*', '+',
',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C',
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[',
'\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', 0, 0};
/* map the ASCII codes of the essential keys for BASIC of a
* German keyboard, most notable is < and > which is ö/a
*/
char usbkeymapGerman[] =
{' ', '!', '!', '#', '$', '%', '/', '>', ')', '=', '(', '+',
',', '-', '.', '-', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', ':', '<', ';', '=', ':', '_', '"', 'A', 'B', 'C',
'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Z', 'Y', '[',
'#', '+', '&', '?', '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'z', 'y', '{', '\'', '*', 0, 0};
/*
* he weak functions from the keyboard controller class implemented
*/
void keyPressed() {}
void keyReleased() {
switch (keyboard.getOemKey()) {
case 40:
usbkey=10;
break;
case 42:
case 76:
usbkey=127;
break;
case 41:
usbkey=27;
break;
default:
usbkey=keyboard.getKey();
#if ARDUINOKBDLANG_GERMAN
if (usbkey>31 && usbkey<128) usbkey=usbkeymapGerman[usbkey-32];
#else
if (usbkey>31 && usbkey<128) usbkey=usbkeymapUS[usbkey-32];
#endif
}
}
#endif
/*
* keyboard controller code
*/
#ifdef ZX81KEYBOARD
const byte zx81pins[] = {ZX81PINS};
#endif
/*
* A helper function for the I2C keyboard code. We get one character
* from the board. A -1 is an I2C error condition for a non reply of
* the keyboard.
*/
#ifdef ARDUINOI2CKBD
char i2ckbdgetkey() {
char c=0;
Wire.requestFrom(I2CKBDADDR, 1);
if (Wire.available()) {
c=Wire.read();
if (c == -1) { ioer=1; c=0; }
}
return c;
}
#endif
/* start the keyboard */
void kbdbegin() {
#ifdef PS2KEYBOARD
#ifdef ARDUINOKBDLANG_GERMAN
keyboard.begin(PS2DATAPIN, PS2IRQPIN, PS2Keymap_German);
#else
keyboard.begin(PS2DATAPIN, PS2IRQPIN, PS2Keymap_US);
#endif
#else
#ifdef PS2FABLIB
PS2Controller.begin(PS2Preset::KeyboardPort0);
#ifdef ARDUINOKBDLANG_GERMAN
PS2Controller.keyboard()->setLayout(&fabgl::GermanLayout);
#else
PS2Controller.keyboard()->setLayout(&fabgl::USLayout);
#endif
#else
#ifdef USBKEYBOARD
/* nothing to be done here */
#else
#ifdef ZX81KEYBOARD
keyboard.begin(zx81pins);
#else
#ifdef ARDUINOI2CKBD
/* this is from the T-Deck examples, time needed for the keyboard to start */
delay(500);
#else
#ifdef GIGAUSBKBD
pinMode(PA_15, OUTPUT); /* no digitalWrite in original example file */
keyb.begin();
#endif
#endif
#endif
#endif
#endif
#endif
}
uint8_t kbdstat(uint8_t c) {return 0; }
uint16_t kbdins(char* b, uint16_t nb) { return consins(b, nb); }
uint8_t kbdavailable(){
#ifdef PS2KEYBOARD
return keyboard.available();
#else
#ifdef PS2FABLIB
if (fabgllastchar) return Terminal.available()+1; else return Terminal.available();
#else
#ifdef USBKEYBOARD
/* if we already have a key, tell the caller we have one */
if (usbkey) return 1;
/* if not, look it up */
if (usbkey) return 1; else return 0;
#else
#ifdef ZX81KEYBOARD
return keyboard.available();
#else
#ifdef ARDUINOI2CKBD
/* do we have a key stored, then return it */
if (i2ckey) {
return 1;
} else {
i2ckey=i2ckbdgetkey();
if (i2ckey != 0) return 1;
}
#else
#if defined(GIGAUSBKBD)
return keyb.available();
#endif
#endif
#endif
#endif
#endif
#endif
#ifdef HASKEYPAD
/* a poor man's debouncer, unstable state returns 0 */
char c=keypadread();
if (c != 0) {
bdelay(2);
if (c == keypadread()) return 1;
}
#endif
return 0;
}
char kbdread() {
char c = 0;
/* wait for the keystrike in a tight loop, yielding a lot */
while(!kbdavailable()) byield();
#ifdef PS2KEYBOARD
c=keyboard.read();
#endif
#ifdef PS2FABLIB
if (fabgllastchar) { c=fabgllastchar; fabgllastchar=0; }
else c=Terminal.read();
#else
#ifdef USBKEYBOARD
/* if we have read a key before, return it else look it up */
c=usbkey;
usbkey=0;
#else
#ifdef ZX81KEYBOARD
c=keyboard.read();
#else
#ifdef ARDUINOI2CKBD
/* a key is stored from a previous kbdavailable() */
if (i2ckey) {
c=i2ckey;
i2ckey=0;
} else {
/* if not look for a key, no need to store it */
c=i2ckbdgetkey();
}
#else
#if defined(GIGAUSBKBD)
/* sometimes 0s are returned even with avail > 0 */
auto _key = keyb.read();
c=keyb.getAscii(_key);
#endif
#endif
#endif
#endif
#endif
#ifdef HASKEYPAD
if (c == 0) { /* we have no character from a real keyboard, ask the keypad */
c=keypadread();
/* if the keypad is in non repeat mode, block */
if (!kbdrepeat) while(kbdavailable()) byield();
}
#endif
if (c == 13) c=10;
return c;
}
char kbdcheckch() {
#ifdef PS2KEYBOARD
/*
* only works with the patched library https://github.com/slviajero/PS2Keyboard
*/
#ifdef PS2KEYBOARD_HASPEEK
return keyboard.peek();
#else
/*
* for the original library https://github.com/PaulStoffregen/PS2Keyboard
* GET does not work properly with it as there is no peek functionality which is needed
* for non blocking IO and the ability to stop programs
*/
if (kbdavailable()) return kbdread(); else return 0;
#endif
#else
#ifdef PS2FABLIB
if (fabgllastchar) return fabgllastchar;
if (kbdavailable()) { fabgllastchar=Terminal.read(); return fabgllastchar; }
#else
#ifdef USBKEYBOARD
return usbkey;
#else
#ifdef ZX81KEYBOARD
return keyboard.lastKey; /* dont peek here as checkch called in a fast loop in statement(), peek done in byield*/
#else
#ifdef ARDUINOI2CKBD
if (!i2ckey) {
i2ckey=i2ckbdgetkey();
}
return i2ckey;
#else
#if defined(GIGAUSBKBD)
auto _key = keyb.peek();
return keyb.getAscii(_key);
#endif
#endif
#endif
#endif
#endif
#endif
#ifdef HASKEYPAD
return keypadread();
#endif
return 0;
}
/*
* Display driver code - documentation is in the header file
*/
#ifdef DISPLAYDRIVER
/* the cursor position */
uint8_t dspmycol = 0;
uint8_t dspmyrow = 0;
/* the escape state of the vt52 terminal */
uint8_t dspesc = 0;
/* which update mode do we have */
uint8_t dspupdatemode = 0;
/* how do we handle wrap 0 is wrap, 1 is no wrap */
uint8_t dspwrap = 0;
/* the print mode */
uint8_t dspprintmode = 0;
/* the scroll control variables */
uint8_t dspscrollmode = 0;
uint8_t dsp_scroll_rows = 1;
uint8_t dspstat(uint8_t c) { return 1; }
void dspsetcursorx(uint8_t c) {
if (c>=0 && c<dsp_columns) dspmycol=c;
dspprintchar(0, dspmycol, dspmyrow); /* needed to really position the cursor if there is a hardware cursor */
}
void dspsetcursory(uint8_t r) {
if (r>=0 && r<dsp_rows) dspmyrow=r;
dspprintchar(0, dspmycol, dspmyrow); /* needed to really position the cursor if there is a hardware cursor */
}
uint8_t dspgetcursorx() { return dspmycol; }
uint8_t dspgetcursory() { return dspmyrow; }
uint8_t dspactive() {
return od == ODSP;
}
/* to whom the bell tolls - implement this to you own liking */
void dspbell() {}
/*
* Control the update modes for page update displays
* like Epaper, Nokia 5110 and SSD1306
* dspgraphupdate is the helper for the graphics functions.
*/
void dspsetupdatemode(uint8_t c) {
dspupdatemode=c;
}
uint8_t dspgetupdatemode() {
return dspupdatemode;
}
void dspgraphupdate() {
if (dspupdatemode == 0) dspupdate();
}
/* scrollable displays need a character buffer */
#ifdef DISPLAYCANSCROLL
#ifndef DISPLAYHASCOLOR
const uint16_t dspfgvgacolor = 0;
#endif
dspbuffer_t dspbuffer[dsp_rows][dsp_columns];
/* buffer access functions */
/* needed for @D() */
dspbuffer_t dspget(uint16_t i) {
if (i>=0 && i<=dsp_columns*dsp_rows-1) return dspbuffer[i/dsp_columns][i%dsp_columns]; else return 0;
}
/* print line and screen helpers */
dspbuffer_t dspgetrc(uint8_t r, uint8_t c) { return dspbuffer[r][c]; }
dspbuffer_t dspgetc(uint8_t c) { return dspbuffer[dspmyrow][c]; }
/* this functions prints a character and updates the display buffer */
void dspsetxy(dspbuffer_t ch, uint8_t c, uint8_t r) {
if (r>=0 && c>=0 && r<dsp_rows && c<dsp_columns) {
dspbuffer[r][c]=(dspbuffer_t)ch | (dspfgvgacolor << 8);
if (ch != 0) dspprintchar(ch, c, r); else dspprintchar(' ', c, r);
}
}
/* needed for @D() access */
void dspset(uint16_t i, dspbuffer_t ch) {
uint8_t c=i%dsp_columns;
uint8_t r=i/dsp_columns;
dspsetxy(ch, c, r);
}
/* 0 normal scroll, 1 enable waitonscroll function */
void dspsetscrollmode(uint8_t c, uint8_t l) {
dspscrollmode = c;
dsp_scroll_rows = l;
}
/* clear the buffer */
void dspbufferclear() {
uint8_t r,c;
for (r=0; r<dsp_rows; r++)
for (c=0; c<dsp_columns; c++)
dspbuffer[r][c]=0;
dspmyrow=0;
dspmycol=0;
}
#ifdef DISPLAYHASCOLOR
/* separates characters and color/font */
const uint16_t charmask = 0x80FF;
#endif
/* do the scroll */
void dspscroll(uint8_t scroll_rows, uint8_t scroll_top=0){
uint8_t r,c;
dspbuffer_t a,b;
/* scroll data up and redraw the display */
for (r=scroll_top; r<dsp_rows-scroll_rows; r++) {
for (c=0; c<dsp_columns; c++) {
a=dspbuffer[r][c];
b=dspbuffer[r+scroll_rows][c];
if ( a != b ) {
if (b != 0) {
#ifdef DISPLAYHASCOLOR
dspsavepen();
dspsetfgcolor((b >> 8) & 15);
dspprintchar(b & charmask, c, r);
dsprestorepen();
#else
dspprintchar(b, c, r);
#endif
} else {
dspprintchar(' ', c, r);
}
}
dspbuffer[r][c]=b;
}
}
/* delete the characters in the remaining lines */
for (r=dsp_rows-scroll_rows; r<dsp_rows; r++) {
for (c=0; c<dsp_columns; c++) {
if (dspbuffer[r][c] != 0 && ( dspbuffer[r][c]) != 32) dspprintchar(' ', c, r);
dspbuffer[r][c]=0;
}
}
/* set the cursor to the first free line */
dspmycol=0;
dspmyrow=dsp_rows-scroll_rows;
}
/* do the reverse scroll only one line implemented */
void dspreversescroll(uint8_t line){
uint8_t r,c;
dspbuffer_t a,b;
/* scroll data up and redraw the display */
for (r=dsp_rows; r>line; r--) {
for (c=0; c<dsp_columns; c++) {
a=dspbuffer[r][c];
b=dspbuffer[r-1][c];
if ( a != b ) {
if (b != 0) {
#ifdef DISPLAYHASCOLOR
dspsavepen();
dspsetfgcolor((b >> 8) & 15);
dspprintchar(b & charmask, c, r);
dsprestorepen();
#else
dspprintchar(b, c, r);
#endif
} else {
dspprintchar(' ', c, r);
}
}
dspbuffer[r][c]=b;
}
}
/* delete the characters in the remaining line */
for (c=0; c<dsp_columns; c++) {
if (dspbuffer[line][c] != 0 && dspbuffer[line][c] != 32) dspprintchar(' ', c, r);
dspbuffer[line][c]=0;
}
/* set the cursor to the free line */
dspmyrow=line;
}
/* again a break in API, using inch here */
char dspwaitonscroll() {
char c;
if ( dspscrollmode == 1 ) {
if (dspmyrow == dsp_rows-1) {
c=inch();
if (c == ' ') dspwrite(12);
return c;
}
}
return 0;
}
#else
/* code for low memory simple display access */
/* buffer access functions - stubs for displays without buffer */
dspbuffer_t dspget(uint16_t i) { return 0; }
dspbuffer_t dspgetrc(uint8_t r, uint8_t c) { return 0; }
dspbuffer_t dspgetc(uint8_t c) { return 0; }
void dspsetxy(dspbuffer_t ch, uint8_t c, uint8_t r) {
if (ch != 0) dspprintchar(ch, c, r); else dspprintchar(' ', c, r);
}
void dspset(uint16_t i, dspbuffer_t ch) {
uint8_t c=i%dsp_columns;
uint8_t r=i/dsp_columns;
dspsetxy(ch, c, r);
}
/* unbuffered display, the only thing we can do is go home */
void dspbufferclear() {
dspmyrow=0;
dspmycol=0;
}
/* no scroll no wait */
char dspwaitonscroll() { return 0; }
/* a stub for dspwrite */
void dspscroll(uint8_t scroll_rows, uint8_t scroll_top=0){
dspmyrow=dsp_rows-1;
}
/* no scroll without buffer */
void dspsetscrollmode(uint8_t c, uint8_t l) {}
void dspreversescroll(uint8_t a){}
#endif
/* the generic write function for displays with and without buffers */
void dspwrite(char c){
int8_t dspmycolt;
/* on escape call the vt52 state engine, it modifies the character */
#ifdef HASVT52
if (dspesc) {
dspvt52(&c);
}
#endif
/* do we print? */
#ifdef ARDUINOPRT
if (dspprintmode) {
prtwrite(c);
if (sendcr && c == 10) prtwrite(13); /* some printers want cr */
if (dspprintmode == 2) return; /* do not print in mode 2 */
}
#endif
/* the buildin control characters for all displays */
switch(c) {
case 0:
return;
case 7: // the function dspbell() is not implemented.
dspbell();
return;
case 9: // tab moves on 8 positions
dspmycolt = dspmycol/8;
if ((dspmycolt+1)*8<dsp_columns-1) dspmycol=(dspmycolt+1)*8;
return;
case 10: // this is LF Unix style doing also a CR
dspmyrow++;
if (dspmyrow >= dsp_rows) dspscroll(dsp_scroll_rows);
dspmycol=0;
if (dspupdatemode == 1) dspupdate();
return;
case 11: // vertical tab - converted to line feed without carriage return
if (dspmyrow < dsp_rows-1) dspmyrow++;
return;
case 12: // form feed is clear screen plus home
dspbufferclear();
dspclear();
return;
case 13: // classical carriage return, no form feed
dspmycol=0;
return;
case 27: // escape - initiate vtxxx mode
dspesc=1;
return;
case 28: // cursor back - this is what terminal applications send for cursor back
if (dspmycol > 0) dspmycol--;
return;
case 29: // cursor forward - this is what terminal applications send for cursor back
if (dspmycol < dsp_columns-1) dspmycol++;
return;
case 8: // back space is delete the moment
case 127: // delete
if (dspmycol > 0) {
dspmycol--;
dspsetxy(0, dspmycol, dspmyrow);
}
return;
case 2: // we abuse start of text as a home sequence, may also be needed for Epaper later
dspmycol=dspmyrow=0;
return;
case 3: // ETX = Update display for buffered display like Epaper
dspupdate();
return;
default: // eliminate all non printables - problematic for LCD special character
if (c<32) return;
break;
}
dspsetxy(c, dspmycol, dspmyrow);
dspmycol++;
if (dspmycol == dsp_columns) {
if (!dspwrap) { /* we simply ignore the cursor */
dspmycol=0;
dspmyrow=(dspmyrow + 1);
}
if (dspmyrow >= dsp_rows) dspscroll(dsp_scroll_rows);
}
if (dspupdatemode == 0) dspupdate();
}
/*
* This is the minimalistic VT52 state engine. It is an interface to
* process single byte control sequences of the form <ESC> char
*/
#ifdef HASVT52
/* the state variable */
char vt52s = 0;
/* the graphics mode mode - unused so far */
uint8_t vt52graph = 0;
/* the secondary cursor */
uint8_t vt52mycol = 0;
uint8_t vt52myrow = 0;
/* temp variables for column and row , do them here and not in the case: guess why */
uint8_t vt52tmpr;
uint8_t vt52tmpc;
/* an output buffer for the vt52 terminal */
const uint8_t vt52buffersize = 4;
char vt52outbuffer[vt52buffersize] = { 0, 0, 0, 0 };
uint8_t vt52bi = 0;
uint8_t vt52bj = 0;
/* the reader from the buffer */
char vt52read() {
if (vt52bi<=vt52bj) { vt52bi = 0; vt52bj = 0; } /* empty, reset */
if (vt52bi>vt52bj) return vt52outbuffer[vt52bj++];
return 0;
}
/* the avail from the buffer */
uint8_t vt52avail() { if (vt52bi > vt52bj) return vt52bi-vt52bj; else return 0; }
/* putting something into the buffer */
void vt52push(char c) {
if (vt52bi < vt52buffersize) vt52outbuffer[vt52bi++]=c;
}
/* clear the buffer */
void vt52clear() {
vt52bi=0;
}
/* something little */
uint8_t vt52number(char c) {
uint8_t b=c;
if (b>31) return b-32; else return 0;
}
/*
* The VT52 data registers for the graphics and wiring extension.
* x,y are 14 bit and z is 7bit. Data is transferred in VT52 style
* -> numerical value plus 32 to map the data to printable characters
* Access is done through the the ESC x, ESC y and ESC z sequences:
* ESC x #1 #2
* sets the xregister to (#1-32)+(#2-32)*128
*/
#if defined(DISPLAYHASGRAPH) || defined(VT52WIRING)
#define VT52HASREGISTERS
/* the three register variables */
uint16_t vt52regx = 0;
uint16_t vt52regy = 0;
uint8_t vt52regz = 0;
/* one argument cache for two byte arguments */
uint8_t vt52arg = 0;
#endif
/*
* graphics code in VT52, if you want to control graphics from the character stream
* The ESC g <ch> sequence sends a graphics command as the second byte after the g
*
* Valid values for g are
* s: set the graphics cursor
* p: plot a point
* l: draw a line
* L: draw a line and move the cursor to the endpoint
* r: draw a rectangle
* R: draw a filled rectangle
* c: draw a circle
* C: draw a filled circle
*
*/
#ifdef DISPLAYHASGRAPH
/* the grahics cursor of VT52 */
uint16_t vt52graphx = 0;
uint16_t vt52graphy = 0;
/* execute one graphics command */
void vt52graphcommand(uint8_t c) {
switch(c) {
case 's': /* set the cursor */
vt52graphx=vt52regx;
vt52graphy=vt52regy;
break;
case 'p': /* plot a point at the cursor */
plot(vt52graphx, vt52graphy);
break;
case 'l': /* plot a line */
line(vt52graphx, vt52graphy, vt52regx, vt52regy);
break;
case 'L': /* plot a line and update the cursor, needed for drawing shapes */
line(vt52graphx, vt52graphy, vt52regx, vt52regy);
vt52graphx=vt52regx;
vt52graphy=vt52regy;
break;
case 'r': /* plot a rectangle */
rect(vt52graphx, vt52graphy, vt52regx, vt52regy);
break;
case 'c': /* plot a circle */
circle(vt52graphx, vt52graphy, vt52regx);
break;
case 'R': /* plot a filled rectangle */
frect(vt52graphx, vt52graphy, vt52regx, vt52regy);
break;
case 'C': /* plot a filled circle */
fcircle(vt52graphx, vt52graphy, vt52regx);
break;
}
}
#endif
/*
* This is an odd part of the vt52 code with this, the terminal
* can control the digital and analog pins.
*/
#ifdef VT52WIRING
#define VT52HASREGISTERS
void vt52wiringcommand(uint8_t c) {
switch(c) {
case 'p': /* pinMode z */
pinMode(vt52regz, vt52regx);
break;
case 'l': /* digital write low pin z*/
digitalWrite(vt52regz, LOW);
break;
case 'h': /* digital write high pin z*/
digitalWrite(vt52regz, HIGH);
break;
case 'r': /* digital read from pin z */
vt52push(digitalRead(vt52regz)+32);
break;
case 'a': /* analog write pin z to value x */
analogWrite(vt52regz, vt52regx);
break;
case 'A': /* analog read from pin z */
break;
case 't': /* tone at pin z, frequency x, duration y */
tone(vt52regz, vt52regx, vt52regy);
break;
}
}
#endif
/* the actual vt52 state engine */
void dspvt52(char* c){
/* reading and processing multi byte commands */
switch (vt52s) {
case 'Y':
if (dspesc == 2) {
dspsetcursory(vt52number(*c));
dspesc=1;
*c=0;
return;
}
if (dspesc == 1) {
dspsetcursorx(vt52number(*c));
*c=0;
}
vt52s=0;
break;
case 'b':
dspsetfgcolor(vt52number(*c));
*c=0;
vt52s=0;
break;
case 'c':
dspsetbgcolor(vt52number(*c));
*c=0;
vt52s=0;
break;
#ifdef VT52HASREGISTERS
case 'x':
if (dspesc == 2) {
vt52arg=vt52number(*c);
dspesc=1;
*c=0;
return;
}
if (dspesc == 1) {
vt52regx=vt52arg+vt52number(*c)*128;
*c=0;
}
vt52s=0;
break;
case 'y':
if (dspesc == 2) {
vt52arg=vt52number(*c);
dspesc=1;
*c=0;
return;
}
if (dspesc == 1) {
vt52regy=vt52arg+vt52number(*c)*127;
*c=0;
}
vt52s=0;
break;
case 'z':
vt52regz=vt52number(*c);
*c=0;
vt52s=0;
break;
#endif
#ifdef DISPLAYHASGRAPH
case 'g':
vt52graphcommand(*c);
*c=0;
vt52s=0;
break;
#endif
#ifdef VT52WIRING
case 'a':
vt52wiringcommand(*c);
*c=0;
vt52s=0;
break;
#endif
}
/* commands of the terminal in text mode */
switch (*c) {
case 'v': /* GEMDOS / TOS extension enable wrap */
dspwrap=0;
break;
case 'w': /* GEMDOS / TOS extension disable wrap */
dspwrap=1;
break;
case '^': /* Printer extensions - print on */
dspprintmode=1;
break;
case '_': /* Printer extensions - print off */
dspprintmode=0;
break;
case 'W': /* Printer extensions - print without display on */
dspprintmode=2;
break;
case 'X': /* Printer extensions - print without display off */
dspprintmode=0;
break;
case 'V': /* Printer extensions - print cursor line */
#if defined(ARDUINOPRT) && defined(DISPLAYCANSCROLL)
for (uint8_t i=0; i<dsp_columns; i++) prtwrite(dspgetc(i));
#endif
break;
case ']': /* Printer extension - print screen */
#if defined(ARDUINOPRT) && defined(DISPLAYCANSCROLL)
for (uint8_t j=0; j<dsp_rows; j++)
for (uint8_t i=0; i<dsp_columns; i++) prtwrite(dspgetrc(j, i));
#endif
break;
case 'F': /* enter graphics mode */
vt52graph=1;
break;
case 'G': /* exit graphics mode */
vt52graph=0;
break;
case 'Z': // Ident
vt52clear();
vt52push(27);
vt52push('/');
#ifndef ARDUINOPRT
vt52push('K');
#else
vt52push('L');
#endif
break;
case '=': // alternate keypad on
case '>': // alternate keypad off
break;
case 'b': // GEMDOS / TOS extension text color
case 'c': // GEMDOS / TOS extension background color
vt52s=*c;
dspesc=1;
*c=0;
return;
case 'e': // GEMDOS / TOS extension enable cursor
dspsetcursor(1);
break;
case 'f': // GEMDOS / TOS extension disable cursor
dspsetcursor(0);
break;
case 'p': // GEMDOS / TOS extension reverse video
dspsetreverse(1);
break;
case 'q': // GEMDOS / TOS extension normal video
dspsetreverse(0);
break;
case 'A': // cursor up
if (dspmyrow>0) dspmyrow--;
break;
case 'B': // cursor down
if (dspmyrow < dsp_rows-1) dspmyrow++;
break;
case 'C': // cursor right
if (dspmycol < dsp_columns-1) dspmycol++;
break;
case 'D': // cursor left
if (dspmycol>0) dspmycol--;
break;
case 'E': // GEMDOS / TOS extension clear screen
dspbufferclear();
dspclear();
break;
case 'H': // cursor home
dspmyrow=dspmycol=0;
break;
case 'Y': // Set cursor position
vt52s='Y';
dspesc=2;
*c=0;
return;
case 'J': // clear to end of screen
for (int i=dspmycol+dsp_columns*dspmyrow; i<dsp_columns*dsp_rows; i++) dspset(i, 0);
break;
case 'd': // GEMDOS / TOS extension clear to start of screen
for (int i=0; i<dspmycol+dsp_columns*dspmyrow; i++) dspset(i, 0);
break;
case 'K': // clear to the end of line
for (uint8_t i=dspmycol; i<dsp_columns; i++) dspsetxy(0, i, dspmyrow);
break;
case 'l': // GEMDOS / TOS extension clear line
for (uint8_t i=0; i<dsp_columns; i++) dspsetxy(0, i, dspmyrow);
break;
case 'o': // GEMDOS / TOS extension clear to start of line
for (uint8_t i=0; i<=dspmycol; i++) dspsetxy(0, i, dspmyrow);
break;
case 'k': // GEMDOS / TOS extension restore cursor
dspmycol=vt52mycol;
dspmyrow=vt52myrow;
break;
case 'j': // GEMDOS / TOS extension save cursor
vt52mycol=dspmycol;
vt52myrow=dspmyrow;
break;
case 'I': // reverse line feed
if (dspmyrow>0) dspmyrow--; else dspreversescroll(0);
break;
case 'L': // Insert line
dspreversescroll(dspmyrow);
break;
case 'M':
vt52tmpr = dspmyrow;
vt52tmpc = dspmycol;
dspscroll(1, dspmyrow);
dspmyrow=vt52tmpr;
dspmycol=vt52tmpc;
break;
#ifdef VT52HASREGISTERS
case 'x': // set the x register
case 'y': // set the y register
vt52s=*c;
dspesc=2;
*c=0;
return;
case 'z': // set the z register
vt52s=*c;
dspesc=1;
*c=0;
return;
break;
#endif
#ifdef DISPLAYHASGRAPH
case 'g': // execute a graphics command
vt52s=*c;
dspesc=1;
*c=0;
return;
break;
#endif
#ifdef VT52WIRING
case 'a': // execute a wiring command
vt52s=*c;
dspesc=1;
*c=0;
return;
break;
#endif
}
dspesc=0;
*c=0;
}
#endif
#else
#ifdef GRAPHDISPLAYDRIVER
/* a super simple display driver for graphics only systems, only handles drawing
and graphics cursor stuff, experimental for epapers */
#undef HASVT52
#define GBUFFERSIZE 80
static char gbuffer[GBUFFERSIZE];
void dspouts(char* s, uint16_t l) {
int i;
for (i=0; i<l-1 && i<GBUFFERSIZE-1; i++) gbuffer[i]=s[i];
gbuffer[i]=0;
dspprintstring(gbuffer);
}
/* single character handling is triggered by PUT, it controls cursor movement */
void dspwrite(char c) {
switch(c) {
case 0:
return;
case 10: // this is LF Unix style doing also a CR
dspgraphcursor_x=0;
case 11: // vertical tab - converted to line feed without carriage return
dspgraphcursor_y+=dspfontsize;
return;
case 12: // form feed is clear screen plus home
dspclear();
case 2: // we abuse start of text as a home sequence, may also be needed for Epaper later
dspgraphcursor_x=0;
dspgraphcursor_y=dspfontsize;
return;
case 13: // classical carriage return, no form feed, we stay in the same line
dspgraphcursor_x=0;
return;
default: // eliminate all non printables
if (c<32) return;
break;
}
dspouts(&c, 1);
}
int dspstat(char c) { return 1; }
int dspgetcursorx() { return dspgraphcursor_x; }
int dspgetcursory() { return dspgraphcursor_y; }
void dspsetcursorx(int v) { dspgraphcursor_x = v; }
void dspsetcursory(int v) { dspgraphcursor_y = v; }
char dspwaitonscroll() { return 0; };
uint8_t dspactive() {return 1; }
void dspsetupdatemode(uint8_t c) {}
#else
/* stubs for systems without a display driver, BASIC uses these functions */
const int dsp_rows=0;
const int dsp_columns=0;
void dspsetupdatemode(uint8_t c) {}
void dspwrite(char c){};
void dspbegin() {};
uint8_t dspstat(uint8_t c) {return 0; }
char dspwaitonscroll() { return 0; };
uint8_t dspactive() {return 0; }
void dspsetscrollmode(uint8_t c, uint8_t l) {}
void dspscroll(uint8_t a, uint8_t b){}
char vt52read() { return 0; }
uint8_t vt52avail() { return 0; }
#endif
#endif
/*
* Real time clock access for the system. See runtime.h for the
* configuration of the clock access.
*/
#if defined(ARDUINORTC)
/*
* I2C clocks DS1307, DS3231, and DS3232 using Wire directly
* no buffering of values, access directly to the clock registers
*/
/* No begin method needed */
void rtcbegin() {}
/* get the time from the registers */
uint16_t rtcget(uint8_t i) {
/* set the address of the read */
wirestart(RTCI2CADDR, 0);
wirewritebyte(i);
wirestop();
/* now read */
wirestart(RTCI2CADDR, 1);
uint8_t v=wirereadbyte();
switch (i) {
case 0:
case 1:
return (v & 0b00001111)+((v >> 4) & 0b00000111) * 10;
case 2:
return (v & 0b00001111)+((v >> 4) & 0b00000011) * 10 ; /* only 24 hour support */
case 3:
return (v & 0b00001111);
case 4:
return (v & 0b00001111) + ((v >> 4) & 0b00000011) * 10;
case 5:
return (v & 0b00001111) + ((v >> 4) & 0b00000001) * 10;
case 6:
return (v & 0b00001111) + (v >> 4) * 10;
default:
return v;
}
}
/* set the registers */
void rtcset(uint8_t i, uint16_t v) {
uint8_t b;
/* to bcd if we deal with a clock byte (0-6) */
if (i<7) b=(v%10 + ((v/10) << 4)); else b=v;
switch (i) {
case 0:
case 1:
b = b & 0b01111111;
break;
case 2:
case 4:
b = b & 0b00111111; /* only 24 hour support */
break;
case 3:
b = b & 0b00000111;
break;
case 5:
b = b & 0b00011111;
}
/* send the address and then the byte */
wirestart(RTCI2CADDR, 0);
wirewritebyte(i);
wirewritebyte(b);
wirestop();
}
#elif defined(HASBUILTINRTC)
/*
* Built-in clocks of STM32 and MKR are based on the RTCZero interface
* an rtc object is created after loading the libraries
* for NRENESAs the interface is slightly different
*/
#ifndef ARDUINO_ARCH_RENESAS
/* begin method */
void rtcbegin() {
rtc.begin(); /* 24 hours mode */
}
/* get the time */
uint16_t rtcget(uint8_t i) {
switch (i) {
case 0:
return rtc.getSeconds();
case 1:
return rtc.getMinutes();
case 2:
return rtc.getHours();
case 3:
// return rtc.getWeekDay();
return 0;
case 4:
return rtc.getDay();
case 5:
return rtc.getMonth();
case 6:
return rtc.getYear();
default:
return 0;
}
}
/* set the time */
void rtcset(uint8_t i, uint16_t v) {
switch (i) {
case 0:
rtc.setSeconds(v);
break;
case 1:
rtc.setMinutes(v);
break;
case 2:
rtc.setHours(v);
break;
case 3:
// rtc.setWeekDay(v);
break;
case 4:
rtc.setDay(v);
break;
case 5:
rtc.setMonth(v);
break;
case 6:
rtc.setYear(v);
break;
default:
return;
}
}
#else
/* NRENESA code, interface different to my great ennui! */
/* begin method */
void rtcbegin() {
RTC.begin(); /* 24 hours mode */
}
/* get the time */
uint16_t rtcget(uint8_t i) {
RTC.getTime(rtc);
switch (i) {
case 0:
return rtc.getSeconds();
case 1:
return rtc.getMinutes();
case 2:
return rtc.getHour();
case 3:
return static_cast<int>(rtc.getDayOfWeek());
case 4:
return rtc.getDayOfMonth();
case 5:
return Month2int(rtc.getMonth());
case 6:
return rtc.getYear();
default:
return 0;
}
}
/* set the time */
void rtcset(uint8_t i, uint16_t v) {
RTC.getTime(rtc);
switch (i) {
case 0:
rtc.setSecond(v);
break;
case 1:
rtc.setMinute(v);
break;
case 2:
rtc.setHour(v);
break;
case 3:
rtc.setDayOfWeek(static_cast<DayOfWeek>(v));
break;
case 4:
rtc.setDayOfMonth(v);
break;
case 5:
rtc.setMonthOfYear(static_cast<Month>(v-1));
break;
case 6:
rtc.setYear(v);
break;
default:
return;
}
RTC.setTime(rtc);
}
#endif
#elif defined(ARDUINORTCEMULATION)
/*
* A millis() based real time clock emulation. It creates a 32bit Unix time
* variable and adds millis()/1000
*/
/* the begin method - standard interface to BASIC*/
void rtcbegin() {}
/* the current unix time - initialized to the begin of the epoch */
long rtcutime = 0;
/* the offset of millis()/1000 when we last set the clock */
long rtcutimeoffset = 0;
/* the current unix date - initialized to the begin of the epoch */
struct { uint8_t second; uint8_t minute; uint8_t hour; uint8_t weekday; uint8_t day; uint8_t month; uint16_t year; }
rtctime = { 0, 0, 0, 4, 1, 1, 1970 };
/* the number of days per month */
const int rtcmonthdays[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
/* convert the time to a unix time number from https://de.wikipedia.org/wiki/Unixzeit */
void rtctimetoutime() {
int leapyear = ((rtctime.year-1)-1968)/4 - ((rtctime.year-1)-1900)/100 + ((rtctime.year-1)-1600)/400;
long epochdays = (rtctime.year-1970)*365 + leapyear + rtcmonthdays[rtctime.month-1] + rtctime.day - 1;
if ((rtctime.month > 2) && (rtctime.year%4 == 0 && (rtctime.year%100 != 0 || rtctime.year%400 == 0))) epochdays+=1;
rtcutime = rtctime.second + 60*(rtctime.minute + 60*(rtctime.hour + 24*epochdays));
}
/* convert the unix time to time and date from https://de.wikipedia.org/wiki/Unixzeit */
void rtcutimetotime() {
const unsigned long int secondsperday = 86400ul; /* 24* 60 * 60 */
const unsigned long int daysperyear = 365ul; /* no leap year */
const unsigned long int daysinfoury = 1461ul; /* 4*365 + 1 */
const unsigned long int daysinhundredy = 36524ul; /* 100*365 + 25 - 1 */
const unsigned long int daysinfourhundredy = 146097ul; /* 400*365 + 100 - 4 + 1 */
const unsigned long int daynumberzero = 719468ul; /* day number of March, 1 1970 */
unsigned long int daynumber = daynumberzero + rtcutime/secondsperday;
unsigned long int secondssincemidnight = rtcutime%secondsperday;
unsigned long int temp;
/* the weekday is based in the daynumber since March, 1, 1970 a SUNDAY */
rtctime.weekday = daynumber % 7;
/* leap years of the Gregorian calendar */
temp = 4 * (daynumber + daysinhundredy + 1) / daysinfourhundredy - 1;
rtctime.year = 100 * temp;
daynumber -= daysinhundredy * temp + temp / 4;
/* leap years of the Julian calendar */
temp = 4 * (daynumber + daysperyear + 1) / daysinfoury - 1;
rtctime.year += temp;
daynumber -= daysperyear * temp + temp / 4;
/* calculate day and month, reference March 1 */
rtctime.month = (5 * daynumber + 2) / 153;
rtctime.day = daynumber - (rtctime.month * 153 + 2) / 5 + 1;
/* recalulate to a normal year */
rtctime.month += 3;
if (rtctime.month > 12) {
rtctime.month -= 12;
rtctime.year++;
}
/* calculate hours, months, seconds */
rtctime.hour = secondssincemidnight / 3600;
rtctime.minute = secondssincemidnight % 3600 / 60;
rtctime.second = secondssincemidnight % 60;
}
/* get the time elements -> standard interface to BASIC */
uint16_t rtcget(uint8_t i) {
/* add to the last time we set the clock and subtract the offset*/
rtcutime = millis()/1000 + rtcutimeoffset;
/* calulate the time data */
rtcutimetotime();
switch (i) {
case 0:
return rtctime.second;
case 1:
return rtctime.minute;
case 2:
return rtctime.hour;
case 3:
return rtctime.weekday;
case 4:
return rtctime.day;
case 5:
return rtctime.month;
case 6:
return rtctime.year;
default:
return 0;
}
}
/* set the time elements -> standard interface to BASIC */
void rtcset(uint8_t i, uint16_t v) {
/* how much time has elapsed since we last set the clock */
rtcutime = millis()/1000 + rtcutimeoffset;
/* generate the time structure */
rtcutimetotime();
/* set the clock */
switch (i) {
case 0:
if (v>=0 && v<60) rtctime.second=v;
break;
case 1:
if (v>=0 && v<60) rtctime.minute=v;
break;
case 2:
if (v>=0 && v<24) rtctime.hour=v;
break;
case 3:
if (v>=0 && v<7) rtctime.weekday=v;
break;
case 4:
if (v>0 && v<32) rtctime.day=v;
break;
case 5:
if (v>0 && v<13) rtctime.month=v;
break;
case 6:
if (v>=1970 && v<2100) rtctime.year=v;
break;
default:
return;
}
/* recalulate the right offset by first finding the second value of the new date*/
rtctimetoutime();
/* remember when we set the clock */
rtcutimeoffset = rtcutime - millis()/1000;
}
#elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_MBED_GIGA)
/*
* On ESP32 we use the builtin clock, this is a generic Unix time mechanism equivalent
* to the code in hardware-posix.h. The code works also for the GIGA which is surprising!
*/
/* no begin needed */
void rtcbegin() {}
/* get the time */
uint16_t rtcget(uint8_t i) {
struct tm rtctime;
time_t now;
time(&now);
localtime_r(&now, &rtctime);
switch (i) {
case 0:
return rtctime.tm_sec;
case 1:
return rtctime.tm_min;
case 2:
return rtctime.tm_hour;
case 3:
return rtctime.tm_wday;
case 4:
return rtctime.tm_mday;
case 5:
return rtctime.tm_mon+1;
case 6:
if (rtctime.tm_year > 100) return rtctime.tm_year-100; else return rtctime.tm_year;
default:
return 0;
}
return 0;
}
/* set the time */
void rtcset(uint8_t i, uint16_t v) {
struct tm rtctime;
struct timeval tv;
/* get the time stucture from the system */
time_t now;
time(&now);
localtime_r(&now, &rtctime);
/* change what needs to be changed */
switch (i) {
case 0:
rtctime.tm_sec = v%60;
break;
case 1:
rtctime.tm_min = v%60;
break;
case 2:
rtctime.tm_hour = v%24;
break;
case 3:
rtctime.tm_wday = v%7;
break;
case 4:
rtctime.tm_mday = v;
break;
case 5:
rtctime.tm_mon = v-1;
break;
case 6:
if (v > 1900) v=v-1900; /* get years to the right value */
if (v < 50) v=v+100;
rtctime.tm_year = v;
break;
}
/* calculate the seconds and put it back*/
time_t epocht = mktime(&rtctime);
if (epocht > 2082758399){
tv.tv_sec = epocht - 2082758399;
} else {
tv.tv_sec = epocht;
}
tv.tv_usec = 0;
settimeofday(&tv, NULL);
}
#else
/* no clock at all, no operations */
void rtcbegin() {}
uint16_t rtcget(uint8_t i) { return 0; }
void rtcset(uint8_t i, uint16_t v) { }
#endif
/*
* External EEPROM as a filesystem is handled through an EFS filesystem
* object, see https://github.com/slviajero/EepromFS for details.
* Here the most common parameters are set as a default.
* The EFS library is now part of the interpreter runtime.
*/
#ifdef ARDUINOEFS
#undef ARDUINOI2CEEPROM
#ifndef EFSEEPROMADDR
#define EFSEEPROMADDR 0x50
#endif
#ifdef EFSEEPROMSIZE
EepromFS EFS(EFSEEPROMADDR, EFSEEPROMSIZE);
#else
EepromFS EFS(EFSEEPROMADDR);
#endif
#endif
/*
* External EEPROM is also handled through an EFS filesystem object
* in raw mode. This handles buffering with a minium of ram.
*/
#if defined(ARDUINOI2CEEPROM) && defined(ARDUINOI2CEEPROM_BUFFERED)
#ifndef I2CEEPROMADDR
#define I2CEEPROMADDR 0x50
#endif
#ifdef I2CEEPROMSIZE
EepromFS EFSRAW(I2CEEPROMADDR, I2CEEPROMSIZE);
#else
EepromFS EFSRAW(I2CEEPROMADDR);
#endif
#endif
/*
* definitions for ESP Wifi and MQTT, super experimental.
* As networking is only used for MQTT at the moment,
* mqtt, Wifi and Ethernet comes all in one.
*
* No encryption/authetication is implemented in MQTT.
* Only public, open servers can be used.
*
* MQTT topics can only be 32 bytes long.
* Buffered incoming and outgoing messages can be 128 bytes
* per default.
*
* wifisettings.h is the generic network definition file
* all network settings are compiled into the code
* BASIC cannot change them at runtime.
*/
#ifdef ARDUINOMQTT
#include "wifisettings.h"
#ifdef ARDUINOETH
EthernetClient bethclient;
PubSubClient bmqtt(bethclient);
#else
WiFiClient bwifi;
PubSubClient bmqtt(bwifi);
#endif
/* the buffers of the outgoing and incoming MQTT topic, they are char!! */
char mqtt_otopic[MQTTLENGTH];
char mqtt_itopic[MQTTLENGTH];
/*
* the buffers for MQTT messages, input and output goes
* through these static buffers.
*/
volatile char mqtt_buffer[MQTTBLENGTH];
volatile uint16_t mqtt_messagelength;
volatile char mqtt_obuffer[MQTTBLENGTH];
volatile uint16_t mqtt_charsforsend;
/* the name of the client, generated pseudo randomly to avoind
naming conflicts */
char mqttname[MQTTNAMELENGTH] = "iotbasicxxx";
void mqttsetname() {
uint32_t m = millis();
mqttname[8]=(char)(65+m%26);
m=m/26;
mqttname[9]=(char)(65+m%26);
m=m/26;
mqttname[10]=(char)(65+m%26);
}
/*
* network begin method
* needs the settings from wifisettings.h
*
* Default is right now that Wifi is started at boot
* This may change in the future.
*
* Ethernet begin has to be reviewed to avoid DHCP
* start timeout if network is not connected
*/
void netbegin() {
#ifdef ARDUINOETH
#ifdef ETHPIN
Ethernet.init(ETHPIN);
#endif
Ethernet.begin(mac);
#else
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.setAutoReconnect(1);
#endif
#if defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_UNOR4_WIFI)
WiFi.begin(ssid, password);
#endif
#if defined(ARDUINO_ARCH_MBED_GIGA)
int counter=0;
int status=WL_IDLE_STATUS;
while (status != WL_CONNECTED && counter++ < 10) {
status=WiFi.begin(ssid, password);
bdelay(500);
}
#endif
#endif
}
/*
* network stop methode, needed sometime to reinit networking
* completely or to save power
*
*/
void netstop() {
#ifdef ARDUINOETH
/* to be done */
#else
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
WiFi.mode(WIFI_OFF);
#endif
#if defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_ARCH_MBED_GIGA)
WiFi.end();
#endif
#endif
}
/*
* network reconnect methode
*
*/
void netreconnect() {
#ifdef ARDUINOETH
/* */
#else
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
WiFi.reconnect();
bdelay(1000);
#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_ARCH_MBED_GIGA)
/* on these platforms there is no reconnect method, we simply stop and start again */
netstop();
netbegin();
#endif
#endif
}
/*
* network connected method
* on ESP Wifi try to reconnect, the delay is odd
* This is a partial reconnect, BASIC needs to handle
* repeated reconnects
*/
uint8_t netconnected() {
#ifdef ARDUINOETH
return bethclient.connected();
#else
return WiFi.status() == WL_CONNECTED;
#endif
}
/*
* mqtt callback function, using the byte type here
* because payload can be binary - interface to BASIC
* strings need to be done
*
* mqtt event handling in BASIC can be triggered here
* we leave the types Arduino style
*/
void mqttcallback(char* t, byte* p, unsigned int l) {
short i;
mqtt_messagelength=l;
for(i=0; i<l; i++) mqtt_buffer[i]=(char)p[i];
}
/*
* starting mqtt
* set the server, register the callback and purge all topics
*/
void mqttbegin() {
bmqtt.setServer(mqtt_server, mqtt_port);
bmqtt.setCallback(mqttcallback);
*mqtt_itopic=0;
*mqtt_otopic=0;
mqtt_charsforsend=0;
}
/* the interface to the usr function */
uint8_t mqttstat(uint8_t c) {
#if defined(ARDUINOMQTT)
if (c == 0) return 1;
if (c == 1) return mqttstate();
#endif
return 0;
}
/*
* reconnecting mqtt - exponential backoff here
* exponental backoff reconnect in 10 ms * 2^n intervals
* no randomization
*/
uint8_t mqttreconnect() {
uint16_t timer=10;
uint8_t reconnect=0;
/* all good and nothing to be done, we are connected */
if (bmqtt.connected()) return 1;
/* try to reconnect the network */
if (!netconnected()) { netreconnect(); bdelay(5000); }
if (!netconnected()) return 0;
/* create a new random name right now */
mqttsetname();
/* try to reconnect assuming that the network is connected */
while (!bmqtt.connected() && timer < 400) {
if (*mqtt_user == 0) {
bmqtt.connect(mqttname);
} else {
bmqtt.connect(mqttname, mqtt_user, mqtt_passwd);
}
bmqtt.connect(mqttname);
bdelay(timer);
timer=timer*2;
reconnect=1;
}
/* after reconnect resubscribe if there is a valid topic */
if (*mqtt_itopic && bmqtt.connected() && reconnect) bmqtt.subscribe(mqtt_itopic);
return bmqtt.connected();
}
/* mqtt state information */
uint8_t mqttstate() {
return bmqtt.state();
}
/* subscribing to a topic */
void mqttsubscribe(const char* t) {
uint16_t i;
for (i=0; i<MQTTLENGTH; i++) {
if ((mqtt_itopic[i]=t[i]) == 0 ) break;
}
if (!mqttreconnect()) {ioer=1; return;};
if (!bmqtt.subscribe(mqtt_itopic)) ioer=1;
}
void mqttunsubscribe() {
if (!mqttreconnect()) {ioer=1; return;};
if (!bmqtt.unsubscribe(mqtt_itopic)) ioer=1;
*mqtt_itopic=0;
}
/*
* set the topic we pushlish, coming from OPEN
* BASIC can do only one topic.
*
*/
void mqttsettopic(const char *t) {
uint16_t i;
for (i=0; i<MQTTLENGTH; i++) {
if ((mqtt_otopic[i]=t[i]) == 0 ) break;
}
}
/*
* print a mqtt message
* we buffer until we reach either cr or until the buffer is full
* this is needed to do things like PRINT A,B,C as one message
*/
void mqttouts(const char *m, uint16_t l) {
uint16_t i=0;
while(mqtt_charsforsend < MQTTBLENGTH && i<l) mqtt_obuffer[mqtt_charsforsend++]=m[i++];
if (mqtt_obuffer[mqtt_charsforsend-1] == '\n' || mqtt_charsforsend > MQTTBLENGTH) {
if (!mqttreconnect()) {ioer=1; return;};
if (!bmqtt.publish(mqtt_otopic, (uint8_t*) mqtt_obuffer, (unsigned int) mqtt_charsforsend-1, false)) ioer=1;
mqtt_charsforsend=0;
}
}
/* the write command is simple */
void mqttwrite(const char c) { mqttouts(&c, 1); }
/* return the messagelength as avail */
uint16_t mqttavailable() {
return mqtt_messagelength;
}
/* checkch is just a dummy */
char mqttcheckch() {
if (mqtt_messagelength>0) return mqtt_buffer[0]; else return 0;
}
/*
* ins copies the buffer into a basic string
* - behold the jabberwock - length gynmastics
*/
uint16_t mqttins(char *b, uint16_t nb) {
uint16_t z;
for (z=0; z<nb && z<mqtt_messagelength; z++) b[z+1]=mqtt_buffer[z];
b[0]=z;
mqtt_messagelength=0;
*mqtt_buffer=0;
return z;
}
/* just one character to emulate basic get */
char mqttread() {
char ch=0;
uint16_t i;
if (mqtt_messagelength>0) {
ch=mqtt_buffer[0];
for (i=0; i<mqtt_messagelength-1; i++) mqtt_buffer[i]=mqtt_buffer[i+1];
mqtt_messagelength--;
}
return ch;
}
#else
void netbegin() {}
uint8_t netconnected() { return 0; }
void mqttbegin() {}
uint8_t mqttstate() {return 0;}
void mqttsubscribe(char *t) {}
void mqttsettopic(char *t) {}
void mqttouts(char *m, uint16_t l) {}
uint16_t mqttins(char *b, uint16_t nb) { return 0; };
char mqttread() {return 0;};
#endif
/*
* EEPROM handling, these function enable the @E array and
* loading and saving to EEPROM with the "!" mechanism
*/
void ebegin(){
/* this is the EEPROM dummy */
#if (defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) ) && defined(ARDUINOEEPROM)
EEPROM.begin(EEPROMSIZE);
#endif
#if (defined(ARDUINO_ARCH_STM32)) && defined(ARDUINOEEPROM)
eeprom_buffer_fill();
#endif
#if (defined(ARDUINO_ARCH_XMC)) && defined(ARDUINOEEPROM)
EEPROM.begin();
#endif
#if (defined(ARDUINO_ARCH_SAMD)) && defined(ARDUINOEEPROM)
/* no begin method needed */
#endif
#if (defined(ARDUINO_ARCH_MBED_NANO)) && defined(ARDUINOEEPROM)
EEPROM.begin();
#endif
/* an unbuffered EEPROM, typically used to store a program */
#if defined(ARDUINOI2CEEPROM) && !defined(ARDUINOI2CEEPROM_BUFFERED)
/*
* we test the size with a heuristic, but beware, EEPROM addressing
* is circular. We only consider the two most common sizes, 4096 and 32768.
* This is tricky.
*/
#ifndef I2CEEPROMSIZE
int c4=eread(4094);
int c32=eread(32766);
eupdate(4094, 42);
eupdate(32766, 84);
if (ioer !=0 ) return;
if (eread(32766) == 84 && eread(4094) == 42) i2ceepromsize = 32767; else i2ceepromsize = 4096;
eupdate(4094, c4);
eupdate(32766, c32);
#else
i2ceepromsize = I2CEEPROMSIZE;
#endif
#endif
/* there also can be a raw EFS object */
#if defined(ARDUINOI2CEEPROM) && defined(ARDUINOI2CEEPROM_BUFFERED)
EFSRAW.begin();
#ifndef I2CEEPROMSIZE
i2ceepromsize = EFSRAW.esize();
#else
i2ceepromsize = I2CEEPROMSIZE;
#endif
#endif
}
void eflush(){
/* code for the EEPROM dummy */
#if (defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || \
defined(ARDUINO_ARCH_XMC) || defined(ARDUINO_ARCH_SAMD)) \
&& defined(ARDUINOEEPROM)
EEPROM.commit();
#endif
#if (defined(ARDUINO_ARCH_STM32)) && defined(ARDUINOEEPROM)
eeprom_buffer_flush();
#endif
/* flushing the I2C EEPROM */
#if defined(ARDUINOI2CEEPROM) && defined(ARDUINOI2CEEPROM_BUFFERED)
EFSRAW.rawflush();
#endif
}
#if defined(ARDUINOEEPROM) && !defined(ARDUINOI2CEEPROM)
uint16_t elength() {
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
return EEPROMSIZE;
#endif
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR) || defined(ARDUINO_ARCH_XMC) || \
defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_RENESAS) || defined(ARDUINO_ARCH_SAMD)
return EEPROM.length();
#endif
#ifdef ARDUINO_ARCH_LGT8F
return EEPROM.length();
#endif
return 0;
}
void eupdate(uint16_t a, int8_t c) {
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)|| defined(AARDUINO_ARCH_LGT8F) || \
defined(ARDUINO_ARCH_XMC) || defined(ARDUINO_ARCH_SAMD)
EEPROM.write(a, c);
#else
#if defined(ARDUINO_ARCH_STM32)
eeprom_buffered_write_byte(a, c);
#else
EEPROM.update(a, c);
#endif
#endif
}
int8_t eread(uint16_t a) {
#ifdef ARDUINO_ARCH_STM32
return (int8_t) eeprom_buffered_read_byte(a);
#else
return (int8_t) EEPROM.read(a);
#endif
}
#else
#if defined(ARDUINOI2CEEPROM)
uint16_t elength() {
return i2ceepromsize;
}
void eupdate(uint16_t a, int8_t c) {
#if defined(ARDUINOI2CEEPROM_BUFFERED)
EFSRAW.rawwrite(a, c);
#else
/* go directly into the EEPROM */
if (eread(a) != c) {
/* set the address and send the byte*/
/*
Wire.beginTransmission(I2CEEPROMADDR);
Wire.write((int)a/256);
Wire.write((int)a%256);
Wire.write((int)c);
ioer=Wire.endTransmission();
*/
wirestart(I2CEEPROMADDR, 0);
wirewritebyte((int)a/256);
wirewritebyte((int)a%256);
wirewritebyte((int)c);
ioer=wirestop();
/* wait the max time */
bdelay(5);
}
#endif
}
int8_t eread(uint16_t a) {
#ifdef ARDUINOI2CEEPROM_BUFFERED
return (int8_t) EFSRAW.rawread(a);
#else
/* read directly from the EEPROM */
/* set the address */
wirestart(I2CEEPROMADDR, 0);
wirewritebyte((int)a/256);
wirewritebyte((int)a%256);
ioer=wirestop();
/* read a byte */
if (ioer == 0) {
wirestart(I2CEEPROMADDR, 1);
return wirereadbyte();
} else return 0;
#endif
}
#else
/* no EEPROM present */
uint16_t elength() { return 0; }
void eupdate(uint16_t a, int8_t c) { return; }
int8_t eread(uint16_t a) { return 0; }
#endif
#endif
/*
* A set of function to directly accessing IO ports. This is
* hardware dependend and needs to be implemented for each
* platform.
*
* Unfinished code right now, test code for the UNO.
*/
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_DUEMILANOVE) || defined(ARDUINO_AVR_NANO)
void portwrite(uint8_t p, int v) {
switch (p) {
case 0: PORTB = v; break;
case 1: PORTC = v; break;
case 2: PORTD = v; break;
default: break;
}
}
int portread(uint8_t p) {
switch (p) {
case 0: return PORTB; break;
case 1: return PORTC; break;
case 2: return PORTD; break;
default: return 0;
}
}
void ddrwrite(uint8_t p, int v) {
switch (p) {
case 0: DDRB = v; break;
case 1: DDRC = v; break;
case 2: DDRD = v; break;
default: break;
}
}
int ddrread(uint8_t p) {
switch (p) {
case 0: return DDRB; break;
case 1: return DDRC; break;
case 2: return DDRD; break;
default: return 0;
}
}
int pinread(uint8_t p) {
switch (p) {
case 0: return PINB; break;
case 1: return PINC; break;
case 2: return PIND; break;
default: return 0;
}
}
#else
void portwrite(uint8_t p, int v) {}
int portread(uint8_t p) { return 0; }
void ddrwrite(uint8_t p, int v) {}
int ddrread(uint8_t p) { return 0; }
int pinread(uint8_t p) { return 0; }
#endif
/*
* the wrappers of the arduino io functions
* awrite requires ESP32 2.0.2 core, else disable awrite()
*/
uint16_t aread(uint8_t p) { return analogRead(p); }
uint8_t dread(uint8_t p) { return digitalRead(p); }
void awrite(uint8_t p, uint16_t v){ analogWrite(p, v); }
void dwrite(uint8_t p, uint8_t v){ if (v) digitalWrite(p, HIGH); else digitalWrite(p, LOW); }
/* we normalize the pinMode as ESP32, ESP8266, and other boards behave rather
* differently. Following Arduino conventions we map 0 always to INPUT
* and 1 always to OUTPUT, all the other numbers are passed through to the HAL
* layer of the platform.
* Example: OUTPUT on ESP32 is 3 and 1 is assigned to INPUT.
* XMC also needs special treatment here.
* uint8_t is here for a reason.
*/
void pinm(uint8_t p, uint8_t m){
if (m == 0) m=INPUT;
else if (m == 1) m=OUTPUT;
pinMode(p,m);
}
/* wrapper around pulsein */
uint32_t pulsein(uint8_t pin, uint8_t val, uint32_t t) {
return pulseIn(pin, val, t);
}
/* write a pulse in microsecond range */
void pulseout(uint16_t unit, uint8_t pin, uint16_t duration, uint16_t val, uint16_t repetition, uint16_t interval) {
pinMode(pin, OUTPUT);
while (repetition--) {
digitalWrite(pin, val);
delayMicroseconds(duration*unit);
digitalWrite(pin, !val);
delayMicroseconds(interval*unit);
}
}
/* the BREAKPIN mechanism */
#if defined(BREAKPIN) && defined(INPUT_PULLUP)
void breakpinbegin() { pinm(BREAKPIN, INPUT_PULLUP); }
uint8_t getbreakpin() { return dread(BREAKPIN); }
#else
/* there is no pins hence no breakpin */
void breakpinbegin() {}
uint8_t getbreakpin() { return 1; } /* we return 1 because the breakpin is defined INPUT_PULLUP */
#endif
/*
* A tone emulation based on the byield loop. The maximum frequency depends
* on the byield call speed. 10-20 kHz is possible. Will be unreliable if
* the loop functions need a lot of CPU cycles.
*/
#if defined(ARDUINOTONEEMULATION)
uint8_t tone_enabled;
uint8_t tone_pinstate = 0;
uint32_t tone_intervall;
uint32_t tone_micros;
uint16_t tone_duration;
uint32_t tone_start;
uint8_t tone_pin;
void playtone(uint8_t pin, uint16_t frequency, uint16_t duration, uint8_t volume) {
/* the pin we are using is reset every time this is called*/
tone_pin=pin;
pinMode(tone_pin, OUTPUT);
digitalWrite(tone_pin, LOW);
tone_pinstate=0;
/* duration 0 is a long tone */
if (duration == 0) duration=32767;
/* frequency 0 stops playing */
if (frequency == 0) {
tone_enabled=0;
return;
}
/* calculate the toggle intervall in micros and remember where we are */
tone_intervall=1000000/frequency/2;
tone_micros=micros();
/* set the duration and remember where we are*/
tone_duration=duration;
tone_start=millis();
/* start the play */
tone_enabled=1;
}
void tonetoggle() {
/* is this active? */
if (!tone_enabled) return;
/* check if we are done playing */
if (millis() > tone_duration+tone_start) {
tone_enabled=0;
digitalWrite(tone_pin, LOW);
tone_pinstate=0;
return;
}
/* if not, check if the intervall is over */
if (micros() > tone_intervall+tone_micros) {
tone_micros=micros();
tone_pinstate=!tone_pinstate;
digitalWrite(tone_pin, tone_pinstate);
}
}
#elif defined(ARDUINO_TTGO_T7_V14_Mini32) && defined(PS2FABLIB)
/* fabGL soundgenerator code of suggestes by testerrossa
* pin numbers below 128 are real arduino pins while
* pin numvers from 128 onwards are sound generator capabilities
* this is different from the original code
*
* Sound generator capabilities are numbered as follows
* 128: Sine wave
* 129: Symmetric square wave
* 130: Sawtooth
* 131: Triangle
* 132: VIC noise
* 133: noise
*
* 256-511: square wave with variable duty cycle
*
*/
void playtone(uint8_t pin, uint16_t frequency, uint16_t duration, uint8_t volume) {
/* frequency 0 is off */
if (frequency == 0) {
soundGenerator.play(false);
soundGenerator.clear();
return;
}
/* duration 0 is a long tone */
if (duration == 0) duration=60000;
if (pin == 128) soundGenerator.playSound(SineWaveformGenerator(), frequency, duration, volume);
if (pin == 129) soundGenerator.playSound(SquareWaveformGenerator(), frequency, duration, volume);
if (pin == 130) soundGenerator.playSound(SawtoothWaveformGenerator(), frequency, duration, volume);
if (pin == 131) soundGenerator.playSound(TriangleWaveformGenerator(), frequency, duration, volume);
if (pin == 132) soundGenerator.playSound(VICNoiseGenerator(), frequency, duration, volume);
if (pin == 133) soundGenerator.playSound(NoiseWaveformGenerator(), frequency, duration, volume);
if (pin >= 255 && pin < 512 ) {
pin=pin-255;
SquareWaveformGenerator sqw;
sqw.setDutyCycle(pin);
soundGenerator.playSound(sqw, frequency, duration, volume);
}
}
#else
/* playtone is just a wrapper around tone and noTone */
void playtone(uint8_t pin, uint16_t frequency, uint16_t duration, uint8_t volume) {
if (frequency == 0) {
noTone(pin);
} else if (duration == 0) {
tone(pin, frequency);
} else {
tone(pin, frequency, duration);
}
}
#endif
/*
* The byield function is called after every statement
* it allows two levels of background tasks.
*
* BASICBGTASK controls if time for background tasks is
* needed, usually set by hardware features
*
* YIELDINTERVAL by default is 32, generating a 32 ms call
* to the network loop function.
*
* LONGYIELDINTERVAL by default is 1000, generating a one second
* call to maintenance functions.
*/
uint32_t lastyield=0;
uint32_t lastlongyield=0;
int countfasttick = 0;
void byield() {
/* the fast ticker for all fast timing functions */
fastticker();
/* the loop function for non BASIC stuff */
bloop();
#if defined(BASICBGTASK)
/* what time is it, call millis only once */
long m=millis();
/* yield all 32 milliseconds */
if (m-lastyield > YIELDINTERVAL-1) {
yieldfunction();
lastyield=m;
}
/* yield every second */
if (m-lastlongyield > LONGYIELDINTERVAL-1) {
longyieldfunction();
lastlongyield=m;
}
#endif
/* call the background task scheduler on some platforms implemented in hardware-* */
}
/* delay must be implemented to use byield() while waiting */
void bdelay(uint32_t t) {
unsigned long i;
if (t>0) {
i=millis();
while (millis() < i+t) byield();
}
}
/*
* This code measures the fast ticker frequency in microseconds
* It leaves the data in variable F. Activate this only for test
* purposes.
*/
#ifdef FASTTICKERPROFILE
int avgfastticker() {
return countfasttick;
}
void clearfasttickerprofile() {
countfasttick=0;
}
#endif
/* fastticker is the hook for all fast timing functions */
void fastticker() {
/* fastticker profiling test code */
#if defined(FASTTICKERPROFILE)
countfasttick++;
#endif
/* toggle the tone pin */
#ifdef ARDUINOTONEEMULATION
tonetoggle();
#endif
}
/* everything that needs to be done often - 32 ms */
void yieldfunction() {
#ifdef ARDUINOMQTT
bmqtt.loop();
#endif
#ifdef ARDUINOUSBKBD
usb.Task();
#endif
#ifdef ARDUINOZX81KBD
(void) keyboard.peek(); /* scan once and set lastkey properly every 32 ms */
#endif
/* delay(0) is only needed on ESP8266! it calls the scheduler - no bdelay here!! */
#if defined(ARDUINO_ARCH_ESP8266)
delay(0);
#endif
}
/* everything that needs to be done not so often - 1 second */
void longyieldfunction() {
#ifdef ARDUINOETH
Ethernet.maintain();
#endif
#ifdef BREAKINBACKGROUND
if (checkch() == BREAKCHAR) breakcondition=1;
#endif
}
/*
* The file system driver - all methods needed to support BASIC fs access
* Different filesystems need different prefixes and fs objects, these
* filesystems use the stream API
*/
/* typical file name length */
#define FBUFSIZE 32
#if defined(ARDUINOSD) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(STM32SDIO) ||defined(ESPSDMMC)
File ifile;
File ofile;
char tempname[FBUFSIZE]; /* this is needed for the catalog code as these platforms do not give static C strings back */
#if defined(ARDUINOSD) || defined(STM32SDIO) ||defined(ESPSDMMC)
File root;
File file;
#ifdef ARDUINO_ARCH_ESP32
const char rootfsprefix[2] = "/";
#else
const char rootfsprefix[1] = "";
#endif
#endif
#if defined(ESPSPIFFS) || defined(ESP32FAT)
const char rootfsprefix[2] = "/";
#ifdef ARDUINO_ARCH_ESP8266
Dir root;
File file;
#endif
#ifdef ARDUINO_ARCH_ESP32
File root;
File file;
#endif
#endif
#ifdef ARDUINO_ARCH_ESP8266
/* #define FILE_OWRITE (O_READ | O_WRITE | O_CREAT | O_TRUNC) */
/* this is the ESP8266 definition for kernel 3.0, 3.1 has fixed this */
#define FILE_OWRITE (sdfat::O_READ | sdfat::O_WRITE | sdfat::O_CREAT | sdfat::O_TRUNC)
#else
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_STM32)
#define FILE_OWRITE FILE_WRITE
#else
#define FILE_OWRITE (O_READ | O_WRITE | O_CREAT | O_TRUNC)
#endif
#endif
#endif
/* using the POSIX API in LittleFS */
#ifdef RP2040LITTLEFS
#define FILESYSTEMDRIVER
FILE* ifile;
FILE* ofile;
DIR* root;
struct dirent* file;
LittleFS_MBED *myFS;
const char rootfsprefix[10] = MBED_LITTLEFS_FILE_PREFIX;
#endif
/*
* the API of the USB filesystem on GIGA and Portenta.
* Looks like LITTLEFS. Pointers to the objects msd and usb are used and
* allocation is postponed to fsbegin(). This was done to try to get the code
* restartable, but it doesn't really work. Still, using the pointers
* is cleaner and more C++ish.
*/
#ifdef GIGAUSBFS
#define FILESYSTEMDRIVER
USBHostMSD* msdp;
mbed::FATFileSystem* usbp;
FILE* ifile;
FILE* ofile;
DIR* root;
struct dirent *file;
const char rootfsprefix[10] = "/usb/";
#endif
/* use EEPROM as filesystem */
#ifdef ARDUINOEFS
byte ifile;
byte ofile;
byte file;
#endif
/* the buildin file system for ro access */
#ifdef HASBUILDIN
char* buildin_ifile = 0;
uint16_t buildin_ifilepointer = 0;
char* buildin_file = 0;
char* buildin_pgm = 0;
uint8_t buildin_rootactive = 0;
uint16_t buildin_rootpointer = 0;
char buildin_tempname[FBUFSIZE]; /* this is needed for the catalog code as strings need to be copied from progmem */
#endif
/* these filesystems may have a path prefixes, we treat STM32SDIO like an SD here */
#if defined(RP2040LITTLEFS) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(ARDUINOSD) || defined(GIGAUSBFS) || defined(ESPSDMMC)
char tmpfilename[10+FBUFSIZE];
/* add the prefix to the filename */
char* mkfilename(const char* filename) {
uint8_t i,j;
for(i=0; i<10 && rootfsprefix[i]!=0; i++) tmpfilename[i]=rootfsprefix[i];
tmpfilename[i++]='/';
for(j=0; j<FBUFSIZE && filename[j]!=0; j++) tmpfilename[i++]=filename[j];
tmpfilename[i]=0;
return tmpfilename;
}
/* remove the prefix from the filename */
const char* rmrootfsprefix(const char* filename) {
uint8_t i=0;
while (filename[i] == rootfsprefix[i] && rootfsprefix[i] !=0 && filename[i] !=0 ) i++;
if (filename[i]=='/') i++;
return &filename[i];
}
#endif
/*
* filesystem starter for SPIFFS and SD on ESP, ESP32 and Arduino plus LittleFS
* and then also GIGAUSBFS
* the verbose option needs to be checked .
*
* Also here the driver for the buildin file system.
*
* if SDPIN is empty it is the standard SS pin of the platfrom
*
*/
uint8_t fsstart;
void fsbegin() {
#ifdef ARDUINOSD
#ifndef SDPIN
#define SDPIN
#endif
if (SD.begin(SDPIN)) fsstart=1; else fsstart=0;
#endif
#ifdef STM32SDIO
static int fsbegins = 0;
while (fsbegins<100) {
if (SD.begin(SD_DETECT_PIN)) { fsstart=1; break; } else fsstart=0;
bdelay(20);
fsbegins++;
}
#endif
#if defined(ESPSPIFFS) && defined(ARDUINO_ARCH_ESP8266)
if (SPIFFS.begin()) fsstart=1; else fsstart=0;
#endif
#if defined(ESPSPIFFS) && defined(ARDUINO_ARCH_ESP32)
if (SPIFFS.begin()) fsstart=1; else fsstart=0;
#endif
#if defined(ESP32FAT) && defined(ARDUINO_ARCH_ESP32)
if (FFat.begin()) fsstart=1; else fsstart=0;
#endif
#if defined(ESPSDMMC) && defined(ARDUINO_ARCH_ESP32)
if (SD_MMC.begin()) fsstart=1; else fsstart=0;
#endif
#ifdef RP2040LITTLEFS
myFS = new LittleFS_MBED();
if (myFS->init()) fsstart=1; else fsstart=0;
#endif
#ifdef GIGAUSBFS
static int fsbegins = 0;
int usbfsstat;
if (RTDEBUG) consolelog("Starting USB file system.\n");
/* power up the USB A port */
pinMode(PA_15, OUTPUT);
if (RTDEBUG) consolelog("Power down USB port.\n");
digitalWrite(PA_15, LOW);
bdelay(100);
if (RTDEBUG) consolelog("Power up USB port.\n");
digitalWrite(PA_15, HIGH);
bdelay(100);
/* delete old objects */
if (msdp) delete msdp;
if (usbp) delete usbp;
/* create the msdp and the filesystem object */
msdp=new USBHostMSD();
usbp=new mbed::FATFileSystem("usb");
/* connect and mount */
while(!msdp->connect() && fsbegins++ < 10) { bdelay(500); }
if (msdp->connected()) {
if (RTDEBUG) { consolelog("msd connected\n"); }
usbfsstat=usbp->mount(msdp);
if (RTDEBUG) {
consolelog("USB mount status is ");
consolelognum(usbfsstat);
consolelog("\n");
}
fsstart=!usbfsstat;
} else {
if (RTDEBUG) { consolelog("msd not connected\n"); }
fsstart=0;
}
if (RTDEBUG) { consolelog("fsstart is: "); consolelognum(fsstart);
consolelog("\n"); }
#endif
#ifdef ARDUINOEFS
uint8_t s=EFS.begin();
if (s>0) {
fsstart=s;
} else {
if (EFS.format(32)) fsstart=32; else fsstart=0;;
}
#endif
#ifdef HASBUILDIN
/* hopefully nothing to be done */
#endif
}
/*
* Maps information of the start of the filesystem to
* the USR(16,x) call. USR(16,0) checks if a filesystem is
* present and USR(16,1) checks if the filesystem has been mounted.
*/
uint8_t fsstat(uint8_t c) {
#if defined(FILESYSTEMDRIVER)
if (c == 0) return 1;
if (c == 1) return fsstart;
#endif
return 0;
}
/*
* File I/O function on an Arduino
*
* filewrite(), fileread(), fileavailable() as byte access
* open and close is handled separately by (i/o)file(open/close)
* only one file can be open for write and read at the same time
*/
void filewrite(char c) {
#if defined(ARDUINOSD) || defined(ESPSPIFFS)||defined(ESP32FAT)||defined(STM32SDIO)|| defined(ESPSDMMC)
if (ofile) { ofile.write(c); return; }
#endif
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS)
if (ofile) { fputc(c, ofile); return; }
#endif
#if defined(ARDUINOEFS)
if (ofile) {
if (!EFS.fputc(c, ofile)) ioer=-1;
return;
}
#endif
/* if there is no open ofile, set the ioerror flag to notify BASIC about it */
ioer=1;
}
char fileread() {
char c;
/* the buildin file is active, we handle this first, else we allow for another FS */
#if defined(HASBUILDIN)
if (buildin_ifile != 0) {
c=pgm_read_byte(buildin_ifile+buildin_ifilepointer);
if (c != '\f') buildin_ifilepointer++; else { ioer=-1; c=255; }
return c;
}
#endif
#if defined(ARDUINOSD) || defined(ESPSPIFFS)||defined(ESP32FAT)||defined(STM32SDIO) || defined(ESPSDMMC)
if (ifile) c=ifile.read(); else { ioer=1; return 0; }
if (c == -1 || c == 255) ioer=-1;
return c;
#endif
#if defined(RP2040LITTLEFS)||defined(GIGAUSBFS)
if (ifile) c=fgetc(ifile); else { ioer=1; return 0; }
if (c == -1 || c == 255) ioer=-1;
return c;
#endif
#ifdef ARDUINOEFS
if (ifile) c=EFS.fgetc(ifile); else { ioer=1; return 0; }
if (c == -1|| c == 255) ioer=-1;
return c;
#endif
return 0;
}
int fileavailable(){
/* for the buildin file \f is the end of file, return true is it has not been reached */
#if defined(HASBUILDIN)
char c;
if (buildin_ifile != 0) {
c=pgm_read_byte(buildin_ifile+buildin_ifilepointer);
if (c != '\f') return 1; else return 0;
}
#endif
#if defined(ARDUINOSD) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(STM32SDIO) || defined(ESPSDMMC)
return ifile.available();
#endif
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS)
return !feof(ifile);
#endif
#ifdef ARDUINOEFS
return EFS.available(ifile);
#endif
return 0;
}
uint8_t ifileopen(const char* filename){
/* we first try to open a file on the builin file system, then of the other ones */
#if defined(HASBUILDIN)
byte i, file;
char* name;
/* opening a new file */
buildin_ifilepointer=0;
buildin_ifile=0;
file=0;
while(1) {
/* find a name in progmem, 0 if no more name */
name=(char*)pgm_read_ptr(&buildin_program_names[file]);
if (name == 0) break;
/* compare the name with the filename */
for(i=0;i<32;i++) {
char c=pgm_read_byte(&name[i]);
/* we are at the end of the name, if filename is also ending, we got it */
if (c == 0 && filename[i] == 0) {
buildin_ifile=(char*)pgm_read_ptr(&buildin_programs[file]);
return (buildin_ifile != 0);
}
/* a character mismatch means we need to go to the next name */
if (c != filename[i]) {
file++;
break;
}
}
}
#endif
/*
* All filesystems that are not buildin are checked for correct open
* This is needed for GIGA as the USB host tends to block.
*/
if (!fsstart) return 0;
#if defined(ARDUINOSD)
ifile=SD.open(mkfilename(filename), FILE_READ);
return ifile != 0;
#endif
#ifdef ESPSDMMC
ifile=SD_MMC.open(mkfilename(filename), FILE_READ);
return ifile != 0;
#endif
#if defined(STM32SDIO)
ifile=SD.open(filename);
return ifile != 0;
#endif
#ifdef ESPSPIFFS
ifile=SPIFFS.open(mkfilename(filename), "r");
return ifile != 0;
#endif
#ifdef ESP32FAT
ifile=FFat.open(mkfilename(filename), "r");
return ifile != 0;
#endif
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS)
ifile=fopen(mkfilename(filename), "r");
return ifile != 0;
#endif
#ifdef ARDUINOEFS
ifile=EFS.fopen(filename, "r");
return ifile != 0;
#endif
return 0;
}
void ifileclose(){
#if defined(HASBUILDIN)
if (buildin_ifile) {
buildin_ifile=0;
buildin_ifilepointer=0;
}
#endif
#if defined(ARDUINOSD) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(STM32SDIO) || defined(ESPSDMMC)
if (ifile) ifile.close();
#endif
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS)
if (ifile) fclose(ifile);
#endif
#ifdef ARDUINOEFS
if (ifile) EFS.fclose(ifile);
#endif
}
uint8_t ofileopen(const char* filename, const char* m){
/*
* All filesystems that are not buildin are checked for correct open
* This is needed for GIGA as the USB host tends to block.
*/
if (!fsstart) return 0;
#if defined(ARDUINOSD)
if (*m == 'w') ofile=SD.open(mkfilename(filename), FILE_OWRITE);
/* ESP32 has FILE_APPEND defined */
#ifdef FILE_APPEND
if (*m == 'a') ofile=SD.open(mkfilename(filename), FILE_APPEND);
#else
if (*m == 'a') ofile=SD.open(mkfilename(filename), FILE_WRITE);
#endif
return ofile != 0;
#endif
#if defined(ESPSDMMC)
if (*m == 'w') ofile=SD_MMC.open(mkfilename(filename), FILE_OWRITE);
/* ESP32 has FILE_APPEND defined */
#ifdef FILE_APPEND
if (*m == 'a') ofile=SD_MMC.open(mkfilename(filename), FILE_APPEND);
#else
if (*m == 'a') ofile=SD_MMC.open(mkfilename(filename), FILE_WRITE);
#endif
return ofile != 0;
#endif
#if defined(STM32SDIO)
if (*m == 'w') ofile=SD.open(filename, FILE_OWRITE);
/* ESP32 has FILE_APPEND defined */
#ifdef FILE_APPEND
if (*m == 'a') ofile=SD.open(filename, FILE_APPEND);
#else
if (*m == 'a') ofile=SD.open(filename, FILE_WRITE);
#endif
return ofile != 0;
#endif
#ifdef ESPSPIFFS
ofile=SPIFFS.open(mkfilename(filename), m);
return ofile != 0;
#endif
#ifdef ESP32FAT
ofile=FFat.open(mkfilename(filename), m);
return ofile != 0;
#endif
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS)
ofile=fopen(mkfilename(filename), m);
return ofile != 0;
#endif
#ifdef ARDUINOEFS
ofile=EFS.fopen(filename, m);
return ofile != 0;
#endif
/* if no other filesystem has satisfied the write open BUILDIN reports the error */
#if defined(HASBUILDIN)
ioer=-1;
return 0;
#endif
return 0;
}
void ofileclose(){
#if defined(ARDUINOSD) || defined(ESPSPIFFS)||defined(ESP32FAT)|| defined(STM32SDIO) || defined(ESPSDMMC)
if (ofile) ofile.close();
#endif
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS)
if (ofile) fclose(ofile);
#endif
#ifdef ARDUINOEFS
if (ofile) EFS.fclose(ofile);
#endif
}
/*
* directory handling for the catalog function
* these methods are needed for a walkthtrough of
* one directory
*
* rootopen()
* while rootnextfile()
* if rootisfile() print rootfilename() rootfilesize()
* rootfileclose()
* rootclose()
*/
void rootopen() {
/* we first open the buildin and remember we are processing it, if there is another root, we also open it */
#ifdef HASBUILDIN
buildin_rootpointer=0;
buildin_rootactive=1;
#endif
/*
* All filesystems that are not buildin are checked for correct open
* This is needed for GIGA as the USB host tends to block.
*/
if (!fsstart) return;
#if defined(ARDUINOSD) || defined(STM32SDIO)
root=SD.open("/");
#endif
#if defined(ESPSDMMC)
root=SD_MMC.open("/");
#endif
#ifdef ESPSPIFFS
#ifdef ARDUINO_ARCH_ESP8266
root=SPIFFS.openDir("/");
#endif
#ifdef ARDUINO_ARCH_ESP32
root=SPIFFS.open("/");
#endif
#endif
#ifdef ESP32FAT
root=FFat.open("/");
#endif
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS)
root=opendir(rootfsprefix);
#endif
#ifdef ARDUINOEFS
EFS.dirp=0;
#endif
}
uint8_t rootnextfile() {
/* we first iterate through the buildins, then fall through to the other filesystems */
#ifdef HASBUILDIN
if (buildin_rootactive) {
buildin_file=(char*)pgm_read_ptr(&buildin_program_names[buildin_rootpointer]);
if (buildin_file != 0) {
buildin_pgm=(char*)pgm_read_ptr(&buildin_programs[buildin_rootpointer]);
buildin_rootpointer++;
return 1;
} else {
buildin_rootactive=0;
}
}
#endif
/*
* All filesystems that are not buildin are checked for correct open
* This is needed for GIGA as the USB host tends to block.
*/
if (!fsstart) return 0;
#if defined(ARDUINOSD) || defined(STM32SDIO)||defined(ESPSDMMC)
file=root.openNextFile();
return (file != 0);
#endif
#ifdef ESPSPIFFS
#ifdef ARDUINO_ARCH_ESP8266
if (root.next()) {
file=root.openFile("r");
return 1;
} else {
return 0;
}
#endif
#ifdef ARDUINO_ARCH_ESP32
file=root.openNextFile();
return (file != 0);
#endif
#endif
#ifdef ESP32FAT
#ifdef ARDUINO_ARCH_ESP32
file=root.openNextFile();
return (file != 0);
#endif
#endif
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS)
file = readdir(root);
return (file != 0);
#endif
#ifdef ARDUINOEFS
file = EFS.readdir();
return (file != 0);
#endif
return 0;
}
uint8_t rootisfile() {
#ifdef HASBUILDIN
if (buildin_rootactive) return 1;
#endif
#if defined(ARDUINOSD) || defined(STM32SDIO) || defined(ESPSDMMC)
return (!file.isDirectory());
#endif
#ifdef ESPSPIFFS
#ifdef ARDUINO_ARCH_ESP8266
return 1;
#endif
#ifdef ARDUINO_ARCH_ESP32
return (!file.isDirectory());
#endif
#endif
#ifdef ESP32FAT
#ifdef ARDUINO_ARCH_ESP32
return (!file.isDirectory());
#endif
#endif
#if defined(RP2040LITTLEFS)||defined(GIGAUSBFS)
return (file->d_type == DT_REG);
#endif
#ifdef ARDUINOEFS
return 1;
#endif
return 0;
}
const char* rootfilename() {
#ifdef HASBUILDIN
if (buildin_rootactive) {
for(int i=0; i<FBUFSIZE; i++) {
buildin_tempname[i]=(char)pgm_read_byte(&buildin_file[i]);
if (buildin_tempname[i] == 0) return buildin_tempname;
}
}
#endif
#if defined(ARDUINOSD) || defined(ESPSDMMC)
return rmrootfsprefix(file.name());
#endif
#if defined(STM32SDIO)
return (char*) file.name();
#endif
#ifdef ESPSPIFFS
#ifdef ARDUINO_ARCH_ESP8266
int i=0;
String s=root.fileName();
for (i=0; i<s.length(); i++) tempname[i]=s[i];
tempname[i]=0;
return rmrootfsprefix(tempname);
#endif
#ifdef ARDUINO_ARCH_ESP32
return rmrootfsprefix(file.name());
#endif
#endif
#ifdef ESP32FAT
#ifdef ARDUINO_ARCH_ESP32
return rmrootfsprefix(file.name());
#endif
#endif
#if defined(RP2040LITTLEFS)||defined(GIGAUSBFS)
return (file->d_name);
#endif
#ifdef ARDUINOEFS
return EFS.filename(file);
#endif
return 0;
}
uint32_t rootfilesize() {
#ifdef HASBUILDIN
int i=0;
if (buildin_rootactive && buildin_pgm) {
for (;;i++) {
char ch=pgm_read_byte(&buildin_pgm[i]);
if (ch == 0) return i;
}
}
#endif
#if defined(ARDUINOSD) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(STM32SDIO) || defined(ESPSDMMC)
return file.size();
#endif
#if defined(RP2040LITTLEFS)||defined(GIGAUSBFS)
/* to be done */
#endif
#ifdef ARDUINOEFS
return EFS.filesize(file);
#endif
return 0;
}
void rootfileclose() {
#if defined(ARDUINOSD) || defined(ESPSPIFFS) || defined(ESP32FAT) || defined(STM32SDIO) || defined(ESPSDMMC)
file.close();
#endif
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS)
/* nothing to be done here as we work on dirents */
#endif
#ifdef ARDUINOEFS
#endif
}
void rootclose(){
#ifdef HASBUILDIN
buildin_rootactive=0;
#endif
#if defined(ARDUINOSD) || defined(STM32SDIO) || defined(ESPSDMMC)
root.close();
#endif
#ifdef ESPSPIFFS
#ifdef ARDUINO_ARCH_ESP8266
return;
#endif
#ifdef ARDUINO_ARCH_ESP32
root.close();
#endif
#endif
#ifdef ESP32FAT
#ifdef ARDUINO_ARCH_ESP32
root.close();
#endif
#endif
#ifdef RP2040LITTLEFS
#endif
#ifdef GIGAUSBFS
if (root) (void) closedir(root);
#endif
#ifdef ARDUINOEFS
#endif
}
/*
* remove method for files
*/
void removefile(const char *filename) {
#if defined(ARDUINOSD)
SD.remove(mkfilename(filename));
return;
#endif
#if defined(ESPSDMMC)
SD_MMC.remove(mkfilename(filename));
return;
#endif
#if defined(STM32SDIO)
SD.remove(filename);
return;
#endif
#ifdef ESPSPIFFS
SPIFFS.remove(mkfilename(filename));
return;
#endif
#ifdef ESP32FAT
FFat.remove(mkfilename(filename));
return;
#endif
#if defined(RP2040LITTLEFS) || defined(GIGAUSBFS)
remove(mkfilename(filename));
return;
#endif
#ifdef ARDUINOEFS
EFS.remove(filename);
return;
#endif
}
/*
* formatting for fdisk of the internal filesystems
*/
void formatdisk(uint8_t i) {
#if defined(ARDUINOSD) || defined(STM32SDIO)||defined(GIGAUSBFS) || defined(ESPSDMMC)
return;
return;
#endif
#ifdef ESPSPIFFS
if (SPIFFS.format()) { SPIFFS.begin(); fsstart=1; } else { fsstart=0; }
#endif
#ifdef ESP32FAT
FFat.end();
if (FFat.format()) { FFat.begin(); fsstart=1; } else { fsstart=0; }
#endif
#ifdef RP2040LITTLEFS
fs.reformat(&bd); /* bd: this comes from the header */
return;
#endif
#ifdef ARDUINOEFS
if (i>0) {
if (EFS.format(i)) { EFS.begin(); fsstart=1; } else { fsstart=0; }
} else ioer=1;
return;
#endif
}
/*
* The buffer code, a simple buffer to store output and
* input data. It can be used as a device in BASIC using the
* modifier &0.
*/
/* use the input buffer variable from BASIC here, it is extern to runtime */
void bufferbegin() {}
/* write to the buffer, works only until 127
uses vt52 style commands to handle the buffer content*/
void bufferwrite(char c) {
if (!nullbuffer) return;
switch (c) {
case 12: /* clear screen */
//nullbuffer[nullbuffer[0]+1]=0;
nullbuffer[0]=0;
nullbuffer[1]=0;
break;
case 10:
case 13: /* cr and lf ignored */
break;
case 8: /* backspace */
if (nullbuffer[0]>0) nullbuffer[0]--;
break;
default:
if (nullbuffer[0] < nullbufsize-1 && nullbuffer[0] < 127) {
nullbuffer[++nullbuffer[0]]=c;
nullbuffer[nullbuffer[0]+1]=0; /* null terminate */
}
break;
}
}
/* read not needed right now */
char bufferread() { return 0; }
uint16_t bufferavailable() { return 0; }
char buffercheckch() { return 0; }
void bufferflush() { }
uint16_t bufferins(char *b, uint16_t nb) { return 0; }
/*
* Primary serial code uses the Serial object or Picoserial
*
* The picoseria an own interrupt function. This is used to fill
* the input buffer directly on read. Write is standard like in
* the serial code.
*
* As echoing is done in the interrupt routine, the code cannot be
* used to receive keystrokes from serial and then display the echo
* directly to a display. To do this the write command in picogetchar
* would have to be replaced with a outch() that then redirects to the
* display driver. This would be very tricky from the timing point of
* view if the display driver code is slow.
*
* The code for the UART control is mostly taken from PicoSerial
* https://github.com/gitcnd/PicoSerial, originally written by Chris Drake
* and published under GPL3.0 just like this code
*
*/
#ifdef ARDUINOPICOSERIAL
volatile char picochar;
volatile uint8_t picoa = 0;
volatile char* picob = NULL;
int16_t picobsize = 0;
volatile int16_t picoi = 1;
volatile uint16_t picoz; /* ugly */
/* this is mostly taken from PicoSerial */
const uint16_t MIN_2X_BAUD = F_CPU/(4*(2*0XFFF + 1)) + 1;
/* the begin code */
void picobegin(uint32_t baud) {
uint16_t baud_setting;
cli();
if ((F_CPU != 16000000UL || baud != 57600) && baud > MIN_2X_BAUD) {
UCSR0A = 1 << U2X0;
baud_setting = (F_CPU / 4 / baud - 1) / 2;
} else {
UCSR0A = 0;
baud_setting = (F_CPU / 8 / baud - 1) / 2;
}
/* assign the baud_setting */
UBRR0H = baud_setting >> 8;
UBRR0L = baud_setting;
/* enable transmit and receive */
UCSR0B |= (1 << TXEN0) | (1 << RXEN0) | (1 << RXCIE0);
sei();
}
/* the write code, sending bytes directly to the UART */
void picowrite(char b) {
while (((1 << UDRIE0) & UCSR0B) || !(UCSR0A & (1 << UDRE0))) {}
UDR0 = b;
}
/*
* picogetchar: this is the interrupt service routine. It
* recieves a character and feeds it into a buffer and echos it
* back. The logic here is that the ins() code sets the buffer
* to the input buffer. Only then the routine starts writing to the
* buffer. Once a newline is received, the length information is set
* and picoa is also set to 1 indicating an available string, this stops
* recevieing bytes until the input is processed by the calling code.
*/
void picogetchar(char c){
if (picob && (!picoa)) {
picochar=c;
if (picochar != '\n' && picochar != '\r' && picoi<picobsize-1) {
if (c == 127 || c == 8) {
if (picoi>1) picoi--; else return;
} else
picob[picoi++]=picochar;
picowrite(picochar);
} else {
picoa = 1;
picob[picoi]=0;
picob[0]=picoi;
picoz=picoi;
picoi=1;
}
picochar=0; /* every buffered byte is deleted */
} else {
if (c != 10) picochar=c;
}
}
/* the ins code of picoserial, called like this in consins */
uint16_t picoins(char *b, uint16_t nb) {
picob=b;
picobsize=nb;
picoa=0;
/* once picoa is set, the interrupt routine records characters
* until a cr and then resets picoa to 0 */
while (!picoa) byield();
outch(10);
return picoz;
}
/* on an UART interrupt, the getchar function is called */
#ifdef USART_RX_vect
ISR(USART_RX_vect) {
char c=UDR0;
picogetchar(c);
}
#else
/* for MEGAs and other with many UARTs */
ISR(USART0_RX_vect) {
char c=UDR0;
picogetchar(c);
}
#endif
#endif
/*
* blocking serial single char read for Serial
* unblocking for Picoserial because it is not
* character oriented -> blocking is handled in
* consins instead.
*/
char serialread() {
#ifdef ARDUINOPICOSERIAL
char c;
c=picochar;
picochar=0;
return c;
#else
while (!SERIALPORT.available()) byield();
return SERIALPORT.read();
#endif
}
/*
* serial begin code with a one second delay after start
* this is not needed any more
*/
void serialbegin() {
#ifdef ARDUINOPICOSERIAL
picobegin(serial_baudrate);
#else
SERIALPORT.begin(serial_baudrate);
#endif
bdelay(1000);
}
/* state information on the serial port */
uint8_t serialstat(uint8_t c) {
if (c == 0) return 1;
if (c == 1) return serial_baudrate/100;
return 0;
}
/* write to a serial stream */
void serialwrite(char c) {
#ifdef ARDUINOPICOSERIAL
picowrite(c);
#else
/* write never blocks. discard any bytes we can't get rid of */
SERIALPORT.write(c);
/* if (Serial.availableForWrite()>0) Serial.write(c); */
#endif
}
/* check on a character, needed for breaking */
char serialcheckch() {
#ifdef ARDUINOPICOSERIAL
return picochar;
#else
if (SERIALPORT.available()) return SERIALPORT.peek(); else return 0; // should really be -1
#endif
}
/* avail method, needed for AVAIL() */
uint16_t serialavailable() {
#ifdef ARDUINOPICOSERIAL
return picoi;
#else
return SERIALPORT.available();
#endif
}
/* flush serial */
void serialflush() {
#ifdef ARDUINOPICOSERIAL
return;
#else
while (SERIALPORT.available()) SERIALPORT.read();
#endif
}
/* the serial ins */
uint16_t serialins(char *b, uint16_t nb) {
#ifdef ARDUINOPICOSERIAL
return picoins(b, nb);
#else
return consins(b, nb);
#endif
}
/*
* Start a secondary serial port for printing and/or networking
* This is either Serial1 on a MEGA or DUE or Nano Every or a SoftwareSerial
* instance
*/
#ifdef ARDUINOPRT
#if !defined(ARDUINO_AVR_MEGA2560) && !defined(ARDUINO_SAM_DUE) && !defined(ARDUINO_AVR_NANO_EVERY) \
&& !defined(ARDUINO_NANO_RP2040_CONNECT) && !defined(ARDUINO_RASPBERRY_PI_PICO) \
&& !defined(ARDUINO_SEEED_XIAO_M0) && !defined(ARDUINO_ARCH_RENESAS) &&! defined(ARDUINO_ARCH_MBED)
#include <SoftwareSerial.h>
SoftwareSerial PRTSERIAL(SOFTSERIALRX, SOFTSERIALTX);
#endif
/* second serial port */
void prtbegin() {
PRTSERIAL.begin(serial1_baudrate);
}
/* the open functions are not needed here */
char prtopen(char* filename, uint16_t mode) {}
void prtclose() {}
uint8_t prtstat(uint8_t c) {
if (c == 0) return 1;
if (c == 1) return serial1_baudrate;
return 0;
}
void prtwrite(char c) {
PRTSERIAL.write(c);
}
char prtread() {
while (!PRTSERIAL.available()) byield();
return PRTSERIAL.read();
}
char prtcheckch() {
if (PRTSERIAL.available()) return PRTSERIAL.peek(); else return 0;
}
uint16_t prtavailable() {
return PRTSERIAL.available();
}
void prtset(uint32_t s) {
serial1_baudrate=s;
prtbegin();
}
uint16_t prtins(char* b, uint16_t nb) {
if (blockmode > 0) return inb(b, nb); else return consins(b, nb);
}
#endif
/*
* The wire code, direct access to wire communication
* in master mode wire_slaveid is the I2C address bound
* to channel &7 and wire_myid is 0
* in slave mode wire_myid is the devices slave address
* and wire_slaveid is 0
* ARDUINOWIREBUFFER is the maximum length of meesages the
* underlying library can process. This is 32 for the Wire
* library
*/
/* this is always here, if Wire is needed by a subsysem, full wire always loads the Arduino wire library */
#if defined(HASWIRE)
/* default begin is as a master
* This doesn't work properly on the ESP32C3 platform
* See https://github.com/espressif/arduino-esp32/issues/6376
* for more details
*/
void wirebegin() {
#ifndef SDA_PIN
Wire.begin();
#else
Wire.begin(SDA_PIN, SCL_PIN);
#endif
}
#endif
/* this is code needed for the OPEN/CLOSE and wireslave mechanisms */
#if defined(HASWIRE)
uint8_t wire_slaveid = 0;
uint8_t wire_myid = 0;
#define ARDUINOWIREBUFFER 32
#ifdef ARDUINOWIRESLAVE
char wirereceivebuffer[ARDUINOWIREBUFFER];
short wirereceivechars = 0;
char wirerequestbuffer[ARDUINOWIREBUFFER];
short wirerequestchars = 0;
#endif
void wireslavebegin(uint8_t s) {
#ifndef SDA_PIN
Wire.begin(s);
#else
Wire.begin(SDA_PIN, SCL_PIN, s);
#endif
}
/* wire status - just checks if wire is compiled */
uint8_t wirestat(uint8_t c) {
switch (c) {
case 0:
return 1;
#ifdef ARDUINOWIRESLAVE
case 1:
return wirerequestchars;
#endif
default:
return 0;
}
}
/* available characters - test code ecapsulation prep for slave*/
uint16_t wireavailable() {
/* as a master we return 1, as a slave the received chars*/
if (wire_myid == 0) return 1;
#ifdef ARDUINOWIRESLAVE
else return wirereceivechars;
#else
else return 0;
#endif
}
#ifdef ARDUINOWIRESLAVE
/* event handler for receive */
void wireonreceive(int h) {
wirereceivechars=h;
if (h>ARDUINOWIREBUFFER) h=ARDUINOWIREBUFFER;
for (int i=0; i<h; i++) wirereceivebuffer[i]=Wire.read();
}
/* event handler for request, deliver the message and forget the buffer */
void wireonrequest() {
Wire.write(wirerequestbuffer, wirerequestchars);
wirerequestchars=0;
}
#endif
/*
* as a master open sets the slave id for the communication
* no extra begin while we stay master
*/
void wireopen(uint8_t s, uint8_t m) {
if (m == 0) {
wire_slaveid=s;
/* we have been a slave and become master, restart wire*/
if (wire_myid != 0) {
wirebegin();
wire_myid=0;
}
} else if ( m == 1 ) {
wire_myid=s;
wire_slaveid=0;
#ifdef ARDUINOWIRESLAVE
wireslavebegin(wire_myid);
Wire.onReceive(&wireonreceive);
Wire.onRequest(&wireonrequest);
#endif
}
}
/* input an entire string */
uint16_t wireins(char *b, uint8_t l) {
uint16_t z;
if (wire_myid == 0) {
z=0;
if (l>ARDUINOWIREBUFFER) l=ARDUINOWIREBUFFER;
if (!Wire.requestFrom(wire_slaveid, l)) ioer=1;
while (Wire.available() && z<l) b[++z]=Wire.read();
} else {
#ifdef ARDUINOWIRESLAVE
for (z=0; z<wirereceivechars && z<l; z++) b[z+1]=wirereceivebuffer[z];
wirereceivechars=0; /* clear the entire buffer on read, bytewise read not really supported */
#endif
}
b[0]=z;
return z;
}
/* just a helper to make GET work, wire is string oriented */
char wireread() {
char wbuffer[2];
if (wireins(wbuffer, 1)) return wbuffer[1]; else return 0;
}
/* send an entire string - truncate radically */
void wireouts(char *b, uint8_t l) {
int16_t z;
if (l>ARDUINOWIREBUFFER) l=ARDUINOWIREBUFFER;
if (wire_myid == 0) {
Wire.beginTransmission(wire_slaveid);
#ifdef ARDUINO_ARCH_ESP32
for(z=0; z<l; z++) Wire.write(b[z]);
#else
Wire.write(b, l);
#endif
ioer=Wire.endTransmission();
} else {
#ifdef ARDUINOWIRESLAVE
for (int i=0; i<l; i++) wirerequestbuffer[i]=b[i];
wirerequestchars=l;
#endif
}
}
#endif
/* plain wire without FILE I/O functions */
#if defined(HASSIMPLEWIRE) || defined(HASWIRE)
/* AVR boards can be handled with a smaller Wire implementation, this
should go into a separate library soon */
#if defined(ARDUINODIRECTI2C)
/* a few constants */
uint32_t const F_TWI = 100000L;
uint8_t const TWSR_MTX_DATA_ACK = 0x28;
uint8_t const TWSR_MTX_ADR_ACK = 0x18;
uint8_t const TWSR_MRX_ADR_ACK = 0x40;
uint8_t const TWSR_START = 0x08;
uint8_t const TWSR_REP_START = 0x10;
uint8_t const I2C_READ = 1;
uint8_t const I2C_WRITE = 0;
/* counts the bytes to be read, in this implementation not used, kept for later */
uint8_t wirecount;
/* start the bus and set the frequency in TWBR */
void wirebegin() {
digitalWrite(SDA, HIGH);
digitalWrite(SCL, HIGH);
TWSR = 0;
TWBR = (F_CPU/F_TWI - 16)/2;
}
/* a generic start function for read and write*/
void wirestart (uint8_t port, uint8_t n) {
/* form the right address*/
port=port<<1;
/* remember the requested bytes and go to read mode if read */
if (n) {
wirecount=n;
port |= I2C_READ;
}
/* start the bus and wait */
TWCR = 1<<TWINT | 1<<TWSTA | 1<<TWEN;
while (!(TWCR & 1<<TWINT));
/* check the status */
if ((TWSR & 0xF8) != TWSR_START && (TWSR & 0xF8) != TWSR_REP_START) { ioer=1; return; }
/* send the address and direction */
TWDR = port;
TWCR = 1<<TWINT | 1<<TWEN;
while (!(TWCR & 1<<TWINT));
/* check the status */
if (port & I2C_READ) {
if ((TWSR & 0xF8) == TWSR_MRX_ADR_ACK) ioer=0; else ioer=1;
} else {
if ((TWSR & 0xF8) == TWSR_MTX_ADR_ACK) ioer=0; else ioer=1;
}
}
/* stop, return value just a formality, error handling is done differently*/
uint8_t wirestop () {
TWCR = 1<<TWINT | 1<<TWEN | 1<<TWSTO;
while (TWCR & 1<<TWSTO);
return ioer;
}
/* one byte write */
void wirewritebyte(uint8_t b) {
TWDR = b;
TWCR = 1<<TWINT | 1<<TWEN;
while (!(TWCR & 1<<TWINT));
if (!((TWSR & 0xF8) == TWSR_MTX_DATA_ACK)) ioer=1;
}
/* one byte read without stopping the bus */
int16_t wirereadbyte () {
if (wirecount != 0) wirecount--;
TWCR = 1<<TWINT | 1<<TWEN | ((wirecount == 0) ? 0 : (1<<TWEA));
while (!(TWCR & 1<<TWINT));
return TWDR;
}
#else /* not ARDUINODIRECTI2C this uses the standard Wire objects but provide the same API */
/* start the Wire object*/
void wirebegin() {
#ifndef SDA_PIN
Wire.begin();
#else
Wire.begin(SDA_PIN, SCL_PIN);
#endif
}
/* initiate communication for read or write */
void wirestart(uint8_t port, uint8_t n) {
if (n) {
if (!Wire.requestFrom(port, n)) ioer=1;
} else {
Wire.beginTransmission(port);
}
}
/* stop is only explicitly used on write */
uint8_t wirestop() { return Wire.endTransmission(); }
void wirewritebyte(uint8_t data) { Wire.write(data); }
int16_t wirereadbyte() { return Wire.read(); }
#endif
#endif
/*
* Read from the radio interface, radio is always block
* oriented. This function is called from ins for an entire
* line.
*
* In blockmode the entire message is returned in the
* receiving string while in line mode the length of the
* string is adapted. Blockmode can be used to transfer
* binary data.
*/
#ifdef ARDUINORF24
/*
* definitions for the nearfield module, still very experimental
* We use the standard RF24 nearfield library
*/
const char rf24_ce = RF24CEPIN;
const char rf24_csn = RF24CSNPIN;
#include <nRF24L01.h>
#include <RF24.h>
rf24_pa_dbm_e rf24_pa = RF24_PA_MAX;
RF24 radio(rf24_ce, rf24_csn);
/* radio status */
uint8_t radiostat(uint8_t c) {
if (c == 0) return 1;
if (c == 1) return radio.isChipConnected();
return 0;
}
/* generate a uint64_t pipe address from the filename string for RF24 */
uint64_t pipeaddr(const char *f){
uint64_t t = 0;
t=(uint8_t)f[0];
for(short i=1; i<=4; i++) t=t*256+(uint8_t)f[i];
return t;
}
/* read an entire string */
uint16_t radioins(char *b, uint8_t nb) {
uint16_t z;
if (radio.available()) {
radio.read(b+1, nb);
if (!blockmode) {
for (z=0; z<nb; z++) if (b[z+1]==0) break;
} else {
z=radio.getPayloadSize();
if (z > nb) z=nb;
}
b[0]=z;
} else {
b[0]=0;
b[1]=0;
z=0;
}
return z;
}
/* radio is not character oriented, this is only added to make GET work
or single byte payloads, radio, like file is treated nonblocking here */
char radioread() {
char rbuffer[2];
if (radioins(rbuffer, 1)) return rbuffer[1]; else return 0;
}
/* write to radio, no character mode here */
void radioouts(char *b, uint8_t l) {
radio.stopListening();
if (!radio.write(b, l)) ioer=1;
radio.startListening();
}
/* radio available */
uint16_t radioavailable() {
return radio.available();
}
/*
* we always read from pipe 1 and use pipe 0 for writing,
* the filename is the pipe address, by default the radio
* goes to reading mode after open and is only stopped for
* write
*/
void iradioopen(const char *filename) {
if (!radio.begin()) ioer=1;
radio.openReadingPipe(1, pipeaddr(filename));
radio.startListening();
}
void oradioopen(const char *filename) {
if (!radio.begin()) ioer=1;
radio.openWritingPipe(pipeaddr(filename));
}
void radioset(uint8_t s) {
if ((s<0) && (s>3)) {ioer=1; return; }
rf24_pa=(rf24_pa_dbm_e) s;
radio.setPALevel(rf24_pa);
}
#else
uint8_t radiostat(uint8_t c) { return 0; }
uint64_t pipeaddr(const char *f){ return 0; }
uint16_t radioins(char *b, uint8_t nb) { b[0]=0; b[1]=0; return 0; }
void radioouts(char *b, uint8_t l) {}
uint16_t radioavailable() { return 0; }
void iradioopen(const char *filename) {}
void oradioopen(const char *filename) {}
void radioset(uint8_t s) {}
#endif
/*
* Arduino Sensor library code
* The sensorread() is a generic function called by
* SENSOR basic function and command. The first argument
* is the sensor and the second argument the value.
* sensorread(n, 0) checks if the sensorstatus.
*/
#ifdef ARDUINOSENSORS
#ifdef ARDUINODHT
#include "DHT.h"
DHT dht(DHTPIN, DHTTYPE);
#endif
#ifdef ARDUINOSHT
#include <SHT3x.h>
SHT3x SHT;
#endif
#ifdef ARDUINOMQ2
#include <MQ2.h>
MQ2 mq2(MQ2PIN);
#endif
#ifdef ARDUINOLMS6
#include <Arduino_LSM6DSOX.h>
/* https://www.arduino.cc/en/Reference/Arduino_LSM6DSOX */
#endif
#ifdef ARDUINOAHT
#include <Adafruit_AHTX0.h>
Adafruit_AHTX0 aht;
#endif
#ifdef ARDUINOBMP280
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
#endif
#ifdef ARDUINOBME280
#include <Adafruit_BME280.h>
Adafruit_BME280 bme;
/* add your own code here */
#endif
void sensorbegin(){
#ifdef ARDUINODHT
dht.begin();
#endif
#ifdef ARDUINOSHT
SHT.Begin();
#endif
#ifdef ARDUINOMQ2
mq2.begin();
#endif
#ifdef ARDUINOAHT
aht.begin();
#endif
#ifdef ARDUINOBMP280
bmp.begin();
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
#endif
#ifdef ARDUINOBME280
bme.begin();
#endif
}
float sensorread(uint8_t s, uint8_t v) {
switch (s) {
case 0:
return analogRead(A0+v);
case 1:
#ifdef ARDUINODHT
switch (v) {
case 0:
return 1;
case 1:
return dht.readHumidity();
case 2:
return dht.readTemperature();
}
#endif
return 0;
case 2:
#ifdef ARDUINOSHT
switch (v) {
case 0:
return 1;
case 1:
SHT.UpdateData();
return SHT.GetRelHumidity();
case 2:
SHT.UpdateData();
return SHT.GetTemperature();
}
#endif
return 0;
case 3:
#ifdef ARDUINOMQ2
switch (v) {
case 0:
return 1;
case 1:
(void) mq2.read(false);
return mq2.readLPG();;
case 2:
(void) mq2.read(false);
return mq2.readCO();
case 3:
(void) mq2.read(false);
return mq2.readSmoke();
}
#endif
return 0;
case 4:
#ifdef ARDUINOAHT
sensors_event_t humidity, temp;
switch (v) {
case 0:
return 1;
case 1:
aht.getEvent(&humidity, &temp);
return temp.temperature;
case 2:
aht.getEvent(&humidity, &temp);
return humidity.relative_humidity;
}
#endif
return 0;
case 5:
#ifdef ARDUINOBMP280
switch (v) {
case 0:
return 1;
case 1:
return bmp.readTemperature();
case 2:
return bmp.readPressure() / 100.0;
case 3:
return bmp.readAltitude(1013.25);
}
#endif
return 0;
case 6:
#ifdef ARDUINOBME280
switch (v) {
case 0:
return 1;
case 1:
return bme.readTemperature();
case 2:
return bme.readPressure() / 100.0;
case 3:
return bme.readAltitude(1013.25);
case 4:
return bme.readHumidity();
}
#endif
/* add your own sensor code here */
return 0;
default:
return 0;
}
}
#else
void sensorbegin() {}
float sensorread(uint8_t s, uint8_t v) {return 0;};
#endif
/*
* event handling wrappers, to keep Arduino specifics out of BASIC
*/
#ifdef ARDUINOINTERRUPTS
uint8_t pintointerrupt(uint8_t pin) { return digitalPinToInterrupt(pin); }
void attachinterrupt(uint8_t interrupt, void (*f)(), uint8_t mode) { attachInterrupt(interrupt, f, (PinStatus) mode); };
void detachinterrupt(uint8_t pin) { detachInterrupt(digitalPinToInterrupt(pin)); };
#else
uint8_t pintointerrupt(uint8_t pin) { return 0; }
void attachinterrupt(uint8_t interrupt, void (*f)(), uint8_t mode) { };
void detachinterrupt(uint8_t pin) { };
#endif
/*
* Experimental code to drive SPI SRAM
*
* Currently only the 23LCV512 is implemented, assuming a
* 64kB SRAM
* The code below is taken in part from the SRAMsimple library
*
* two buffers are implemented:
* - a ro buffer used by memread, this buffer is mainly reading the token
* stream at runtime.
* - a rw buffer used by memread2 and memwrite2, this buffer is mainly accessing
* the heap at runtime. In interactive mode this is also the interface to read
* and write program code to memory
*
*/
#ifdef ARDUINOSPIRAM
/*
* we use the standard slave select pin as default
* RAMPIN
*/
#ifndef RAMPIN
#define RAMPIN SS
#endif
#define SPIRAMWRMR 1
#define SPIRAMRDMR 5
#define SPIRAMREAD 3
#define SPIRAMWRITE 2
#define SPIRAMRSTIO 0xFF
#define SPIRAMSEQ 0x40
#define SPIRAMBYTE 0x00
/* the RAM begin method sets the RAM to sequential mode */
uint16_t spirambegin() {
pinMode(RAMPIN, OUTPUT);
digitalWrite(RAMPIN, LOW);
SPI.transfer(SPIRAMRSTIO);
SPI.transfer(SPIRAMWRMR);
//SPI.transfer(SPIRAMBYTE);
SPI.transfer(SPIRAMSEQ);
digitalWrite(RAMPIN, HIGH);
return 65535;
}
/* the simple unbuffered byte read, with a cast to signed 8 bit integer */
int8_t spiramrawread(uint16_t a) {
uint8_t c;
digitalWrite(RAMPIN, LOW);
SPI.transfer(SPIRAMREAD);
SPI.transfer((uint8_t)(a >> 8));
SPI.transfer((uint8_t)a);
c = SPI.transfer(0x00);
digitalWrite(RAMPIN, HIGH);
return c;
}
/* one read only buffer for access from the token stream
memread calls this */
uint16_t spiram_robufferaddr = 0;
uint8_t spiram_robuffervalid=0;
const uint8_t spiram_robuffersize = 32;
int8_t spiram_robuffer[spiram_robuffersize];
/* one rw buffer for access to the heap and all the program editing
memread2 and memwrite2 call this*/
uint16_t spiram_rwbufferaddr = 0;
uint8_t spiram_rwbuffervalid=0;
const uint8_t spiram_rwbuffersize = 32; /* also change the addressmask if you want to play here */
int8_t spiram_rwbuffer[spiram_rwbuffersize];
uint8_t spiram_rwbufferclean = 1;
const uint16_t spiram_addrmask=0xffe0;
/* const address_t spiram_addrmask=0xffd0; // 64 byte frame */
/* the elementary buffer access functions used almost everywhere */
void spiram_bufferread(uint16_t a, int8_t* b, uint16_t l) {
digitalWrite(RAMPIN, LOW);
SPI.transfer(SPIRAMREAD);
SPI.transfer((uint8_t)(a >> 8));
SPI.transfer((uint8_t)a);
SPI.transfer(b, l);
digitalWrite(RAMPIN, HIGH);
}
void spiram_bufferwrite(uint16_t a, int8_t* b, uint16_t l) {
digitalWrite(RAMPIN, LOW);
SPI.transfer(SPIRAMWRITE);
SPI.transfer((uint8_t)(a >> 8));
SPI.transfer((uint8_t)a);
SPI.transfer(b, l);
digitalWrite(RAMPIN, HIGH);
}
int8_t spiram_robufferread(uint16_t a) {
/* we address a byte known to the rw buffer, then get it from there */
if (spiram_rwbuffervalid && ((a & spiram_addrmask) == spiram_rwbufferaddr)) {
return spiram_rwbuffer[a-spiram_rwbufferaddr];
}
/* page fault, we dont have the byte in the ro buffer, so read from the chip*/
if (!spiram_robuffervalid || a >= spiram_robufferaddr + spiram_robuffersize || a < spiram_robufferaddr ) {
spiram_bufferread(a, spiram_robuffer, spiram_robuffersize);
spiram_robufferaddr=a;
spiram_robuffervalid=1;
}
return spiram_robuffer[a-spiram_robufferaddr];
}
/* flush the buffer */
void spiram_rwbufferflush() {
if (!spiram_rwbufferclean) {
spiram_bufferwrite(spiram_rwbufferaddr, spiram_rwbuffer, spiram_rwbuffersize);
spiram_rwbufferclean=1;
}
}
int8_t spiram_rwbufferread(uint16_t a) {
/* page fault, do read, ignore the ro buffer buffer */
uint16_t p=a & spiram_addrmask;
if (!spiram_rwbuffervalid || (p != spiram_rwbufferaddr)) {
/* flush the buffer if needed */
spiram_rwbufferflush();
/* and reload it */
spiram_bufferread(p, spiram_rwbuffer, spiram_rwbuffersize);
spiram_rwbufferaddr=p; /* we only handle full pages here */
spiram_rwbuffervalid=1;
}
return spiram_rwbuffer[a-spiram_rwbufferaddr];
}
/* the buffered file write */
void spiram_rwbufferwrite(uint16_t a, int8_t c) {
uint16_t p=a&spiram_addrmask;
/* correct the two buffers if needed */
if (spiram_robuffervalid && a >= spiram_robufferaddr && a < spiram_robufferaddr + spiram_robuffersize) {
spiram_robuffer[a-spiram_robufferaddr]=c;
}
/* if we have the frame, write it, mark the buffer dirty and return */
if (spiram_rwbuffervalid && p == spiram_rwbufferaddr) {
spiram_rwbuffer[a-spiram_rwbufferaddr]=c;
spiram_rwbufferclean=0;
return;
}
/* if we end up here the write was impossible because we dont have the frame, so flush and then get it */
spiram_rwbufferflush();
(void) spiram_rwbufferread(a);
spiram_rwbuffer[a-spiram_rwbufferaddr]=c;
spiram_rwbufferclean=0;
}
/* the simple unbuffered byte write, with a cast to signed char */
void spiramrawwrite(uint16_t a, int8_t c) {
digitalWrite(RAMPIN, LOW);
SPI.transfer(SPIRAMWRITE);
SPI.transfer((uint8_t)(a >> 8));
SPI.transfer((uint8_t)a);
SPI.transfer((uint8_t) c);
digitalWrite(RAMPIN, HIGH);
/* also refresh the ro buffer if in the frame */
if (a >= spiram_robufferaddr && a < spiram_robufferaddr + spiram_robuffersize && spiram_robufferaddr > 0)
spiram_robuffer[a-spiram_robufferaddr]=c;
/* and the rw buffer if needed) */
if (a >= spiram_rwbufferaddr && a < spiram_rwbufferaddr + spiram_rwbuffersize && spiram_rwbufferaddr > 0)
spiram_rwbuffer[a-spiram_rwbufferaddr]=c;
}
#endif
#if defined(USEMEMINTERFACE)
/*
* to handle strings in situations with a memory interface two more buffers are
* needed they store intermediate results of string operations. The buffersize
* limits the maximum string length indepents of how big strings are set
*
* default is 128, on an MEGA 512 is possible
*/
#ifdef ARDUINO_AVR_MEGA2560
#define SPIRAMSBSIZE 512
#else
#define SPIRAMSBSIZE 128
#endif
/* the string buffers of the memory interface */
char spistrbuf1[SPIRAMSBSIZE];
char spistrbuf2[SPIRAMSBSIZE];
#endif
|