Skip to content

Commit

Permalink
Added digital port toggling.
Browse files Browse the repository at this point in the history
You can now toggle a port by clicking on the corresponding button.
  • Loading branch information
73VW committed Mar 16, 2018
1 parent 5e2b82c commit 681e38a
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 122 deletions.
97 changes: 74 additions & 23 deletions arduino/arduinozaure/arduinozaure.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
bool isOn[NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS];

void setup()
{
Serial.begin(38400);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
for (int i = 0; i < NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS; i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
isOn[i] = false;
}
}

void loop()
Expand All @@ -14,41 +22,84 @@ void loop()

void annonce()
{
while(Serial.available() == 0)
while (Serial.available() == 0)
{
Serial.print("My name is Arduinozaure. Send [ok] to start. Number of analogic pins: ");
Serial.println(NUM_ANALOG_INPUTS);
Serial.print("My name is Arduinozaure. Send [ok] to start. Number of analogic inputs: ");
Serial.print(NUM_ANALOG_INPUTS);
Serial.print(", number of digital outputs: ");
Serial.println(NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS);
delay(250);
String cmd = Serial.readString();
if(cmd == "ok")
if (cmd == "ok")
break;
}
}

void attente()
{
Serial.println("You can now ask a sensor value. Send me its pin number.");
while(true)
char inChar;
Serial.println("You can now ask for reading [r] or writing [w].");
while (true)
{
unsigned long start = millis();
int pinNumber = 0;
while(Serial.available() == 0)
{
if(millis() - start > 10000)
return;
inChar = Serial.read();

if (inChar == 'r') {
reading();
Serial.write(".");
}
String m = Serial.readString();
pinNumber = m.toInt();
String msg;
if(pinNumber >= 0 && pinNumber < NUM_ANALOG_INPUTS){
pinMode(pinNumber, INPUT);
long val = analogRead(pinNumber);
msg = val;
else if (inChar == 'w') {
writing();
Serial.write(".");
}
}
}

void reading()
{
unsigned long start = millis();
int pinNumber = 0;
while (Serial.available() == 0)
{
if (millis() - start > 10000)
return;
}
String m = Serial.readString();
pinNumber = m.toInt();
String msg;
if (pinNumber >= 0 && pinNumber < NUM_ANALOG_INPUTS) {
pinMode(pinNumber, INPUT);
long val = analogRead(pinNumber);
msg = val;
}
else
{
msg = "Wrong pin number specified..";
}
Serial.println(msg);
}

void writing()
{
unsigned long start = millis();
int pinNumber = 0;
while (Serial.available() == 0)
{
if (millis() - start > 10000)
return;
}
String m = Serial.readString();
pinNumber = m.toInt();
String msg;
if (pinNumber >= 0 && pinNumber < NUM_DIGITAL_PINS - NUM_ANALOG_INPUTS) {
if (isOn[pinNumber])
digitalWrite(pinNumber, LOW);
else
{
msg = "Wrong pin number specified..\r\nTry again..";
}
Serial.println(msg);
digitalWrite(pinNumber, HIGH);
isOn[pinNumber] = !isOn[pinNumber];
}
else
{
msg = "Wrong pin number specified..";
}
Serial.println(msg);
}
2 changes: 1 addition & 1 deletion arduinozore/.config/card/QXJkdWlubyBVbm8=.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
!Card
name: Arduino Uno
nb_input_pins: 6
nb_output_pins: 12
nb_output_pins: 14
2 changes: 0 additions & 2 deletions arduinozore/handlers/crudHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class CrudHandler(BaseHandler):

def get(self, slug=None, method=None):
"""Handle get request."""

if slug is None:
self.list()
return
Expand All @@ -33,7 +32,6 @@ def post(self, slug=None, method=None):
Broswers can't send put and delete request so we have to fake them.
"""

try:
_method = self.get_argument('_method')
except Exception:
Expand Down
10 changes: 3 additions & 7 deletions arduinozore/handlers/device.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
"""Index page handler package."""
import os
import re
import socket

from sensors import get_classes
from models.card import Card
from models.device import Device
from models.sensor import Sensor
from settings import DEVICE_CONFIG_FOLDER
from settings import SSL_PORT
from settings import path
from yaml import dump

from .crudHandler import CrudHandler
from .tools import get_arduino
from .tools import get_config_name
from .tools import load_config_from_arduino
from models.device import Device
from models.card import Card
from models.sensor import Sensor


class DevicePageHandler(CrudHandler):
Expand Down
4 changes: 2 additions & 2 deletions arduinozore/handlers/index.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Index page handler package."""

from .baseHandler import BaseHandler
from .tools import get_arduinos
from models.device import Device

from .baseHandler import BaseHandler


class IndexPageHandler(BaseHandler):
"""Index page handler."""
Expand Down
19 changes: 19 additions & 0 deletions arduinozore/handlers/serialManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ def __init__(self):
print("Manager inited")
sys.stdout.flush()
self.datas = Manager().dict()
self.out = Manager().dict()

def toggle_pin(self, port, pin):
"""Toggle pin on card connected to port."""
if port not in dict(self.out):
self.out[port] = pin

print(port)
print(self.out[port])
sys.stdout.flush()

def get_toggelable_pin(self, port):
"""Return toggelable pins for port."""
try:
toggle = self.out[port]
del self.out[port]
return toggle
except KeyError:
return None

def set_datas(self, port, datas):
"""Set data from child process."""
Expand Down
25 changes: 12 additions & 13 deletions arduinozore/handlers/serialReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
from multiprocessing import Manager
from multiprocessing import Process

from serial import Serial
from settings import DEVICE_CONFIG_FOLDER
from settings import path
from yaml import safe_load

from models.device import Device
from models.sensor import Sensor
from serial import Serial


class SerialReader(Process):
Expand Down Expand Up @@ -73,28 +69,26 @@ def init_serial(self):
self.ser = Serial(self.serial_port, timeout=None, baudrate=38400)

self.pattern = re.compile(r'[\t\r\n]+')
msg1 = 'My name is Arduinozaure. Send [ok] to start. Number of analogic pins: 6'
msg2 = 'You can now ask a sensor value. Send me its pin number.'
msg2 = 'You can now ask for reading [r] or writing [w].'

self.ser.read(self.ser.inWaiting())
datas = re.sub(self.pattern, '', self.ser.readline().decode())
while datas != msg1:
time.sleep(0.4)
datas = re.sub(self.pattern, '', self.ser.readline().decode())
while datas != msg2:
self.ser.read(self.ser.inWaiting())
self.ser.write("ok".encode())
time.sleep(0.4)
datas = re.sub(self.pattern, '', self.ser.readline().decode())
print(datas)
sys.stdout.flush()

def read_serial(self):
"""Read serial port."""

try:
while not self.exit.is_set():
self.ser.read(self.ser.inWaiting())
self.get_port_list()
for port in dict(self.ports):
self.ser.write(str.encode(str(port)))
print(self.ser.read(self.ser.inWaiting()).decode())
self.ser.write('r'.encode() + str.encode(str(port)))
data = re.sub(self.pattern, '',
self.ser.readline().decode())
if self.sensors[port] is not None:
Expand All @@ -103,5 +97,10 @@ def read_serial(self):
else:
self.value[port] = data
self.parent.set_datas(self.serial_port, dict(self.value))
port = self.parent.get_toggelable_pin(self.serial_port)
if port is not None:
print(self.ser.read(self.ser.inWaiting()).decode())
self.ser.write('w'.encode() + str.encode(str(port)))

except (KeyboardInterrupt) as e:
exit()
9 changes: 3 additions & 6 deletions arduinozore/handlers/ws.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""WebSocket handler package."""
import sys
from multiprocessing import Process

import sensors
from tornado.websocket import WebSocketHandler


Expand Down Expand Up @@ -35,10 +33,9 @@ def open(self, slug):

def on_message(self, message):
"""Handle incomming messages."""
if message is not "Capteurs":
sens_name = self.sensor.name
class_ = getattr(sensors, message)
self.sensor = class_(sens_name)
print(message)
sys.stdout.flush()
WSHandler.serial_manager.toggle_pin(self.port, int(message))

def on_close(self):
"""Handle connection closing."""
Expand Down
13 changes: 8 additions & 5 deletions arduinozore/models/device.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Device model package."""

from serial.tools import list_ports
from settings import DEVICE_CONFIG_FOLDER

from .model import Model
from .card import Card
from serial.tools import list_ports
import sys
from .model import Model


class Port(Model):
Expand Down Expand Up @@ -99,8 +99,11 @@ def get_connected_devices(cls):
def get_config(cls, name):
"""Get config by name."""
configs = cls.get_all()
return next(
(config for config in configs if config.name == name), None)
if configs is not None:
return next(
(config for config in configs if config.name == name), None)
else:
return configs

@classmethod
def get_identifier_from_serial(cls, serial):
Expand Down
53 changes: 0 additions & 53 deletions arduinozore/sensors.py

This file was deleted.

Loading

0 comments on commit 681e38a

Please sign in to comment.