Running the TM1638 model 1


/*
 *   TM1638_tryout source code, works for arduino uno and seeeduino xiao
 *   pins are defined in header file
 *   put header and cpp file in the same folder as the source file
 */

#include "TM1638_fn.h"
bool counting=false;

void setup() {
   pinMode(strobe, OUTPUT);
   pinMode(clk, OUTPUT);
   pinMode(data, OUTPUT);
// display on
  data_command(0x8B);  //display control + 1011  on + 10/16 brightness
  fn_clear();
  press_button_text();
}

uint8_t program=0;
uint8_t pressed;

void loop() {

 pressed=0;
 pressed=readButtons();  // non blocking

 if (pressed == 128) {
  fn_clear();
  program = 0;
 } else if (program > 0) {
    switch (program) {
      case 1 : count(pressed);break;
      case 2: turnon_leds(pressed);break;
    }
 } else {
    switch (pressed) {
      case 0: break;
      case 1:   break;
      case 2:  program=2; break;
      case 4:  test_numbers(); break; 
      case 8:  seg_dance(); break;
      case 16: fixed_address_led_run(); break;
      case 32: program=1; break;
      case 64: break;
      case 128: break;
    }
    
}

} // loop   
/* ----------------------------------------------------
  @file     TM1638_fn.h
  @author   Maren Schouten
  @brief    header file for Tm1638 model 1
*/
#include 

#ifndef TM1638_FN_H
#define TM1638_FN_H
//#define UNO
#define XIAO  

#if defined(UNO)
#define strobe 4
#define clk  6
#define data 7
#elif defined(XIAO)
#define strobe 1
#define clk  2
#define data 3
#endif



void data_command(char value); 
// run data command
void fn_clear(); 
 // clear display
void press_button_text(void);
// print "press" + "buttonS"
unsigned char readButtons(void);
// pushed buttons returned as byte
void fixed_address_led_run(void);
// led chase back and forth
void seg_dance(void);
// individual segments are randomly turned on and off
void turnon_leds(uint8_t butt_on);
//turn on led(s) based on butt_on byte
void test_numbers(void);
// display 0 to 7 on 7-segments
uint8_t conv_num_seg(uint8_t num);
// from number to 7 segment display
void count(uint8_t butt_on);
// count up first 4 segments
 

#endif
/*-------------------------------------------------------
 *  TM1638_fn.cpp
*/
#include "TM1638_fn.h"
#include <arduino.h>

   

uint8_t global_digits[4] = {0,0,0,0};
 
void data_command(char value) {
 digitalWrite(strobe,LOW);
 shiftOut(data, clk, LSBFIRST, value);  
 digitalWrite(strobe, HIGH);
}
 
 // clear display
void fn_clear() {
 data_command(0x40); //auto increment is necessary
 digitalWrite(strobe, LOW);
 shiftOut(data, clk, LSBFIRST, 0xc0); // 1100 0000 , set starting address to 0
 for(int i = 0; i < 16; i++) {  //auto advance address 0-15 
     shiftOut(data, clk, LSBFIRST, 0x00);  // if 06 set all to 1, if FF set all to 8 dot
 }
 digitalWrite(strobe, HIGH);
}

void press_button_text(void) {
 uint8_t promptText[] = { 0x73, 0x50, 0x79, 0x6d, 0x6d, 0x00, 0x00, 0x00,
                          0x7c, 0x1c, 0x78, 0x78, 0x5c, 0x54, 0x6d, 0x00 };
  uint8_t k=0;
  
  data_command(0x44); // fixed addressing
  while (k < 9) {
    for (uint8_t i= 0; i < 8; i++){
      digitalWrite(strobe,LOW);
      shiftOut(data, clk, LSBFIRST, 0xC0 + (i << 1));
      shiftOut(data, clk, LSBFIRST, promptText[i+k]);
      digitalWrite(strobe, HIGH);
    }
    k+=8;        
    delay(2000);                  
  }
}


unsigned char readButtons(void)
{
 unsigned char buttons = 0;
  digitalWrite(strobe, LOW);
 shiftOut(data, clk, LSBFIRST, 0x42); // command mode + read data  0010
 pinMode(data, INPUT);
 /* 4 bytes bit 0 and bit 4 
    s1 & s5 000x 000x 
    s2 & s6 000x 000x 
    s3 & s7 000x 000x  
    s4 & s8 000x 000x      
  */
 for (unsigned char i = 0; i < 4; i++) {  
    unsigned char v = shiftIn(data, clk, LSBFIRST) << i;  
    buttons |= v;
 }
 pinMode(data, OUTPUT);
 digitalWrite(strobe, HIGH);
 return buttons;
}

void seg_dance(void) {
 data_command(0x40);  // incremental addressing
 unsigned char v=0;
  for (int j=0; j<40; j++) {
      digitalWrite(strobe, LOW);
      shiftOut(data, clk, LSBFIRST, 0xc0); // 1100 0000 , set starting address to 0

      for (int i=0; i < 8; i++) {  //       0,1,2,3,4,5,6,7
          v = 1 << random(7);
          shiftOut(data, clk, LSBFIRST, v);  // a,b,c,d,e,f,g,.
          shiftOut(data, clk, LSBFIRST, 0x00);  // leds: off
      }
      digitalWrite(strobe, HIGH);
      delay(30);
  }
}

