#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "pico/binary_info.h"
//#include "tusb.h"

const uint LED_PIN = 25;

// #define PUSH_BUTTON_01  4
const uint PUSH_BUTTON_01 = 4;
int led_state = 0;

void button_debounce_01(){
    // debounce button and check if the button went from high to low and is staying low
    // 20 ms delay for H to L edge debounce
    bool push_btn_1_state;
    printf("button_debounce_01() entered\n");
    // sleep_ms(1);
    //sleep_us(500);
    printf("sleep_ms() ended\n");
    if( gpio_get(PUSH_BUTTON_01) == 0){
        // debounce succesful, button press detected
        printf("01\n");
        if(led_state == 0){
          led_state = 1;
        } else{
          led_state = 0;
        }
    }
    else {
        printf("02\n");
        return;     // button press NOT detected
    }
    printf("03\n");
}

int main() {

  bi_decl(bi_program_description("This is a test binary."));
  bi_decl(bi_1pin_with_name(LED_PIN, "On-board LED"));

  stdio_init_all();

  // while (!tud_cdc_connected()) {
  //     tight_loop_contents();
  // }

  printf("hello button debounce test\n");

  gpio_init(LED_PIN);
  gpio_set_dir(LED_PIN, GPIO_OUT);

  gpio_init(2);
  gpio_init(4);
  gpio_init(6);
  gpio_set_dir(2, GPIO_IN);
  gpio_set_dir(4, GPIO_IN);
  gpio_set_dir(6, GPIO_IN);
  gpio_pull_up(2);
  gpio_pull_up(4);
  gpio_pull_up(6);
  gpio_set_irq_enabled_with_callback(PUSH_BUTTON_01, GPIO_IRQ_EDGE_FALL , true, &button_debounce_01);

// #if 0
//   while (1) {
//     gpio_put(LED_PIN, 0);
//     sleep_ms(250);
//     gpio_put(LED_PIN, 1);
//     puts("Hello World\n");
//     sleep_ms(1000);
//   }
// #endif

  while(1){
    int i = gpio_get(PUSH_BUTTON_01);
    printf("i = %d\n", i);
    gpio_put(LED_PIN, i);
    sleep_ms(500);
  }
}

// #include <stdio.h>
// #include "pico/stdlib.h"
// #include "hardware/gpio.h"

// void gpio_callback() {
//     printf("GPIO RISE\n");
// }

// int main() {
//     stdio_init_all();

//     printf("Hello GPIO IRQ\n");
//     gpio_init(2);
//     gpio_set_dir(2, GPIO_IN);
//     gpio_pull_up(2);
//     gpio_set_irq_enabled_with_callback(2, GPIO_IRQ_EDGE_RISE, true, &gpio_callback);

//     // Wait forever
//     while (1);

//     return 0;
// }

// #include <stdio.h>
// #include "pico/stdlib.h"
// // #include "hardware/gpio.h"
// // #include "hardware/regs/addressmap.h"
// // #include "hardware/regs/io_bank0.h"
// // #include "hardware/regs/timer.h"
// // #include "hardware/regs/m0plus.h"
// //#include "core_cm0plus.h"
// //#include "stm32f0xx.h"

// void main_asm();

// void link_gpio_set_dir(int pin, int dir)
// {
//   gpio_set_dir(pin, dir);
// }

// void link_gpio_put(int pin, int value)
// {
//   gpio_put(pin, value);
// }

// // void SysTick_Handler(void)
// // {
// //     // Increment a millisecond counter
// //     // Perform system-level functions
// // }

// // void link_print(int i)
// // {
// //   printf("isr_systick=%d\n", i);
// // }

// int main(void)
// {
//   //uint32_t systick_handler_addr = (uint32_t)&SysTick_Handler;
//   //*((volatile uint32_t *)0xE000ED04) = systick_handler_addr;
//   printf("Hello\n");
//   main_asm();
// }
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
gnd1:GND
led1:A
led1:C