-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
179 lines (142 loc) · 4.86 KB
/
code.py
File metadata and controls
179 lines (142 loc) · 4.86 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
"""
Device Initialization Test Script
This script tests the initialization of the stepper motor, NeoPixel ring,
and WiFi connectivity using the ECEGMotor, NeoPixelRing, and WifiManager classes.
After successful initialization, the device enters an idle state with a
breathing LED effect.
Author: Aiden Cherniske
Date: 2026-01-30
"""
import board
import busio
import time
from motorFunctions import ECEGMotor
from neopixelFunctions import NeoPixelRing
from wifiFunctions import WifiManager
# Configuration Constants
IDLE_COLOR = NeoPixelRing.COLOR_BLUE
IDLE_BRIGHTNESS_MIN = 0.02
IDLE_BRIGHTNESS_MAX = 0.15
IDLE_BREATH_PERIOD = 3.0 # seconds for a full breath cycle
IDLE_UPDATE_RATE = 0.03 # seconds between updates
TEST_API_URL = "https://jsonplaceholder.typicode.com/todos/1"
def initialize_motor(i2c):
"""
Initialize the stepper motor and perform homing.
Args:
i2c: I2C bus instance
Returns:
ECEGMotor instance or None if initialization failed
"""
print("Initializing stepper motor...")
try:
motor = ECEGMotor(i2c)
print(f"Stepper motor initialized. "
f"Current step: {motor.current_step}, "
f"degree: {motor.current_degree:.1f}")
# Perform homing sequence
print("\nPerforming motor homing sequence...")
motor.find_home()
print(f"Motor homed successfully. "
f"Current step: {motor.current_step}, "
f"degree: {motor.current_degree:.1f}")
return motor
except Exception as e:
print(f"Stepper motor initialization/homing failed: {e}")
return None
def initialize_neopixel():
"""
Initialize the NeoPixel ring.
Returns:
NeoPixelRing instance or None if initialization failed
"""
print("Initializing NeoPixel ring...")
try:
ring = NeoPixelRing()
ring.fill(NeoPixelRing.COLOR_GREEN)
print("NeoPixel ring initialized and set to green.")
return ring
except Exception as e:
print(f"NeoPixel ring initialization failed: {e}")
return None
def initialize_wifi():
"""
Initialize WiFi connection and test API access.
Returns:
WifiManager instance or None if initialization failed
"""
print("Initializing WiFi...")
try:
wifi_manager = WifiManager(verbose=True, auto_connect=True)
wifi_manager.print_status()
print("WiFi initialized and status printed.")
# Test API request
test_api_request(wifi_manager)
return wifi_manager
except Exception as e:
print(f"WiFi initialization failed: {e}")
return None
def test_api_request(wifi_manager):
"""
Test API request to verify internet connectivity.
Args:
wifi_manager: WifiManager instance
"""
try:
print(f"Testing API request to {TEST_API_URL}...")
response = wifi_manager.get(TEST_API_URL)
print("API request successful. Response:")
print(response.text)
except Exception as e:
print(f"API request failed: {e}")
def run_idle_breathing(ring):
"""
Run the idle breathing effect indefinitely.
Uses a smooth sine wave for natural breathing motion.
Press Ctrl+C to exit.
Args:
ring: NeoPixelRing instance
"""
import math
print("\n=== Entering Idle State ===")
print(f"Breathing effect: {IDLE_COLOR}")
print(f"Brightness range: {IDLE_BRIGHTNESS_MIN:.2f} - {IDLE_BRIGHTNESS_MAX:.2f}")
print(f"Period: {IDLE_BREATH_PERIOD:.1f} seconds")
print("Press Ctrl+C to exit\n")
# Use the built-in breathing effect with infinite cycles
try:
steps = int(IDLE_BREATH_PERIOD / IDLE_UPDATE_RATE / 2)
ring.breathing_effect(
color=IDLE_COLOR,
min_brightness=IDLE_BRIGHTNESS_MIN,
max_brightness=IDLE_BRIGHTNESS_MAX,
steps=steps,
delay=IDLE_UPDATE_RATE,
cycles=999999 # Effectively infinite
)
except KeyboardInterrupt:
print("\nExiting idle state.")
ring.clear()
def main():
"""Main initialization and test sequence."""
print("\n" + "=" * 60)
print("Device Initialization Test")
print("=" * 60 + "\n")
# Initialize I2C bus for motor
print("Initializing I2C bus for stepper motor...")
i2c = busio.I2C(board.SCL, board.SDA)
time.sleep(0.5)
# Initialize all components
motor = initialize_motor(i2c)
ring = initialize_neopixel()
wifi = initialize_wifi()
print("\n" + "=" * 60)
print("Initialization Test Complete")
print("=" * 60 + "\n")
# Enter idle state if NeoPixel ring initialized successfully
if ring is not None:
run_idle_breathing(ring)
else:
print("Cannot enter idle state: NeoPixel ring not initialized")
if __name__ == "__main__":
main()