uint8_t conv_num_seg(uint8_t num) {   
  uint8_t segment_display;
  uint8_t num_seg[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7F, 0x67 };
  segment_display=num_seg[num];                          
  return segment_display; 
}

void count(uint8_t butt_on) {
  if (butt_on > 8)  return;
 
   /*
   if 1  global[0]++, 
   if 2  global[1]++, 
   if 3  global[2]++, 
   if 4  global[3]++, 
   if (global[3] > 9) global[3] = 0, global[2]++, 
   if (global[2] > 9) global[2] = 0, global[1]++,
   if (global[1] > 9) global[1] = 0, global[0]++,
   if (global[0] > 9) global[0] = 0,
   show global[0],global[1],global[2],global[3]; */
   uint8_t seven_seg;
  
   for (int8_t i=0; i < 4; i++) {  
      if ((butt_on & 0x01) == 0x01) { //s1 ?
        global_digits[i]++;  
      }
      butt_on = butt_on >> 1;  
   }
   for (int8_t i=3; i > -1; i--) {  
    if (global_digits[i] == 10) {
      global_digits[i]=0;
      if (i > 0) global_digits[i-1]++;
    }
   }
    data_command(0x44); // fixed addressing
    for (uint8_t i= 0; i < 4; i++){
      digitalWrite(strobe,LOW);
      shiftOut(data, clk, LSBFIRST, 0xC0 + (i <<1)); 
      seven_seg=conv_num_seg(global_digits[i]); 
      shiftOut(data, clk, LSBFIRST, seven_seg);
      digitalWrite(strobe, HIGH);
    }
   delay(190);
}


void test_numbers(void) {
 unsigned char numbers[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 
                             0x7F, 0x67, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 };
 data_command(0x40);  // incremental addressing
 digitalWrite(strobe, LOW);
 shiftOut(data, clk, LSBFIRST, 0xc0); // 1100 0000 , set starting address to 0
 for (int i=0; i < 8; i++) {  //  0,1,2,3,4,5,6,7
  shiftOut(data, clk, LSBFIRST, numbers[i]); 
  shiftOut(data, clk, LSBFIRST, 0x00);  // leds: off
 }
 digitalWrite(strobe, HIGH);
 delay(1500);
 digitalWrite(strobe, LOW);
 shiftOut(data, clk, LSBFIRST, 0xc0); // 1100 0000 , set starting address to 0
 for (int i=8; i < 16; i++) {  //  0,1,2,3,4,5,6,7
  shiftOut(data, clk, LSBFIRST, numbers[i]); 
  shiftOut(data, clk, LSBFIRST, 0x00);  // leds: off
 }
 digitalWrite(strobe, HIGH);
}

void turnon_leds(uint8_t butt_on) {  
 
   digitalWrite(strobe, LOW);
   shiftOut(data, clk, LSBFIRST, 0x44); // fixed address  
   digitalWrite(strobe, HIGH); 
   
   for (int8_t i=1; i < 16; i += 2) {  // 1,3,5,7,9,11,13,15  
      digitalWrite(strobe, LOW);
      shiftOut(data, clk, LSBFIRST, 0xC0 + i); 
      if ((butt_on & 0x01) == 0x01)  shiftOut(data, clk, LSBFIRST, 0x01); // on
      else shiftOut(data, clk, LSBFIRST, 0x00); // off
      butt_on = butt_on >> 1;
      digitalWrite(strobe, HIGH);
   }
}

void fixed_address_led_run(void) {
  data_command(0x44);     // command mode + fixed addressing
  // 1100 0000 , set starting address to led1
  
  for (uint8_t i=1; i < 16; i+=2) { 
    digitalWrite(strobe, LOW);
    shiftOut(data, clk, LSBFIRST, 0xC0 + i); // 1,3,5,7,9,11,13,15 , set starting address to led1
    shiftOut(data, clk, LSBFIRST, 0x01);
    digitalWrite(strobe, HIGH);
    delay(300);
    digitalWrite(strobe, LOW);
    shiftOut(data, clk, LSBFIRST, 0xC0 + i); // 1,3,5,7,9,11,13,15 , set starting address to led1
    shiftOut(data, clk, LSBFIRST, 0x00);
    digitalWrite(strobe, HIGH); 
  }
  for (int8_t i=15; i > 0; i-=2) {   // NOT unsigned !!!
    digitalWrite(strobe, LOW);
    shiftOut(data, clk, LSBFIRST, 0xC0 + i); // 15,13,11,9,7,5,3,1
    shiftOut(data, clk, LSBFIRST, 0x01);
    digitalWrite(strobe, HIGH);
    delay(300);
    digitalWrite(strobe, LOW);
    shiftOut(data, clk, LSBFIRST, 0xC0 + i); 
    shiftOut(data, clk, LSBFIRST, 0x00);
    digitalWrite(strobe, HIGH); 
  }
}