• Putting the kitty to sleep

    marble09/24/2018 at 19:15 0 comments

    One of the features that was missing in my firmware was the sleep/power save mode. Since I already spent all my GPIOs on the LEDs and the sensor, I had to build a logic with the reset button.

    The solution I found was to use the EEPROM.

    When the AVR resets, it reads the first byte from the EEPROM, negates it, and writes it back. When the value read is 1/true, the program runs. When it is not, the MCU is put to sleep.

    All of this logic was simply put at the top of the main function body.

    #include <avr/interrupt.h>
    #include <avr/eeprom.h> 
    #include <avr/sleep.h> 
    
    int main(void) {
        /* When reset, read whether program shall be run */
        uint8_t run = eeprom_read_byte (EEPROM_ADDR);
        /* invert value and write back to eeprom */
        eeprom_write_byte (EEPROM_ADDR, run ? 0 : 1);
        if(!run) {
            /* disable all interrupts and go to sleep */
            cli();
            sleep_mode();
        }
        while(1) {
            /* control loop code here */
        }
    } 
    

  • Writing some Code

    marble09/24/2017 at 15:53 0 comments

    First I wrote some simple C programs that made sure that all the connections to the LEDs worked and a little program that displayed one color when the sensor value is low and another when it is high. I already put parts of different concern in separate C files for later inconvenience.

    I wanted to keep the functionality that the beating of the cat changed the mode. This results in a very simple state machine with the sensor ping going low and high again as the only type of transition. That I made into an enum variable which is incremented in a interrupt service function.

    The problem in the first place was that there was the need for smooth color transitions. to accomplish that I went for an implementation of a sinusoidal HSB fade.

    To accomplish that on an Attiny in a fast and efficient manner I chose to use a lookup table which generated on python. Because right now I went with only doing 8 bit software PWM I chose an amplitude of 255.

    from math import cos, pi
    LUT_LEN = 512
    LUT_AMP = 2**8-1
    lut = [int(cos(i*pi/2/LUT_LEN)*LUT_AMP) for i in range(0,LUT_LEN)]
    print(lut)

     The printout can be copy pasted into C code.

    The result right now looks like this. The striped one can only see on camera of course due to aliasing.

  • Bridging the Charge Status and Installing a new Controller

    marble09/24/2017 at 15:11 0 comments

    The original controller get's the charge status (charging or not charging) via a pin and accordingly makes an LED blink. The substitution for that is a wire bridging the ingoing and outgoing ping, resulting in the charge status LED glowing when the cat charges.

    Because the original controller has no IC markings and I couldn't determine the type, I decided to desolder it and replace it with an Attiny85. I soldered the DIP-8 version of this controller onto a double sided prototyping board and filled and wired the holes in the middle in a way that I can program it with the use of pogo pins. Then I hot glued it onto a place on the PCB where the LEDs wouldn't cast a shadow.

    The last step was to wire the power traces to the power pins, the RGBW traces to PB0..3 and the sensor trace to PB4.

  • Reversing

    marble09/23/2017 at 21:26 0 comments

    The cat is a LED lamp with a lithium battery cell that is to be charged via a micro USB port. With the push of a button next to micro USB port the cat can be turned on and off.

    By beating the body or the base of the cat, the mode is changed. The Modes are "warm white light on", "RGB color steps" and "LEDs off". The color steps are very annoying and the goal of this project was to replace this with pleasant color fading.

    On the base of the lamp is the main PCB which is single sided and holds basic components for battery charging and protection, the main IC which does all the smarts, some analog circuitry for the "beating sensor", transistors and resistors for the LEDs, and the LEDs themselves - four warm white ones and four RGB ones. Each color is connected in parallel.

    The beating sensor is a piezo element which is held in the middle of the base.

    Under the main PCB is the battery and an interfacing PCB which is connected via a 4 leads cable and holds the USB port, a switch and a LED which blinks when charged.

    The main IC does everything. It has contacts going in for the power, the charge status and the sensor and going out for the four colors and the charging status LED. Some pins are not used. Here you can see the footprint after removal and cleanup.

    In the picture the upper left pin is pin 1.

    1NCNC14
    2Charge Status INNC13
    3Piezo INCharge Status LED OUT12
    4VccGND11
    5Warm White OUTBlue OUT10
    6Button INRed OUT9
    7NCGreen OUT8