Commit 3f46c099 authored by Nelso Jost's avatar Nelso Jost
Browse files

NEW: structure refactor; commom pio Makefile usage; DEV: esplogger with...

NEW: structure refactor; commom pio Makefile usage; DEV: esplogger with arduino software serial suport tested
parent b4ff0355
......@@ -5,23 +5,23 @@
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include <Ticker.h>
#include <SoftwareSerial.h>
#include "nodemcu_pinout.h"
#include "repl.h"
#include "repl_gpio.h"
#include "repl_sdcard.h"
#include "wifi.h"
#include "repl_wifi.h"
#include <SoftwareSerial.h>
#define BAUDRATE 115200
#define PIN_LED 0 // D3
#define PIN_BUTTON 2 // D4
#define PIN_LED D3
#define PIN_BUTTON D0
#define ARDUINO_TX 5 // D1 (soft RX)
#define ARDUINO_RX 4 // D2 (soft TX)
#define ARDUINO_TX D1 // (soft RX)
#define ARDUINO_RX D2 // (soft TX)
void REPL_WIFISCAN(REPL * repl);
void REPL_WIFISTATUS(REPL * repl);
void REPL_WIFICONNECT(REPL * repl);
void REPL_ARDUINO(REPL * repl);
......@@ -32,18 +32,13 @@ REPL_COMMAND commands[] =\
{
ALLCMD_REPL_GPIO,
ALLCMD_REPL_SDCARD,
{"wifiscan", REPL_WIFISCAN, "wifiscan",
"List all availables SSID in range for connection."},
{"wifistatus", REPL_WIFISTATUS, "wifistatus",
"Print WiFi debug info (connection status)."},
{"wificonnect", REPL_WIFICONNECT, "wificonnect <ssid> <password>",
"Connect to wifi network."},
ALLCMD_REPL_WIFI,
{"arduino", REPL_ARDUINO, "arduino <command>",
"Sendo command to arduino."},
"Sendo command to Arduino via software serial."},
{"ad", REPL_ARDUINO, "ad <command>",
"Sendo command to Arduino via software serial."},
{NULL}
};
......@@ -54,13 +49,16 @@ int button_state = 0;
Ticker timer_button;
void setup()
{
Serial.begin(9600);
timer_button.attach(0.1, toggle_led);
Serial.begin(BAUDRATE);
arduinoSerial.begin(9600);
pinMode(PIN_BUTTON, INPUT);
timer_button.attach(0.1, toggle_led);
}
void loop()
{
repl.update();
......@@ -83,22 +81,14 @@ void toggle_led()
}
void REPL_WIFISCAN(REPL * repl)
{
wifiscan();
}
void REPL_WIFISTATUS(REPL * repl)
{
wifistatus();
}
void REPL_WIFICONNECT(REPL * repl)
{
wificonnect(repl->get_arg(1), repl->get_arg(2), 10);
}
void REPL_ARDUINO(REPL * repl)
{
arduinoSerial.print("baka");
Serial.println();
arduinoSerial.print(repl->get_arg(1));
delay(1500);
while (arduinoSerial.available())
{
Serial.write(arduinoSerial.read());
}
}
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Mapping of NodeMCU pinout to real ESP GPIO numbers.
*---------------------------------------------------------------------------*/
#ifndef NODEMCU_PINOUT_H
#define NODEMCU_PINOUT_H
#define D0 16 // DEEP WAKE
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0 // FLASH
#define D4 2 // LED_BUILTIN (inverted logic)
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial)
#define D10 1 // TX0 (Serial)
#endif
......@@ -85,7 +85,7 @@ void REPL::_complete(void)
full_name = String(rc->name);
if (full_name.startsWith(partial_name))
{
Serial.print("\n\n[HELP] " + String(rc->prototype) + "\n" +
Serial.print("\n[HELP] " + String(rc->prototype) + "\n" +
this->prompt + " " + full_name + " ");
this->_command_buffer = full_name + " ";
}
......
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Read-Eval-Print-Loop functionality
* Purpose: Implementation of REPL GPIO functions.
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include "repl.h"
......
......@@ -6,6 +6,7 @@
#ifndef REPL_GPIO_H
#define REPL_GPIO_H
#define CMD_REPL_DWRITE {\
"dwrite", REPL_DWRITE, "dwrite <pin> <value>",\
"Perform pinMode(pin, OUTPUT) and digitalWrite(pin, value)."}
......@@ -32,6 +33,7 @@
"Blink builtin led (pin 2) with given <interval> in seconds (int|float). " \
"Use \"blink 0\" to stop it."}
#define ALLCMD_REPL_GPIO \
CMD_REPL_DWRITE, \
CMD_REPL_AWRITE, \
......@@ -40,6 +42,7 @@
CMD_REPL_LOG, \
CMD_REPL_BLINK
void REPL_DWRITE(REPL * repl);
void REPL_AWRITE(REPL * repl);
void REPL_AREAD(REPL * repl);
......@@ -47,4 +50,5 @@ void REPL_DREAD(REPL * repl);
void REPL_LOG(REPL * repl);
void REPL_BLINK(REPL * repl);
#endif
......@@ -8,9 +8,13 @@
#include "sdcard.h"
void REPL_SDCARDINFO(REPL * repl)
void REPL_SDINFO(REPL * repl)
{
print_sdcard_info();
}
void REPL_SDLOG(REPL * repl)
{
}
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* GPIO controlurpose: Provide basic SD card management commands for the REPL.
* Purpose: Wrap SD card management functions as commands for the REPL.
*---------------------------------------------------------------------------*/
#ifndef REPL_SDCARD_H
#define REPL_SDCARD_H
#define CMD_REPL_SDCARDINFO {\
"sdcardinfo", REPL_SDCARDINFO, "sdcardinfo", \
#define CMD_REPL_SDINFO {\
"sdinfo", REPL_SDINFO, "sdinfo", \
"Show a summary of the SD card info."}
#define ALLCMD_REPL_SDCARD\
CMD_REPL_SDCARDINFO\
CMD_REPL_SDINFO\
void REPL_SDCARDINFO(REPL * repl);
void REPL_SDINFO(REPL * repl);
#endif
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Read-Eval-Print-Loop functionality
*---------------------------------------------------------------------------*/
#include <Arduino.h>
#include "repl.h"
#include "wifi.h"
void REPL_WIFISCAN(REPL * repl)
{
wifiscan();
}
void REPL_WIFISTATUS(REPL * repl)
{
wifistatus();
}
void REPL_WIFICONNECT(REPL * repl)
{
wificonnect(repl->get_arg(1), repl->get_arg(2), 10);
}
/*-----------------------------------------------------------------------------
* Author: Nelso G. Jost (nelsojost@gmail.com)
* License: GPLv2
* Purpose: Wrap wifi functions into REPL commands.
*---------------------------------------------------------------------------*/
#ifndef REPL_WIFI_H
#define REPL_WIFI_H
#define CMD_REPL_WIFISCAN {\
"wifiscan", REPL_WIFISCAN, "wifiscan", \
"List all availables SSID in range for connection."}
#define CMD_REPL_WIFISTATUS {\
"wifistatus", REPL_WIFISTATUS, "wifistatus", \
"Print WiFi connection status (debug)."}
#define CMD_REPL_WIFICONNECT {\
"wificonnect", REPL_WIFICONNECT, "wificonnect <ssid> <password>" \
"Connect to a wifi network. If successful (threshold 10 sec), " \
"it will autoconnect if board reseted."}
#define ALLCMD_REPL_WIFI \
CMD_REPL_WIFISCAN, \
CMD_REPL_WIFISTATUS, \
CMD_REPL_WIFICONNECT
void REPL_WIFISCAN(REPL * repl);
void REPL_WIFISTATUS(REPL * repl);
void REPL_WIFICONNECT(REPL * repl);
#endif
......@@ -46,6 +46,7 @@ void wifiscan(void)
void wifistatus(void)
{
Serial.println();
WiFi.printDiag(Serial);
}
......
from requests import post
from getpass import getpass
URL = 'http://dados.cta.if.ufrgs.br/emm/api/get/boardhash'
board_id = input('Board ID: ')
user_password = getpass('Password: ')
r = post(URL, json={'board_id': board_id,
'user_password': user_password})
try:
js = r.json()
if 'Error' in js:
raise Exception
filename = input('Filename: ')
with open(filename, 'w') as f:
f.write(js['board_hash'])
except Exception as e:
print(e)
import os
ports = [x for x in os.listdir("/dev") if x.startswith("ttyUSB")
or x.startswith("ttyACM")]
if ports and isinstance(ports[0], str):
print(ports[0])
else:
print("Error: Unable to find a ttyUSB/ttyACM serial port")
raise SystemExit(1)
BOARD := uno
PIODIR := .pio
MAKEFILEDIR := $(shell python -c "import os; print(os.path.dirname(\
'$(shell readlink Makefile)'))")
PIOMAN := python3 $(MAKEFILEDIR)/pioman.py
QTSERMON := python3 $(MAKEFILEDIR)/qtsermon.py
.PHONY: build monitor uno due nano mega qtsermon picocom clean baka
all: build monitor
monitor: qtsermon
build:
$(PIOMAN) build --board $(BOARD) --pio-dir $(PIODIR)
uno:
$(PIOMAN) build --board uno --pio-dir $(PIODIR)
due:
$(PIOMAN) build --board diecimilaatmega328 --pio-dir $(PIODIR)
nano:
$(PIOMAN) build --board nanoatmega328 --pio-dir $(PIODIR)
mega:
$(PIOMAN) build --board megaatmega2560 --pio-dir $(PIODIR)
qtsermon:
$(QTSERMON) --baud $(shell $(PIOMAN) getbaud)
picocom:
picocom --baud $(shell $(GET_BAUD)) $(shell $(GET_PORT))
getbaud:
@ $(PIOMAN) getbaud
clean:
rm -rf $(PIODIR) .travis.yml
define GET_PORT
platformio device list | head -1
endef
PIODIR = .pio
INOPROJECT = .
BAUD = 115200
BOARD = uno
MAKEFILEROOT := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
.PHONY: firmware build upload monitor help clean
all: firmware
install-pip:
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py && rm get-pip.py
install-platformio:
which pip || $(MAKE) install-pip
sudo pip install platformio
sudo usermod -a -G dialout $$USER
wget https://raw.githubusercontent.com/platformio/platformio/develop/scripts/99-platformio-udev.rules
sudo mv -v 99-platformio-udev.rules /etc/udev/rules.d
sudo service udev restart
firmware: build upload monitor
build:
which platformio || $(MAKE) install-platformio
@ echo "$$PIO_BUILD" | sh
upload:
which platformio || $(MAKE) install-platformio
cd $(PIODIR) && platformio run -t upload
monitor:
platformio device monitor --baud $(BAUD)
qtmonitor:
python3 $(MAKEFILEROOT)/../qtsermon.py -b $(BAUD)
help:
@ echo "$$HELP_MAKEFILE"
clean:
rm -rfv $(PIODIR)
define HELP_MAKEFILE
Usage: make <target> where <target> can be:
build Use platformio to build the firmware
upload Use platformio to upload to the board (rebuild if necessary)
monitor Use platformio serial monitor (auto-detect port)
firmware Perform all three steps in a row.
endef
export HELP_MAKEFILE
define PIO_BUILD
mkdir -p $(PIODIR)
rm -rf $(PIODIR)/src
platformio init --board $(BOARD) -d $(PIODIR)
cp -rf $(INOPROJECT)/* $(PIODIR)/src/
cd $(PIODIR) && platformio run
endef
export PIO_BUILD
import PyQt5.Qt as qt
from PyQt5 import Qt
import argparse
import serial
......@@ -6,7 +6,7 @@ import time
import os
class MainWindow(qt.QMainWindow):
class MainWindow(Qt.QMainWindow):
def __init__(self, baud, port=None):
''' Initialize UI.
......@@ -19,28 +19,50 @@ class MainWindow(qt.QMainWindow):
self.init_serial(baud, port)
self.setupUi()
self.timer = qt.QTimer()
self.timer = Qt.QTimer()
self.timer.timeout.connect(self.read_serial)
self.timer.start(10);
def setupUi(self):
self.center_widget = qt.QWidget(parent=self)
self.center_widget = Qt.QWidget(parent=self)
self.edit_input = qt.QLineEdit()
self.edit_input = Qt.QLineEdit()
self.edit_input.setStyleSheet(
"""QLineEdit{{ font-family: 'Ubuntu Mono' }}""")
self.edit_input.returnPressed.connect(self.write_serial)
self.edit_output = qt.QTextEdit()
self.edit_output = Qt.QTextEdit()
self.edit_output.setStyleSheet(
"""QLineEdit{{ font-family: 'Ubuntu Mono' ; line-height: 0.5em }}""")
layout = qt.QVBoxLayout()
layout = Qt.QVBoxLayout()
layout.addWidget(self.edit_input)
layout.addWidget(self.edit_output)
self.center_widget.setLayout(layout)
self.setCentralWidget(self.center_widget)
self.status_bar = qt.QStatusBar()
self.status_bar = Qt.QStatusBar()
self.setStatusBar(self.status_bar)
self.setFixedWidth(500)
self.setGeometry(700, 200, 800, 600)
self.setGeometry(200, 200, 800, 600)
def setLineSpacing(self, line_spacing):
line_count = 0
block = self.edit_output.document().begin()
while block.isValid():
tc = Qt.QTextCursor(block)
fmt = block.blockFormat()
if (fmt.topMargin() != line_spacing or
fmt.bottomMargin() != line_spacing):
fmt.setTopMargin(line_spacing);
# fmt.setBottomMargin(line_spacing);
tc.setBlockFormat(fmt);
block = block.next()
line_count += 1
def init_serial(self, baud, port=None):
''' Attempt to create a PySerial connection.
......@@ -73,19 +95,23 @@ class MainWindow(qt.QMainWindow):
if self.serial.in_waiting:
raw = self.serial.readall()
print(raw)
self.edit_output.append(raw.decode('ASCII')[:-2])
try:
self.edit_output.append(raw.decode('ASCII'))
except:
return
def write_serial(self):
self.serial.write(bytes(self.edit_input.text(), encoding='UTF-8'))
self.serial.write(bytes(self.edit_input.text(),
encoding='UTF-8'))
self.edit_input.clear()
def keyPressEvent(self, event):
if event.key() == qt.Qt.Key_Escape:
if event.key() == Qt.Qt.Key_Escape:
self.close()
@classmethod
def run(cls, baud, port=None):
app = qt.QApplication([])
app = Qt.QApplication([])
window = cls(baud, port)
window.show()
app.exec_()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment