Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: which part of this switch/case function determines each cases run-length?

  1. #1
    Junior Member NeTgHoSt's Avatar
    Join Date
    May 2020
    Location
    California
    Posts
    13

    which part of this switch/case function determines each cases run-length?

    https://youtu.be/W9AQagplJAE

    Code:
    void draw(void) {
      switch (draw_state >> 3) {
        case 0: drawRunAnim(draw_state & 7); break;
        case 1: drawIronmanLogo(draw_state & 7); break;
      }
    }
    I borrowed some code for my projects experimenting with an SSD1306 i2C display from the u8g2 "Graphics Test" example, some of which uses this advanced (for me) switch/case function to call each graphic test function.

    My code just draws two alternating xmp bitmaps, one a still image and the other a 5 frame "animation." They scroll across the screen as they alternate.

    I deleted case 3-9 because they aren't needed yet. What I don't understand still, is the (draw_state & 7) parameters for each case. Isn't the "&" symbol for pointers or something? Changing the 7 to a higher number did not increase the time the drawIronmanLogo function ran.. so, what is it? Also, the >> 3 bit shift, what the heck does it do??? That number doesn't seem to respond well to adjustment, either. Forgive my ignorance if I should know these things already, but I only learned how to count in binary and hex yesterday, finally. Here is the full code. Note that some stuff such as the buzzer library aren't being used yet.

    Code:
    #include <U8g2lib.h>
    #include <SPI.h>
    #include <Wire.h>
    #include "Buzzer.h"
    
    //Compiler / Global Defines
    #define BUZZER_PIN_SIG 6  // Beeper pin
    #define battLevelPin A9   // Battery reading input
    #define battCheckButton 2 //Battery level check button
    int led = 13;
    
    //Sensor and Display Connection Setup etc.
    U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
    Buzzer buzzer(BUZZER_PIN_SIG);
    
    #define ironman_width 44
    #define ironman_height 64
    static const unsigned char ironman_bits[] PROGMEM = {
      0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x3f, 0x00, 0x00,
      0x00, 0xf8, 0xff, 0xff, 0x01, 0x00, 0x00, 0x86, 0xff, 0x1f, 0x06, 0x00,
      0x80, 0x81, 0xff, 0x1f, 0x18, 0x00, 0xc0, 0x80, 0xff, 0x1f, 0x30, 0x00,
      0x20, 0x80, 0xff, 0x1f, 0xc0, 0x00, 0x10, 0x00, 0xff, 0x0f, 0x80, 0x01,
      0x08, 0x00, 0xff, 0x0f, 0x00, 0x01, 0x08, 0x00, 0xff, 0x0f, 0x00, 0x02,
      0x08, 0x00, 0xff, 0x0f, 0x00, 0x02, 0x08, 0x00, 0xfe, 0x07, 0x00, 0x02,
      0x08, 0x00, 0xfe, 0x07, 0x00, 0x02, 0x08, 0x00, 0xfe, 0x07, 0x00, 0x02,
      0x08, 0x00, 0x00, 0x00, 0x00, 0x06, 0x08, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x08, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x0c, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x0c, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x0c, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x06, 0x00, 0x00, 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x06, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xc6, 0x07, 0x00, 0x00, 0x7c, 0x0e,
      0xe6, 0xff, 0xff, 0xff, 0xff, 0x0e, 0xe6, 0xff, 0x03, 0xf8, 0xff, 0x0c,
      0x02, 0x3f, 0x00, 0x80, 0x1f, 0x0c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x0c,
      0x03, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0c,
      0x03, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0c,
      0x03, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x07, 0x00, 0x00, 0x00, 0x00, 0x06, 0x16, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x2e, 0x00, 0x00, 0x00, 0x80, 0x07, 0x6e, 0x00, 0x00, 0x00, 0x40, 0x07,
      0x6e, 0x00, 0x00, 0x00, 0x60, 0x07, 0xde, 0x00, 0x00, 0x00, 0xb0, 0x07,
      0xde, 0x00, 0x00, 0x00, 0xb0, 0x07, 0x9c, 0x01, 0x00, 0x00, 0xd8, 0x03,
      0xbc, 0x01, 0x00, 0x00, 0xf8, 0x03, 0x3c, 0x01, 0x00, 0x00, 0xe8, 0x03,
      0x7c, 0x03, 0x00, 0x00, 0xf4, 0x03, 0xfc, 0x02, 0x00, 0x00, 0xf4, 0x03,
      0xfc, 0xc2, 0xff, 0x1f, 0xfa, 0x03, 0xf8, 0x25, 0xff, 0x27, 0xfa, 0x01,
      0xf8, 0x38, 0x00, 0x60, 0xf9, 0x01, 0xf8, 0x10, 0x00, 0xc0, 0xf8, 0x01,
      0xf0, 0x00, 0x00, 0x00, 0xf8, 0x01, 0xf0, 0x00, 0x00, 0x00, 0xf8, 0x01,
      0xf0, 0x00, 0xf8, 0x00, 0xf8, 0x00, 0xe0, 0xc1, 0xff, 0x0f, 0xf8, 0x00,
      0xc0, 0xe3, 0xff, 0x1f, 0x7c, 0x00, 0x80, 0xff, 0xff, 0x7f, 0x3e, 0x00,
      0x00, 0xff, 0xff, 0x7f, 0x0f, 0x00, 0x00, 0xf6, 0x00, 0xfe, 0x07, 0x00,
      0x00, 0x3c, 0x00, 0xb0, 0x01, 0x00, 0x00, 0x08, 0x00, 0x40, 0x00, 0x00
    };
    
    #define runAnim_width 44
    #define runAnim_height 52
    static const unsigned char runAnim1_bits[] PROGMEM = {
      0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x41, 0x00,
      0x00, 0x00, 0x00, 0x80, 0xff, 0x00, 0x00, 0x00, 0x06, 0x88, 0xff, 0x01,
      0x00, 0x00, 0x0f, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x1f, 0xfe, 0xff, 0x03,
      0x00, 0x00, 0xfe, 0xff, 0xff, 0x03, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07,
      0x00, 0x00, 0xfc, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xff, 0xff, 0x81, 0x0f, 0x00, 0x00, 0xff, 0xff, 0x00, 0x0f,
      0x00, 0x00, 0xfe, 0x1f, 0x00, 0x0e, 0x00, 0x00, 0xfc, 0x0f, 0x00, 0x0c,
      0x00, 0x00, 0xfc, 0x07, 0x0c, 0x0c, 0x00, 0x00, 0xfc, 0x03, 0x0c, 0x0c,
      0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0c, 0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0f,
      0x00, 0x00, 0xfc, 0x03, 0x80, 0x07, 0x00, 0x78, 0xfe, 0x07, 0x80, 0x03,
      0x00, 0xfc, 0xe7, 0x0f, 0xc0, 0x01, 0x80, 0xfe, 0xc3, 0x1f, 0xe0, 0x00,
      0xc0, 0xe7, 0xc1, 0xff, 0x70, 0x00, 0xe0, 0x03, 0xc0, 0xff, 0x39, 0x00,
      0x70, 0x01, 0xc0, 0xff, 0x1f, 0x00, 0x38, 0xf8, 0xe0, 0xff, 0x1f, 0x00,
      0x38, 0xfc, 0xff, 0xff, 0x3f, 0x00, 0x70, 0xfe, 0xff, 0xff, 0x73, 0x08,
      0xe0, 0x97, 0xff, 0xff, 0x39, 0x0c, 0xc0, 0x83, 0xff, 0xff, 0x70, 0x0e,
      0x80, 0xc1, 0xff, 0xff, 0xe0, 0x07, 0x00, 0xc0, 0xff, 0xff, 0xc5, 0x03,
      0x00, 0xc0, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x1f, 0x08,
      0x00, 0xf0, 0xff, 0xff, 0xf9, 0x0f, 0x00, 0xf8, 0xff, 0xcf, 0xf0, 0x0f,
      0x00, 0xfc, 0xff, 0xcf, 0xe0, 0x0f, 0x00, 0xce, 0xff, 0xff, 0x00, 0x00,
      0x00, 0x87, 0xff, 0xff, 0x00, 0x00, 0x80, 0x03, 0xff, 0xff, 0x81, 0x01,
      0xc0, 0x01, 0xfe, 0xff, 0xc3, 0x03, 0xe0, 0x00, 0xfe, 0xff, 0xe7, 0x07,
      0x70, 0x00, 0xff, 0xff, 0xff, 0x03, 0x38, 0x80, 0xff, 0xff, 0xff, 0x03,
      0x1c, 0xc0, 0xe1, 0xff, 0xff, 0x03, 0x0e, 0xe0, 0xc0, 0xff, 0xff, 0x03,
      0x0e, 0x70, 0x00, 0xfe, 0xff, 0x07, 0x1c, 0x70, 0x00, 0xfc, 0xff, 0x03,
      0x38, 0xe0, 0x00, 0xe0, 0xff, 0x01, 0x70, 0xe0, 0x00, 0xc0, 0xff, 0x00,
      0xe0, 0x7f, 0x00, 0x00, 0x7e, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0x3c, 0x00
    };
    
    static const unsigned char runAnim2_bits[] PROGMEM = {
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00,
      0x00, 0x00, 0x00, 0xc0, 0xff, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x01,
      0x00, 0x00, 0x00, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x02, 0xfe, 0xff, 0x03,
      0x00, 0x00, 0xff, 0xff, 0xff, 0x03, 0x00, 0x00, 0xff, 0xff, 0xff, 0x07,
      0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xff, 0xff, 0x81, 0x0f, 0x00, 0x00, 0xff, 0xff, 0x00, 0x0f,
      0x00, 0x00, 0xfe, 0x1f, 0x00, 0x0e, 0x00, 0x00, 0xfc, 0x0f, 0x00, 0x0c,
      0x00, 0x00, 0xfc, 0x07, 0x0c, 0x0c, 0x00, 0x00, 0xfc, 0x03, 0x0c, 0x0c,
      0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0c, 0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0f,
      0x00, 0x00, 0xfc, 0x03, 0x80, 0x07, 0x00, 0x00, 0xfe, 0x07, 0x80, 0x03,
      0x00, 0x00, 0x9f, 0x0f, 0xc0, 0x01, 0x00, 0x80, 0x0f, 0x1f, 0xe0, 0x00,
      0x00, 0x80, 0x07, 0xfe, 0x7c, 0x00, 0x00, 0x80, 0x03, 0xfc, 0x3f, 0x00,
      0x00, 0x80, 0x03, 0xfc, 0x1f, 0x00, 0x00, 0x00, 0x07, 0xfe, 0x0f, 0x00,
      0x00, 0x00, 0x07, 0xff, 0x0f, 0x00, 0x00, 0x80, 0x07, 0xff, 0x1f, 0x00,
      0x00, 0xc0, 0x07, 0xff, 0x3f, 0x00, 0x00, 0xc0, 0x83, 0xff, 0x73, 0x00,
      0x00, 0xc0, 0xc1, 0xff, 0xe1, 0x00, 0x00, 0xe0, 0xc0, 0xff, 0xc0, 0x01,
      0x00, 0xf0, 0xc0, 0xff, 0xc0, 0x01, 0x00, 0xf8, 0xe0, 0xff, 0xe9, 0x00,
      0x00, 0xf0, 0xf0, 0xff, 0x7f, 0x00, 0x00, 0xf0, 0xf9, 0xff, 0x3f, 0x00,
      0x00, 0xf8, 0xff, 0x9f, 0x17, 0x00, 0x00, 0x38, 0xff, 0x0f, 0x03, 0x00,
      0x00, 0x38, 0xfe, 0x0f, 0x07, 0x00, 0x00, 0x78, 0xfc, 0x9f, 0x03, 0x00,
      0x00, 0x38, 0xf8, 0xff, 0x01, 0x00, 0x00, 0x70, 0xf8, 0xff, 0x00, 0x00,
      0x00, 0xe0, 0xff, 0x5f, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x0f, 0x00, 0x00,
      0x00, 0x80, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00,
      0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00,
      0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0x00, 0x00,
      0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00
    };
    
    static const unsigned char runAnim3_bits[] PROGMEM = {
      0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x41, 0x00,
      0x00, 0x00, 0x00, 0x80, 0xff, 0x00, 0x00, 0x00, 0x06, 0x88, 0xff, 0x01,
      0x00, 0x00, 0x0f, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x1f, 0xfe, 0xff, 0x07,
      0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xfc, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xff, 0xff, 0x07, 0x0e, 0x00, 0x00, 0xff, 0xff, 0x03, 0x0c,
      0x00, 0x00, 0xfe, 0x7f, 0x00, 0x0c, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x0c,
      0x00, 0x00, 0xfc, 0x1f, 0x30, 0x0c, 0x00, 0x00, 0xfc, 0x0f, 0x30, 0x0c,
      0x00, 0x00, 0xf8, 0x07, 0x00, 0x0c, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x0e,
      0x00, 0x00, 0xf0, 0x03, 0x00, 0x07, 0x00, 0x00, 0xf8, 0x07, 0x80, 0x03,
      0x00, 0x00, 0x1c, 0x0e, 0xc0, 0x01, 0x00, 0x00, 0x0e, 0x5c, 0xe0, 0x00,
      0x00, 0x00, 0x0f, 0xfc, 0x70, 0x00, 0x00, 0x80, 0x0f, 0xfc, 0x39, 0x00,
      0x00, 0x00, 0x0f, 0xf8, 0x1f, 0x00, 0x00, 0x00, 0x0f, 0x30, 0x0f, 0x00,
      0x00, 0x00, 0x0f, 0x30, 0x0e, 0x00, 0x00, 0x00, 0x1f, 0x78, 0x1e, 0x00,
      0x00, 0x00, 0x3f, 0xf8, 0x3f, 0x00, 0x00, 0x80, 0x7f, 0xf0, 0x7f, 0x00,
      0x00, 0xc0, 0xff, 0xe0, 0x79, 0x00, 0x00, 0xc0, 0xff, 0x01, 0x78, 0x00,
      0x00, 0xc0, 0xff, 0x03, 0x3c, 0x00, 0x00, 0xe0, 0xff, 0x67, 0x3e, 0x00,
      0x00, 0xf0, 0xff, 0xff, 0x3f, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x3f, 0x00,
      0x00, 0xfc, 0xff, 0x9f, 0x79, 0x00, 0x00, 0xfe, 0xff, 0x0f, 0x38, 0x00,
      0x00, 0xff, 0xff, 0x07, 0x1c, 0x00, 0x80, 0xff, 0xff, 0x03, 0x0e, 0x00,
      0xc0, 0xff, 0xff, 0x01, 0x07, 0x00, 0xe0, 0xff, 0xff, 0x80, 0x03, 0x00,
      0xf0, 0xff, 0xff, 0xc0, 0x01, 0x00, 0xf8, 0xff, 0xcf, 0xe1, 0x00, 0x00,
      0xfc, 0xff, 0x81, 0xff, 0x00, 0x00, 0xfe, 0xff, 0x00, 0xff, 0x00, 0x00,
      0xff, 0x7f, 0x00, 0x7e, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00,
      0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00,
      0xfe, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x0f, 0x00, 0x00, 0x00, 0x00
    };
    
    static const unsigned char runAnim4_bits[] PROGMEM = {
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00,
      0x00, 0x00, 0x00, 0xc0, 0xff, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x01,
      0x00, 0x00, 0x00, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x02, 0xfe, 0xff, 0x07,
      0x00, 0x00, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xff, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xff, 0xff, 0x07, 0x0e, 0x00, 0x00, 0xff, 0xff, 0x03, 0x0c,
      0x00, 0x00, 0xfe, 0x7f, 0x00, 0x0c, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x0c,
      0x00, 0x00, 0xfc, 0x1f, 0x30, 0x0c, 0x00, 0x00, 0xfc, 0x0f, 0x30, 0x0c,
      0x00, 0x00, 0xf8, 0x07, 0x00, 0x0c, 0x00, 0x80, 0xf9, 0x03, 0x00, 0x0e,
      0x00, 0xc0, 0xff, 0x03, 0x00, 0x07, 0x00, 0xe0, 0xff, 0x07, 0x80, 0x03,
      0x00, 0x70, 0xfe, 0x0f, 0xc0, 0x01, 0x00, 0x38, 0xfe, 0x5c, 0xe0, 0x00,
      0x00, 0x1c, 0x7f, 0xf8, 0x7c, 0x00, 0x00, 0x8e, 0x3f, 0xf0, 0x7f, 0x00,
      0x00, 0xc7, 0x3f, 0xa0, 0xff, 0x03, 0x80, 0xe3, 0x3f, 0x00, 0xff, 0x07,
      0xc0, 0xf1, 0x3f, 0x00, 0x7e, 0x0e, 0xe0, 0xf8, 0x7f, 0x10, 0x3c, 0x0e,
      0xe0, 0xfc, 0xff, 0x3f, 0x18, 0x07, 0xc0, 0xcf, 0xff, 0x7f, 0x80, 0x03,
      0x80, 0xc7, 0xff, 0xff, 0xc0, 0x01, 0x00, 0xc3, 0xff, 0xff, 0xe1, 0x00,
      0x00, 0xc0, 0xff, 0xff, 0x7f, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x3f, 0x00,
      0x00, 0xe0, 0xff, 0xff, 0x19, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x01, 0x00,
      0x00, 0x80, 0xff, 0xff, 0x03, 0x00, 0x00, 0x80, 0xff, 0xff, 0x03, 0x00,
      0x00, 0xf0, 0x01, 0xfe, 0x03, 0x00, 0x00, 0xf0, 0x01, 0xfe, 0x07, 0x00,
      0x00, 0xf0, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x1c, 0x00,
      0x00, 0xfc, 0xff, 0x7f, 0x38, 0x00, 0x00, 0xfe, 0xff, 0x3f, 0x70, 0x00,
      0x00, 0xfc, 0x07, 0x1e, 0xe0, 0x00, 0x00, 0xfc, 0x03, 0x1c, 0xc0, 0x01,
      0x00, 0xfe, 0x01, 0x38, 0x80, 0x0f, 0x00, 0xfc, 0x00, 0x70, 0x00, 0x0f,
      0x00, 0x48, 0x00, 0xe0, 0x00, 0x0e, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x0e,
      0x00, 0x00, 0x00, 0x80, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f
    };
    
    static const unsigned char runAnim5_bits[] PROGMEM = {
      0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x41, 0x00,
      0x00, 0x00, 0x00, 0x80, 0xff, 0x00, 0x00, 0x00, 0x06, 0x88, 0xff, 0x01,
      0x00, 0x00, 0x0f, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x1f, 0xfe, 0xff, 0x03,
      0x00, 0x00, 0xfe, 0xff, 0xff, 0x03, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07,
      0x00, 0x00, 0xfc, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xff, 0xff, 0x01, 0x0e, 0x00, 0x00, 0xff, 0xff, 0x00, 0x0c,
      0x00, 0x00, 0xfe, 0x1f, 0x00, 0x0c, 0x00, 0x00, 0xfc, 0x0f, 0x00, 0x0c,
      0x00, 0x00, 0xfc, 0x07, 0x0c, 0x0c, 0x00, 0x00, 0xfc, 0x03, 0x0c, 0x0c,
      0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0c, 0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0f,
      0x00, 0x00, 0xfc, 0x03, 0x80, 0x07, 0x00, 0x00, 0xfe, 0x07, 0x80, 0x03,
      0x00, 0x00, 0x9f, 0x0f, 0xc0, 0x01, 0x00, 0x00, 0x0f, 0x1f, 0xe0, 0x00,
      0x00, 0x00, 0x07, 0xfe, 0x70, 0x00, 0x00, 0x80, 0x03, 0xfc, 0x39, 0x00,
      0x00, 0xc0, 0x03, 0xfc, 0x1f, 0x00, 0x00, 0xe0, 0x07, 0xfc, 0x0f, 0x00,
      0x00, 0xc0, 0x0f, 0xf8, 0x0f, 0x00, 0x00, 0xc0, 0x0f, 0xf0, 0x0f, 0x00,
      0x00, 0xc0, 0x0f, 0x80, 0x0f, 0x00, 0x00, 0xc0, 0x1f, 0x00, 0x0f, 0x00,
      0x00, 0xc0, 0xff, 0x00, 0x1f, 0x00, 0x00, 0xc0, 0xff, 0x81, 0x0f, 0x00,
      0x00, 0xc0, 0xff, 0xff, 0x07, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x03, 0x00,
      0x00, 0xe0, 0xff, 0xff, 0x01, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x00, 0x00,
      0x00, 0x80, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x7f, 0x00, 0x00,
      0x00, 0x00, 0x80, 0xff, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0x01, 0x00,
      0x00, 0x00, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x07, 0x00,
      0x00, 0x00, 0xff, 0xff, 0x07, 0x00, 0x00, 0x80, 0xff, 0xff, 0x03, 0x00,
      0x00, 0x80, 0xe7, 0x7f, 0x00, 0x00, 0x00, 0x80, 0xc3, 0x3f, 0x00, 0x00,
      0x00, 0x80, 0x83, 0x3f, 0x00, 0x00, 0x00, 0x80, 0x03, 0x3f, 0x00, 0x00,
      0x00, 0x80, 0x03, 0x1e, 0x00, 0x00, 0x00, 0x80, 0x07, 0x0e, 0x00, 0x00,
      0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00
    };
    
    #define runAnim_x 40
    #define runAnim_y 8
    #define runAnim_delay 50
    uint8_t scroll = 128;
    int minScroll = -48;
    
    void drawframe() {
      scroll--;
      if (scroll > 128 || scroll < minScroll) {
        scroll = 128;
      }
      u8g2.updateDisplayArea(0, 0, 16, 8);
      u8g2.clearBuffer();
    
      /*  tileScroll = scroll/8;
        if (tileScroll < 1 || tileScroll > 10) {
          u8g2.updateDisplayArea(tileScroll, 1, 6, 6);
        }
        else {
          u8g2.updateDisplayArea(10, 1, 6, 6);
        }
        u8g2.clearBuffer();
      */
    }
    
    void drawRunAnim(uint8_t a) {
    
      u8g2.drawXBMP( scroll, runAnim_y, runAnim_width, runAnim_height, runAnim1_bits);
      drawframe();
      delay(runAnim_delay);
    
      u8g2.drawXBMP( scroll, runAnim_y, runAnim_width, runAnim_height, runAnim2_bits);
      drawframe();
      delay(runAnim_delay);
    
      u8g2.drawXBMP( scroll, runAnim_y, runAnim_width, runAnim_height, runAnim3_bits);
      drawframe();
      delay(runAnim_delay);
    
      u8g2.drawXBMP( scroll, runAnim_y, runAnim_width, runAnim_height, runAnim4_bits);
      drawframe();
      delay(runAnim_delay);
    
      u8g2.drawXBMP( scroll, runAnim_y, runAnim_width, runAnim_height, runAnim5_bits);
      drawframe();
      delay(runAnim_delay);
    }
    
    #define ironman_x 42
    #define ironman_y 0
    
    void drawIronmanLogo(uint8_t a) {
      u8g2.drawXBMP( scroll+10, ironman_y, ironman_width, ironman_height, ironman_bits);
      drawframe();
      delay(50);
    }
    
    void setup() {
      u8g2.begin();
      Serial.begin(115200);
      pinMode(led, OUTPUT);
      pinMode(2, INPUT_PULLUP);
    
    }
    
    uint8_t draw_state = 0;
    
    void draw(void) {
      switch (draw_state >> 3) {
        case 0: drawRunAnim(draw_state & 7); break;
        case 1: drawIronmanLogo(draw_state & 7); break;
      }
    }
    
    boolean showFirstBattLevel = false;
    unsigned int battLevel = 0;
    unsigned int battLevelChange = 0;
    unsigned int battUpdateInterval = 2000;
    unsigned long previousMillis = 0;
    int i = 1;
    
    void chkBattLvl() {
      unsigned int battAdcAverage = 0;
      for (int i = 1; i < 10; i++) {
        battAdcAverage += analogRead(battLevelPin);
      }
      unsigned int battAdcReading = (battAdcAverage / 10);
      unsigned int battLevel = map(battAdcReading, 640, 740, 0, 10);
      battLevel = constrain(battLevel, 0, 10);
      if (showFirstBattLevel == false) {
        u8g2.setFont(u8g2_font_6x10_tf);
        u8g2.setDrawColor(1);
        u8g2.drawStr(0, 0, battLevel);
        u8g2.updateDisplayArea(0, 0, 2, 2);
        showFirstBattLevel = true;
      }
      if (battLevelChange != battLevel) {
        unsigned long currentMillis = millis();
        if ((unsigned long)(currentMillis - previousMillis) >= battUpdateInterval) {
          previousMillis = currentMillis;
          u8g2.setFont(u8g2_font_6x10_tf);
          u8g2.setDrawColor(1);
          u8g2.drawStr(0, 0, battLevel);
          u8g2.updateDisplayArea(0, 0, 2, 2);
          delay(10);
        }
      }
      battLevelChange = battLevel;
    }
    
    void loop() {
      //digitalWrite(led, HIGH);
    
      draw();
      chkBattLvl();
    
      Serial.println(draw_state);
      draw_state++;
      if ( draw_state >= 2 * 8 ) {
        draw_state = 0;
        Serial.println("draw_state reset");
      }
    
      //digitalWrite(led, LOW);
    }
    Last edited by NeTgHoSt; 07-16-2020 at 06:06 AM.

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    11,967
    So many questions ... might serve well to work with a tutorial and write some code on Teensy and get used to printing and editing ... mostly what I do here looking at code.

    Like:
    tutorialspoint.com/What-are-shift-operators-in-Cplusplus

    see Binary AND Operator :: tutorialspoint.com/cplusplus/cpp_operators.htm

    tutorialspoint.com/cplusplus/cpp_switch_statement.htm

  3. #3
    Junior Member NeTgHoSt's Avatar
    Join Date
    May 2020
    Location
    California
    Posts
    13
    Sorry it sounded like a lot of questions; actually the only question I'm really looking to get answered from posting this, is in the title. The "other" questions were actually just me being more specific as to what I don't understand in my main question; which is how these 3 parts of code work exactly. I pretty much typed that whole code up myself, so if you think all I know how to do is copy-paste, you're incorrect. I understand what the >>3 does in mathematics; I do NOT understand how (draw_state >> 3) is anything but zero, when it starts at 0000 0000.. 0 >> 3 = 0 doesn't it? As such, in the parameter (draw_state & 7), doesn't 0000 0000 & 0000 0111 = 0000 0000? How am I getting this wrong?

  4. #4
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    216
    If draw_state is some unsigned integer type, then (draw_state >> 3) == (draw_state / 8), and ((draw_state & 7) == (draw_state % 8)) (modulo, i.e. remainder if divided by 8).
    Code:
    draw_state │ function
    ───────────┼─────────────────────
        0      │ drawRunAnim(0);
        1      │ drawRunAnim(1);
        2      │ drawRunAnim(2);
        3      │ drawRunAnim(3);
        4      │ drawRunAnim(4);
        5      │ drawRunAnim(5);
        6      │ drawRunAnim(6);
        7      │ drawRunAnim(7);
        8      │ drawIronmanLogo(0);
        9      │ drawIronmanLogo(1);
       10      │ drawIronmanLogo(2);
       11      │ drawIronmanLogo(3);
       12      │ drawIronmanLogo(4);
       13      │ drawIronmanLogo(5);
       14      │ drawIronmanLogo(6);
       15      │ drawIronmanLogo(7);
    None of the other draw_state values do anything in that switch statement.
    Last edited by Nominal Animal; 07-16-2020 at 08:35 AM.

  5. #5
    Junior Member NeTgHoSt's Avatar
    Join Date
    May 2020
    Location
    California
    Posts
    13

    Unhappy

    Thank you Nominal, but I'm still not fully understanding.. What purpose exactly do the parameters in each function and case serve to do? If it read:

    Code:
    void drawRunAnim() { ...
    //and
    case 0: drawRunAnim(); break;
    what would it do if those parameters were omitted? How do I determine what goes in those parentheses? I still don't understand WHY the original coder added this modulus and bitshift stuff in there.. Is there maybe a simpler way to write these case statements? In all the documentation I've read concerning switch/case, they never elaborate what goes into the parentheses... maybe your previous explanation of the parameter contents will make sense once I understand what the purpose of the parameters are in the first place.. tia!

    Here is my updated code. Among replacing some delays with millis, I attempted to change (draw_state & 7) to (draw_state & 40) to make the drawIronmanLogo case/function run more than 7 times.. It runs the same 7-frame length somehow. So I know I'm missing something important. X)

    Code:
    #include <U8g2lib.h>
    #include <SPI.h>
    #include <Wire.h>
    #include "Buzzer.h"
    
    //Compiler / Global Defines
    #define BUZZER_PIN_SIG 6  // Beeper pin
    #define battLevelPin A9   // Battery reading input
    #define battCheckButton 2 //Battery level check button
    int led = 13;
    int i = 1;
    boolean showFirstBattLevel = false;
    unsigned int battLevel = 0;
    unsigned int battLevelChange = 0;
    unsigned int battUpdateInterval = 500;
    unsigned int battCheckInterval = 50;
    unsigned long previousMillis1 = 0;
    unsigned long previousMillis2 = 0;
    
    
    //Sensor and Display Connection Setup etc.
    U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
    Buzzer buzzer(BUZZER_PIN_SIG);
    
    #define ironman_width 44
    #define ironman_height 64
    static const unsigned char ironman_bits[] PROGMEM = {
      0x00, 0x00, 0xfc, 0x03, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x3f, 0x00, 0x00,
      0x00, 0xf8, 0xff, 0xff, 0x01, 0x00, 0x00, 0x86, 0xff, 0x1f, 0x06, 0x00,
      0x80, 0x81, 0xff, 0x1f, 0x18, 0x00, 0xc0, 0x80, 0xff, 0x1f, 0x30, 0x00,
      0x20, 0x80, 0xff, 0x1f, 0xc0, 0x00, 0x10, 0x00, 0xff, 0x0f, 0x80, 0x01,
      0x08, 0x00, 0xff, 0x0f, 0x00, 0x01, 0x08, 0x00, 0xff, 0x0f, 0x00, 0x02,
      0x08, 0x00, 0xff, 0x0f, 0x00, 0x02, 0x08, 0x00, 0xfe, 0x07, 0x00, 0x02,
      0x08, 0x00, 0xfe, 0x07, 0x00, 0x02, 0x08, 0x00, 0xfe, 0x07, 0x00, 0x02,
      0x08, 0x00, 0x00, 0x00, 0x00, 0x06, 0x08, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x08, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x0c, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x0c, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x0c, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x06, 0x00, 0x00, 0x00, 0x00, 0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x06, 0x00, 0x00, 0x00, 0x00, 0x0e, 0xc6, 0x07, 0x00, 0x00, 0x7c, 0x0e,
      0xe6, 0xff, 0xff, 0xff, 0xff, 0x0e, 0xe6, 0xff, 0x03, 0xf8, 0xff, 0x0c,
      0x02, 0x3f, 0x00, 0x80, 0x1f, 0x0c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x0c,
      0x03, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0c,
      0x03, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0c,
      0x03, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x07, 0x00, 0x00, 0x00, 0x00, 0x06, 0x16, 0x00, 0x00, 0x00, 0x00, 0x06,
      0x2e, 0x00, 0x00, 0x00, 0x80, 0x07, 0x6e, 0x00, 0x00, 0x00, 0x40, 0x07,
      0x6e, 0x00, 0x00, 0x00, 0x60, 0x07, 0xde, 0x00, 0x00, 0x00, 0xb0, 0x07,
      0xde, 0x00, 0x00, 0x00, 0xb0, 0x07, 0x9c, 0x01, 0x00, 0x00, 0xd8, 0x03,
      0xbc, 0x01, 0x00, 0x00, 0xf8, 0x03, 0x3c, 0x01, 0x00, 0x00, 0xe8, 0x03,
      0x7c, 0x03, 0x00, 0x00, 0xf4, 0x03, 0xfc, 0x02, 0x00, 0x00, 0xf4, 0x03,
      0xfc, 0xc2, 0xff, 0x1f, 0xfa, 0x03, 0xf8, 0x25, 0xff, 0x27, 0xfa, 0x01,
      0xf8, 0x38, 0x00, 0x60, 0xf9, 0x01, 0xf8, 0x10, 0x00, 0xc0, 0xf8, 0x01,
      0xf0, 0x00, 0x00, 0x00, 0xf8, 0x01, 0xf0, 0x00, 0x00, 0x00, 0xf8, 0x01,
      0xf0, 0x00, 0xf8, 0x00, 0xf8, 0x00, 0xe0, 0xc1, 0xff, 0x0f, 0xf8, 0x00,
      0xc0, 0xe3, 0xff, 0x1f, 0x7c, 0x00, 0x80, 0xff, 0xff, 0x7f, 0x3e, 0x00,
      0x00, 0xff, 0xff, 0x7f, 0x0f, 0x00, 0x00, 0xf6, 0x00, 0xfe, 0x07, 0x00,
      0x00, 0x3c, 0x00, 0xb0, 0x01, 0x00, 0x00, 0x08, 0x00, 0x40, 0x00, 0x00
    };
    
    #define runAnim_width 44
    #define runAnim_height 52
    static const unsigned char runAnim1_bits[] PROGMEM = {
      0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x41, 0x00,
      0x00, 0x00, 0x00, 0x80, 0xff, 0x00, 0x00, 0x00, 0x06, 0x88, 0xff, 0x01,
      0x00, 0x00, 0x0f, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x1f, 0xfe, 0xff, 0x03,
      0x00, 0x00, 0xfe, 0xff, 0xff, 0x03, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07,
      0x00, 0x00, 0xfc, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xff, 0xff, 0x81, 0x0f, 0x00, 0x00, 0xff, 0xff, 0x00, 0x0f,
      0x00, 0x00, 0xfe, 0x1f, 0x00, 0x0e, 0x00, 0x00, 0xfc, 0x0f, 0x00, 0x0c,
      0x00, 0x00, 0xfc, 0x07, 0x0c, 0x0c, 0x00, 0x00, 0xfc, 0x03, 0x0c, 0x0c,
      0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0c, 0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0f,
      0x00, 0x00, 0xfc, 0x03, 0x80, 0x07, 0x00, 0x78, 0xfe, 0x07, 0x80, 0x03,
      0x00, 0xfc, 0xe7, 0x0f, 0xc0, 0x01, 0x80, 0xfe, 0xc3, 0x1f, 0xe0, 0x00,
      0xc0, 0xe7, 0xc1, 0xff, 0x70, 0x00, 0xe0, 0x03, 0xc0, 0xff, 0x39, 0x00,
      0x70, 0x01, 0xc0, 0xff, 0x1f, 0x00, 0x38, 0xf8, 0xe0, 0xff, 0x1f, 0x00,
      0x38, 0xfc, 0xff, 0xff, 0x3f, 0x00, 0x70, 0xfe, 0xff, 0xff, 0x73, 0x08,
      0xe0, 0x97, 0xff, 0xff, 0x39, 0x0c, 0xc0, 0x83, 0xff, 0xff, 0x70, 0x0e,
      0x80, 0xc1, 0xff, 0xff, 0xe0, 0x07, 0x00, 0xc0, 0xff, 0xff, 0xc5, 0x03,
      0x00, 0xc0, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x1f, 0x08,
      0x00, 0xf0, 0xff, 0xff, 0xf9, 0x0f, 0x00, 0xf8, 0xff, 0xcf, 0xf0, 0x0f,
      0x00, 0xfc, 0xff, 0xcf, 0xe0, 0x0f, 0x00, 0xce, 0xff, 0xff, 0x00, 0x00,
      0x00, 0x87, 0xff, 0xff, 0x00, 0x00, 0x80, 0x03, 0xff, 0xff, 0x81, 0x01,
      0xc0, 0x01, 0xfe, 0xff, 0xc3, 0x03, 0xe0, 0x00, 0xfe, 0xff, 0xe7, 0x07,
      0x70, 0x00, 0xff, 0xff, 0xff, 0x03, 0x38, 0x80, 0xff, 0xff, 0xff, 0x03,
      0x1c, 0xc0, 0xe1, 0xff, 0xff, 0x03, 0x0e, 0xe0, 0xc0, 0xff, 0xff, 0x03,
      0x0e, 0x70, 0x00, 0xfe, 0xff, 0x07, 0x1c, 0x70, 0x00, 0xfc, 0xff, 0x03,
      0x38, 0xe0, 0x00, 0xe0, 0xff, 0x01, 0x70, 0xe0, 0x00, 0xc0, 0xff, 0x00,
      0xe0, 0x7f, 0x00, 0x00, 0x7e, 0x00, 0xc0, 0x3f, 0x00, 0x00, 0x3c, 0x00
    };
    
    static const unsigned char runAnim2_bits[] PROGMEM = {
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00,
      0x00, 0x00, 0x00, 0xc0, 0xff, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x01,
      0x00, 0x00, 0x00, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x02, 0xfe, 0xff, 0x03,
      0x00, 0x00, 0xff, 0xff, 0xff, 0x03, 0x00, 0x00, 0xff, 0xff, 0xff, 0x07,
      0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xff, 0xff, 0x81, 0x0f, 0x00, 0x00, 0xff, 0xff, 0x00, 0x0f,
      0x00, 0x00, 0xfe, 0x1f, 0x00, 0x0e, 0x00, 0x00, 0xfc, 0x0f, 0x00, 0x0c,
      0x00, 0x00, 0xfc, 0x07, 0x0c, 0x0c, 0x00, 0x00, 0xfc, 0x03, 0x0c, 0x0c,
      0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0c, 0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0f,
      0x00, 0x00, 0xfc, 0x03, 0x80, 0x07, 0x00, 0x00, 0xfe, 0x07, 0x80, 0x03,
      0x00, 0x00, 0x9f, 0x0f, 0xc0, 0x01, 0x00, 0x80, 0x0f, 0x1f, 0xe0, 0x00,
      0x00, 0x80, 0x07, 0xfe, 0x7c, 0x00, 0x00, 0x80, 0x03, 0xfc, 0x3f, 0x00,
      0x00, 0x80, 0x03, 0xfc, 0x1f, 0x00, 0x00, 0x00, 0x07, 0xfe, 0x0f, 0x00,
      0x00, 0x00, 0x07, 0xff, 0x0f, 0x00, 0x00, 0x80, 0x07, 0xff, 0x1f, 0x00,
      0x00, 0xc0, 0x07, 0xff, 0x3f, 0x00, 0x00, 0xc0, 0x83, 0xff, 0x73, 0x00,
      0x00, 0xc0, 0xc1, 0xff, 0xe1, 0x00, 0x00, 0xe0, 0xc0, 0xff, 0xc0, 0x01,
      0x00, 0xf0, 0xc0, 0xff, 0xc0, 0x01, 0x00, 0xf8, 0xe0, 0xff, 0xe9, 0x00,
      0x00, 0xf0, 0xf0, 0xff, 0x7f, 0x00, 0x00, 0xf0, 0xf9, 0xff, 0x3f, 0x00,
      0x00, 0xf8, 0xff, 0x9f, 0x17, 0x00, 0x00, 0x38, 0xff, 0x0f, 0x03, 0x00,
      0x00, 0x38, 0xfe, 0x0f, 0x07, 0x00, 0x00, 0x78, 0xfc, 0x9f, 0x03, 0x00,
      0x00, 0x38, 0xf8, 0xff, 0x01, 0x00, 0x00, 0x70, 0xf8, 0xff, 0x00, 0x00,
      0x00, 0xe0, 0xff, 0x5f, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x0f, 0x00, 0x00,
      0x00, 0x80, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00,
      0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00,
      0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0x00, 0x00,
      0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00
    };
    
    static const unsigned char runAnim3_bits[] PROGMEM = {
      0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x41, 0x00,
      0x00, 0x00, 0x00, 0x80, 0xff, 0x00, 0x00, 0x00, 0x06, 0x88, 0xff, 0x01,
      0x00, 0x00, 0x0f, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x1f, 0xfe, 0xff, 0x07,
      0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xfc, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xff, 0xff, 0x07, 0x0e, 0x00, 0x00, 0xff, 0xff, 0x03, 0x0c,
      0x00, 0x00, 0xfe, 0x7f, 0x00, 0x0c, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x0c,
      0x00, 0x00, 0xfc, 0x1f, 0x30, 0x0c, 0x00, 0x00, 0xfc, 0x0f, 0x30, 0x0c,
      0x00, 0x00, 0xf8, 0x07, 0x00, 0x0c, 0x00, 0x00, 0xf0, 0x03, 0x00, 0x0e,
      0x00, 0x00, 0xf0, 0x03, 0x00, 0x07, 0x00, 0x00, 0xf8, 0x07, 0x80, 0x03,
      0x00, 0x00, 0x1c, 0x0e, 0xc0, 0x01, 0x00, 0x00, 0x0e, 0x5c, 0xe0, 0x00,
      0x00, 0x00, 0x0f, 0xfc, 0x70, 0x00, 0x00, 0x80, 0x0f, 0xfc, 0x39, 0x00,
      0x00, 0x00, 0x0f, 0xf8, 0x1f, 0x00, 0x00, 0x00, 0x0f, 0x30, 0x0f, 0x00,
      0x00, 0x00, 0x0f, 0x30, 0x0e, 0x00, 0x00, 0x00, 0x1f, 0x78, 0x1e, 0x00,
      0x00, 0x00, 0x3f, 0xf8, 0x3f, 0x00, 0x00, 0x80, 0x7f, 0xf0, 0x7f, 0x00,
      0x00, 0xc0, 0xff, 0xe0, 0x79, 0x00, 0x00, 0xc0, 0xff, 0x01, 0x78, 0x00,
      0x00, 0xc0, 0xff, 0x03, 0x3c, 0x00, 0x00, 0xe0, 0xff, 0x67, 0x3e, 0x00,
      0x00, 0xf0, 0xff, 0xff, 0x3f, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x3f, 0x00,
      0x00, 0xfc, 0xff, 0x9f, 0x79, 0x00, 0x00, 0xfe, 0xff, 0x0f, 0x38, 0x00,
      0x00, 0xff, 0xff, 0x07, 0x1c, 0x00, 0x80, 0xff, 0xff, 0x03, 0x0e, 0x00,
      0xc0, 0xff, 0xff, 0x01, 0x07, 0x00, 0xe0, 0xff, 0xff, 0x80, 0x03, 0x00,
      0xf0, 0xff, 0xff, 0xc0, 0x01, 0x00, 0xf8, 0xff, 0xcf, 0xe1, 0x00, 0x00,
      0xfc, 0xff, 0x81, 0xff, 0x00, 0x00, 0xfe, 0xff, 0x00, 0xff, 0x00, 0x00,
      0xff, 0x7f, 0x00, 0x7e, 0x00, 0x00, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00,
      0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00,
      0xfe, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x0f, 0x00, 0x00, 0x00, 0x00
    };
    
    static const unsigned char runAnim4_bits[] PROGMEM = {
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00,
      0x00, 0x00, 0x00, 0xc0, 0xff, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x01,
      0x00, 0x00, 0x00, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x02, 0xfe, 0xff, 0x07,
      0x00, 0x00, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xff, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xff, 0xff, 0x07, 0x0e, 0x00, 0x00, 0xff, 0xff, 0x03, 0x0c,
      0x00, 0x00, 0xfe, 0x7f, 0x00, 0x0c, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x0c,
      0x00, 0x00, 0xfc, 0x1f, 0x30, 0x0c, 0x00, 0x00, 0xfc, 0x0f, 0x30, 0x0c,
      0x00, 0x00, 0xf8, 0x07, 0x00, 0x0c, 0x00, 0x80, 0xf9, 0x03, 0x00, 0x0e,
      0x00, 0xc0, 0xff, 0x03, 0x00, 0x07, 0x00, 0xe0, 0xff, 0x07, 0x80, 0x03,
      0x00, 0x70, 0xfe, 0x0f, 0xc0, 0x01, 0x00, 0x38, 0xfe, 0x5c, 0xe0, 0x00,
      0x00, 0x1c, 0x7f, 0xf8, 0x7c, 0x00, 0x00, 0x8e, 0x3f, 0xf0, 0x7f, 0x00,
      0x00, 0xc7, 0x3f, 0xa0, 0xff, 0x03, 0x80, 0xe3, 0x3f, 0x00, 0xff, 0x07,
      0xc0, 0xf1, 0x3f, 0x00, 0x7e, 0x0e, 0xe0, 0xf8, 0x7f, 0x10, 0x3c, 0x0e,
      0xe0, 0xfc, 0xff, 0x3f, 0x18, 0x07, 0xc0, 0xcf, 0xff, 0x7f, 0x80, 0x03,
      0x80, 0xc7, 0xff, 0xff, 0xc0, 0x01, 0x00, 0xc3, 0xff, 0xff, 0xe1, 0x00,
      0x00, 0xc0, 0xff, 0xff, 0x7f, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x3f, 0x00,
      0x00, 0xe0, 0xff, 0xff, 0x19, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x01, 0x00,
      0x00, 0x80, 0xff, 0xff, 0x03, 0x00, 0x00, 0x80, 0xff, 0xff, 0x03, 0x00,
      0x00, 0xf0, 0x01, 0xfe, 0x03, 0x00, 0x00, 0xf0, 0x01, 0xfe, 0x07, 0x00,
      0x00, 0xf0, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x1c, 0x00,
      0x00, 0xfc, 0xff, 0x7f, 0x38, 0x00, 0x00, 0xfe, 0xff, 0x3f, 0x70, 0x00,
      0x00, 0xfc, 0x07, 0x1e, 0xe0, 0x00, 0x00, 0xfc, 0x03, 0x1c, 0xc0, 0x01,
      0x00, 0xfe, 0x01, 0x38, 0x80, 0x0f, 0x00, 0xfc, 0x00, 0x70, 0x00, 0x0f,
      0x00, 0x48, 0x00, 0xe0, 0x00, 0x0e, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x0e,
      0x00, 0x00, 0x00, 0x80, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f
    };
    
    static const unsigned char runAnim5_bits[] PROGMEM = {
      0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x41, 0x00,
      0x00, 0x00, 0x00, 0x80, 0xff, 0x00, 0x00, 0x00, 0x06, 0x88, 0xff, 0x01,
      0x00, 0x00, 0x0f, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x1f, 0xfe, 0xff, 0x03,
      0x00, 0x00, 0xfe, 0xff, 0xff, 0x03, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07,
      0x00, 0x00, 0xfc, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0x0f,
      0x00, 0x00, 0xff, 0xff, 0x01, 0x0e, 0x00, 0x00, 0xff, 0xff, 0x00, 0x0c,
      0x00, 0x00, 0xfe, 0x1f, 0x00, 0x0c, 0x00, 0x00, 0xfc, 0x0f, 0x00, 0x0c,
      0x00, 0x00, 0xfc, 0x07, 0x0c, 0x0c, 0x00, 0x00, 0xfc, 0x03, 0x0c, 0x0c,
      0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0c, 0x00, 0x00, 0xf8, 0x01, 0xc0, 0x0f,
      0x00, 0x00, 0xfc, 0x03, 0x80, 0x07, 0x00, 0x00, 0xfe, 0x07, 0x80, 0x03,
      0x00, 0x00, 0x9f, 0x0f, 0xc0, 0x01, 0x00, 0x00, 0x0f, 0x1f, 0xe0, 0x00,
      0x00, 0x00, 0x07, 0xfe, 0x70, 0x00, 0x00, 0x80, 0x03, 0xfc, 0x39, 0x00,
      0x00, 0xc0, 0x03, 0xfc, 0x1f, 0x00, 0x00, 0xe0, 0x07, 0xfc, 0x0f, 0x00,
      0x00, 0xc0, 0x0f, 0xf8, 0x0f, 0x00, 0x00, 0xc0, 0x0f, 0xf0, 0x0f, 0x00,
      0x00, 0xc0, 0x0f, 0x80, 0x0f, 0x00, 0x00, 0xc0, 0x1f, 0x00, 0x0f, 0x00,
      0x00, 0xc0, 0xff, 0x00, 0x1f, 0x00, 0x00, 0xc0, 0xff, 0x81, 0x0f, 0x00,
      0x00, 0xc0, 0xff, 0xff, 0x07, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x03, 0x00,
      0x00, 0xe0, 0xff, 0xff, 0x01, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x00, 0x00,
      0x00, 0x80, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x7f, 0x00, 0x00,
      0x00, 0x00, 0x80, 0xff, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0x01, 0x00,
      0x00, 0x00, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x07, 0x00,
      0x00, 0x00, 0xff, 0xff, 0x07, 0x00, 0x00, 0x80, 0xff, 0xff, 0x03, 0x00,
      0x00, 0x80, 0xe7, 0x7f, 0x00, 0x00, 0x00, 0x80, 0xc3, 0x3f, 0x00, 0x00,
      0x00, 0x80, 0x83, 0x3f, 0x00, 0x00, 0x00, 0x80, 0x03, 0x3f, 0x00, 0x00,
      0x00, 0x80, 0x03, 0x1e, 0x00, 0x00, 0x00, 0x80, 0x07, 0x0e, 0x00, 0x00,
      0x00, 0x00, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x0f, 0x00, 0x00
    };
    
    #define runAnim_x 40
    #define runAnim_y 12
    #define runAnim_delay 50
    int scroll = 128;
    int minScroll = -48;
    
    void drawframe() {
      scroll--;
      if (scroll > 128 || scroll < minScroll) {
        scroll = 128;
      }
      u8g2.updateDisplayArea(0, 2, 16, 6);
      u8g2.clearBuffer();
      chkBattLvl();
    
      u8g2.clearBuffer();
    
      /*  tileScroll = scroll/8;
        if (tileScroll < 1 || tileScroll > 10) {
          u8g2.updateDisplayArea(tileScroll, 1, 6, 6);
        }
        else {
          u8g2.updateDisplayArea(10, 1, 6, 6);
        }
        u8g2.clearBuffer();
      */
    }
    
    void drawRunAnim(uint8_t a) {
    
      u8g2.drawXBMP( scroll, runAnim_y, runAnim_width, runAnim_height, runAnim1_bits);
      drawframe();
      delay(runAnim_delay);
    
      u8g2.drawXBMP( scroll, runAnim_y, runAnim_width, runAnim_height, runAnim2_bits);
      drawframe();
      delay(runAnim_delay);
    
      u8g2.drawXBMP( scroll, runAnim_y, runAnim_width, runAnim_height, runAnim3_bits);
      drawframe();
      delay(runAnim_delay);
    
      u8g2.drawXBMP( scroll, runAnim_y, runAnim_width, runAnim_height, runAnim4_bits);
      drawframe();
      delay(runAnim_delay);
    
      u8g2.drawXBMP( scroll, runAnim_y, runAnim_width, runAnim_height, runAnim5_bits);
      drawframe();
      delay(runAnim_delay);
    }
    
    #define ironman_x 42
    #define ironman_y 0
    
    void drawIronmanLogo(uint8_t a) {
      u8g2.drawXBMP( scroll + 10, ironman_y, ironman_width, ironman_height, ironman_bits);
      drawframe();
      chkBattLvl();
      delay(200);
    }
    
    
    
    uint8_t draw_state = 0;
    
    void draw(void) {
      switch (draw_state >> 3) {
        case 0: drawRunAnim(draw_state & 7); break;
        case 1: drawIronmanLogo(draw_state & 40); break;
      }
    }
    
    int chkBattLvl() {
      unsigned long currentMillis = millis();
      if ((unsigned long)(currentMillis - previousMillis1) >= battUpdateInterval) {
        previousMillis1 = currentMillis;
        Serial.println("Begin Batt Check!");
        unsigned int battAdcAverage = 0;
        for (int i = 1; i < 10; i++) {
          battAdcAverage += analogRead(battLevelPin);
        }
        unsigned int battAdcReading = (battAdcAverage / 10);
        unsigned int battLevel = map(battAdcReading, 640, 740, 27, 127);
        battLevel = constrain(battLevel, 27, 127);
        unsigned int battPercent = map(battAdcReading, 640, 740, 0, 100);
        battPercent = constrain(battLevel, 0, 100);
        if (showFirstBattLevel == false) {
          Serial.println("First Batt Check!");
          u8g2.setDrawColor(1);
          u8g2.drawStr(110, 12, battLevel);
          u8g2.updateDisplayArea(0, 0, 16, 2);
          showFirstBattLevel = true;
        }
        Serial.print("Batt Level change value: ");
        Serial.println(battLevelChange);
        Serial.print("Batt Level value: ");
        Serial.println(battLevel);
        if (battLevelChange != battLevel) {
          Serial.println("Batt Level Change!");
          unsigned long currentMillis2 = millis();
          if ((unsigned long)(currentMillis - previousMillis2) >= battCheckInterval) {
            u8g2.setFont(u8g2_font_6x10_tn);
            u8g2.setDrawColor(1);
            u8g2.drawBox(0, 0, battLevel, 16);
            u8g2.drawStr(110, 12, battPercent);
            u8g2.updateDisplayArea(0, 0, 16, 2);
            battLevelChange = battLevel;
            previousMillis2 = currentMillis;
            return battLevel;   
          }
        }
      }
    }
    
    
    void setup() {
      u8g2.begin();
      Serial.begin(115200);
      pinMode(led, OUTPUT);
      pinMode(2, INPUT_PULLUP);
    
    }
    
    void loop() {
    
      draw();
    
      Serial.println(draw_state);
      draw_state++;
      if ( draw_state >= 49 ) {
        draw_state = 0;
        Serial.println("draw_state reset");
      }
    }

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    11,967
    Quote Originally Posted by NeTgHoSt View Post
    Sorry it sounded like a lot of questions; actually the only question I'm really looking to get answered from posting this, is in the title. The "other" questions were actually just me being more specific as to what I don't understand in my main question; which is how these 3 parts of code work exactly. I pretty much typed that whole code up myself, so if you think all I know how to do is copy-paste, you're incorrect. I understand what the >>3 does in mathematics; I do NOT understand how (draw_state >> 3) is anything but zero, when it starts at 0000 0000.. 0 >> 3 = 0 doesn't it? As such, in the parameter (draw_state & 7), doesn't 0000 0000 & 0000 0111 = 0000 0000? How am I getting this wrong?
    In reading the p#1 it seemed the questions involved understanding the code and basic operators. Linking to example tutorial code was expecting it to explain {better and with examples} where my time didn't allow, and in self reference resolution thought the exploration would help more.

    Unless the value 0-7 all do the same thing as case 0: it will eliminate typing. There is little or no point in doing the work in the switch statement by shift. If the numeric values are known {which they have to be or the /8 of the >>3 won't have a known effect} then put a case there for them without adding the shift overhead/confusion. The compiler will do the compares and jumps in a similar manner.

    If things don't work right put a default: case with a Serial.print( value ) - to show numbers not expected - and then add the appropriate case entry to do nothing.

  7. #7
    Senior Member
    Join Date
    Feb 2015
    Location
    Finland
    Posts
    216
    Quote Originally Posted by NeTgHoSt View Post
    What purpose exactly do the parameters in each function and case serve to do?
    It is a common programming pattern for "animator" functions to take a number as a parameter, describing the progress along the animation.

    However, looking at the code, neither drawRunAnim() nor drawIronmanLogo() use the parameter at all.

    So, you could just as well replace the switch statement with
    Code:
        if (draw_state >= 0 && draw_state <= 7) {
            drawRunAnim();
        } else
        if (draw_state >= 8 && draw_state <= 15) {
            drawIronmanLogo();
        }
    and the (uint8_t a) with (void) in the definition of both drawRunAnim() and drawIronmanLogo() functions.

    The code is rather odd, in my opinion.

    Quote Originally Posted by NeTgHoSt View Post
    I still don't understand WHY the original coder added this modulus and bitshift stuff in there..
    Ah, but that's the reason we add comments to our code. The code tells us what it does (so comments like "increment x" are worse than useless!), and comments should tell us the programmer intent, algorithm the code is supposed to implement, or programmer reasoning. We have none of that here, so I suspect the programmer was basically throwing spaghetti at the wall, and as soon as something stuck, went on to the next part.

    It looks like the programmer originally intended the two animation functions to show just one step of the animation, depending on the parameter specified. Then, they gave up, and instead had the function do the full animation with waits in between.

    Quote Originally Posted by NeTgHoSt View Post
    I attempted to change (draw_state & 7) to (draw_state & 40)
    No, that wouldn't work even if the animation function did use the parameter.

    The (x & (M-1)) only matches (x % M) when M is a power of two: 20=1, 21=2, 22=4, 23=8, 24=16, 25=32, and so on.

    You are correct in that the expression a & b yields a number whose binary representation has ones where both a and b have ones too, and zeros everywhere else. It turns out that for masks that are one less than a power of two, this matches the unsigned modulus with that power of two. Because binary AND is a trivial operation for microcontrollers, and modulus is a slow one (relatively speaking! here, with delay(50) and delay(200) following the animation, using the modulo operator would be absolutely fine, even though the compiler would generate somewhat more code for it than the single machine instruction for the AND operation), programmers often favour power-of-two step counts.

    Here, if you replace the switch statement with
    Code:
        if (draw_state >= 0 && draw_state < 40) {
            drawRunAnim(0); /* Since it does not use the parameter at all! */
        } else
        if (draw_state >= 40 && draw_state < 48) {
            drawIronmanLogo(0); /* Here too the parameter is not used! */
        }
    then you'd get the first part of the animation stretched to five times as long, with the second animation staying the same length.

    Remember, the two functions are written to do a full cycle of animations, and the call to drawRunAnim() or drawIronmanLogo() just run one full cycle of the animation, including delays between frames.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •