Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
200 lines (175 loc) · 6.21 KB
/
Copy pathMakefile
File metadata and controls
200 lines (175 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
SHELL := /bin/bash
PROJECT_NAME := melodi
TOP_DIR := $(CURDIR)
# Device mapping for board detection
DEVICE_TTGO := 1A86:55D4
# DEVICE_M0 := 239A:800B
# DEVICE_RPI := 239A:80F1
# Default board environment
BOARD_ENV ?= ttgo
# Colors for output
GREEN := \033[0;32m
YELLOW := \033[0;33m
NC := \033[0m
# Check for required tools
REQUIRED_TOOLS := pio jq gum
CHECK_TOOLS := $(foreach tool,$(REQUIRED_TOOLS),$(if $(shell which $(tool) 2>/dev/null),$(tool),))
MISSING_TOOLS := $(filter-out $(CHECK_TOOLS),$(REQUIRED_TOOLS))
ifneq ($(MISSING_TOOLS),)
$(error Missing required tools: $(MISSING_TOOLS). Please install them first)
endif
$(info ------------------------------------------)
$(info Project: $(PROJECT_NAME))
$(info ------------------------------------------)
.PHONY: build b upload u refresh r monitor m send s recv g clean help h
# Get board information
define get_board
$(shell pio device list --json-output | jq -c '.[] | select(.hwid != "n/a")' | gum choose --limit=1 --select-if-one | jq -r '.port')
endef
define get_board_info
$(shell board=$$(pio device list --json-output | jq -c '.[] | select(.hwid != "n/a")' | gum choose --limit=1 --select-if-one); \
echo "$$(echo "$$board" | jq -r '.port')|$$(echo "$$board" | jq -r '.hwid' | grep -oP 'VID:PID=\K[^ ]+' || echo 'unknown')")
endef
# Auto-detect board environment based on device ID
define detect_board_env
$(shell board=$$(pio device list --json-output | jq -c '.[] | select(.hwid != "n/a")' | gum choose --limit=1 --select-if-one); \
hwid=$$(echo "$$board" | jq -r '.hwid' | grep -oP 'VID:PID=\K[^ ]+' || echo 'unknown'); \
case "$$hwid" in \
"1A86:55D4") echo "ttgo" ;; \
"239A:800B") echo "m0" ;; \
"239A:80F1") echo "rpi" ;; \
*) echo "$(BOARD_ENV)" ;; \
esac)
endef
build:
@echo -e "$(GREEN)Building for $(BOARD_ENV)...$(NC)"
@if [ -z "$(BOARD_ENV)" ]; then \
echo "Building all boards..."; \
pio -f -c vim run; \
else \
pio -f -c vim run -e $(BOARD_ENV); \
fi
b: build
upload:
@echo -e "$(GREEN)Detecting board...$(NC)"
@board=$$(pio device list --json-output | jq -c '.[] | select(.hwid != "n/a")' | gum choose --limit=1 --select-if-one); \
port=$$(echo "$$board" | jq -r '.port'); \
hwid=$$(echo "$$board" | jq -r '.hwid' | grep -oP 'VID:PID=\K[^ ]+' || echo 'unknown'); \
if [ -z "$$port" ]; then \
echo -e "$(YELLOW)No board detected!$(NC)"; \
exit 1; \
fi; \
case "$$hwid" in \
"1A86:55D4") board_env="ttgo" ;; \
"239A:800B") board_env="m0" ;; \
"239A:80F1") board_env="rpi" ;; \
*) board_env="unknown" ;; \
esac; \
echo -e "$(GREEN)Found $$board_env board at $$port ($$hwid)$(NC)"; \
if [ "$$board_env" = "unknown" ]; then \
echo -e "$(YELLOW)Unknown board type! Using default: $(BOARD_ENV)$(NC)"; \
board_env="$(BOARD_ENV)"; \
fi; \
if [ -n "$(IPV6)" ]; then \
export BOARD_IPV6=$(IPV6); \
echo -e "$(GREEN)Setting IPv6: $(IPV6)$(NC)"; \
fi; \
if [ -z "$(NO_CONFIRM)" ]; then \
gum confirm --default "Upload to $$board_env at $$port?" && pio -f -c vim run -e $$board_env -t upload --upload-port $$port; \
else \
pio -f -c vim run -e $$board_env -t upload --upload-port $$port; \
fi; \
if [ -n "$(MONITOR)" ]; then \
sleep 1; \
$(MAKE) monitor PORT=$$port; \
fi
u: upload
refresh:
@echo -e "$(GREEN)Refreshing project files...$(NC)"
@pio project init --ide vim && pio run --target compiledb
r: refresh
monitor:
@echo -e "$(GREEN)Starting serial monitor...$(NC)"
@if [ -n "$(PORT)" ]; then \
pio device monitor --baud 115200 --port $(PORT); \
else \
port=$$(pio device list --json-output | jq -c '.[] | select(.hwid != "n/a")' | gum choose --limit=1 --select-if-one | jq -r '.port'); \
if [ -z "$$port" ]; then \
echo -e "$(YELLOW)No board detected!$(NC)"; \
exit 1; \
fi; \
echo -e "$(GREEN)Monitoring $$port...$(NC)"; \
pio device monitor --baud 115200 --port $$port; \
fi
m: monitor
send:
@echo -e "$(GREEN)Sending message...$(NC)"
@port=$$(pio device list --json-output | jq -c '.[] | select(.hwid != "n/a")' | gum choose --limit=1 --select-if-one | jq -r '.port'); \
if [ -z "$$port" ]; then \
echo -e "$(YELLOW)No board detected!$(NC)"; \
exit 1; \
fi; \
ipv6="$(IPV6)"; \
if [ -z "$$ipv6" ]; then \
ipv6=$$(gum input --placeholder="IPv6 address (default: broadcast)"); \
if [ -z "$$ipv6" ]; then \
ipv6="broadcast"; \
fi; \
fi; \
content="$(CONTENT)"; \
if [ -z "$$content" ]; then \
content=$$(gum input --placeholder="Message content (default: Hello LoRa)"); \
if [ -z "$$content" ]; then \
content="Hello from LoRa mesh network!"; \
fi; \
fi; \
python3 script/send.py --port $$port --address "$$ipv6" --payload "$$content"
s: send
recv:
@echo -e "$(GREEN)Receiving messages...$(NC)"
@if [ -n "$(PORT)" ]; then \
python3 script/recv.py --port $(PORT); \
else \
port=$$(pio device list --json-output | jq -c '.[] | select(.hwid != "n/a")' | gum choose --limit=1 --select-if-one | jq -r '.port'); \
if [ -z "$$port" ]; then \
echo -e "$(YELLOW)No board detected!$(NC)"; \
exit 1; \
fi; \
echo -e "$(GREEN)Listening on $$port...$(NC)"; \
python3 script/recv.py --port $$port; \
fi
g: recv
clean:
@echo -e "$(GREEN)Cleaning build files...$(NC)"
@pio run --target clean
@rm -rf .pio
help:
@echo
@echo "Usage: make [target] [OPTIONS]"
@echo
@echo "Available targets:"
@echo " build (b) - Build the firmware for BOARD_ENV"
@echo " upload (u) - Upload firmware to connected board"
@echo " refresh (r) - Refresh IDE project files"
@echo " monitor (m) - Monitor serial output"
@echo " send (s) - Send a test message"
@echo " recv (g) - Receive messages"
@echo " clean - Clean build files"
@echo " help (h) - Show this help"
@echo
@echo "Options:"
@echo " BOARD_ENV=<env> - Board environment (default: ttgo)"
@echo " IPV6=<address> - IPv6 address for upload or send"
@echo " NO_CONFIRM=1 - Skip upload confirmation"
@echo " MONITOR=1 - Monitor after upload"
@echo " PORT=<port> - Specific serial port"
@echo " CONTENT=<text> - Message content for send"
@echo
@echo "Examples:"
@echo " make build"
@echo " make upload MONITOR=1"
@echo " make upload IPV6=2001:db8::1 NO_CONFIRM=1"
@echo " make send IPV6=2001:db8::2 CONTENT='Hello World'"
@echo " make monitor PORT=/dev/ttyACM0"
@echo
h: help