├── .gitignore ├── CMakeLists.txt ├── README.md ├── Telemetrix4RpiPico.c ├── Telemetrix4RpiPico.pio ├── archive └── dht.c ├── cmake-build-release └── Telemetrix4RpiPico.uf2 ├── images └── tmx.png ├── include └── Telemetrix4RpiPico.h ├── license.txt └── pico_sdk_import.cmake /.gitignore: -------------------------------------------------------------------------------- 1 | # Project exclude paths 2 | /cmake-build-debug/ 3 | # /cmake-build-release/ -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12) 2 | set(CMAKE_CXX_STANDARD 14) 3 | include(pico_sdk_import.cmake) 4 | project(Telemetrix4RpiPico) 5 | pico_sdk_init() 6 | add_executable(Telemetrix4RpiPico 7 | Telemetrix4RpiPico.c 8 | ) 9 | 10 | pico_generate_pio_header(Telemetrix4RpiPico ${CMAKE_CURRENT_LIST_DIR}/Telemetrix4RpiPico.pio) 11 | pico_enable_stdio_usb(Telemetrix4RpiPico 1) 12 | pico_add_extra_outputs(Telemetrix4RpiPico) 13 | target_link_libraries(Telemetrix4RpiPico pico_stdlib hardware_pwm 14 | pico_unique_id hardware_watchdog hardware_adc hardware_i2c 15 | hardware_pio hardware_clocks hardware_spi) 16 | 17 | add_executable(pio_Telemetrix4RpiPico) 18 | # generate the header file into the source tree as it is included in the RP2040 datasheet 19 | pico_generate_pio_header(pio_Telemetrix4RpiPico ${CMAKE_CURRENT_LIST_DIR}/Telemetrix4RpiPico.pio 20 | OUTPUT_DIR ${CMAKE_CURRENT_LIST_DIR}/generated) 21 | 22 | target_sources(pio_Telemetrix4RpiPico PRIVATE Telemetrix4RpiPico.c) 23 | pico_add_extra_outputs(pio_Telemetrix4RpiPico) 24 | 25 | add_custom_target(pio_Telemetrix4RpiPico_datasheet DEPENDS ${CMAKE_CURRENT_LIST_DIR}/generated/Telemetrix4RpiPico.py) 26 | add_custom_command(OUTPUT ${CMAKE_CURRENT_LIST_DIR}/generated/Telemetrix4RpiPico.py 27 | DEPENDS Telemetrix4RpiPico.pio 28 | COMMAND Pioasm -o python Telemetrix4RpiPico.pio ./generated/Telemetrix4RpiPico 29 | ) 30 | add_dependencies(pio_Telemetrix4RpiPico pio_Telemetrix4RpiPico_datasheet) 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Telemetrix4RpiPico 2 | 3 | ![](images/tmx.png) 4 | 5 | The Pico server code may be viewed [here.](https://github.com/MrYsLab/Telemetrix4RpiPico) 6 | 7 | You may download the .uf2 file [here.](https://github.com/MrYsLab/Telemetrix4RpiPico/raw/master/cmake-build-release/Telemetrix4RpiPico.uf2) 8 | 9 | The following functionality is implemented in this release: 10 | 11 | * Analog Input 12 | * Digital Input, Digital Input Pullup, Digital Input Pulldown 13 | * PWM output 14 | * Loopback (for client/server link debugging) 15 | * I2C Support 16 | * SPI Support 17 | * NeoPixel Support 18 | * Servo Support 19 | * HC-SR04 Type Sonar Distance Sensor Support 20 | * DHT 11 and 22 Humidity/Temperature Sensor Support 21 | * Autodetect PICO device over USB Serial. 22 | * Automatic board reset of the PICO using the watchdog timer when application exits. 23 | * Board will blink twice upon reset. 24 | * Retrieval of the PICO's unique ID. 25 | 26 | The Telemetrix4RpiPico server, in conjunction with its [client peer](https://github.com/MrYsLab/telemetrix-rpi-pico), 27 | allows you to control a Raspberry Pi Pico remotely from your 28 | PC. A complete [User's Guide](https://mryslab.github.io/telemetrix-rpi-pico/) is available describing how to 29 | install and use both the server and client. 30 | 31 | To install the server, follow these [installation instructions](https://mryslab.github.io/telemetrix-rpi-pico/install_pico_server/). 32 | -------------------------------------------------------------------------------- /Telemetrix4RpiPico.c: -------------------------------------------------------------------------------- 1 | 2 | /******************************************************** 3 | * Copyright (c) 2021 Alan Yorinks All rights reserved. 4 | 5 | This program is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE 7 | Version 3 as published by the Free Software Foundation; either 8 | or (at your option) any later version. 9 | This library is distributed in the hope that it will be useful,f 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | General Public License for more details. 13 | 14 | You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE 15 | along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | /******************** Attributions *********************************** 20 | * This file contains modifications of the work of others to support some 21 | * of the project's features. 22 | * 23 | * Neopixel support: https://github.com/raspberrypi/pico-examples/tree/master/pio/ws2812 24 | * 25 | * DHT sensor support: https://github.com/raspberrypi/pico-examples/tree/master/gpio/dht_sensor 26 | * 27 | * HC-SR04 sensor support: https://github.com/GitJer/Some_RPI-Pico_stuff/tree/main/HCSR04 28 | * 29 | *************************************************************************/ 30 | 31 | #pragma clang diagnostic push 32 | #pragma ide diagnostic ignored "EndlessLoop" 33 | 34 | #include "include/Telemetrix4RpiPico.h" 35 | 36 | /******************************************************************* 37 | * GLOBAL VARIABLES, AND STORAGE 38 | ******************************************************************/ 39 | 40 | const uint LED_PIN = 25; // board LED 41 | 42 | // buffer to hold incoming command data 43 | uint8_t command_buffer[MAX_COMMAND_LENGTH]; 44 | 45 | bool stop_reports = false; // a flag to stop sending all report messages 46 | 47 | // an array of digital_pin_descriptors 48 | pin_descriptor the_digital_pins[MAX_DIGITAL_PINS_SUPPORTED]; 49 | 50 | // an array of analog_pin_descriptors 51 | analog_pin_descriptor the_analog_pins[MAX_ANALOG_PINS_SUPPORTED]; 52 | 53 | // number of active sonars 54 | int sonar_count = -1; 55 | uint sonar_offset; 56 | 57 | // hc-sr04 pio support values 58 | PIO sonar_pio = pio1; 59 | 60 | // sonar device descriptors 61 | sonar_data the_hc_sr04s = {.next_sonar_index = 0}; 62 | 63 | // number of active dht devices 64 | int dht_count = -1; 65 | 66 | // dht device descriptors 67 | dht_data the_dhts = {.next_dht_index = 0}; 68 | 69 | // pio for neopixel values 70 | PIO np_pio = pio0; 71 | uint np_sm = 0; 72 | 73 | // neopixel storage for up to 150 pixel string 74 | // Each entry contains an RGG array. 75 | 76 | uint8_t pixel_buffer[MAXIMUM_NUM_NEOPIXELS][3]; 77 | 78 | uint actual_number_of_pixels; 79 | 80 | static inline void put_pixel(uint32_t pixel_grb) { 81 | pio_sm_put_blocking(pio0, 0, pixel_grb << 8u); 82 | 83 | } 84 | 85 | static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) { 86 | return 87 | ((uint32_t) (r) << 8) | 88 | ((uint32_t) (g) << 16) | 89 | (uint32_t) (b); 90 | } 91 | 92 | // PWM values 93 | uint32_t top; 94 | 95 | // for dht repeating read timer 96 | struct repeating_timer timer; 97 | volatile bool timer_fired = false; 98 | 99 | 100 | /******************* REPORT BUFFERS *******************/ 101 | // NOTE First value in the array is the number of reporting 102 | // data elements. It does not include itself in this count. 103 | 104 | // buffer to hold data for the loop_back command 105 | // The last element will be filled in by the loopback command 106 | int loop_back_report_message[] = {2, (int) SERIAL_LOOP_BACK, 0}; 107 | 108 | // buffer to hold data for send_debug_info command 109 | uint debug_info_report_message[] = {4, DEBUG_PRINT, 0, 0, 0}; 110 | 111 | // buffer to hold firmware version info 112 | int firmware_report_message[] = {3, FIRMWARE_REPORT, FIRMWARE_MAJOR, FIRMWARE_MINOR}; 113 | 114 | // buffer to hold i2c report data 115 | int i2c_report_message[64]; 116 | 117 | // buffer to hold spi report data 118 | int spi_report_message[64]; 119 | 120 | 121 | // get_pico_unique_id report buffer 122 | int unique_id_report_report_message[] = {9, REPORT_PICO_UNIQUE_ID, 123 | 0, 0, 0, 0, 0, 0, 0, 0}; 124 | // digital input report buffer 125 | int digital_input_report_message[] = {3, DIGITAL_REPORT, 0, 0}; 126 | 127 | // analog input report message 128 | int analog_input_report_message[] = {4, ANALOG_REPORT, 0, 0, 0}; 129 | 130 | // sonar report message 131 | int sonar_report_message[] = {4, SONAR_DISTANCE, 0, 0, 0}; 132 | 133 | // dht report message 134 | int dht_report_message[] = {6, DHT_REPORT, 0, 0, 0, 0, 0,}; 135 | 136 | /***************************************************************** 137 | * THE COMMAND TABLE 138 | When adding a new command update the command_table. 139 | The command length is the number of bytes that follow 140 | the command byte itself, and does not include the command 141 | byte in its length. 142 | The command_func is a pointer the command's function. 143 | ****************************************************************/ 144 | // An array of pointers to the command functions 145 | command_descriptor command_table[] = 146 | { 147 | {&serial_loopback}, 148 | {&set_pin_mode}, 149 | {&digital_write}, 150 | {&pwm_write}, 151 | {&modify_reporting}, 152 | {&get_firmware_version}, 153 | {&get_pico_unique_id}, 154 | {&servo_attach}, 155 | {&servo_write}, 156 | {&servo_detach}, 157 | {&i2c_begin}, 158 | {&i2c_read}, 159 | {&i2c_write}, 160 | {&sonar_new}, 161 | {&dht_new}, 162 | {&stop_all_reports}, 163 | {&enable_all_reports}, 164 | {&reset_data}, 165 | {&reset_board}, 166 | {&init_neo_pixels}, 167 | {&show_neo_pixels}, 168 | {&set_neo_pixel}, 169 | {&clear_all_neo_pixels}, 170 | {&fill_neo_pixels}, 171 | {&init_spi}, 172 | {&write_blocking_spi}, 173 | {&read_blocking_spi}, 174 | {&set_format_spi}, 175 | {&spi_cs_control} 176 | }; 177 | 178 | 179 | /*************************************************************************** 180 | * DEBUGGING FUNCTIONS 181 | **************************************************************************/ 182 | 183 | /************************************************************ 184 | * Loop back the received character 185 | */ 186 | void serial_loopback() { 187 | loop_back_report_message[LOOP_BACK_DATA] = command_buffer[DATA_TO_LOOP_BACK]; 188 | serial_write(loop_back_report_message, 189 | sizeof(loop_back_report_message) / sizeof(int)); 190 | } 191 | 192 | /****************************************************************** 193 | * Send debug info report 194 | * @param id: 8 bit value 195 | * @param value: 16 bit value 196 | */ 197 | // A method to send debug data across the serial link 198 | void send_debug_info(uint id, uint value) { 199 | debug_info_report_message[DEBUG_ID] = id; 200 | debug_info_report_message[DEBUG_VALUE_HIGH_BYTE] = (value & 0xff00) >> 8; 201 | debug_info_report_message[DEBUG_VALUE_LOW_BYTE] = value & 0x00ff; 202 | serial_write((int *) debug_info_report_message, 203 | sizeof(debug_info_report_message) / sizeof(int)); 204 | } 205 | 206 | /************************************************************ 207 | * Blink the board led 208 | * @param blinks - number of blinks 209 | * @param delay - delay in milliseconds 210 | */ 211 | void led_debug(int blinks, uint delay) { 212 | for (int i = 0; i < blinks; i++) { 213 | gpio_put(LED_PIN, 1); 214 | sleep_ms(delay); 215 | gpio_put(LED_PIN, 0); 216 | sleep_ms(delay); 217 | } 218 | } 219 | 220 | /******************************************************************************* 221 | * COMMAND FUNCTIONS 222 | ******************************************************************************/ 223 | 224 | /************************************************************************ 225 | * Set a Pins mode 226 | */ 227 | void set_pin_mode() { 228 | uint pin; 229 | uint mode; 230 | pin = command_buffer[SET_PIN_MODE_GPIO_PIN]; 231 | mode = command_buffer[SET_PIN_MODE_MODE_TYPE]; 232 | 233 | switch (mode) { 234 | case DIGITAL_INPUT: 235 | case DIGITAL_INPUT_PULL_UP: 236 | case DIGITAL_INPUT_PULL_DOWN: 237 | the_digital_pins[pin].pin_mode = mode; 238 | the_digital_pins[pin].reporting_enabled = command_buffer[SET_PIN_MODE_DIGITAL_IN_REPORTING_STATE]; 239 | gpio_init(pin); 240 | gpio_set_dir(pin, GPIO_IN); 241 | if (mode == DIGITAL_INPUT_PULL_UP) { 242 | gpio_pull_up(pin); 243 | } 244 | if (mode == DIGITAL_INPUT_PULL_DOWN) { 245 | gpio_pull_down(pin); 246 | } 247 | break; 248 | case DIGITAL_OUTPUT: 249 | the_digital_pins[pin].pin_mode = mode; 250 | gpio_init(pin); 251 | gpio_set_dir(pin, GPIO_OUT); 252 | break; 253 | case PWM_OUTPUT: 254 | /* Here we will set the operating frequency to be 50 hz to 255 | simplify support PWM as well as servo support. 256 | */ 257 | the_digital_pins[pin].pin_mode = mode; 258 | 259 | const uint32_t f_hz = 50; // frequency in hz. 260 | 261 | uint slice_num = pwm_gpio_to_slice_num(pin); // get PWM slice for the pin 262 | 263 | // set frequency 264 | // determine top given Hz using the free-running clock 265 | uint32_t f_sys = clock_get_hz(clk_sys); 266 | float divider = (float) (f_sys / 1000000UL); // run the pwm clock at 1MHz 267 | pwm_set_clkdiv(slice_num, divider); // pwm clock should now be running at 1MHz 268 | top = 1000000UL / f_hz - 1; // calculate the TOP value 269 | pwm_set_wrap(slice_num, (uint16_t) top); 270 | 271 | // set the current level to 0 272 | pwm_set_gpio_level(pin, 0); 273 | 274 | pwm_set_enabled(slice_num, true); // let's go! 275 | gpio_set_function(pin, GPIO_FUNC_PWM); 276 | break; 277 | 278 | case ANALOG_INPUT: 279 | //if the temp sensor was selected, then turn it on 280 | if (pin == ADC_TEMPERATURE_REGISTER) { 281 | adc_set_temp_sensor_enabled(true); 282 | } 283 | the_analog_pins[pin].reporting_enabled = command_buffer[SET_PIN_MODE_ANALOG_IN_REPORTING_STATE]; 284 | // save the differential value 285 | the_analog_pins[pin].differential = 286 | (int) ((command_buffer[SET_PIN_MODE_ANALOG_DIFF_HIGH] << 8) + 287 | command_buffer[SET_PIN_MODE_ANALOG_DIFF_LOW]); 288 | break; 289 | default: 290 | break; 291 | } 292 | } 293 | 294 | /********************************************************** 295 | * Set a digital output pin's value 296 | */ 297 | void digital_write() { 298 | uint pin; 299 | uint value; 300 | pin = command_buffer[DIGITAL_WRITE_GPIO_PIN]; 301 | value = command_buffer[DIGITAL_WRITE_VALUE]; 302 | gpio_put(pin, (bool) value); 303 | } 304 | 305 | /********************************************** 306 | * Set A PWM Pin's value 307 | */ 308 | void pwm_write() { 309 | uint pin; 310 | uint16_t value; 311 | 312 | pin = command_buffer[PWM_WRITE_GPIO_PIN]; 313 | 314 | value = (command_buffer[SET_PIN_MODE_PWM_HIGH_VALUE] << 8) + 315 | command_buffer[SET_PIN_MODE_PWM_LOW_VALUE]; 316 | pwm_set_gpio_level(pin, value); 317 | } 318 | 319 | /*************************************************** 320 | * Control reporting 321 | */ 322 | void modify_reporting() { 323 | int pin = command_buffer[MODIFY_REPORTING_PIN]; 324 | 325 | switch (command_buffer[MODIFY_REPORTING_TYPE]) { 326 | case REPORTING_DISABLE_ALL: 327 | for (int i = 0; i < MAX_DIGITAL_PINS_SUPPORTED; i++) { 328 | the_digital_pins[i].reporting_enabled = false; 329 | } 330 | for (int i = 0; i < MAX_ANALOG_PINS_SUPPORTED; i++) { 331 | the_analog_pins[i].reporting_enabled = false; 332 | } 333 | break; 334 | case REPORTING_ANALOG_ENABLE: 335 | the_analog_pins[pin].reporting_enabled = true; 336 | break; 337 | case REPORTING_ANALOG_DISABLE: 338 | the_analog_pins[pin].reporting_enabled = false; 339 | break; 340 | case REPORTING_DIGITAL_ENABLE: 341 | if (the_digital_pins[pin].pin_mode != PIN_MODE_NOT_SET) { 342 | the_digital_pins[pin].reporting_enabled = true; 343 | } 344 | break; 345 | case REPORTING_DIGITAL_DISABLE: 346 | if (the_digital_pins[pin].pin_mode != PIN_MODE_NOT_SET) { 347 | the_digital_pins[pin].reporting_enabled = false; 348 | } 349 | break; 350 | default: 351 | break; 352 | } 353 | } 354 | 355 | /*********************************************************************** 356 | * Retrieve the current firmware version 357 | */ 358 | void get_firmware_version() { 359 | serial_write(firmware_report_message, 360 | sizeof(firmware_report_message) / sizeof(int)); 361 | } 362 | 363 | /************************************************************** 364 | * Retrieve the Pico's Unique ID 365 | */ 366 | void get_pico_unique_id() { 367 | // get the unique id 368 | pico_unique_board_id_t board_id; 369 | pico_get_unique_board_id(&board_id); 370 | 371 | unique_id_report_report_message[2] = (board_id.id[0]); 372 | unique_id_report_report_message[3] = (board_id.id[1]); 373 | unique_id_report_report_message[4] = (board_id.id[2]); 374 | unique_id_report_report_message[5] = (board_id.id[3]); 375 | unique_id_report_report_message[6] = (board_id.id[4]); 376 | unique_id_report_report_message[7] = (board_id.id[5]); 377 | 378 | serial_write(unique_id_report_report_message, 379 | sizeof(unique_id_report_report_message) / sizeof(int)); 380 | } 381 | 382 | /******************************************** 383 | * Stop reporting for all input pins 384 | */ 385 | void stop_all_reports() { 386 | stop_reports = true; 387 | sleep_ms(20); 388 | stdio_flush(); 389 | } 390 | 391 | /********************************************** 392 | * Enable reporting for all input pins 393 | */ 394 | void enable_all_reports() { 395 | stdio_flush(); 396 | stop_reports = false; 397 | sleep_ms(20); 398 | } 399 | 400 | /****************************************** 401 | * Use the watchdog time to reset the board. 402 | */ 403 | void reset_board() { 404 | watchdog_reboot(0, 0, 0); 405 | watchdog_enable(10, 1); 406 | } 407 | 408 | void i2c_begin() { 409 | // get the GPIO pins associated with this i2c instance 410 | uint sda_gpio = command_buffer[I2C_SDA_GPIO_PIN]; 411 | uint scl_gpio = command_buffer[I2C_SCL_GPIO_PIN]; 412 | 413 | // set the i2c instance - 0 or 1 414 | if (command_buffer[I2C_PORT] == 0) { 415 | i2c_init(i2c0, 100 * 1000); 416 | } else { 417 | i2c_init(i2c1, 100 * 1000); 418 | } 419 | gpio_set_function(sda_gpio, GPIO_FUNC_I2C); 420 | gpio_set_function(scl_gpio, GPIO_FUNC_I2C); 421 | gpio_pull_up(sda_gpio); 422 | gpio_pull_up(scl_gpio); 423 | } 424 | 425 | void i2c_read() { 426 | 427 | // The report_message offsets: 428 | // 0 = packet length - this must be calculated 429 | // 1 = I2C_READ_REPORT 430 | // 2 = The i2c port - 0 or 1 431 | // 3 = i2c device address 432 | // 4 = i2c read register 433 | // 5 = number of bytes read 434 | // 6... = bytes read 435 | 436 | // length of i2c report packet 437 | int num_of_bytes_to_send = I2C_READ_START_OF_DATA + command_buffer[I2C_READ_LENGTH]; 438 | 439 | // We have a separate buffer ot store the data read from the device 440 | // and combine that data back into the i2c report buffer. 441 | // This gets around casting. 442 | uint8_t data_from_device[command_buffer[I2C_READ_LENGTH]]; 443 | 444 | // return value from write and read i2c sdk commands 445 | int i2c_sdk_call_return_value; 446 | 447 | // selector for i2c0 or i2c1 448 | i2c_inst_t *i2c; 449 | 450 | // Determine the i2c port to use. 451 | if (command_buffer[I2C_PORT]) { 452 | i2c = i2c1; 453 | } else { 454 | i2c = i2c0; 455 | } 456 | 457 | // If there is an i2c register specified, set the register pointer 458 | if (command_buffer[I2C_READ_NO_STOP_FLAG] != I2C_NO_REGISTER_SPECIFIED) { 459 | i2c_sdk_call_return_value = i2c_write_blocking(i2c, 460 | (uint8_t) command_buffer[I2C_DEVICE_ADDRESS], 461 | (const uint8_t *) &command_buffer[I2C_READ_REGISTER], 1, 462 | (bool) command_buffer[I2C_READ_NO_STOP_FLAG]); 463 | if (i2c_sdk_call_return_value == PICO_ERROR_GENERIC) { 464 | return; 465 | } 466 | } 467 | 468 | // now do the read request 469 | i2c_sdk_call_return_value = i2c_read_blocking(i2c, 470 | (uint8_t) command_buffer[I2C_DEVICE_ADDRESS], 471 | data_from_device, 472 | (size_t) (command_buffer[I2C_READ_LENGTH]), 473 | (bool) command_buffer[I2C_READ_NO_STOP_FLAG]); 474 | if (i2c_sdk_call_return_value == PICO_ERROR_GENERIC) { 475 | i2c_report_message[I2C_PACKET_LENGTH] = I2C_ERROR_REPORT_LENGTH; // length of the packet 476 | i2c_report_message[I2C_REPORT_ID] = I2C_READ_FAILED; //report ID 477 | i2c_report_message[I2C_REPORT_PORT] = command_buffer[I2C_PORT]; 478 | i2c_report_message[I2C_REPORT_DEVICE_ADDRESS] = command_buffer[I2C_DEVICE_ADDRESS]; 479 | 480 | serial_write(i2c_report_message, I2C_ERROR_REPORT_NUM_OF_BYTE_TO_SEND); 481 | return; 482 | } 483 | 484 | // copy the data returned from i2c device into the report message buffer 485 | for (uint i = 0; i < i2c_sdk_call_return_value; i++) { 486 | i2c_report_message[i + I2C_READ_START_OF_DATA] = data_from_device[i]; 487 | } 488 | // length of the packet 489 | i2c_report_message[I2C_PACKET_LENGTH] = (uint8_t) (i2c_sdk_call_return_value + 490 | I2C_READ_DATA_BASE_BYTES); 491 | 492 | i2c_report_message[I2C_REPORT_ID] = I2C_READ_REPORT; 493 | 494 | // i2c_port 495 | i2c_report_message[I2C_REPORT_PORT] = command_buffer[I2C_PORT]; 496 | 497 | // i2c_address 498 | i2c_report_message[I2C_REPORT_DEVICE_ADDRESS] = command_buffer[I2C_DEVICE_ADDRESS]; 499 | 500 | // i2c register 501 | i2c_report_message[I2C_REPORT_READ_REGISTER] = command_buffer[I2C_READ_REGISTER]; 502 | 503 | // number of bytes read from i2c device 504 | i2c_report_message[I2C_REPORT_READ_NUMBER_DATA_BYTES] = (uint8_t) i2c_sdk_call_return_value; 505 | 506 | serial_write((int *) i2c_report_message, num_of_bytes_to_send); 507 | 508 | } 509 | 510 | void i2c_write() { 511 | // i2c instance pointer 512 | i2c_inst_t *i2c; 513 | 514 | // Determine the i2c port to use. 515 | if (command_buffer[I2C_PORT]) { 516 | i2c = i2c1; 517 | } else { 518 | i2c = i2c0; 519 | } 520 | 521 | int i2c_sdk_call_return_value = i2c_write_blocking(i2c, (uint8_t) command_buffer[I2C_DEVICE_ADDRESS], 522 | &(command_buffer[I2C_WRITE_BYTES_TO_WRITE]), 523 | command_buffer[I2C_WRITE_NUMBER_OF_BYTES], 524 | (bool) command_buffer[I2C_WRITE_NO_STOP_FLAG]); 525 | 526 | if (i2c_sdk_call_return_value == PICO_ERROR_GENERIC) { 527 | i2c_report_message[I2C_PACKET_LENGTH] = I2C_ERROR_REPORT_LENGTH; // length of the packet 528 | i2c_report_message[I2C_REPORT_ID] = I2C_WRITE_FAILED; //report ID 529 | i2c_report_message[I2C_REPORT_PORT] = command_buffer[I2C_PORT]; 530 | i2c_report_message[I2C_REPORT_DEVICE_ADDRESS] = command_buffer[I2C_DEVICE_ADDRESS]; 531 | 532 | serial_write(i2c_report_message, I2C_ERROR_REPORT_NUM_OF_BYTE_TO_SEND); 533 | return; 534 | } 535 | } 536 | 537 | void init_neo_pixels() { 538 | // initialize the pico support a NeoPixel string 539 | uint offset = pio_add_program(np_pio, &ws2812_program); 540 | ws2812_init(np_pio, np_sm, offset, command_buffer[NP_PIN_NUMBER], 800000, 541 | false); 542 | 543 | actual_number_of_pixels = command_buffer[NP_NUMBER_OF_PIXELS]; 544 | 545 | // set the pixels to the fill color 546 | for (int i = 0; i < actual_number_of_pixels; i++) { 547 | pixel_buffer[i][RED] = command_buffer[NP_RED_FILL]; 548 | pixel_buffer[i][GREEN] = command_buffer[NP_GREEN_FILL]; 549 | pixel_buffer[i][BLUE] = command_buffer[NP_BLUE_FILL]; 550 | } 551 | show_neo_pixels(); 552 | sleep_ms(1); 553 | 554 | } 555 | 556 | void set_neo_pixel() { 557 | // set a single neopixel in the pixel buffer 558 | pixel_buffer[command_buffer[NP_PIXEL_NUMBER]][RED] = command_buffer[NP_SET_RED]; 559 | pixel_buffer[command_buffer[NP_PIXEL_NUMBER]][GREEN] = command_buffer[NP_SET_GREEN]; 560 | pixel_buffer[command_buffer[NP_PIXEL_NUMBER]][BLUE] = command_buffer[NP_SET_BLUE]; 561 | if (command_buffer[NP_SET_AUTO_SHOW]) { 562 | show_neo_pixels(); 563 | } 564 | } 565 | 566 | void show_neo_pixels() { 567 | // show the neopixels in the buffer 568 | for (int i = 0; i < actual_number_of_pixels; i++) { 569 | put_pixel(urgb_u32(pixel_buffer[i][RED], 570 | pixel_buffer[i][GREEN], 571 | pixel_buffer[i][BLUE])); 572 | } 573 | } 574 | 575 | void clear_all_neo_pixels() { 576 | // set all the neopixels in the buffer to all zeroes 577 | for (int i = 0; i < actual_number_of_pixels; i++) { 578 | pixel_buffer[i][RED] = 0; 579 | pixel_buffer[i][GREEN] = 0; 580 | pixel_buffer[i][BLUE] = 0; 581 | } 582 | if (command_buffer[NP_CLEAR_AUTO_SHOW]) { 583 | show_neo_pixels(); 584 | } 585 | } 586 | 587 | void fill_neo_pixels() { 588 | // fill all the neopixels in the buffer with the 589 | // specified rgb values. 590 | for (int i = 0; i < actual_number_of_pixels; i++) { 591 | pixel_buffer[i][RED] = command_buffer[NP_FILL_RED]; 592 | pixel_buffer[i][GREEN] = command_buffer[NP_FILL_GREEN]; 593 | pixel_buffer[i][BLUE] = command_buffer[NP_FILL_BLUE]; 594 | } 595 | if (command_buffer[NP_FILL_AUTO_SHOW]) { 596 | show_neo_pixels(); 597 | } 598 | } 599 | 600 | void sonar_new() { 601 | // add the sonar to the sonar struct to be processed within 602 | // the main loop 603 | uint trig_pin = command_buffer[SONAR_TRIGGER_PIN]; 604 | uint echo_pin = command_buffer[SONAR_ECHO_PIN]; 605 | 606 | // for the first HC-SR04, add the program. 607 | if (sonar_count == -1) { 608 | sonar_offset = pio_add_program(sonar_pio, &hc_sr04_program); 609 | } 610 | sonar_count++; 611 | if (sonar_count > MAX_SONARS) { 612 | return; 613 | } 614 | the_hc_sr04s.sonars[sonar_count].trig_pin = trig_pin; 615 | the_hc_sr04s.sonars[sonar_count].echo_pin = echo_pin; 616 | 617 | hc_sr04_init(sonar_pio, (uint) sonar_count, sonar_offset, trig_pin, echo_pin); 618 | } 619 | 620 | bool repeating_timer_callback(struct repeating_timer *t) { 621 | //printf("Repeat at %lld\n", time_us_64()); 622 | timer_fired = true; 623 | return true; 624 | } 625 | 626 | void dht_new() { 627 | if (dht_count > MAX_DHTS) { 628 | return; 629 | } 630 | if(dht_count == -1){ 631 | // first time through start repeating timer 632 | add_repeating_timer_ms(2000, repeating_timer_callback, NULL, &timer); 633 | } 634 | dht_count++; 635 | 636 | uint dht_pin = command_buffer[DHT_DATA_PIN]; 637 | the_dhts.dhts[dht_count].data_pin = dht_pin; 638 | the_dhts.dhts[dht_count].previous_time = get_absolute_time(); 639 | gpio_init(dht_pin); 640 | } 641 | 642 | void init_spi(){ 643 | spi_inst_t *spi_port; 644 | uint spi_baud_rate; 645 | uint cs_pin; 646 | 647 | // initialize the spi port 648 | if(command_buffer[SPI_PORT] == 0){ 649 | spi_port = spi0; 650 | } 651 | else{ 652 | spi_port = spi1; 653 | } 654 | 655 | spi_baud_rate = ((command_buffer[SPI_FREQ_MSB] << 24) + 656 | (command_buffer[SPI_FREQ_3] << 16) + 657 | (command_buffer[SPI_FREQ_2] << 8) + 658 | (command_buffer[SPI_FREQ_1] )); 659 | 660 | spi_init(spi_port, spi_baud_rate); 661 | 662 | // set gpio pins for miso, mosi and clock 663 | gpio_set_function(command_buffer[SPI_MISO], GPIO_FUNC_SPI); 664 | gpio_set_function(command_buffer[SPI_MOSI], GPIO_FUNC_SPI); 665 | gpio_set_function(command_buffer[SPI_CLK_PIN], GPIO_FUNC_SPI); 666 | 667 | // initialize chip select GPIO pins 668 | for(int i = 0; i < command_buffer[SPI_CS_LIST_LENGTH]; i++){ 669 | cs_pin = command_buffer[SPI_CS_LIST + i]; 670 | // Chip select is active-low, so we'll initialise it to a driven-high state 671 | gpio_init(cs_pin); 672 | gpio_set_dir(cs_pin, GPIO_OUT); 673 | gpio_put(cs_pin, 1); 674 | } 675 | } 676 | 677 | void spi_cs_control(){ 678 | uint8_t cs_pin; 679 | uint8_t cs_state; 680 | 681 | cs_pin = command_buffer[SPI_SELECT_PIN]; 682 | cs_state = command_buffer[SPI_SELECT_STATE]; 683 | asm volatile("nop \n nop \n nop"); 684 | gpio_put(cs_pin, cs_state); 685 | asm volatile("nop \n nop \n nop"); 686 | } 687 | 688 | void read_blocking_spi(){ 689 | // The report_message offsets: 690 | // 0 = packet length - this must be calculated 691 | // 1 = SPI_READ_REPORT 692 | // 2 = The i2c port - 0 or 1 693 | // 3 = number of bytes read 694 | // 4... = bytes read 695 | 696 | 697 | spi_inst_t *spi_port; 698 | size_t data_length; 699 | uint8_t repeated_transmit_byte; 700 | uint8_t data[command_buffer[SPI_READ_LEN]]; 701 | 702 | if(command_buffer[SPI_PORT] == 0){ 703 | spi_port = spi0; 704 | } 705 | else{ 706 | spi_port = spi1; 707 | } 708 | 709 | data_length = command_buffer[SPI_READ_LEN]; 710 | //memset(data, 0, data_length); 711 | memset(data, 0, sizeof(data)); 712 | 713 | repeated_transmit_byte = command_buffer[SPI_REPEATED_DATA]; 714 | 715 | // read data 716 | spi_read_blocking(spi_port, repeated_transmit_byte, data, data_length); 717 | sleep_ms(100); 718 | 719 | // build a report from the data returned 720 | spi_report_message[SPI_PACKET_LENGTH] = SPI_REPORT_NUMBER_OF_DATA_BYTES + data_length; 721 | spi_report_message[SPI_REPORT_ID] = SPI_REPORT; 722 | spi_report_message[SPI_REPORT_PORT] = command_buffer[SPI_PORT]; 723 | spi_report_message[SPI_REPORT_NUMBER_OF_DATA_BYTES] = data_length; 724 | for(int i=0; i < data_length; i++){ 725 | spi_report_message[SPI_DATA + i] = data[i]; 726 | } 727 | serial_write((int *) spi_report_message, 728 | SPI_DATA + data_length); 729 | 730 | } 731 | 732 | void write_blocking_spi() { 733 | spi_inst_t *spi_port; 734 | uint cs_pin; 735 | size_t data_length; 736 | 737 | if(command_buffer[SPI_PORT] == 0){ 738 | spi_port = spi0; 739 | } 740 | else{ 741 | spi_port = spi1; 742 | } 743 | 744 | data_length = command_buffer[SPI_WRITE_LEN]; 745 | // write data 746 | spi_write_blocking(spi_port, &command_buffer[SPI_WRITE_DATA], data_length); 747 | } 748 | 749 | 750 | void set_format_spi(){ 751 | spi_inst_t *spi_port; 752 | uint data_bits = command_buffer[SPI_NUMBER_OF_BITS]; 753 | spi_cpol_t cpol = command_buffer[SPI_CLOCK_PHASE]; 754 | spi_cpha_t cpha = command_buffer[SPI_CLOCK_POLARITY]; 755 | 756 | if(command_buffer[SPI_PORT] == 0){ 757 | spi_port = spi0; 758 | } 759 | else{ 760 | spi_port = spi1; 761 | } 762 | spi_set_format(spi_port, data_bits, cpol, cpha, 1); 763 | } 764 | 765 | 766 | /******************* FOR FUTURE RELEASES **********************/ 767 | 768 | void reset_data() {} 769 | 770 | /***************** Currently Unused ***************************/ 771 | void servo_attach() {} 772 | 773 | void servo_write() {} 774 | 775 | void servo_detach() {} 776 | 777 | /****************************************************** 778 | * INTERNALLY USED FUNCTIONS 779 | *****************************************************/ 780 | 781 | /*************************************************** 782 | * Retrieve the next command and process it 783 | */ 784 | void get_next_command() { 785 | int packet_size; 786 | uint8_t packet_data; 787 | command_descriptor command_entry; 788 | 789 | // clear the command buffer for the new incoming command 790 | memset(command_buffer, 0, sizeof(command_buffer)); 791 | 792 | // Get the number of bytes of the command packet. 793 | // The first byte is the command ID and the following bytes 794 | // are the associated data bytes 795 | if ((packet_size = getchar_timeout_us(0)) == PICO_ERROR_TIMEOUT) { 796 | // no data, let the main loop continue to run to handle inputs 797 | return; 798 | } else { 799 | // get the rest of the packet 800 | for (int i = 0; i < packet_size; i++) { 801 | if ((packet_data = (uint8_t) getchar_timeout_us(0)) == PICO_ERROR_TIMEOUT) { 802 | sleep_ms(1); 803 | } 804 | command_buffer[i] = packet_data; 805 | } 806 | 807 | // the first byte is the command ID. 808 | // look up the function and execute it. 809 | // data for the command starts at index 1 in the command_buffer 810 | command_entry = command_table[command_buffer[0]]; 811 | 812 | // uncomment to see the command and first byte of data 813 | //send_debug_info(command_buffer[0], command_buffer[1]); 814 | 815 | // call the command 816 | 817 | command_entry.command_func(); 818 | } 819 | } 820 | 821 | /************************************** 822 | * Scan all pins set as digital inputs 823 | * and generate a report. 824 | */ 825 | void scan_digital_inputs() { 826 | int value; 827 | 828 | // report message 829 | 830 | // index 0 = packet length 831 | // index 1 = report type 832 | // index 2 = pin number 833 | // index 3 = value 834 | 835 | for (int i = 0; i < MAX_DIGITAL_PINS_SUPPORTED; i++) { 836 | if (the_digital_pins[i].pin_mode == DIGITAL_INPUT || 837 | the_digital_pins[i].pin_mode == DIGITAL_INPUT_PULL_UP || 838 | the_digital_pins[i].pin_mode == DIGITAL_INPUT_PULL_DOWN) { 839 | if (the_digital_pins[i].reporting_enabled) { 840 | // if the value changed since last read 841 | value = gpio_get(the_digital_pins[i].pin_number); 842 | if (value != the_digital_pins[i].last_value) { 843 | the_digital_pins[i].last_value = value; 844 | digital_input_report_message[DIGITAL_INPUT_GPIO_PIN] = i; 845 | digital_input_report_message[DIGITAL_INPUT_GPIO_VALUE] = value; 846 | serial_write(digital_input_report_message, 4); 847 | } 848 | } 849 | } 850 | } 851 | } 852 | 853 | void scan_analog_inputs() { 854 | uint16_t value; 855 | 856 | // report message 857 | 858 | // byte 0 = packet length 859 | // byte 1 = report type 860 | // byte 2 = pin number 861 | // byte 3 = high order byte of value 862 | // byte 4 = low order byte of value 863 | 864 | int differential; 865 | 866 | for (uint8_t i = 0; i < MAX_ANALOG_PINS_SUPPORTED; i++) { 867 | if (the_analog_pins[i].reporting_enabled) { 868 | adc_select_input(i); 869 | value = adc_read(); 870 | differential = abs(value - the_analog_pins[i].last_value); 871 | if (differential >= the_analog_pins[i].differential) { 872 | //trigger value achieved, send out the report 873 | the_analog_pins[i].last_value = value; 874 | // input_message[1] = the_analog_pins[i].pin_number; 875 | analog_input_report_message[ANALOG_INPUT_GPIO_PIN] = (uint8_t) i; 876 | analog_input_report_message[ANALOG_VALUE_HIGH_BYTE] = value >> 8; 877 | analog_input_report_message[ANALOG_VALUE_LOW_BYTE] = value & 0x00ff; 878 | serial_write(analog_input_report_message, 5); 879 | } 880 | } 881 | } 882 | } 883 | 884 | void scan_sonars() { 885 | // read the next sonar device 886 | // one device is read each cycle 887 | if (sonar_count >= 0) { 888 | read_sonar(the_hc_sr04s.next_sonar_index); 889 | the_hc_sr04s.next_sonar_index++; 890 | if (the_hc_sr04s.next_sonar_index > sonar_count) { 891 | the_hc_sr04s.next_sonar_index = 0; 892 | } 893 | } 894 | } 895 | 896 | void read_sonar(uint sm) { 897 | // value is used to read from the sm RX FIFO 898 | uint32_t clock_cycles; 899 | // clear the FIFO: do a new measurement 900 | pio_sm_clear_fifos(sonar_pio, sm); 901 | // give the sm some time to do a measurement and place it in the FIFO 902 | sleep_ms(100); 903 | // check that the FIFO isn't empty 904 | if (pio_sm_is_rx_fifo_empty(sonar_pio, sm)) { 905 | // its empty so create a report returning a distance of zero 906 | sonar_report_message[SONAR_TRIG_PIN] = (uint8_t) the_hc_sr04s.sonars[sm].trig_pin; 907 | sonar_report_message[CM_WHOLE_VALUE] = 0; 908 | sonar_report_message[CM_FRAC_VALUE] = 0; 909 | serial_write(sonar_report_message, 5); 910 | return; 911 | } 912 | 913 | // read one data item from the FIFO 914 | // Note: every test for the end of the echo pulse takes 2 pio clock ticks, 915 | // but changes the 'timer' by only one 916 | clock_cycles = 2 * pio_sm_get(sonar_pio, sm); 917 | // using 918 | // - the time for 1 pio clock tick (1/125000000 s) 919 | // - speed of sound in air is about 340 m/s 920 | // - the sound travels from the HCS-R04 to the object and back (twice the distance) 921 | // we can calculate the distance in cm by multiplying with 0.000136 922 | float cm = (float) clock_cycles * 0.000136; 923 | 924 | // convert the value into 2 integers - left and right of the decimal point 925 | float nearest = roundf(cm * 100) / 100; 926 | int intpart = (int) nearest; 927 | int decpart = (int) ((nearest - intpart) * 100); 928 | 929 | sonar_report_message[SONAR_TRIG_PIN] = (uint8_t) the_hc_sr04s.sonars[sm].trig_pin; 930 | sonar_report_message[CM_WHOLE_VALUE] = intpart; 931 | sonar_report_message[CM_FRAC_VALUE] = decpart; 932 | serial_write(sonar_report_message, 5); 933 | } 934 | 935 | void scan_dhts() { 936 | // read the next dht device 937 | // one device is read each cycle 938 | if (dht_count >= 0) { 939 | if (timer_fired) { 940 | timer_fired = false; 941 | int the_index = the_dhts.next_dht_index; 942 | read_dht(the_dhts.dhts[the_index].data_pin); 943 | the_dhts.next_dht_index++; 944 | if (the_dhts.next_dht_index > dht_count) { 945 | the_dhts.next_dht_index = 0; 946 | } 947 | } 948 | } 949 | } 950 | 951 | void read_dht(uint dht_pin) { 952 | int data[5] = {0, 0, 0, 0, 0}; 953 | uint last = 1; 954 | uint j = 0; 955 | float temp_celsius; 956 | float humidity; 957 | float nearest; 958 | int temp_int_part; 959 | int temp_dec_part; 960 | int humidity_int_part; 961 | int humidity_dec_part; 962 | 963 | gpio_set_dir(dht_pin, GPIO_OUT); 964 | gpio_put(dht_pin, 0); 965 | sleep_ms(20); 966 | gpio_set_dir(dht_pin, GPIO_IN); 967 | 968 | sleep_us(1); 969 | for (uint i = 0; i < DHT_MAX_TIMINGS; i++) { 970 | uint count = 0; 971 | while (gpio_get(dht_pin) == last) { 972 | count++; 973 | sleep_us(1); 974 | if (count == 255) break; 975 | } 976 | last = gpio_get(dht_pin); 977 | if (count == 255) break; 978 | 979 | if ((i >= 4) && (i % 2 == 0)) { 980 | data[j / 8] <<= 1; 981 | if (count > 46) data[j / 8] |= 1; 982 | j++; 983 | } 984 | } 985 | if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) { 986 | humidity = (float) ((data[0] << 8) + data[1]) / 10; 987 | if (humidity > 100) { 988 | humidity = data[0]; 989 | } 990 | temp_celsius = (float) (((data[2] & 0x7F) << 8) + data[3]) / 10; 991 | if (temp_celsius > 125) { 992 | temp_celsius = data[2]; 993 | } 994 | if (data[2] & 0x80) { 995 | temp_celsius = -temp_celsius; 996 | } 997 | 998 | nearest = roundf(temp_celsius * 100) / 100; 999 | temp_int_part = (int) nearest; 1000 | temp_dec_part = (int) ((nearest - temp_int_part) * 100); 1001 | 1002 | nearest = roundf(humidity * 100) / 100; 1003 | humidity_int_part = (int) nearest; 1004 | humidity_dec_part = (int) ((nearest - humidity_int_part) * 100); 1005 | 1006 | 1007 | } else { 1008 | // bad data return zeros 1009 | temp_int_part = temp_dec_part = 1010 | humidity_int_part = humidity_dec_part = 0; 1011 | } 1012 | dht_report_message[DHT_REPORT_PIN] = (int) dht_pin; 1013 | dht_report_message[DHT_HUMIDITY_WHOLE_VALUE] = humidity_int_part; 1014 | dht_report_message[DHT_HUMIDITY_FRAC_VALUE] = humidity_dec_part; 1015 | 1016 | dht_report_message[DHT_TEMPERATURE_WHOLE_VALUE] = temp_int_part; 1017 | dht_report_message[DHT_TEMPERATURE_FRAC_VALUE] = temp_dec_part; 1018 | serial_write(dht_report_message, 7); 1019 | } 1020 | 1021 | 1022 | /************************************************* 1023 | * Write data to serial interface 1024 | * @param buffer 1025 | * @param num_of_bytes_to_send 1026 | */ 1027 | void serial_write(const int *buffer, int num_of_bytes_to_send) { 1028 | for (int i = 0; i < num_of_bytes_to_send; i++) { 1029 | putchar((buffer[i]) & 0x00ff); 1030 | } 1031 | stdio_flush(); 1032 | 1033 | } 1034 | 1035 | 1036 | /*************************************************************** 1037 | * MAIN FUNCTION 1038 | ****************************************************************/ 1039 | 1040 | int main() { 1041 | stdio_init_all(); 1042 | stdio_set_translate_crlf(&stdio_usb, false); 1043 | stdio_flush(); 1044 | //uint offset = pio_add_program(pio, &Telemetrix4RpiPico_program); 1045 | //ws2812_init(pio, sm, offset, 28, 800000, 1046 | // false); 1047 | 1048 | //stdio_set_translate_crlf(&stdio_usb, false); 1049 | adc_init(); 1050 | // create an array of pin_descriptors for 100 pins 1051 | // establish the digital pin array 1052 | for (uint8_t i = 0; i < MAX_DIGITAL_PINS_SUPPORTED; i++) { 1053 | the_digital_pins[i].pin_number = i; 1054 | the_digital_pins[i].pin_mode = PIN_MODE_NOT_SET; 1055 | the_digital_pins[i].reporting_enabled = false; 1056 | the_digital_pins[i].last_value = 0; 1057 | } 1058 | 1059 | // establish the analog pin array 1060 | for (uint8_t i = 0; i < MAX_ANALOG_PINS_SUPPORTED; i++) { 1061 | the_analog_pins[i].reporting_enabled = false; 1062 | the_analog_pins[i].last_value = 0; 1063 | } 1064 | 1065 | // initialize the sonar structures 1066 | sonar_data the_hc_sr04s = {.next_sonar_index = 0}; 1067 | for (int i = 0; i < MAX_SONARS; i++) { 1068 | the_hc_sr04s.sonars[i].trig_pin = the_hc_sr04s.sonars[i].echo_pin = (uint) -1; 1069 | } 1070 | 1071 | gpio_init(LED_PIN); 1072 | gpio_set_dir(LED_PIN, GPIO_OUT); 1073 | 1074 | // blink the board LED twice to show that the board is 1075 | // starting afresh 1076 | led_debug(2, 250); 1077 | 1078 | // infinite loop 1079 | while (true) { 1080 | get_next_command(); 1081 | if (!stop_reports) { 1082 | scan_digital_inputs(); 1083 | scan_analog_inputs(); 1084 | scan_sonars(); 1085 | scan_dhts(); 1086 | } 1087 | 1088 | 1089 | } 1090 | } 1091 | 1092 | 1093 | #pragma clang diagnostic pop -------------------------------------------------------------------------------- /Telemetrix4RpiPico.pio: -------------------------------------------------------------------------------- 1 | 2 | /******************************************************** 3 | * Copyright (c) 2021 Alan Yorinks All rights reserved. 4 | 5 | This program is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE 7 | Version 3 as published by the Free Software Foundation; either 8 | or (at your option) any later version. 9 | This library is distributed in the hope that it will be useful,f 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | General Public License for more details. 13 | 14 | You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE 15 | along with this library; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 | */ 18 | 19 | ; 20 | ; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 21 | ; 22 | ; SPDX-License-Identifier: BSD-3-Clause 23 | ; 24 | 25 | .program ws2812 26 | .side_set 1 27 | 28 | .define public T1 2 29 | .define public T2 5 30 | .define public T3 3 31 | 32 | .lang_opt python sideset_init = pico.PIO.OUT_HIGH 33 | .lang_opt python out_init = pico.PIO.OUT_HIGH 34 | .lang_opt python out_shiftdir = 1 35 | 36 | .wrap_target 37 | bitloop: 38 | out x, 1 side 0 [T3 - 1] ; Side-set still takes place when instruction stalls 39 | jmp !x do_zero side 1 [T1 - 1] ; Branch on the bit we shifted out. Positive pulse 40 | do_one: 41 | jmp bitloop side 1 [T2 - 1] ; Continue driving high, for a long pulse 42 | do_zero: 43 | nop side 0 [T2 - 1] ; Or drive low, for a short pulse 44 | .wrap 45 | 46 | % c-sdk { 47 | #include "hardware/clocks.h" 48 | 49 | static inline void ws2812_init(PIO pio, uint sm, uint offset, uint pin, float freq, bool rgbw) { 50 | 51 | pio_gpio_init(pio, pin); 52 | pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true); 53 | 54 | pio_sm_config c = ws2812_program_get_default_config(offset); 55 | sm_config_set_sideset_pins(&c, pin); 56 | sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24); 57 | sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX); 58 | 59 | int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3; 60 | float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit); 61 | sm_config_set_clkdiv(&c, div); 62 | 63 | pio_sm_init(pio, sm, offset, &c); 64 | pio_sm_set_enabled(pio, sm, true); 65 | } 66 | %} 67 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 68 | ;;;;;;;; The hc_sr04 code is based on the work by GitJer ;;;;;;;;;;: 69 | ;; https://github.com/GitJer/Some_RPI-Pico_stuff/tree/main/HCSR04 ;; 70 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 71 | 72 | 73 | .program hc_sr04 74 | 75 | .wrap_target 76 | ; give a puls to the HCSR04 Trigger pin 77 | set pins 1 ; set the trigger to 1 78 | ; delay for 10 us (the length of the trigger pulse) 79 | set x 19 ; set x to 10011 (and clear the higher bits) 80 | mov ISR x ; copy x to ISR 81 | in NULL 6 ; shift in 6 more 0 bits 82 | mov x ISR ; move the ISR to x (which now contains 10011000000) 83 | delay1: 84 | jmp x-- delay1 ; count down to 0: a delay of (about) 10 us 85 | 86 | set pins 0 ; make the trigger 0 again, completing the trigger pulse 87 | ; 88 | wait 1 pin 0 ; wait for the echo pin to rise 89 | ; start a counting loop to measure the length of the echo pulse 90 | mov x ~NULL ; start with the value 0xFFFFFFFF 91 | timer: 92 | jmp x-- test ; count down 93 | jmp timerstop ; timer has reached 0, stop count down 94 | test: 95 | jmp pin timer ; test if the echo pin is still 1, if so, continue counting down 96 | timerstop: ; echo pulse is over (or timer has reached 0) 97 | mov ISR ~x ; move the bit-inversed value in x to the ISR 98 | push noblock ; push the ISR into the Rx FIFO 99 | ; delay for 60ms (advice from datasheet to prevent triggering on echos) 100 | set x 28 ; set x to 11100 101 | mov ISR x ; copy x to ISR 102 | in NULL 18 ; shift in 18 more bits 103 | mov x ISR ; move the ISR to x 104 | delay2: 105 | jmp x-- delay2 ; delay (about) 60 ms 106 | .wrap ; start over 107 | 108 | % c-sdk { 109 | static inline void hc_sr04_init(PIO pio, uint sm, uint offset, uint trig_pin, uint echo_pin) { 110 | pio_gpio_init(pio, trig_pin); 111 | pio_gpio_init(pio, echo_pin); 112 | 113 | // load the pio program into the pio memory 114 | //uint offset = pio_add_program(pio, &hc_sr04_program); 115 | // make an sm config 116 | pio_sm_config c = hc_sr04_program_get_default_config(offset); 117 | // set the 'in' pins, also used for 'wait' 118 | sm_config_set_in_pins(&c, echo_pin); 119 | // set the 'jmp' pin 120 | sm_config_set_jmp_pin(&c, echo_pin); 121 | // set the output pin to output 122 | pio_sm_set_consecutive_pindirs(pio, sm, trig_pin, 1, true); 123 | // set the 'set' pins 124 | sm_config_set_set_pins(&c, trig_pin, 1); 125 | // set shift direction 126 | sm_config_set_in_shift(&c, false, false, 0); 127 | // init the pio sm with the config 128 | pio_sm_init(pio, sm, offset, &c); 129 | // enable the sm 130 | pio_sm_set_enabled(pio, sm, true); 131 | } 132 | %} 133 | 134 | 135 | -------------------------------------------------------------------------------- /archive/dht.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3 | * 4 | * SPDX-License-Identifier: BSD-3-Clause 5 | **/ 6 | 7 | #include 8 | #include 9 | #include "time.h" 10 | #include "pico/stdlib.h" 11 | #include "hardware/gpio.h" 12 | 13 | #ifdef PICO_DEFAULT_LED_PIN 14 | #define LED_PIN PICO_DEFAULT_LED_PIN 15 | #endif 16 | 17 | volatile bool timer_fired = false; 18 | 19 | const uint DHT_PIN = 15; 20 | const uint MAX_TIMINGS = 85; 21 | 22 | typedef struct { 23 | float humidity; 24 | float temp_celsius; 25 | } dht_reading; 26 | 27 | 28 | bool repeating_timer_callback(struct repeating_timer *t) { 29 | //printf("Repeat at %lld\n", time_us_64()); 30 | timer_fired = true; 31 | return true; 32 | } 33 | 34 | void read_from_dht(dht_reading *result); 35 | 36 | 37 | dht_reading reading; 38 | 39 | 40 | int main() { 41 | stdio_init_all(); 42 | gpio_init(DHT_PIN); 43 | struct repeating_timer timer; 44 | add_repeating_timer_ms(2000, repeating_timer_callback, NULL, &timer); 45 | 46 | #ifdef LED_PIN 47 | gpio_init(LED_PIN); 48 | gpio_set_dir(LED_PIN, GPIO_OUT); 49 | #endif 50 | 51 | while (1) { 52 | 53 | if (timer_fired) { 54 | timer_fired = false; 55 | read_from_dht(&reading); 56 | float fahrenheit = (reading.temp_celsius * 9 / 5) + 32; 57 | printf("Humidity = %.1f%%, Temperature = %.1fC (%.1fF)\n", 58 | reading.humidity, reading.temp_celsius, fahrenheit); 59 | } 60 | } 61 | } 62 | 63 | void read_from_dht(dht_reading *result) { 64 | int data[5] = {0, 0, 0, 0, 0}; 65 | uint last = 1; 66 | uint j = 0; 67 | 68 | gpio_set_dir(DHT_PIN, GPIO_OUT); 69 | gpio_put(DHT_PIN, 0); 70 | sleep_ms(20); 71 | gpio_set_dir(DHT_PIN, GPIO_IN); 72 | sleep_us(1); 73 | for (uint i = 0; i < MAX_TIMINGS; i++) { 74 | uint count = 0; 75 | while (gpio_get(DHT_PIN) == last) { 76 | count++; 77 | sleep_us(1); 78 | if (count == 255) break; 79 | } 80 | last = gpio_get(DHT_PIN); 81 | if (count == 255) break; 82 | 83 | if ((i >= 4) && (i % 2 == 0)) { 84 | data[j / 8] <<= 1; 85 | if (count > 46) data[j / 8] |= 1; 86 | j++; 87 | } 88 | } 89 | 90 | if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) { 91 | result->humidity = (float) ((data[0] << 8) + data[1]) / 10; 92 | if (result->humidity > 100) { 93 | result->humidity = data[0]; 94 | } 95 | result->temp_celsius = (float) (((data[2] & 0x7F) << 8) + data[3]) / 10; 96 | if (result->temp_celsius > 125) { 97 | result->temp_celsius = data[2]; 98 | } 99 | if (data[2] & 0x80) { 100 | result->temp_celsius = -result->temp_celsius; 101 | } 102 | } else { 103 | printf("Bad data\n"); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /cmake-build-release/Telemetrix4RpiPico.uf2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MrYsLab/Telemetrix4RpiPico/ef298d3bbb9cd86e9bfbd5ab6eddeac2053ddd2d/cmake-build-release/Telemetrix4RpiPico.uf2 -------------------------------------------------------------------------------- /images/tmx.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MrYsLab/Telemetrix4RpiPico/ef298d3bbb9cd86e9bfbd5ab6eddeac2053ddd2d/images/tmx.png -------------------------------------------------------------------------------- /include/Telemetrix4RpiPico.h: -------------------------------------------------------------------------------- 1 | // 2 | // Created by afy on 3/3/21. 3 | // 4 | 5 | #ifndef TELEMETRIX4RPIPICO_TELEMETRIX4RPIPICO_H 6 | #define TELEMETRIX4RPIPICO_TELEMETRIX4RPIPICO_H 7 | 8 | #pragma clang diagnostic push 9 | #pragma ide diagnostic ignored "EndlessLoop" 10 | #include 11 | #include 12 | #include 13 | #include "pico/stdlib.h" 14 | #include "hardware/pwm.h" 15 | #include "pico/unique_id.h" 16 | #include "hardware/watchdog.h" 17 | #include "hardware/adc.h" 18 | #include "hardware/i2c.h" 19 | #include "hardware/pio.h" 20 | #include "hardware/clocks.h" 21 | #include "hardware/spi.h" 22 | #include "Telemetrix4RpiPico.pio.h" 23 | #include "math.h" 24 | 25 | /************************** FORWARD REFERENCES *********************** 26 | We define all functions here as extern to provide allow 27 | forward referencing. 28 | **********************************************************************/ 29 | 30 | extern void serial_loopback(); 31 | 32 | extern void set_pin_mode(); 33 | 34 | extern void digital_write(); 35 | 36 | extern void pwm_write(); 37 | 38 | extern void modify_reporting(); 39 | 40 | extern void get_firmware_version(); 41 | 42 | extern void get_pico_unique_id(); 43 | 44 | extern void servo_attach(); 45 | 46 | extern void servo_write(); 47 | 48 | extern void servo_detach(); 49 | 50 | extern void i2c_begin(); 51 | 52 | extern void i2c_read(); 53 | 54 | extern void i2c_write(); 55 | 56 | extern void sonar_new(); 57 | 58 | extern void serial_write(const int *buffer, int num_of_bytes_to_send); 59 | 60 | extern void led_debug(int blinks, uint delay); 61 | 62 | extern void send_debug_info(uint id, uint value); 63 | 64 | extern void dht_new(); 65 | 66 | extern void stop_all_reports(); 67 | 68 | extern void enable_all_reports(); 69 | 70 | extern void reset_data(); 71 | 72 | extern void reset_board(); 73 | 74 | extern void scan_digital_inputs(); 75 | 76 | extern void scan_analog_inputs(); 77 | 78 | extern void init_neo_pixels(); 79 | 80 | extern void show_neo_pixels(); 81 | 82 | extern void set_neo_pixel(); 83 | 84 | extern void clear_all_neo_pixels(); 85 | 86 | extern void fill_neo_pixels(); 87 | 88 | extern void read_sonar(uint); 89 | 90 | extern void read_dht(uint); 91 | 92 | extern void init_spi(); 93 | 94 | extern void read_blocking_spi(); 95 | 96 | extern void write_blocking_spi(); 97 | 98 | extern void spi_cs_control(); 99 | 100 | extern void set_format_spi(); 101 | 102 | 103 | 104 | 105 | /********************************************************* 106 | * COMMAND DEFINES 107 | ********************************************************/ 108 | 109 | // Commands -received by this sketch 110 | // Add commands retaining the sequential numbering. 111 | // The order of commands here must be maintained in the command_table below. 112 | #define SERIAL_LOOP_BACK 0 113 | #define SET_PIN_MODE 1 114 | #define DIGITAL_WRITE 2 115 | #define PWM_WRITE 3 116 | #define MODIFY_REPORTING 4 // mode(all, analog, or digital), pin, enable or disable 117 | #define GET_FIRMWARE_VERSION 5 118 | #define GET_PICO_UNIQUE_ID 6 119 | #define SERVO_ATTACH 7 // unused 120 | #define SERVO_WRITE 8 // unused 121 | #define SERVO_DETACH 9 // unused 122 | #define I2C_BEGIN 10 123 | #define I2C_READ 11 124 | #define I2C_WRITE 12 125 | #define SONAR_NEW 13 126 | #define DHT_NEW 14 127 | #define STOP_ALL_REPORTS 15 128 | #define ENABLE_ALL_REPORTS 16 129 | #define RESET_DATA 17 130 | #define RESET_BOARD 18 131 | #define INITIALIZE_NEO_PIXELS 19 132 | #define SHOW_NEO_PIXELS 20 133 | #define SET_NEO_PIXEL 21 134 | #define CLEAR_ALL_NEO_PIXELS 22 135 | #define FILL_NEO_PIXELS 23 136 | #define SPI_INIT 24 137 | #define SPI_WRITE_BLOCKING 25 138 | #define SPI_READ_BLOCKING 26 139 | #define SPI_SET_FORMAT 27 140 | #define SPI_CS_CONTROL 28 141 | 142 | /***************************************************** 143 | * MESSAGE OFFSETS 144 | ***************************************************/ 145 | // i2c_common 146 | #define I2C_PORT 1 147 | #define I2C_DEVICE_ADDRESS 2 // read and write 148 | 149 | // i2c_init 150 | #define I2C_SDA_GPIO_PIN 2 151 | #define I2C_SCL_GPIO_PIN 3 152 | 153 | // i2c_read 154 | #define I2C_READ_REGISTER 3 155 | #define I2C_READ_LENGTH 4 156 | #define I2C_READ_NO_STOP_FLAG 5 157 | 158 | // I2c_write 159 | #define I2C_WRITE_NUMBER_OF_BYTES 3 160 | #define I2C_WRITE_NO_STOP_FLAG 4 161 | #define I2C_WRITE_BYTES_TO_WRITE 5 162 | 163 | // This defines how many bytes there are 164 | // that precede the first byte read position 165 | // in the i2c report message buffer. 166 | #define I2C_READ_DATA_BASE_BYTES 5 167 | 168 | // Start of i2c data read within the message buffer 169 | #define I2C_READ_START_OF_DATA 6 170 | 171 | // Indicator that no i2c register is being specified in the command 172 | #define I2C_NO_REGISTER 254 173 | 174 | /******************************* COMMAND BUFFER OFFSETS ************/ 175 | // loop back command buffer offsets 176 | #define DATA_TO_LOOP_BACK 1 177 | 178 | // set pin mode command buffer offsets 179 | #define SET_PIN_MODE_GPIO_PIN 1 180 | #define SET_PIN_MODE_MODE_TYPE 2 181 | 182 | // set pin mode digital input command offsets 183 | #define SET_PIN_MODE_DIGITAL_IN_REPORTING_STATE 3 184 | 185 | // set pin mode PWM output input command offsets 186 | #define SET_PIN_MODE_PWM_HIGH_VALUE 2 187 | #define SET_PIN_MODE_PWM_LOW_VALUE 3 188 | 189 | // ADC number for temperature sensor 190 | #define ADC_TEMPERATURE_REGISTER 4 191 | 192 | // set pin mode analog input command offsets 193 | #define SET_PIN_MODE_ANALOG_IN_REPORTING_STATE 5 194 | #define SET_PIN_MODE_ANALOG_DIFF_HIGH 3 195 | #define SET_PIN_MODE_ANALOG_DIFF_LOW 4 196 | 197 | // set pin mode spi offsets 198 | #define SPI_PORT 1 199 | #define SPI_MISO 2 200 | #define SPI_MOSI 3 201 | #define SPI_CLK_PIN 4 202 | #define SPI_FREQ_MSB 5 203 | #define SPI_FREQ_3 6 204 | #define SPI_FREQ_2 7 205 | #define SPI_FREQ_1 8 206 | #define SPI_CS_LIST_LENGTH 9 207 | #define SPI_CS_LIST 10 // beginning of list 208 | 209 | // spi write blocking offsets 210 | // #define SPI_PORT 1 211 | #define SPI_WRITE_LEN 2 212 | #define SPI_WRITE_DATA 3 213 | 214 | // spi read blocking offsets 215 | // #define SPI_PORT 1 216 | #define SPI_READ_LEN 2 217 | #define SPI_REPEATED_DATA 3 218 | 219 | // spi chipselect command offsets 220 | #define SPI_SELECT_PIN 1 221 | #define SPI_SELECT_STATE 2 222 | 223 | // spi set format command offsets 224 | // #define SPI_PORT 1 225 | #define SPI_NUMBER_OF_BITS 2 226 | #define SPI_CLOCK_PHASE 3 227 | #define SPI_CLOCK_POLARITY 4 228 | 229 | // digital_write 230 | #define DIGITAL_WRITE_GPIO_PIN 1 231 | #define DIGITAL_WRITE_VALUE 2 232 | 233 | // pwm write 234 | #define PWM_WRITE_GPIO_PIN 1 235 | #define PWM_WRITE_HIGH_VALUE 2 236 | #define PWM_WRITE_LOW_VALUE 3 237 | 238 | // modify reporting 239 | // This can be a gpio pin or adc channel 240 | #define MODIFY_REPORTING_PIN 2 241 | #define MODIFY_REPORTING_TYPE 1 242 | 243 | // i2c report buffer offsets 244 | #define I2C_PACKET_LENGTH 0 245 | #define I2C_REPORT_ID 1 246 | #define I2C_REPORT_PORT 2 247 | #define I2C_REPORT_DEVICE_ADDRESS 3 248 | #define I2C_REPORT_READ_REGISTER 4 249 | #define I2C_REPORT_READ_NUMBER_DATA_BYTES 5 250 | 251 | 252 | #define I2C_ERROR_REPORT_LENGTH 4 253 | #define I2C_ERROR_REPORT_NUM_OF_BYTE_TO_SEND 5 254 | 255 | // spi report buffer offsets 256 | #define SPI_PACKET_LENGTH 0 257 | #define SPI_REPORT_ID 1 258 | #define SPI_REPORT_PORT 2 259 | #define SPI_REPORT_NUMBER_OF_DATA_BYTES 3 260 | #define SPI_DATA 4 261 | 262 | #define SPI_READ_DATA_BASE_BYTES 5 263 | 264 | 265 | // init neopixels 266 | // command offsets 267 | #define NP_PIN_NUMBER 1 268 | #define NP_NUMBER_OF_PIXELS 2 269 | #define NP_RED_FILL 3 270 | #define NP_GREEN_FILL 4 271 | #define NP_BLUE_FILL 5 272 | 273 | // NeoPixel clock frequency 274 | #define NP_FREQUENCY 800000 275 | 276 | // Pixel buffer array offsets 277 | #define RED 0 278 | #define GREEN 1 279 | #define BLUE 2 280 | 281 | // set_neo_pixel command offsets 282 | #define NP_PIXEL_NUMBER 1 283 | #define NP_SET_RED 2 284 | #define NP_SET_GREEN 3 285 | #define NP_SET_BLUE 4 286 | #define NP_SET_AUTO_SHOW 5 287 | 288 | // fill_neo_pixels command offsets 289 | #define NP_FILL_RED 1 290 | #define NP_FILL_GREEN 2 291 | #define NP_FILL_BLUE 3 292 | #define NP_FILL_AUTO_SHOW 4 293 | 294 | // clear_all_neo_pixels command offsets 295 | #define NP_CLEAR_AUTO_SHOW 1 296 | 297 | #define MAXIMUM_NUM_NEOPIXELS 150 298 | 299 | // init hc-sr04 300 | // command offsets 301 | #define SONAR_TRIGGER_PIN 1 302 | #define SONAR_ECHO_PIN 2 303 | 304 | /* Maximum number of Sonar devices */ 305 | #define MAX_SONARS 4 306 | 307 | // init dht command offsets 308 | #define DHT_DATA_PIN 1 309 | 310 | /* Maximum number of DHT devices */ 311 | #define MAX_DHTS 2 312 | 313 | /* maximum dht timings */ 314 | const uint DHT_MAX_TIMINGS = 85; 315 | 316 | /*********************** REPORTING BUFFER OFFSETS ******************/ 317 | // loopback buffer offset for data being looped back 318 | #define LOOP_BACK_DATA 2 319 | 320 | // send_debug message buffer offsets 321 | #define DEBUG_ID 2 322 | #define DEBUG_VALUE_HIGH_BYTE 3 323 | #define DEBUG_VALUE_LOW_BYTE 4 324 | 325 | // digital input report buffer offsets 326 | #define DIGITAL_INPUT_GPIO_PIN 2 327 | #define DIGITAL_INPUT_GPIO_VALUE 3 328 | 329 | 330 | // analog input report buffer offsets 331 | #define ANALOG_INPUT_GPIO_PIN 2 332 | #define ANALOG_VALUE_HIGH_BYTE 3 333 | #define ANALOG_VALUE_LOW_BYTE 4 334 | 335 | // sonar report buffer offsets 336 | #define SONAR_TRIG_PIN 2 337 | #define CM_WHOLE_VALUE 3 338 | #define CM_FRAC_VALUE 4 339 | 340 | // dht report buffer offset 341 | #define DHT_REPORT_PIN 2 342 | #define DHT_HUMIDITY_WHOLE_VALUE 3 343 | #define DHT_HUMIDITY_FRAC_VALUE 4 344 | #define DHT_TEMPERATURE_WHOLE_VALUE 5 345 | #define DHT_TEMPERATURE_FRAC_VALUE 6 346 | 347 | /****************************************************** 348 | * PIN MODE DEFINITIONS 349 | *****************************************************/ 350 | #define DIGITAL_INPUT 0 351 | #define DIGITAL_OUTPUT 1 352 | #define PWM_OUTPUT 2 353 | #define DIGITAL_INPUT_PULL_UP 3 354 | #define DIGITAL_INPUT_PULL_DOWN 4 355 | #define ANALOG_INPUT 5 356 | #define SONAR 7 357 | #define DHT 8 358 | 359 | #define PIN_MODE_NOT_SET 255 360 | 361 | /************************************************** 362 | * REPORT DEFINITIONS 363 | **************************************************/ 364 | #define SERIAL_LOOP_BACK_REPORT 0 365 | #define DIGITAL_REPORT 2 366 | #define ANALOG_REPORT 3 367 | #define FIRMWARE_REPORT 5 368 | #define REPORT_PICO_UNIQUE_ID 6 369 | #define SERVO_UNAVAILABLE 7 // for the future 370 | #define I2C_WRITE_FAILED 8 371 | #define I2C_READ_FAILED 9 372 | #define I2C_READ_REPORT 10 373 | #define SONAR_DISTANCE 11 374 | #define DHT_REPORT 12 375 | #define SPI_REPORT 13 376 | #define DEBUG_PRINT 99 377 | 378 | /*************************************************************** 379 | * INPUT PIN REPORTING CONTROL SUB COMMANDS 380 | ***************************************************************/ 381 | #define REPORTING_DISABLE_ALL 0 382 | #define REPORTING_ANALOG_ENABLE 1 383 | #define REPORTING_DIGITAL_ENABLE 2 384 | #define REPORTING_ANALOG_DISABLE 3 385 | #define REPORTING_DIGITAL_DISABLE 4 386 | 387 | /* Maximum Supported pins */ 388 | #define MAX_DIGITAL_PINS_SUPPORTED 30 389 | #define MAX_ANALOG_PINS_SUPPORTED 5 390 | 391 | /* Firmware Version Values */ 392 | #define FIRMWARE_MAJOR 1 393 | #define FIRMWARE_MINOR 1 394 | 395 | // maximum length of a command packet in bytes 396 | #define MAX_COMMAND_LENGTH 30 397 | 398 | 399 | // Indicator that no i2c register is being specified in the command 400 | #define I2C_NO_REGISTER_SPECIFIED 254 401 | 402 | // a descriptor for digital pins 403 | typedef struct { 404 | uint pin_number; 405 | uint pin_mode; 406 | uint reporting_enabled; // If true, then send reports if an input pin 407 | int last_value; // Last value read for input mode 408 | } pin_descriptor; 409 | 410 | // a descriptor for analog pins 411 | typedef struct analog_pin_descriptor { 412 | uint reporting_enabled; // If true, then send reports if an input pin 413 | int last_value; // Last value read for input mode 414 | int differential; // difference between current and last value needed 415 | } analog_pin_descriptor; 416 | 417 | // This structure describes an HC-SR04 type device 418 | typedef struct hc_sr04_descriptor { 419 | uint trig_pin; // trigger pin 420 | uint echo_pin; // echo pin 421 | /* for possible future use 422 | int last_val_whole; // value on right side of decimal 423 | int last_val_frac; // value on left side of decimal 424 | */ 425 | } hc_sr04_descriptor; 426 | 427 | // this structure holds an index into the sonars array 428 | // and the sonars array 429 | typedef struct sonar_data { 430 | int next_sonar_index; 431 | hc_sr04_descriptor sonars[MAX_SONARS]; 432 | }sonar_data; 433 | 434 | // this structure describes a DHT type device 435 | typedef struct dht_descriptor { 436 | uint data_pin; // data pin 437 | absolute_time_t previous_time; 438 | /* for possible future use 439 | int last_val_whole; // value on right side of decimal 440 | int last_val_frac; // value on left side of decimal 441 | */ 442 | } dht_descriptor; 443 | 444 | // this structure holds an index into the dht array 445 | // and the sonars array 446 | typedef struct dht_data { 447 | int next_dht_index; 448 | dht_descriptor dhts[MAX_DHTS]; 449 | }dht_data; 450 | 451 | typedef struct { 452 | // a pointer to the command processing function 453 | void (*command_func)(void); 454 | } command_descriptor; 455 | #endif //TELEMETRIX4RPIPICO_TELEMETRIX4RPIPICO_H 456 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published by 637 | the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . -------------------------------------------------------------------------------- /pico_sdk_import.cmake: -------------------------------------------------------------------------------- 1 | # This is a copy of /external/pico_sdk_import.cmake 2 | 3 | # This can be dropped into an external project to help locate this SDK 4 | # It should be include()ed prior to project() 5 | 6 | if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) 7 | set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) 8 | message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") 9 | endif () 10 | 11 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) 12 | set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) 13 | message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") 14 | endif () 15 | 16 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) 17 | set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) 18 | message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") 19 | endif () 20 | 21 | set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the PICO SDK") 22 | set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of PICO SDK from git if not otherwise locatable") 23 | set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") 24 | 25 | if (NOT PICO_SDK_PATH) 26 | if (PICO_SDK_FETCH_FROM_GIT) 27 | include(FetchContent) 28 | set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) 29 | if (PICO_SDK_FETCH_FROM_GIT_PATH) 30 | get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") 31 | endif () 32 | FetchContent_Declare( 33 | pico_sdk 34 | GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk 35 | GIT_TAG master 36 | ) 37 | if (NOT pico_sdk) 38 | message("Downloading PICO SDK") 39 | FetchContent_Populate(pico_sdk) 40 | set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) 41 | endif () 42 | set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) 43 | else () 44 | message(FATAL_ERROR 45 | "PICO SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." 46 | ) 47 | endif () 48 | endif () 49 | 50 | get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") 51 | if (NOT EXISTS ${PICO_SDK_PATH}) 52 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") 53 | endif () 54 | 55 | set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) 56 | if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) 57 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the PICO SDK") 58 | endif () 59 | 60 | set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the PICO SDK" FORCE) 61 | 62 | include(${PICO_SDK_INIT_CMAKE_FILE}) 63 | --------------------------------------------------------------------------------