#include <Arduino.h>
#include <TM1637Display.h>
 
#define CLK 2
#define DIO 3
 
TM1637Display display(CLK, DIO);
 
#define DOT_MASK 0b01000000
#define LIT_A SEG_E | SEG_F | SEG_A | SEG_B | SEG_C | SEG_G
#define LIT_B SEG_F | SEG_E | SEG_D | SEG_C | SEG_G
#define LIT_C SEG_D | SEG_E | SEG_F | SEG_A
#define LIT_D SEG_G | SEG_E | SEG_D | SEG_C | SEG_B
#define LIT_E SEG_A | SEG_F | SEG_G | SEG_E | SEG_D
#define LIT_F SEG_A | SEG_F | SEG_G | SEG_E
#define LIT_G SEG_A | SEG_F | SEG_E | SEG_D | SEG_C
#define LIT_H SEG_F | SEG_G | SEG_B | SEG_E | SEG_C
#define LIT_I SEG_F | SEG_E
#define LIT_K SEG_A | SEG_C | SEG_E | SEG_F | SEG_G
#define LIT_J SEG_D | SEG_C | SEG_B
#define LIT_L SEG_F | SEG_E | SEG_D
#define LIT_N SEG_E | SEG_G | SEG_C
#define LIT_O SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F
#define LIT_P SEG_E | SEG_F | SEG_A | SEG_B | SEG_G
#define LIT_Q SEG_F | SEG_A | SEG_B | SEG_G | SEG_C
#define LIT_R SEG_E | SEG_G
#define LIT_S SEG_A | SEG_F | SEG_G | SEG_C | SEG_D
#define LIT_T SEG_F | SEG_G | SEG_E | SEG_D
#define LIT_U SEG_F | SEG_E | SEG_D | SEG_C | SEG_B
#define LIT_Y SEG_F | SEG_G | SEG_B | SEG_C | SEG_D
#define LIT_SPACE 0
#define LIT_MINUS SEG_G
 
#define SIGN_0 SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F
#define SIGN_1 SEG_B | SEG_C
#define SIGN_2 SEG_A | SEG_B | SEG_D | SEG_E | SEG_G
#define SIGN_3 SEG_A | SEG_B | SEG_C | SEG_D | SEG_G
#define SIGN_4 SEG_B | SEG_C | SEG_F | SEG_G
#define SIGN_5 SEG_A | SEG_C | SEG_D | SEG_F | SEG_G
#define SIGN_6 SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G
#define SIGN_7 SEG_A | SEG_B | SEG_C
#define SIGN_8 SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G
#define SIGN_9 SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G
 
#define MINUS false  
#define PLUS true
 
uint8_t digits[10] = {
        SIGN_0,
        SIGN_1,
        SIGN_2,
        SIGN_3,
        SIGN_4,
        SIGN_5,
        SIGN_6,
        SIGN_7,
        SIGN_8,
        SIGN_9
};
 
 
 
int count_digits(int number){
    number = abs(number);
    size_t count = 1;
    while (number>=10){
        ++count;
        number/=10;
    }
    return count;
}
 
void DisplayInt8(int num){
    uint8_t blank[4] = {LIT_SPACE, LIT_SPACE, LIT_SPACE, LIT_SPACE};
    int8_t cur = 3;
    bool negative = num < 0;
    num = abs(num);
    while (cur > 0){
      blank[cur] = digits[num%10];
      num /= 10;
      cur--;
    }
    if (!negative){
      blank[0] = LIT_SPACE;
    }
    else blank[0] = LIT_MINUS;
    display.clear();
    display.setSegments(blank);
}

void task1_1(int ms_delay){
    int _begin = 100;
    int _end = -100;
    int _step = -10;
    for (int i = _begin; i >= _end; i+=_step){
        DisplayInt8(i); 
        delay(ms_delay);
    }
}
 
void task1_2(int ms_delay){
    bool _begin_sign = MINUS;
    uint8_t _begin_left = 2;
    uint8_t _begin_right = 0;     //1 == 0.01, 10 ==  0.1
 
    bool _end_sign = PLUS;
    uint8_t _end_left = 2;
    uint8_t _end_right = 0;
 
    uint8_t step_left = 0;
    uint8_t step_right = 20; 
 
    bool i_sign = _begin_sign;
    uint8_t i_left = _begin_left;
    uint8_t i_right = _begin_right; 
 
 
    uint8_t blank[4] = {LIT_SPACE, LIT_SPACE, LIT_SPACE, LIT_SPACE};
 
    while (
       i_sign < _end_sign 
    || i_sign == _end_sign && i_left < _end_left 
    || i_sign == _end_sign && i_left == _end_left && i_right <= _end_right)
    {
       if (i_sign == MINUS){
          blank[0] = LIT_MINUS;
          blank[1] = digits[i_left];
       }
       else{
          blank[0] = LIT_SPACE;
          blank[1] = digits[i_left];
       }
       uint8_t first_digit = i_right/10;
       uint8_t second_digit = i_right%10;
       blank[2] = digits[first_digit];
       blank[3] = digits[second_digit];
       uint8_t dot_mask = DOT_MASK;
       for (int i = 0; i < 4; i++){
          blank[i] |= (dot_mask & 0x80);
          dot_mask <<= 1;
       }
      display.setSegments(blank);
      if (i_sign == PLUS){
        i_right += step_right;
        if (i_right >= 100){
          i_left++;
          i_right%=100;
        }
      }
      else{
        if (i_right >= step_right){
          i_right-=step_right;
        }
        else if (i_left > 0){
          --i_left;
          i_right = 100 + i_right - step_right;
        }
        else{
          i_sign = PLUS;
          i_right = step_right - i_right;
        }
      }
      if (i_sign == PLUS){
        i_left += step_left;
      }
      else {
        if (i_left >= step_left){
          i_left -= step_left;
        }
        else {
          i_sign = PLUS;
          i_left = step_left - i_left;
        }
      }
      delay(ms_delay);
    }
}
 
void task1_3(int ms_delay){
    uint8_t text[] = {LIT_Q, LIT_U, LIT_E, LIT_U, LIT_E, 
    LIT_SPACE,
    LIT_F, LIT_U, LIT_L, LIT_L, 
    LIT_SPACE, LIT_Q, LIT_U, LIT_E};
    int n = sizeof(text) - 3;
    int i = 0;
    while (true){
        uint8_t* cur_text = text + i;
        display.setSegments(cur_text);
        i = (i+1) % n;
        delay(ms_delay);
    }
}
 
void setup()
{
    display.setBrightness(0x0f);
    task1_1(100);
    delay(500);
    task1_2(100);
    delay(500);
    task1_3(100);
}
 
void loop() {
 
 
}
4-Digit Display