Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving the binding to the Serial object and fix SoftwareSerial bug #20

Merged
merged 5 commits into from
Mar 11, 2021
Merged

Conversation

heX16
Copy link
Contributor

@heX16 heX16 commented Mar 3, 2021

Consider the following situations:

  1. We use another MCU (not known to this library) where the set of parameters for Serial.init(...) is different (this situation already happened with ESP32).
  2. We use our own implementation of Serial inherited from Stream (such as AltSoftSerial).
  3. Pin numbers in Serial can change on the fly (cofigure via web interface).
  4. We have a dust sensor and another sensor (of another type) connected to the same SoftwareSerial, but responding to a different query format. This configuration is valid, and saving pins - is a common phenomenon. (similar to the situation in Need option to disable sending command to sensor (active_mode option) #5)
  5. We have 16 sensors that need to be polled. For example these are compressed air lines in industry.

In the current implementation of the library it is problematic to solve these problems (without modifying the library code).
So I decided to expand PR and add more general mechanisms.

In normal projects, the problem of creating objects is solved through the "factory" pattern, but for MK this is superfluous.

It will take some more time to develop (more examples need to be written) and hopefully everyone will like the result.


Russian:
Рассмотрим следующие ситуации:

  1. Мы используем другой МК (не известный этой библиотеке), в котором набор параметров для Serial.init(...) отличается (такая ситуация уже произошла с ESP32).
  2. Мы используем свою собственную реализацию Serial унаследованную от Stream (например AltSoftSerial)..
  3. Номера пин у SoftwareSerial изменяются на лету (кофигурация через web-интерфейс).
  4. У нас датчик пыли и еще один датчик (другого типа) подключенный к этому же SoftwareSerial, но отвечающий на другой формат запроса. Такая конфигурация валидна, и экономия пинов это обычное явление. (похоже на ситуацию в Need option to disable sending command to sensor (active_mode option) #5)
  5. У нас 16 датчиков которые нужно опрашивать. Например это линии сжатого воздуха в промышленности.

В текущей реализации библиотеки решить эти проблемы (без внесения изменений в код библиотеки) проблематично.
Поэтому я решил расширить PR и добавить более общие механизмы.

В обычных проектах проблему создания объектов решают через патерн "фабрика", но для МК это излишне.

Разработка займет еще некоторое время (нужно написать дополнительные примеры) и надеюсь результат понравится всем.

Changes

Core:

SoftwareSerial bug - #19
Improving the binding to the Serial object

ADD: "lib_dir = ../../../" - this is a dirty hack to make projects use the local library to which they belong (PlatformIO did not have the right option)
ADD: special "ifdef" for ESP32 (It is possible to create a universal code for this MC. Maybe I'll do it later.)
DEL: "lib_deps = EspSoftwareSerial@>=6.7.1" - this lib integrated to Arduino-ESP8266-framework. See: plerup/espsoftwareserial#200
@heX16
Copy link
Contributor Author

heX16 commented Mar 3, 2021

image

@heX16
Copy link
Contributor Author

heX16 commented Mar 3, 2021

Working: https://www.instagram.com/p/CL91-rrHANP/

(i use two SoftwareSerial - for CO2 sensor and PM5003 sensor.

@avaldebe
Copy link
Owner

avaldebe commented Mar 4, 2021

Looks like the platformio travis integration now uses Python 3.7, it was 2.7 when I set it up.

In any case, I updated .travis.yml (a0e326e) which fix most of the builds.
The examples/OLED_64x48 is still crashing, looks like the ESP8266_SSD1306 library is no longer available.

@avaldebe
Copy link
Owner

avaldebe commented Mar 4, 2021

The examples/OLED_64x48 is still crashing, looks like the ESP8266_SSD1306 library is no longer available.

The ESP8266_SSD1306 library changed name to ESP8266 and ESP32 OLED driver for SSD1306 displays

Copy link
Owner

@avaldebe avaldebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add a new constructor SerialPM(PMS sensor, SoftwareSerial &serial) if you think it is necessary,
but please keep the SerialPM(PMS sensor, uint8_t rx, uint8_t tx) constructor.

src/PMserial.h Show resolved Hide resolved
examples/SoftwareSerial/SoftwareSerial.ino Show resolved Hide resolved
@avaldebe
Copy link
Owner

avaldebe commented Mar 4, 2021

The ESP8266_SSD1306 library changed name to ESP8266 and ESP32 OLED driver for SSD1306 displays

Fixed on 5e9de28. Please update your branch, so the builds succeed

@heX16
Copy link
Contributor Author

heX16 commented Mar 10, 2021

Work in progress... (see topic)

…x)` (see: #20 (review))

ADD: manual mode - constructor `SerialPM(PMS sensor)` (work in progress!)
UPD: `hwSerial` - change to enum
ADD: getter/setter for `uart` variable. (setSerialPort, getSerialPort)
UPD: `SerialPM::init()` - updating for manual mode
FIX: `SerialPM::trigRead()` - add `#ifdef` for `listen`
@heX16 heX16 changed the title FIX: SoftwareSerial bug SoftwareSerial bug, and improving the binding to the Serial object Mar 10, 2021
@heX16 heX16 changed the title SoftwareSerial bug, and improving the binding to the Serial object Improving the binding to the Serial object and fix SoftwareSerial bug Mar 10, 2021
Copy link
Owner

@avaldebe avaldebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a minor gripe, I can add back these comments at a later stage


[env]
framework = arduino

[env:uno]
; ATmega328, 5V/16MHz
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave the comments like this one, I find them useful to keep track of what each env support

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately PlatformIO erases comments when you change a file from GUI. I hope they ever fix this bug.

Copy link
Contributor Author

@heX16 heX16 Mar 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add back these comments at a later stage

You can use custom fields in environment sections. Just add custom_ prefix.

platformio/platformio-core#3862 (comment)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll do that whenever I came around to add the comments back

platform = atmelavr
board = uno

[env:mini168_3V3]
; ATmega168, 3.3V/8MHz
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

platform = atmelavr
board = pro8MHzatmega168

[env:mini328_3V3]
; ATmega328, 3.3V/8MHz
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...

platform = atmelavr
board = pro8MHzatmega328

[env:esp01]
; ESP8266, 512kB flash
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...

board = esp01
build_flags = -D PMS_RX=2 -D PMS_TX=0

[env:d1_mini]
; ESP8266, 4096kB flash
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...

@avaldebe avaldebe merged commit 4f1c858 into avaldebe:master Mar 11, 2021
@avaldebe
Copy link
Owner

Thanks for this PR. I learned something the Atmel AVR C++ compiler and the code got better.

Thanks again,
Á.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants