# coding: utf-8

@lcd_address = 0x3e
@rtc_address = 0x32
  
def lcd_cmd(i2c, cmd)
  i2c.write(@lcd_address, [0x00, cmd])
end

def lcd_data(i2c, data)
  #自分で書く
end

def lcd_clear(i2c)
  #自分で書く
end

def lcd_home0(i2c)
  #自分で書く
end

def lcd_home1(i2c)
  #自分で書く
end

def lcd_cursor(i2c, x, y)
  #自分で書く
end

def lcd_init(i2c)
  sleep 0.2
  [0x38, 0x39, 0x14, 0x70, 0x56, 0x6c].each do |cmd|
    lcd_cmd(i2c, cmd)
  end
  sleep(0.3)
  #初期化の後半は自分で書く
  sleep(0.1)
end

def lcd_print(i2c, data)
  data.length.times do |n|
    lcd_data(i2c, data[n].ord)
  end
end

def rtc2_init(i2c)
  #自分で書く
end

def rtc2_set(i2c)
  #自分で書く
end

def rtc2_get(i2c, tt)
  # 読み込みは自分で書く
  buf = ...   # 8 バイト分読み込み

  #読み込んだデータをハッシュに代入
  tt['year'] = buf[7]
  tt['mon']  = buf[6]
  tt['mday'] = buf[5]
  tt['hour'] = buf[3] & 0x3f
  tt['min']  = buf[2]
  tt['sec']  = buf[1]
end


#I2C 初期化
i2c = I2C.new(22, 21)

# LCD 初期化
lcd_init(i2c)

# LCD に "Hello World" 表示
# 自分で書くこと


sleep(5)

# RTC 初期化. 時刻設定
rtc2_init(i2c)

# 時刻の初期化 (2021/04/30 23:59:50 にすること)
rtc2_set(i2c)

while true
  # 時刻表示
  tt = {}  #ハッシュ
  rtc2_get(i2c, tt)

  time0 = sprintf("%02x-%02x-%02x", tt['year'], tt['mon'], tt['mday'])
  time1 = sprintf("%02x:%02x:%02x", tt['hour'], tt['min'], tt['sec'])

  # time0, time1 を LCD に表示する部分は自分で書くこと

  sleep(1)
end


