Skip to content
Snippets Groups Projects
Commit 4ed5e157 authored by sorgre's avatar sorgre
Browse files

Working version!

parent b4278963
Branches master
No related tags found
No related merge requests found
macie.py 0 → 100644
import serial, time
import serial.tools.list_ports
class macie:
def __init__(self, read_timeout=3, baud_rate=115200):
self.device = None
for port in serial.tools.list_ports.grep("arduino"):
device = serial.Serial(port.device, 115200, timeout=3)
time.sleep(2) # Give the device a chance!
first_line = device.readline()
if first_line == b'<macie is ready to work>\r\n':
self.device = device
break
else:
device.close()
if self.device is None:
raise RuntimeError("Could not connect to a macie!")
self.device.readlines() # Make sure to empty read buffer
def check_result(self):
return_value = self.device.read()
if return_value == b'':
print('Warning: macie timed out.')
elif return_value != b'\0':
raise RuntimeError("Command timeout: Unknown problem in macie!")
def trigger_led(self, led_index):
self.device.write(str(led_index).encode() + b'\n')
self.check_result()
def flood_light(self, leds="w"):
if leds == "w" or leds == "W":
self.device.write(b'f')
if leds == "rgbw" or leds == "RGBW":
self.device.write(b'F')
self.check_result()
def dark(self):
self.device.write(b'b')
self.check_result()
mace = macie()
for i in range(15):
mace.trigger_led(i)
mace.flood_light()
mace.flood_light("RGBW")
mace.dark()
#include <Adafruit_NeoPixel.h>
#define LED_PIN 7
#define LED_COUNT 15
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_RBGW + NEO_KHZ800);
uint32_t all_white = Adafruit_NeoPixel::Color(255, 255, 255, 255);
uint32_t white = Adafruit_NeoPixel::Color(0, 0, 0, 255);
void setup() {
Serial.begin(115200); // set up Serial library at 9600 bps
strip.begin();
strip.clear();
strip.show();
Serial.println("<macie is ready to work>");
}
void loop() {
static const uint8_t buffer_size = 3;
static char data_buffer[buffer_size];
static uint8_t buffer_idx = 0;
static bool reading_data = true;
if(Serial.available() > 0 && reading_data) {
char next_data = Serial.read();
// single character commands
if (buffer_idx == 0 && next_data == 'b') {
strip.clear();
strip.show();
Serial.write('\0');
return;
}
if (buffer_idx == 0 && next_data == 'B') {
strip.clear();
strip.show();
Serial.write('\0');
return;
}
if (buffer_idx == 0 && next_data == 'F') {
strip.fill(all_white);
strip.show();
return;
}
if (buffer_idx == 0 && next_data == 'f') {
strip.fill(white);
strip.show();
Serial.write('\0');
return;
}
// multi character commands
static char eol = '\n';
if (next_data != eol) {
data_buffer[buffer_idx] = next_data;
buffer_idx++;
if (buffer_idx >= buffer_size)
buffer_idx = buffer_size - 1; // Just reuse the last char
} else { // End of line---stop reading!
data_buffer[buffer_idx] = '\0';
buffer_idx = 0;
reading_data = false;
}
if (!reading_data) {
reading_data = true;
if (data_buffer[0] < '0' || data_buffer[0] > '9') // Non-digit
return;
int x = data_buffer[0] - '0';
if (data_buffer[1] != '\0') { // multi-digit
if (data_buffer[1] < '0' || data_buffer[1] > '9') // Non-digit
return;
x *= 10;
x += data_buffer[1] - '0';
if (data_buffer[2] != '\0') { // multi-digit
if (data_buffer[2] < '0' || data_buffer[2] > '9') // Non-digit
return;
x *= 10;
x += data_buffer[2] - '0';
}
if (data_buffer[3] != '\0')
return;
}
strip.clear();
strip.setPixelColor(x, 0, 0, 0, 255);
strip.show();
Serial.write('\0');
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment