diff --git a/pico-sdk b/pico-sdk index 214c243..ee68c78 160000 --- a/pico-sdk +++ b/pico-sdk @@ -1 +1 @@ -Subproject commit 214c24386ada42d9fff7b799739c6ea24ae179c0 +Subproject commit ee68c78d0afae2b69c03ae1a72bf5cc267a2d94c diff --git a/pico/.gitignore b/pico/.gitignore deleted file mode 100644 index acf56c4..0000000 --- a/pico/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -bin -.vscode diff --git a/pico/CMakeLists.txt b/pico/CMakeLists.txt deleted file mode 100644 index dddb817..0000000 --- a/pico/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -set(PROJECT wind_station) -cmake_minimum_required(VERSION 3.12) -set(PICO_BOARD pico_w) -include(pico_sdk_import.cmake) -project(${PROJECT} C CXX) -pico_sdk_init() -add_executable(${PROJECT} src/main.c) -target_include_directories(${PROJECT} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src) -target_link_libraries(${PROJECT} - pico_stdlib - pico_cyw43_arch_none - pico_cyw43_arch_lwip_threadsafe_background - hardware_adc -) -pico_add_extra_outputs(${PROJECT}) diff --git a/pico/Makefile b/pico/Makefile deleted file mode 100755 index 4c14657..0000000 --- a/pico/Makefile +++ /dev/null @@ -1,11 +0,0 @@ - -build_thing: git_deps - cmake -B bin -DPICO_SDK_PATH=../pico-sdk -DCMAKE_BUILD_TYPE=Debug - make -j8 -C bin -# then manually copy bin/thing.uf2 to the pico - -git_deps: - cd ../pico-sdk && git submodule update --init lib/cyw43-driver lib/lwip - -clean: - rm -rf bin/* diff --git a/pico/pico_sdk_import.cmake b/pico/pico_sdk_import.cmake deleted file mode 100644 index d493cc2..0000000 --- a/pico/pico_sdk_import.cmake +++ /dev/null @@ -1,121 +0,0 @@ -# This is a copy of /external/pico_sdk_import.cmake - -# This can be dropped into an external project to help locate this SDK -# It should be include()ed prior to project() - -# Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd. -# -# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the -# following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following -# disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following -# disclaimer in the documentation and/or other materials provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products -# derived from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, -# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, -# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) - set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) - message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") -endif () - -if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) - set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) - message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") -endif () - -if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) - set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) - message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") -endif () - -if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG)) - set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG}) - message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')") -endif () - -if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG) - set(PICO_SDK_FETCH_FROM_GIT_TAG "master") - message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG") -endif() - -set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") -set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") -set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") -set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK") - -if (NOT PICO_SDK_PATH) - if (PICO_SDK_FETCH_FROM_GIT) - include(FetchContent) - set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) - if (PICO_SDK_FETCH_FROM_GIT_PATH) - get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") - endif () - FetchContent_Declare( - pico_sdk - GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk - GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} - ) - - if (NOT pico_sdk) - message("Downloading Raspberry Pi Pico SDK") - # GIT_SUBMODULES_RECURSE was added in 3.17 - if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0") - FetchContent_Populate( - pico_sdk - QUIET - GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk - GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} - GIT_SUBMODULES_RECURSE FALSE - - SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src - BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build - SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild - ) - else () - FetchContent_Populate( - pico_sdk - QUIET - GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk - GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG} - - SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src - BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build - SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild - ) - endif () - - set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) - endif () - set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) - else () - message(FATAL_ERROR - "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." - ) - endif () -endif () - -get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") -if (NOT EXISTS ${PICO_SDK_PATH}) - message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") -endif () - -set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) -if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) - message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") -endif () - -set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) - -include(${PICO_SDK_INIT_CMAKE_FILE}) diff --git a/pico/src/lwipopts.h b/pico/src/lwipopts.h deleted file mode 100644 index 129d42b..0000000 --- a/pico/src/lwipopts.h +++ /dev/null @@ -1,92 +0,0 @@ -#ifndef _LWIPOPTS_EXAMPLE_COMMONH_H -#define _LWIPOPTS_EXAMPLE_COMMONH_H - - -// Common settings used in most of the pico_w examples -// (see https://www.nongnu.org/lwip/2_1_x/group__lwip__opts.html for details) - -// allow override in some examples -#ifndef NO_SYS -#define NO_SYS 1 -#endif -// allow override in some examples -#ifndef LWIP_SOCKET -#define LWIP_SOCKET 0 -#endif -#if PICO_CYW43_ARCH_POLL -#define MEM_LIBC_MALLOC 1 -#else -// MEM_LIBC_MALLOC is incompatible with non polling versions -#define MEM_LIBC_MALLOC 0 -#endif -#define MEM_ALIGNMENT 4 -#ifndef MEM_SIZE -#define MEM_SIZE 4000 -#endif -#define MEMP_NUM_TCP_SEG 32 -#define MEMP_NUM_ARP_QUEUE 10 -#define PBUF_POOL_SIZE 24 -#define LWIP_ARP 1 -#define LWIP_ETHERNET 1 -#define LWIP_ICMP 1 -#define LWIP_RAW 1 -#define TCP_WND (8 * TCP_MSS) -#define TCP_MSS 1460 -#define TCP_SND_BUF (8 * TCP_MSS) -#define TCP_SND_QUEUELEN ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS)) -#define LWIP_NETIF_STATUS_CALLBACK 1 -#define LWIP_NETIF_LINK_CALLBACK 1 -#define LWIP_NETIF_HOSTNAME 1 -#define LWIP_NETCONN 0 -#define MEM_STATS 0 -#define SYS_STATS 0 -#define MEMP_STATS 0 -#define LINK_STATS 0 -// #define ETH_PAD_SIZE 2 -#define LWIP_CHKSUM_ALGORITHM 3 -#define LWIP_DHCP 1 -#define LWIP_IPV4 1 -#define LWIP_TCP 1 -#define LWIP_UDP 1 -#define LWIP_DNS 1 -#define LWIP_TCP_KEEPALIVE 1 -#define LWIP_NETIF_TX_SINGLE_PBUF 1 -#define DHCP_DOES_ARP_CHECK 0 -#define LWIP_DHCP_DOES_ACD_CHECK 0 - -#ifndef NDEBUG -#define LWIP_DEBUG 1 -#define LWIP_STATS 1 -#define LWIP_STATS_DISPLAY 1 -#endif - -#define ETHARP_DEBUG LWIP_DBG_OFF -#define NETIF_DEBUG LWIP_DBG_OFF -#define PBUF_DEBUG LWIP_DBG_OFF -#define API_LIB_DEBUG LWIP_DBG_OFF -#define API_MSG_DEBUG LWIP_DBG_OFF -#define SOCKETS_DEBUG LWIP_DBG_OFF -#define ICMP_DEBUG LWIP_DBG_OFF -#define INET_DEBUG LWIP_DBG_OFF -#define IP_DEBUG LWIP_DBG_OFF -#define IP_REASS_DEBUG LWIP_DBG_OFF -#define RAW_DEBUG LWIP_DBG_OFF -#define MEM_DEBUG LWIP_DBG_OFF -#define MEMP_DEBUG LWIP_DBG_OFF -#define SYS_DEBUG LWIP_DBG_OFF -#define TCP_DEBUG LWIP_DBG_OFF -#define TCP_INPUT_DEBUG LWIP_DBG_OFF -#define TCP_OUTPUT_DEBUG LWIP_DBG_OFF -#define TCP_RTO_DEBUG LWIP_DBG_OFF -#define TCP_CWND_DEBUG LWIP_DBG_OFF -#define TCP_WND_DEBUG LWIP_DBG_OFF -#define TCP_FR_DEBUG LWIP_DBG_OFF -#define TCP_QLEN_DEBUG LWIP_DBG_OFF -#define TCP_RST_DEBUG LWIP_DBG_OFF -#define UDP_DEBUG LWIP_DBG_OFF -#define TCPIP_DEBUG LWIP_DBG_OFF -#define PPP_DEBUG LWIP_DBG_OFF -#define SLIP_DEBUG LWIP_DBG_OFF -#define DHCP_DEBUG LWIP_DBG_OFF - -#endif /* __LWIPOPTS_H__ */ \ No newline at end of file diff --git a/pico/src/main.c b/pico/src/main.c deleted file mode 100644 index 34ba1e5..0000000 --- a/pico/src/main.c +++ /dev/null @@ -1,156 +0,0 @@ -#include -#include "pico/stdlib.h" -#include "pico/cyw43_arch.h" -#include "pico/cyw43_driver.h" -#include "hardware/adc.h" -#include "lwip/tcp.h" - -#include "wifi_cred.h" - -#define u32 uint32_t -#define u16 uint16_t -#define u8 uint8_t - -char ssid[] = CONFIG_WIFI_SSID; -char pass[] = CONFIG_WIFI_PASSWORD; - -const ip_addr_t SERVER_IP = CONFIG_SERVER_IP; -#define SERVER_PORT CONFIG_SERVER_PORT - -#define SET_LED(state) cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, state) - -struct tcp_pcb *tcp_controller; -#define ENCODER_BITS 3 -u8 rotary_encoder_directon; -u8 rotary_encoder_bits; -u8 rotary_encoder_bit[ENCODER_BITS]; -u16 rotary_encoder_raw[ENCODER_BITS]; -u16 rotary_encoder_min[ENCODER_BITS]; -u16 rotary_encoder_max[ENCODER_BITS]; -u16 rotary_encoder_high[ENCODER_BITS]; -u16 rotary_encoder_low[ENCODER_BITS]; -// static const u32 encoder_adc_pins[ENCODER_BITS] = {26, 27, 28}; - - -void update_raw_values() { - for (int b = 0; b < ENCODER_BITS; b++) { - adc_select_input(b); - rotary_encoder_raw[b] = adc_read(); - } -} - -void calibrate_brightness() { - for (int b = 0; b < ENCODER_BITS; b++) { - rotary_encoder_min[b] = 0xffff; - rotary_encoder_max[b] = 0; - } - for (int i = 0; i < 300; i++) { - SET_LED(i & 2); - update_raw_values(); - for (int b = 0; b < ENCODER_BITS; b++) { - if (rotary_encoder_raw[b] > rotary_encoder_max[b]) - rotary_encoder_max[b] = rotary_encoder_raw[b]; - else if (rotary_encoder_raw[b] < rotary_encoder_min[b]) - rotary_encoder_min[b] = rotary_encoder_raw[b]; - } - sleep_ms(25); - } - for (int b = 0; b < ENCODER_BITS; b++) { - u16 diff = rotary_encoder_max[b] - rotary_encoder_min[b]; - rotary_encoder_low[b] = rotary_encoder_min[b] + diff / 3; - rotary_encoder_high[b] = rotary_encoder_max[b] - diff / 3; - } - SET_LED(1); - sleep_ms(500); - SET_LED(0); -} - -void update_encoder_value() { - update_raw_values(); - for (int b = 0; b < ENCODER_BITS; b++) { - if (rotary_encoder_raw[b] > rotary_encoder_high[b]) - rotary_encoder_bit[b] = 1; - else if (rotary_encoder_raw[b] < rotary_encoder_low[b]) - rotary_encoder_bit[b] = 0; - } - rotary_encoder_bits = 0; - for (int b = 0; b < ENCODER_BITS; b++) { - rotary_encoder_bits <<= 1; - rotary_encoder_bits |= !rotary_encoder_bit[b]; // inverse to make white = 0 - } - - // gray code: 0 1 5 7 3 2 6 4 - // index: 0 1 2 3 4 5 6 7 - // reverse : 0 1 5 4 7 2 6 3 - const u8 reverse_gray_code[1 << ENCODER_BITS] = {0, 1, 5, 4, 7, 2, 6, 3}; - rotary_encoder_directon = reverse_gray_code[rotary_encoder_bits]; -} - -static err_t connected_fn(void *arg, struct tcp_pcb *tpcb, err_t err) { - return ERR_OK; -} - -void ensure_server_connection() { - while (tcp_controller->state != ESTABLISHED) { - int err = tcp_connect(tcp_controller, &SERVER_IP, SERVER_PORT, connected_fn); - while (err++) { - SET_LED(1); - sleep_ms(150); - SET_LED(0); - sleep_ms(300); - } - } -} - -void send_data() { - ensure_server_connection(); - u8 buf[] = "direction: X=X\n"; - buf[11] = rotary_encoder_bits + '0'; - buf[13] = rotary_encoder_directon + '0'; - int err = tcp_write(tcp_controller, buf, 15, 0); -} - - -int main() { - adc_init(); - adc_gpio_init(26); - adc_gpio_init(27); - adc_gpio_init(28); - - if (cyw43_arch_init()) { - SET_LED(1); - sleep_ms(2000); - return -1; - } - cyw43_arch_enable_sta_mode(); - - int err = cyw43_arch_wifi_connect_timeout_ms(ssid, pass, CYW43_AUTH_WPA2_AES_PSK, 10000); - if (err) { - PICO_ERROR_BADAUTH; // jump to definition :) - while (err++) { - SET_LED(1); - sleep_ms(100); - SET_LED(0); - sleep_ms(100); - } - return -1; - } - - tcp_controller = tcp_new_ip_type(IPADDR_TYPE_V4); - ensure_server_connection(); - tcp_write(tcp_controller, "be gay do crime :3\n", 19, 0); - SET_LED(1); - sleep_ms(200); - update_raw_values(); - SET_LED(0); - - - // tcp_controller->state - calibrate_brightness(); - - while (true) { - update_encoder_value(); - send_data(); - sleep_ms(500); - } -} diff --git a/pico/src/wifi_cred.h b/pico/src/wifi_cred.h deleted file mode 100644 index dd1bf59..0000000 --- a/pico/src/wifi_cred.h +++ /dev/null @@ -1,9 +0,0 @@ - -// case sensitive, and must be 2.4GHz -#define CONFIG_WIFI_SSID "Wifi network here" -#define CONFIG_WIFI_PASSWORD "password123" - -// local IP of the computer running the server software -// public IP would work too but requires port forwarding -#define CONFIG_SERVER_IP IPADDR4_INIT_BYTES(192, 168, 0, 108) -#define CONFIG_SERVER_PORT 13122