init converting project from micro python to c
This commit is contained in:
parent
4edd5b1cc2
commit
512e041717
32 changed files with 372 additions and 21 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -1,3 +1,5 @@
|
|||
CIRCUITPY/settings.toml
|
||||
settings.toml
|
||||
.DS_Store
|
||||
.vscode/
|
||||
build/
|
||||
.cache/
|
||||
|
|
|
|||
15
CIRCUITPY/.vscode/settings.json
vendored
15
CIRCUITPY/.vscode/settings.json
vendored
|
|
@ -1,15 +0,0 @@
|
|||
{
|
||||
"python.languageServer": "None",
|
||||
"python.linting.pylintEnabled": false,
|
||||
"python.analysis.diagnosticSeverityOverrides": {
|
||||
"reportMissingModuleSource": "none"
|
||||
},
|
||||
"python.analysis.extraPaths": [
|
||||
"/Users/travis.shears/.vscode/extensions/joedevivo.vscode-circuitpython-0.1.20-darwin-arm64/boards/0x239A/0x80F4",
|
||||
"/Users/travis.shears/.vscode/extensions/joedevivo.vscode-circuitpython-0.1.20-darwin-arm64/stubs",
|
||||
"/Users/travis.shears/Library/Application Support/Code/User/globalStorage/joedevivo.vscode-circuitpython/bundle/20230815/adafruit-circuitpython-bundle-py-20230815/lib"
|
||||
],
|
||||
"circuitpython.board.version": "8.2.0",
|
||||
"circuitpython.board.vid": "0x239A",
|
||||
"circuitpython.board.pid": "0x80F4"
|
||||
}
|
||||
30
CmakeLists.txt
Normal file
30
CmakeLists.txt
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# What CMake to start at
|
||||
cmake_minimum_required( VERSION 3.31 )
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
#set(PICO_BOARD pico CACHE STRING "Board type")
|
||||
|
||||
# Pull in SDK (must be before project)
|
||||
include( pico_sdk_import.cmake )
|
||||
|
||||
# Set the name and version of the project
|
||||
|
||||
project(test C CXX ASM)
|
||||
# project(test VERSION 1.0.0 )
|
||||
|
||||
# Initialize the SDK
|
||||
pico_sdk_init()
|
||||
|
||||
|
||||
# the executable
|
||||
add_executable( node1 node1.c )
|
||||
pico_set_program_version(node1 "0.1")
|
||||
pico_set_program_name(node1 "node_one")
|
||||
|
||||
# pull in common dependencies
|
||||
target_link_libraries( node1 pico_stdlib )
|
||||
|
||||
# create map/bin/hex file etc.
|
||||
pico_add_extra_outputs( node1 )
|
||||
20
README.md
20
README.md
|
|
@ -2,13 +2,23 @@
|
|||
|
||||
A DIY weather station on my balcony that is hooked up to [Home Assistant](https://www.home-assistant.io/).
|
||||
|
||||
The code is [CircuitPython](https://circuitpython.org/). It follows many of the patterns I learned in creating an [aquarium controller](https://git.sr.ht/~travisshears/sewa-reef-controller).
|
||||
The source code started as [CircuitPython](https://circuitpython.org/) following
|
||||
many of the patterns I learned in creating an [aquarium controller](https://git.sr.ht/~travisshears/sewa-reef-controller).
|
||||
I have since switch to using C.
|
||||
|
||||
## Capabilities
|
||||
## Capabilities
|
||||
|
||||
Right now the station is fairly limited measuring only temperature, humidity,
|
||||
and atmospheric pressure. I have plans to expand the project to also measure
|
||||
noise levels, and wind.
|
||||
and atmospheric pressure. I have plans to expand the project to in the end measure:
|
||||
|
||||
- tempature
|
||||
- humidity
|
||||
- atmospheric pressure
|
||||
- noise levels
|
||||
- wind speed
|
||||
- wind direction
|
||||
- rain fall
|
||||
- light
|
||||
|
||||
## Hardware
|
||||
|
||||
|
|
@ -17,12 +27,12 @@ Sensors:
|
|||
|
||||
MCs:
|
||||
- [Raspberry Pi Pico](https://www.raspberrypi.com/products/raspberry-pi-pico/)
|
||||
- [Adafruit Airlift](https://www.adafruit.com/product/4201)
|
||||
|
||||
Other:
|
||||
- [Weatherproof Enclosure](https://shop.pimoroni.com/products/weatherproof-cover-for-outdoor-sensors?variant=40047884468307)
|
||||
|
||||
_Note: I would not recommend buying from pimoroni from EU as shipping tax is huge!_
|
||||
|
||||
## Images
|
||||
|
||||
Circuit board:
|
||||
|
|
|
|||
240
node1.c
Normal file
240
node1.c
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/pio.h"
|
||||
#include "hardware/clocks.h"
|
||||
#include "blink_led.pio.h"
|
||||
|
||||
// == TM1637Display ==
|
||||
// #define TM1637_CMD_DATA_AUTO 0x40
|
||||
#define TM1637_DELAY_US 20 // Delay time in microseconds
|
||||
#define TM1637_CMD_DATA_FIXED 0x44
|
||||
#define TM1637_CMD_ADDR_START 0xC0
|
||||
#define TM1637_CMD_DISPLAY 0x88
|
||||
#define TM1637_BRIGHTNESS 5 // min 0 max 7
|
||||
|
||||
typedef struct {
|
||||
uint clock_pin;
|
||||
uint data_pin;
|
||||
} TM1637Display;
|
||||
|
||||
TM1637Display tm1637_init(uint clock_pin, uint data_pin);
|
||||
void tm1637_write_command(const TM1637Display *d, uint8_t command);
|
||||
void tm1637_display_number(const TM1637Display *d, uint num);
|
||||
void tm1637_start(const TM1637Display *display);
|
||||
void tm1637_stop(const TM1637Display *display);
|
||||
void tm1637_write_bit(const TM1637Display *display, bool bit);
|
||||
bool tm1637_write_byte(const TM1637Display *display, uint8_t byte);
|
||||
void tm1637_write_command(const TM1637Display *display, uint8_t command);
|
||||
|
||||
|
||||
const uint8_t tm1637_num_patterns[10] = {
|
||||
0x3F, // 0
|
||||
0x06, // 1
|
||||
0x5B, // 2
|
||||
0x4F, // 3
|
||||
0x66, // 4
|
||||
0x6D, // 5
|
||||
0x7D, // 6
|
||||
0x07, // 7
|
||||
0x7F, // 8
|
||||
0x6F // 9
|
||||
};
|
||||
|
||||
TM1637Display tm1637_init(uint clock_pin, uint data_pin) {
|
||||
TM1637Display display;
|
||||
display.clock_pin = clock_pin;
|
||||
display.data_pin = data_pin;
|
||||
|
||||
|
||||
gpio_init(display.clock_pin);
|
||||
gpio_init(display.data_pin);
|
||||
|
||||
gpio_set_dir(display.clock_pin, GPIO_OUT);
|
||||
gpio_set_dir(display.data_pin, GPIO_OUT);
|
||||
tm1637_write_command(&display, TM1637_CMD_DISPLAY | TM1637_BRIGHTNESS);
|
||||
return display;
|
||||
}
|
||||
|
||||
void tm1637_start(const TM1637Display *display) {
|
||||
gpio_set_dir(display->data_pin, GPIO_OUT);
|
||||
gpio_put(display->data_pin, 1);
|
||||
gpio_put(display->clock_pin, 1);
|
||||
sleep_us(TM1637_DELAY_US);
|
||||
gpio_put(display->data_pin, 0);
|
||||
sleep_us(TM1637_DELAY_US);
|
||||
gpio_put(display->clock_pin, 0);
|
||||
}
|
||||
|
||||
void tm1637_stop(const TM1637Display *display) {
|
||||
gpio_set_dir(display->data_pin, GPIO_OUT);
|
||||
gpio_put(display->clock_pin, 0);
|
||||
gpio_put(display->data_pin, 0);
|
||||
sleep_us(TM1637_DELAY_US);
|
||||
gpio_put(display->clock_pin, 1);
|
||||
sleep_us(TM1637_DELAY_US);
|
||||
gpio_put(display->data_pin, 1);
|
||||
sleep_us(TM1637_DELAY_US);
|
||||
}
|
||||
|
||||
void tm1637_write_bit(const TM1637Display *display, bool bit) {
|
||||
gpio_put(display->clock_pin, 0);
|
||||
if (bit)
|
||||
{
|
||||
gpio_set_dir(display->data_pin, GPIO_IN);
|
||||
}
|
||||
else
|
||||
{
|
||||
gpio_set_dir(display->data_pin, GPIO_OUT);
|
||||
gpio_put(display->data_pin, 0);
|
||||
}
|
||||
sleep_us(TM1637_DELAY_US);
|
||||
gpio_put(display->clock_pin, 1);
|
||||
sleep_us(TM1637_DELAY_US);
|
||||
gpio_put(display->clock_pin, 0);
|
||||
gpio_set_dir(display->data_pin, GPIO_OUT);
|
||||
}
|
||||
|
||||
bool tm1637_write_byte(const TM1637Display *display, uint8_t byte) {
|
||||
for (int i = 0; i < 8; i++)
|
||||
{
|
||||
tm1637_write_bit(display, byte & 0x01);
|
||||
byte >>= 1;
|
||||
}
|
||||
// Wait for ACK
|
||||
gpio_set_dir(display->data_pin, GPIO_IN);
|
||||
sleep_us(TM1637_DELAY_US);
|
||||
gpio_put(display->clock_pin, 0);
|
||||
sleep_us(TM1637_DELAY_US);
|
||||
gpio_put(display->clock_pin, 1);
|
||||
sleep_us(TM1637_DELAY_US);
|
||||
bool ack = !gpio_get(display->data_pin);
|
||||
gpio_put(display->clock_pin, 0);
|
||||
gpio_set_dir(display->data_pin, GPIO_OUT);
|
||||
return ack;
|
||||
}
|
||||
|
||||
void tm1637_write_command(const TM1637Display *display, uint8_t command) {
|
||||
tm1637_start(display);
|
||||
tm1637_write_byte(display, command);
|
||||
tm1637_stop(display);
|
||||
}
|
||||
|
||||
void tm1637_display_number(const TM1637Display *display, uint num) {
|
||||
if (num > 9999) return;
|
||||
|
||||
uint8_t digits[4] = {0};
|
||||
|
||||
// Extract digits and store them in the array from most significant digit to least significant digit
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
digits[3 - i] = num % 10;
|
||||
num /= 10;
|
||||
}
|
||||
|
||||
tm1637_start(display);
|
||||
bool ack = tm1637_write_byte(display, TM1637_CMD_DATA_FIXED);
|
||||
tm1637_stop(display);
|
||||
if(!ack) return tm1637_display_number(display, num);
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
tm1637_start(display);
|
||||
bool ack1 = tm1637_write_byte(display, TM1637_CMD_ADDR_START + i);
|
||||
bool ack2 = tm1637_write_byte(display, tm1637_num_patterns[digits[i]]);
|
||||
tm1637_stop(display);
|
||||
if(!ack1 || !ack2) return tm1637_display_number(display, num);
|
||||
}
|
||||
}
|
||||
|
||||
// == LEDS ==
|
||||
void init_leds() {
|
||||
gpio_init(PICO_DEFAULT_LED_PIN);
|
||||
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
|
||||
}
|
||||
void set_led(bool state) {
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, state);
|
||||
}
|
||||
// == RGB_LED ==
|
||||
|
||||
// #define RGB_LED_PIN PICO_DEFAULT_LED_PIN
|
||||
#define RGB_PIO_FREQ 2000
|
||||
// PIO blink example: https://www.digikey.com/en/maker/projects/raspberry-pi-pico-and-rp2040-cc-part-3-how-to-use-pio/123ff7700bc547c79a504858c1bd8110
|
||||
// static const uint led_pin = 25;
|
||||
// static const float pio_freq = 2000;
|
||||
void init_rgb_leds() {
|
||||
//Choose PIO instance (0 or 1)
|
||||
PIO pio = pio0;
|
||||
// Get first free state machine in PIO 0
|
||||
uint sm = pio_claim_unused_sm(pio, true);
|
||||
|
||||
// Add PIO program to PIO instruction memory. SDK will find location and
|
||||
// return with the memory offset of the program.
|
||||
uint offset = pio_add_program(pio, &blink_program);
|
||||
|
||||
// Calculate the PIO clock divider
|
||||
float div = (float)clock_get_hz(clk_sys) / RGB_PIO_FREQ;
|
||||
|
||||
// Initialize the program using the helper function in our .pio file
|
||||
blink_program_init(pio, sm, offset, PICO_DEFAULT_LED_PIN, div);
|
||||
|
||||
// Start running our PIO program in the state machine
|
||||
pio_sm_set_enabled(pio, sm, true);
|
||||
|
||||
}
|
||||
|
||||
// == Buttons ==
|
||||
#define ARM_BTN_GPIO 15
|
||||
void init_btn(uint pin) {
|
||||
gpio_init(pin);
|
||||
gpio_set_dir(pin, GPIO_IN);
|
||||
gpio_pull_up(pin);
|
||||
}
|
||||
|
||||
alarm_id_t armed_timer_id;
|
||||
int64_t armed_cb() {
|
||||
printf("ROCKET ARMED\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool arm_btn_pressed = false;
|
||||
void check_btns() {
|
||||
if(!gpio_get(ARM_BTN_GPIO) && !arm_btn_pressed) {
|
||||
printf("BTN PRESSED\n");
|
||||
armed_timer_id = add_alarm_in_ms(5000, armed_cb, NULL, false);
|
||||
arm_btn_pressed = true;
|
||||
set_led(true);
|
||||
}
|
||||
|
||||
if(gpio_get(ARM_BTN_GPIO) && arm_btn_pressed) {
|
||||
printf("BTN LET GO\n");
|
||||
cancel_alarm(armed_timer_id);
|
||||
arm_btn_pressed = false;
|
||||
set_led(false);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
// const TM1637Display digit_display_001 = tm1637_init(12, 13);
|
||||
// const TM1637Display digit_display_002 = tm1637_init(14, 15);
|
||||
|
||||
// init_btn(ARM_BTN_GPIO);
|
||||
// init_leds();
|
||||
init_rgb_leds();
|
||||
|
||||
// static absolute_time_t delayed_by_ms (const absolute_time_t t, uint32_t ms)
|
||||
|
||||
// volatile bool timer_fired = false;
|
||||
|
||||
while (true) {
|
||||
check_btns();
|
||||
sleep_us(50);
|
||||
|
||||
// for (int i = 0; i <= 9999; i ++) {
|
||||
// printf("Display: %d \n", i);
|
||||
// // tm1637_display_number(&digit_display_001, i);
|
||||
// tm1637_display_number(&digit_display_002, i);
|
||||
// sleep_ms(100);
|
||||
// }
|
||||
}
|
||||
}
|
||||
84
pico_sdk_import.cmake
Normal file
84
pico_sdk_import.cmake
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
# This is a copy of <PICO_SDK_PATH>/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()
|
||||
|
||||
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 ()
|
||||
# GIT_SUBMODULES_RECURSE was added in 3.17
|
||||
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
|
||||
FetchContent_Declare(
|
||||
pico_sdk
|
||||
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
|
||||
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
|
||||
GIT_SUBMODULES_RECURSE FALSE
|
||||
)
|
||||
else ()
|
||||
FetchContent_Declare(
|
||||
pico_sdk
|
||||
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
|
||||
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
|
||||
)
|
||||
endif ()
|
||||
|
||||
if (NOT pico_sdk)
|
||||
message("Downloading Raspberry Pi Pico SDK")
|
||||
FetchContent_Populate(pico_sdk)
|
||||
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})
|
||||
Loading…
Add table
Add a link
Reference in a new issue