|
| 1 | +/* -------------------------------------------------------------------------- */ |
| 2 | +/* FILE NAME: pwm_100.ino |
| 3 | + AUTHOR: Daniele Aimo |
| 4 | + |
| 5 | + DATE: 20240422 |
| 6 | + DESCRIPTION: |
| 7 | + LICENSE: Copyright (c) 2024 Arduino SA |
| 8 | + his Source Code Form is subject to the terms fo the Mozilla |
| 9 | + Public License (MPL), v 2.0. You can obtain a copy of the MPL |
| 10 | + at http://mozilla.org/MPL/2.0/. |
| 11 | + NOTES: */ |
| 12 | +/* -------------------------------------------------------------------------- */ |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +#include "OptaBlue.h" |
| 17 | + |
| 18 | +using namespace Opta; |
| 19 | + |
| 20 | +#define UPDATE_TIME 1000 |
| 21 | + |
| 22 | + |
| 23 | +/* -------------------------------------------------------------------------- */ |
| 24 | +/* SETUP */ |
| 25 | +/* -------------------------------------------------------------------------- */ |
| 26 | +void setup() { |
| 27 | +/* -------------------------------------------------------------------------- */ |
| 28 | + Serial.begin(115200); |
| 29 | + delay(2000); |
| 30 | + |
| 31 | + Serial.println("+ SETUP"); |
| 32 | + |
| 33 | + OptaController.begin(); |
| 34 | +} |
| 35 | + |
| 36 | +/* -------------------------------------------------------------------------- */ |
| 37 | +void printUint16(uint16_t v) { |
| 38 | +/* -------------------------------------------------------------------------- */ |
| 39 | + if(v < 10) { |
| 40 | + Serial.print(" "); |
| 41 | + } |
| 42 | + else if(v < 100) { |
| 43 | + Serial.print(" "); |
| 44 | + |
| 45 | + } |
| 46 | + else if(v < 1000) { |
| 47 | + Serial.print(" "); |
| 48 | + |
| 49 | + } |
| 50 | + else if(v < 10000) { |
| 51 | + Serial.print(" "); |
| 52 | + } |
| 53 | + Serial.print(v); |
| 54 | +} |
| 55 | + |
| 56 | +/* -------------------------------------------------------------------------- */ |
| 57 | +void printExpansionType(ExpansionType_t t) { |
| 58 | +/* -------------------------------------------------------------------------- */ |
| 59 | + if(t == EXPANSION_NOT_VALID) { |
| 60 | + Serial.print("Unknown!"); |
| 61 | + } |
| 62 | + else if(t == EXPANSION_OPTA_DIGITAL_MEC) { |
| 63 | + Serial.print("DIGITAL [Mechanical]"); |
| 64 | + } |
| 65 | + else if(t == EXPANSION_OPTA_DIGITAL_STS) { |
| 66 | + Serial.print("DIGITAL [Solid State]"); |
| 67 | + } |
| 68 | + else if(t == EXPANSION_DIGITAL_INVALID) { |
| 69 | + Serial.print("DIGITAL [!!Invalid!!]"); |
| 70 | + } |
| 71 | + else if(t == EXPANSION_OPTA_ANALOG) { |
| 72 | + Serial.print("ANALOG"); |
| 73 | + } |
| 74 | + else { |
| 75 | + Serial.print("Unknown!"); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +int getIntegerFromSerial() { |
| 80 | +/* -------------------------------------------------------------------------- */ |
| 81 | + /* basic function that get a integer from serial |
| 82 | + * (can be improved....)*/ |
| 83 | + |
| 84 | + /* this function returns -1 if the user just hit return without entering |
| 85 | + * any number */ |
| 86 | + int rv = -1; |
| 87 | + /* wait for user to write something on the serial line */ |
| 88 | + while(!Serial.available()) { |
| 89 | + } |
| 90 | + |
| 91 | + /* get the number (everything is not a number is ignored) */ |
| 92 | + while(Serial.available()) { |
| 93 | + |
| 94 | + int num = Serial.read(); |
| 95 | + |
| 96 | + if( (num >= 0x30 && num <= 0x39) ) { |
| 97 | + if(rv == -1) { |
| 98 | + rv = 0; |
| 99 | + } |
| 100 | + rv *= 10; |
| 101 | + rv += num - 0x30; |
| 102 | + } |
| 103 | + } |
| 104 | + return rv; |
| 105 | +} |
| 106 | +/* -------------------------------------------------------------------------- */ |
| 107 | +int getIntegerNonBlocking() { |
| 108 | +/* -------------------------------------------------------------------------- */ |
| 109 | + /* basic function that get a integer from serial |
| 110 | + * (can be improved....) |
| 111 | + * it does not wait for input from serial and return -1 if*/ |
| 112 | + int rv = -1; |
| 113 | + if(Serial.available()) { |
| 114 | + rv = 0; |
| 115 | + } |
| 116 | + |
| 117 | + while(Serial.available()) { |
| 118 | + int num = Serial.read(); |
| 119 | + |
| 120 | + if( (num >= 0x30 && num <= 0x39) ) { |
| 121 | + rv *= 10; |
| 122 | + rv += num - 0x30; |
| 123 | + } |
| 124 | + else if(num == 'm' || num == 'M') { |
| 125 | + rv = 0x7FFFFFFF; |
| 126 | + } |
| 127 | + } |
| 128 | + return rv; |
| 129 | +} |
| 130 | + |
| 131 | +int number_of_expansion = 0; |
| 132 | +int last_number_of_expansion = 0; |
| 133 | +bool print_menu_flag = true; |
| 134 | + |
| 135 | +/* -------------------------------------------------------------------------- */ |
| 136 | +void print_menu() { |
| 137 | +/* -------------------------------------------------------------------------- */ |
| 138 | + if(print_menu_flag) { |
| 139 | + Serial.println("### Climatic chamber tests: Input channels measurement ###"); |
| 140 | + Serial.println(); |
| 141 | + Serial.println("Number of expansions: " + String(number_of_expansion)); |
| 142 | + if(number_of_expansion > 0) { |
| 143 | + Serial.println("List of Expansions: "); |
| 144 | + for(int i = 0; i < number_of_expansion; i++) { |
| 145 | + Serial.print(i); |
| 146 | + Serial.print(") "); |
| 147 | + printExpansionType(OptaController.getExpansionType(i)); |
| 148 | + Serial.println(); |
| 149 | + } |
| 150 | + } |
| 151 | + Serial.println("*** MAIN MENU: (at any moment type 'm' to see the menu)"); |
| 152 | + Serial.println("1. Select the ANALOG expansion"); |
| 153 | + Serial.println("2. Toggle PWM output duty 100% / 0%"); |
| 154 | + Serial.println(":> "); |
| 155 | + print_menu_flag = false; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | +bool output_on[5] = {false,false,false,false,false}; |
| 161 | + |
| 162 | +/* -------------------------------------------------------------------------- */ |
| 163 | +void toggle(int exp_index) { |
| 164 | +/* -------------------------------------------------------------------------- */ |
| 165 | + static unsigned long start = millis() + UPDATE_TIME + 1; |
| 166 | + AnalogExpansion ae = OptaController.getExpansion(exp_index); |
| 167 | + if(millis() - start > UPDATE_TIME) { |
| 168 | + Serial.println("----"); |
| 169 | + start = millis(); |
| 170 | + if(ae) { |
| 171 | + if(!output_on[ae.getIndex()]) { |
| 172 | + // turn on |
| 173 | + output_on[ae.getIndex()] = true; |
| 174 | + for(int k = 0; k < 8; k++) { |
| 175 | + ae.setPwm(k, 100, 100); |
| 176 | + |
| 177 | + } |
| 178 | + Serial.print("Expansion n. "); |
| 179 | + Serial.print(exp_index); |
| 180 | + Serial.println(" -> All PWM channels on 100% duty cycle"); |
| 181 | + } |
| 182 | + else { |
| 183 | + // turn off |
| 184 | + for(int k = 0; k < 8; k++) { |
| 185 | + ae.setPwm(k, 0, 1); |
| 186 | + } |
| 187 | + output_on[ae.getIndex()] = false; |
| 188 | + Serial.print("Expansion n. "); |
| 189 | + Serial.print(exp_index); |
| 190 | + Serial.println(" -> All PWM channels switched off (duty cycle 0%)"); |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | + else { |
| 196 | + Serial.println("Expansion is not digital"); |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +uint8_t selected_expansion = 255; |
| 202 | +bool toggle_output = false; |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | +/* -------------------------------------------------------------------------- */ |
| 207 | +/* LOOP */ |
| 208 | +/* -------------------------------------------------------------------------- */ |
| 209 | +void loop() { |
| 210 | +/* -------------------------------------------------------------------------- */ |
| 211 | + OptaController.update(); |
| 212 | + |
| 213 | + if(number_of_expansion != OptaController.getExpansionNum()) { |
| 214 | + print_menu_flag = true; |
| 215 | + } |
| 216 | + number_of_expansion = OptaController.getExpansionNum(); |
| 217 | + print_menu(); |
| 218 | + int result = getIntegerNonBlocking(); |
| 219 | + |
| 220 | + if(result > -1) { |
| 221 | + if(result == 0x7FFFFFFF) { |
| 222 | + print_menu_flag = true; |
| 223 | + } |
| 224 | + else if(result == 1) { |
| 225 | + Serial.println(" - Select expansion:> "); |
| 226 | + selected_expansion = getIntegerFromSerial(); |
| 227 | + Serial.println(" -> Selected expansion " + |
| 228 | + String(selected_expansion)); |
| 229 | + print_menu_flag = true; |
| 230 | + } |
| 231 | + else if(result == 2) { |
| 232 | + toggle_output = true; |
| 233 | + } |
| 234 | + else if(result == 3) { |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + if(selected_expansion < 5 && toggle_output) { |
| 239 | + toggle_output = false; |
| 240 | + toggle(selected_expansion); |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | + |
| 245 | + |
0 commit comments