diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7db5ae4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +*.xcodeproj diff --git a/SCTVcode/SCTVcode.ino b/SCTVcode/SCTVcode.ino index 992d4d1..967f629 100644 --- a/SCTVcode/SCTVcode.ino +++ b/SCTVcode/SCTVcode.ino @@ -43,7 +43,7 @@ // V 1.1.1 05/14/22 DF Adding SCT-A board with Teensy 4.1, different I/O pins // V 1.2.0 12/14/22 DF Updated USB host library local copy -char versionNo[] = "Version 1.2.0\n"; +char versionNo[] = "Version 1.2.0sf\n"; // THINGS TO DO @@ -111,7 +111,7 @@ char versionNo[] = "Version 1.2.0\n"; // SCTV-E up has Teensy 4.1 and SPI DAC, SCTV-D down use Teensy 3.6 with internal DAC // Comment out the line below if compiling for board with Teensy 3.6 (SCTV-A..D) -#define SCTVE +// #define SCTVE // The SCT has the Teensy 4.1 and external DAC, it also has different pins // defined for many functions. diff --git a/SCTVcode/te_etchasketch.ino b/SCTVcode/te_etchasketch.ino new file mode 100644 index 0000000..e46ae70 --- /dev/null +++ b/SCTVcode/te_etchasketch.ino @@ -0,0 +1,90 @@ +// ------------------------------ Etch-A-Sketch ------------------------------- + +struct Point { + int x; + int y; +}; + +const int MAX_SEGMENTS = 1000; +const int END_TOKEN = -9999; + +Point* segments; +Point* segment; +Point direction; +int x, y; + +struct item etchList[] = {}; + +void getControlsXY() { + // read the position controls for X,Y, and average for smooth readings + for (int i = 0; i < 40; i++) { + x += analogRead(XPosPin) - 512; // read position controls + y += analogRead(YPosPin) - 512; // make bipolar so midpoint is nominal + } + // Center in the display area, scale for correct overshoot + x = x / 20; + y = y / 20; +} + +void resetEtchASketch() { + if (segments == 0) { + segments = (Point*)malloc(sizeof(Point) * (MAX_SEGMENTS+2)); + } + direction.x = 0; + direction.y = 0; + + getControlsXY(); + segment = segments; + segment[0].x = x; + segment[0].y = y; + segment[1].x = END_TOKEN; +} + +void etchASketch() { + if (pushed) { + // selector button pushed: erase all traces + resetEtchASketch(); + pushed = false; + } + + // read X,Y control knobs and extend trace segments to it + getControlsXY(); + int dx = x - segment->x; + int dy = y - segment->y; + int ds = sqrt((dx*dx + dy*dy) * 64); + if (ds != 0) { + // Serial.printf("Etch: xy=(%d,%d), ds=%d\n", x, y, ds); + // int n = segment - segments; + // Serial.printf(" dir=(%d,%d) %d segs, (%d,%d)-(%d,%d)\n", + // direction.x, direction.y, n, segment[0].x, segment[0].y, x, y); + // normalized direction vector for this segment + dx = 64 * dx / ds; + dy = 64 * dy / ds; + int diff = abs(dx - direction.x) + abs(dy - direction.y); + if (diff > 2 && segment < segments + MAX_SEGMENTS) { + // have changed direction: start new segment + // Serial.printf(" dx,dy=(%d,%d), ds=%d\n", dx, dy, ds); + direction.x = dx; + direction.y = dy; + segment++; + segment[0].x = x; + segment[0].y = y; + segment[1].x = END_TOKEN; + } else { + // else just extend last segment + segment->x = x; + segment->y = y; + } + } + + // draw current list of trace segments + Scale = 1; + Shape = lin; + for (Point* seg = segments; seg[1].x != END_TOKEN; seg++) { + XStart = seg[0].x; + YStart = seg[0].y; + XEnd = seg[1].x; + YEnd = seg[1].y; + DoSeg(); // draw a line from (XStart,YStart) to (XEnd,YEnd) + } +} diff --git a/SCTVcode/w_menus.ino b/SCTVcode/w_menus.ino index 83c9b8d..8c7673a 100644 --- a/SCTVcode/w_menus.ino +++ b/SCTVcode/w_menus.ino @@ -83,7 +83,7 @@ struct item locMenu[] = { // This is the clock display option list // If Clock is zero; then draw hands -const int NClks = 8; // number of clock faces to choose from (splash doesn't count) +const int NClks = 9; // number of clock faces to choose from (splash doesn't count) // list of clock face draw lists item * ClkList[] = @@ -97,6 +97,7 @@ item * ClkList[] = time6dList, // 6 digit digital clock with date haikuList, // some poetry at random // flwList, // four letter words at random + etchList, // Etch-A-Sketch splashList, // splash screen vanishes when knob touched 0}; // -------------------------- Menu navigation ----------------------- diff --git a/SCTVcode/z_main.ino b/SCTVcode/z_main.ino index aa8fd5c..f467fb0 100644 --- a/SCTVcode/z_main.ino +++ b/SCTVcode/z_main.ino @@ -146,8 +146,8 @@ void loop() myGps.encode(userial.read()); } - if ((theClock != 1) && (theClock != 2)) // Pong and Tetris use position controls as paddles - { + if ((theClock != 1) && (theClock != 2) && (theClock != 8)) // Pong, Tetris, and Etch-A-Sketch + { // use position controls as paddles xPos = yPos = 0; for (i=0;i<40;i++) { xPos += analogRead(XPosPin) - 512; // read the position controls @@ -189,17 +189,23 @@ void loop() { reset_tetris(); } + if (theClock == 8) + { + resetEtchASketch(); + } EncDir = 0; } whichList = ClkList[theClock]; // point to the clock drawlist we are displaying now if (theClock == 0) DrawClk(); // clock 0 has hands to draw if (theClock == 1) doPong(); // clock 1 is Pong if (theClock == 2) drawTetris(); // clock 2 is Tetris - if (pushed) + if (theClock == 8) etchASketch(); // clock 8 is Etch-A-Sketch + if (theClock != 8 && pushed) { whichList = mainMenu; HotItem = 1; InMenu = true; + Serial.printf("Button pushed. theClock=%d\n", theClock); } }