Simulator

I eventually want to work with the attiny 412 for a future project so the goal for this week is to get more familiarity with the attinys in preparation for that. So sadly on wokwi they did not have attiny 412 so I used the attiny 85 instead. I found a library TinyLED4K to allow me to draw on a screen. How you draw on the screen is you send data one byte at a time which will set the 8 pixels below your cursor on and off. So you can scan over the screen in 8 pixel lines to draw, and just to prove to myself that I knew how to update the screen I made a little pong simulator.

                    
#include 
#include 

struct ballPos {
    uint8_t x = 16;
    uint8_t y = 0;
    int8_t velocityX = 1;
    int8_t velocityY = 1;
} ;

struct leftPaddlePos {
    uint8_t x = 8;
    uint8_t y = 0;
    uint8_t width = 2;
    uint8_t height = 10;
    int8_t velocity = 0;
};

struct rightPaddlePos {
    uint8_t x = 120;
    uint8_t y = 0;
    uint8_t width = 2;
    uint8_t height = 10;
    int8_t velocity = 0;
};

rightPaddlePos rp;
ballPos bp;
leftPaddlePos lp;



bool isPixelOn(uint8_t x, uint8_t y)
{
    bool isRightPaddle = (x >= rp.x) && x <= rp.x + rp.width && y >= rp.y && y >= rp.y && y <= rp.y + rp.height;
    bool isLeftPaddle = x >= lp.x && x <= lp.x + lp.width && y >= lp.y && y <= lp.y + lp.height && y >= lp.y;
    bool isBall = x == bp.x && y == bp.y;
    return isRightPaddle || isLeftPaddle || isBall;
}
void updateScene()
{
    if (bp.x >= rp.x && bp.x <= rp.x + rp.width && bp.y >= rp.y && bp.y >= rp.y && bp.y <= rp.y + rp.height)
    {
    bp.velocityX *= -1;
    bp.velocityY = rp.velocity;
    }
    if (bp.x >= lp.x && bp.x <= lp.x + lp.width && bp.y >= lp.y && bp.y >= lp.y && bp.y <= lp.y + lp.height)
    {
    bp.velocityX *= -1;
    bp.velocityY = lp.velocity;
    }

    if (bp.y < 0 || bp.y > 63)
    {
    bp.velocityY *= -1;
    }
    bp.x += bp.velocityX;
    bp.y += bp.velocityY;
    if (bp.velocityX > 0)
    {
    if(bp.y > rp.y + rp.height)
    {
        rp.velocity = 1;
    }
    else if (bp.y < rp.y)
    {
        rp.velocity = -1;
    }
    else 
    {
        rp.velocity = 0;
    }
    rp.y += rp.velocity;
    }
    else
    {
    if(bp.y > lp.y + lp.height)
    {
        lp.velocity = 1;
    }
    else if (bp.y < lp.y)
    {
        lp.velocity = -1;
    }
    else 
    {
        lp.velocity = 0;
    }
    lp.y += lp.velocity;
    }
}
void renderScene()
{

    for (uint8_t y = 0; y < 8; y++) {
    oled.setCursor(0, y);
    oled.startData();
    for (uint8_t x=0; x<128; x++) {
        uint8_t binNum = 0;
        for (uint8_t k = 0; k < 8; k++)
        {
        if(isPixelOn(x, y*8 + k))
        binNum |= 1 << k;
        }
        oled.sendData(binNum);
        // oled.sendData(0b01010101);
    }
    oled.endData();
    }
    
    
}

// ============================================================================

void setup() {

    oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);

    oled.clear();
    renderScene();
    oled.on();
}

void loop() {
    for (int i = 0; i < 5; i++)
    {
    updateScene();
    }
    
    renderScene();

    delay(10);
}
                        
                    

Embedded Programming

Now that I got a program up and running on the attiny on simulator I wanted to work with a physical attiny.

Uploading Code to the Attiny 412

So in order to program the Attiny I needed to use and arduino as programmer, so I had to upload the code from the jtag2updi repository to my arduino. After that I put a 10uf capacitor between Reset and Ground. Then hooked up pin 6 on the arduino to the UPDI pin on my Attiny with a 4.7k resistor as well as connect VCC to 5V and GND to GND. In order to work with Attiny's you need to download and install the megaTinyCore repo this library will allow you to program the attinys using Arduino IDE. Once I had set up all of these steps I made a simple blink sketch and uploaded the code to my attiny (you go to sketch and click the upload using programmer button)

One sidenote is that the actual recommended method for uploading code to the attiny from my research was to use pyupdi and upload just using a ftdi connector, but I was travelling this week so I had to use the method listed above

Embedded Programming Failures

I could not get my OLED to light up at all, and I think the biggest pitfall was I did not bring a FTDI connector with me and I could not figure out how to see the serial output of the microcontroller without it. I felt like I was programming blind. All I brought with me were some capacitors and some LEDs so I used the LEDs to indicate that my microcontroller was plugged in and confirm that I uploaded a new sketch, but besides that I could not extract anymore information from my microcontroller which made it hard to progress.