#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#define CLOCK_ADDR 0b1101000
#define CLOCK_SDA 10
#define CLOCK_SCL 11
// void clock_init(){
// gpio_init(CLOCK_SDA);
// gpio_init(CLOCK_SCL);
// gpio_set_dir(CLOCK_SDA, GPIO_IN);
// gpio_set_dir(CLOCK_SCL, GPIO_IN);
// gpio_pull_up(CLOCK_SDA);
// gpio_pull_up(CLOCK_SCL);
// }
// void clockk_start(){
// gpio_set_dir(CLOCK_SCL, GPIO_IN);
// gpio_pull_up(CLOCK_SCL);
// gpio_set_dir(CLOCK_SDA, GPIO_IN);
// gpio_pull_up(CLOCK_SDA);
// sleep_us( 100);
// gpio_set_dir(CLOCK_SDA, GPIO_OUT);
// gpio_put(CLOCK_SDA, 0);
// sleep_us(100);
// gpio_set_dir(CLOCK_SCL, GPIO_OUT);
// gpio_put(CLOCK_SCL, 0);
// sleep_us(100);
// }
// void clockk_stop(){
// gpio_set_dir(CLOCK_SCL, GPIO_OUT);
// gpio_put(CLOCK_SCL, 0);
// sleep_us( 100);
// gpio_set_dir(CLOCK_SDA, GPIO_IN);
// gpio_pull_up(CLOCK_SDA);
// sleep_us( 100);
// gpio_set_dir(CLOCK_SCL, GPIO_IN);
// gpio_pull_up(CLOCK_SCL);
// sleep_us( 100);
// }
// bool clockk_write(unsigned char data){
// for(signed char i=7; i>=0; i--){
// if(data & (1<<i)){
// gpio_set_dir(CLOCK_SDA, GPIO_IN);
// gpio_pull_up(CLOCK_SDA);
// }
// else{
// gpio_set_dir(CLOCK_SDA, GPIO_OUT);
// gpio_put(CLOCK_SDA, 0);
// }
// sleep_us( 100);
// //SCL
// gpio_set_dir(CLOCK_SCL, GPIO_IN);
// gpio_pull_up(CLOCK_SCL);
// sleep_us( 100);
// gpio_set_dir(CLOCK_SCL, GPIO_OUT);
// gpio_put(CLOCK_SCL, 0);
// sleep_us( 100);
// }
// gpio_set_dir(CLOCK_SCL, GPIO_IN);
// gpio_pull_up(CLOCK_SCL);
// gpio_set_dir(CLOCK_SDA, GPIO_IN);
// gpio_pull_up(CLOCK_SDA);
// sleep_us( 100);
// bool a = gpio_get(CLOCK_SDA);
// gpio_set_dir(CLOCK_SCL, GPIO_OUT);
// gpio_put(CLOCK_SCL, 0);
// sleep_us( 100);
// return a;
// }
// bool clockk_read(unsigned char *data){
// gpio_set_dir(CLOCK_SDA, GPIO_IN);
// gpio_pull_up(CLOCK_SDA);
// *data = 0;
// for(signed char i=7; i>=0; i--){
// //SCL
// gpio_set_dir(CLOCK_SCL, GPIO_IN);
// gpio_pull_up(CLOCK_SCL);
// sleep_us( 100);
// if(gpio_get(CLOCK_SDA)){
// *data |= 1<<i;
// }
// gpio_set_dir(CLOCK_SCL, GPIO_OUT);
// gpio_put(CLOCK_SCL, 0);
// sleep_us( 100);
// }
// gpio_set_dir(CLOCK_SCL, GPIO_IN);
// gpio_pull_up(CLOCK_SCL);
// gpio_set_dir(CLOCK_SDA, GPIO_IN);
// gpio_pull_up(CLOCK_SDA);
// sleep_us( 100);
// bool a = gpio_get(CLOCK_SDA);
// gpio_set_dir(CLOCK_SCL, GPIO_OUT);
// gpio_put(CLOCK_SCL, 0);
// sleep_us( 100);
// return a;
// }
// //////////////////////////////////////
// unsigned char clockk_read_register(unsigned char addr){
// clockk_start();
// unsigned char r;
// bool a = clockk_write(CLOCK_ADDR<<1);
// a = clockk_write(addr);
// clockk_stop();
// clockk_start();
// a = clockk_write(CLOCK_ADDR<<1 | 1);
// clockk_read(&r);
// clockk_stop();
// return r;
// }
// unsigned char clockk_get_seconds(){
// return clockk_read_register(0);
// }
// unsigned char clockk_get_minutes(){
// return clockk_read_register(1);
// }
// unsigned char clockk_get_hours(){
// return clockk_read_register(2);
// }
#include "hardware/i2c.h"
#define CLOCK_I2C i2c1
unsigned char clockk_get_seconds(){
unsigned char s=0;
unsigned char a=0;
i2c_write_blocking(CLOCK_I2C, CLOCK_ADDR, &a, 1, false);
i2c_read_blocking(CLOCK_I2C, CLOCK_ADDR, &s, 1, false);
return s;
}
unsigned char clockk_get_minutes(){
unsigned char m=0;
unsigned char a=1;
i2c_write_blocking(CLOCK_I2C, CLOCK_ADDR, &a, 1, false);
i2c_read_blocking(CLOCK_I2C, CLOCK_ADDR, &m, 1, false);
return m;
}
unsigned char clockk_get_hours(){
unsigned char h=0;
unsigned char a=2;
i2c_write_blocking(CLOCK_I2C, CLOCK_ADDR, &a, 1, false);
i2c_read_blocking(CLOCK_I2C, CLOCK_ADDR, &h, 1, false);
return h;
}
int main() {
// clock_init();
stdio_init_all();
gpio_init(CLOCK_SDA);
gpio_init(CLOCK_SCL);
gpio_pull_up(CLOCK_SDA);
gpio_pull_up(CLOCK_SCL);
gpio_set_function(CLOCK_SDA, GPIO_FUNC_I2C);
gpio_set_function(CLOCK_SCL, GPIO_FUNC_I2C);
i2c_init (i2c1, 100000); // 100000 | 10^5
printf("Hello, Wokwi!\n");
while (true) {
unsigned char s = clockk_get_seconds();
unsigned char m = clockk_get_minutes();
unsigned char h = clockk_get_hours();
printf("time %x:%x:%x\n", h, m, s);
sleep_ms(1000);
}
}