I2COLDEBUTT — Arduino Uno simulation

An interactive Arduino Uno circuit simulation you can run free in your browser on Velxio, by tcflm.

Components

Sketch code

// Blink LED Example
// Toggles the built-in LED on pin 13
#include <stdint.h>//snprintf格式化字符串
#include <U8g2lib.h>//OLD图形库,提供字体和drawStr/drawBox等方法
#include <Wire.h>//OLDE使用I2C底层
//==============引脚与节拍===========
#define PIN_NEMU 13 //D13菜单/退出键
#define PIN_SET 12  //D12设置/贪睡键
#define MENU_LONG_MS 3000UL//菜单长按阈值
#define SET_LONG_MS 2000UL//设置长按阈值
#define DEBOUNCE_MS 10UL//短按最短按下时长20ms,滤机械抖动

#define FONT_BIG u8g2_font_10x20_tr//大字 
#define FONT_SM u8g2_font_8x13_tr//小字

//=====================按键扫描状态=========================
struct Key{               //结构体,合并单个按键的边沿和长按状态
  uint8_t pin;            //按键引脚
  bool rawPressde;        //最近一次采样的原始电状态
  bool stablePressed;     //消抖确认后的按下状态
  bool longDone;          //本次按住是否已经发出过长按事件
  unsigned long changedMs;//原始电平最近变化时刻
  unsigned long downMs;   //消抖确认按下的时刻,用于计算按住时长
  };
  Key menuKey = {PIN_NEMU,false,false,false,0,0};
  Key setKey = {PIN_SET,false,false,false,0,0};

#define ST_CLOCK 0        //待机走时界面
#define ST_RUN 1          //一级菜单
#define ST_EDIT 2         //时间、闹钟、贪睡编辑界面
#define ST_ALARM 3        //响铃闪烁界面
#define F_HT 0            //小时十位
#define F_HU 1            //小时个位
#define F_MT 2            //分钟十位
#define F_MU 3            //分钟个位

//====================全局变量===============
uint8_t state = ST_CLOCK;  // 当前界面状态,上电默认待机
uint8_t sel = 0;           // 一级菜单选中项,1..3;0表示尚未进入菜单
uint8_t menu = 0;          // 正在编辑的菜单项,1时间/2闹钟/3贪睡
uint8_t field = F_HT;      // 正在调整的时间数位

uint8_t editH = 8;       // 编辑中的小时,范围0..23
uint8_t editM = 30;      // 编辑中的分钟,范围0..59
uint8_t editSnooze = 5;  // 编辑中的贪睡分钟,范围1..30

uint8_t alarmH = 7;     // 闹钟小时,范围0..23
uint8_t alarmM = 0;     // 闹钟分钟,范围0..59
uint8_t snoozeMin = 5;  // 贪睡延迟分钟,范围1..30
uint8_t snoozeH = 0;    // 贪睡目标小时
uint8_t snoozeM = 0;    // 贪睡目标分钟
unsigned long swLast =0; //软件时钟兜底的1秒基准,硬件启动开始计时,秒数
unsigned long lastDrawMS = 0; // 上次OLED完整刷新时刻
uint16_t checkedMinute = 1440; // 上次检测的日内分钟;1440表示尚未检测
uint8_t drawnState = 255;// 上次绘制的界面状态;255保证上电首帧重绘
uint8_t swH=8;        //时
uint8_t swM=30;       //分
uint8_t swS =0;       //秒

U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0,U8X8_PIN_NONE);

//功能函数==========================
void  tickSoft(unsigned long now)//时间记时
{
  while(now - swLast >= 1000UL)
  {         //运行时间有1秒了,
    swLast += 1000UL;
    swS++;//加1秒
    if(swS >= 60)
    {
      swS =0 ;
      swM++;//加一分
      if(swM  >=60)
      {
        swM=0;
        swH++;//加小时
        if(swH >=24) swH =0;//到24小时 归0
      }
    }
  }
}

//================显示辅助格式===========
//把时分格式化为HH:MM字符串
void fmtTine(char * out,int h,int m)
{
  snprintf(out,6,"%02d:02d",h,m);//按12:30显示,十位补0,08:05
}
//计算字符串在128PX宽屏上水平居中的起点X座标
int centerX(const char *s)
{
  return (128-u8g2.getStrWidth(s))/2;//getStrWidth返回字符串像素宽度,屏宽-字宽/2
}
//=================界面绘制====================
void drawClock()
{
  char tm[6];
  char line[16];
  int h,m,s;
  h=swH ;m=swM;s=swS;
  u8g2.firstPage();
  do{
    u8g2.setFont(FONT_BIG);
    fmtTine(tm,h,m);
    u8g2.drawStr(centerX(tm), 28, tm);//居中显示时间
    u8g2.setFont(FONT_SM)

More projects by tcflm