Pico-Arduino
PicoPinFunction.h
1 #pragma once
2 
3 #include "pico/stdlib.h"
4 #include "hardware/gpio.h"
5 #include "hardware/adc.h"
6 #include "hardware/gpio.h"
7 #include "hardware/adc.h"
8 #include "hardware/pwm.h"
9 #include "hardware/clocks.h"
10 
19 enum PinFunction {PIN_FUNC_UNDEFINED, PIN_FUNC_GPIO, PIN_FUNC_ADC, PIN_FUNC_PWM};
20 
24 struct PinInfo {
25  PinFunction pin_function;
26  PinMode pinMode;
27  bool is_setup = false;
28 };
29 
31  public:
32  static PicoPinFunction &instance(){
33  static PicoPinFunction *inst_ptr;
34  if (inst_ptr==nullptr){
35  inst_ptr = new PicoPinFunction();
36  }
37  return *inst_ptr;
38  }
39 
40  // defines the actual Arduino PinMode
41  bool setPinMode(pin_size_t pinNumber, PinMode pinMode=INPUT){
42  Logger.debug("PicoGPIOFunction::PinMode");
43  bool changed = false;
44  if ( pinInfo[pinNumber].pinMode != pinMode){
45  Logger.debug("PicoGPIOFunction::PinMode -> has changed");
46  pinInfo[pinNumber].pinMode = pinMode;
47  pinInfo[pinNumber].is_setup = false;
48  changed = true;
49  }
50  return changed;
51  }
52 
53  bool isInput(pin_size_t pinNumber){
54  return pinInfo[pinNumber].pinMode==INPUT;
55  }
56 
57  bool isOutput(pin_size_t pinNumber){
58  return pinInfo[pinNumber].pinMode==OUTPUT;
59  }
60 
61  void clear(pin_size_t pinNumber){
62  Logger.debug("clear");
63  gpio_set_function(pinNumber, GPIO_FUNC_NULL);
64  pinInfo[pinNumber].is_setup = false;
65  }
66 
67  // setup Pico pin init function bysed on functionality
68  void usePin(pin_size_t pinNumber, PinFunction pinFunction){
69  //Logger.debug("PicoGPIOFunction::usePin", Logger.toStr(pinNumber));
70  PinInfo *info = & (pinInfo[pinNumber]);
71  //Logger.debug("is_setup:", pinInfo[pinNumber].is_setup ? "true" : "false");
72  if (!info->is_setup) {
73  Logger.debug("PicoGPIOFunction::usePin");
74  // just define the function
75  info->pin_function = pinFunction;
76  switch(info->pin_function){
77 
78  case PIN_FUNC_GPIO:
79  Logger.debug("setup PIN_FUNC_GPIO");
80  gpio_init(pinNumber);
81  gpio_set_dir(pinNumber, info->pinMode==OUTPUT ? GPIO_OUT: GPIO_IN);
82  if (info->pinMode==INPUT_PULLUP) gpio_pull_up(pinNumber);
83  if (info->pinMode==INPUT_PULLDOWN) gpio_pull_down(pinNumber);
84  info->is_setup = true;
85  break;
86 
87  case PIN_FUNC_ADC:
88  Logger.debug("setup PIN_FUNC_ADC");
89  initADC();
90  adc_gpio_init(pinNumber);
91  info->is_setup = true;
92  break;
93 
94  default:
95  Logger.warning("unsupported pin function");
96  break;
97 
98  }
99  }
100 
101  // for the PIN_FUNC_ADC we might need to swith the adc
102  if (info->pin_function==PIN_FUNC_ADC){
103  int adc = pinNumber - 26;
104  adcSelect(adc);
105  }
106 
107  }
108 
109  // select the ADC if it has been changed
110  bool adcSelect(int adc){
111  bool changed = false;
112  if (current_adc != adc){
113  Logger.debug("adc_select_input",Logger.toStr(adc));
114  adc_select_input((id_t)adc);
115  current_adc = adc;
116  changed = true;
117  }
118  return changed;
119  }
120 
121  // calls adc_init() if necessary - returns true if it has been intialized (the first time)
122  bool initADC(){
123  // init if necessary
124  bool result = false;
125  if (!adc_init_flag){
126  Logger.info("adc_init");
127  adc_init();
128  adc_init_flag = true;
129  result = true;
130  }
131  return result;
132  }
133 
134  void setPwmConfig(pwm_config config){
135  this->pwmconfig = config;
136  }
137 
138  protected:
139  PinInfo* pinInfo;
140  bool adc_init_flag;
141  int current_adc;
142  pwm_config pwmconfig;
143 
144 
145  PicoPinFunction(int maxPins=40){
146  Logger.debug("PicoGPIOFunction");
147  pinInfo = new PinInfo[maxPins];
148  }
149 };
150 
Definition: PicoPinFunction.h:30
Information about an the status and the Arduino PinMode of an individual pin.
Definition: PicoPinFunction.h:24