great progress done, switched to serial library because doing this manually sucked

master
Yorick GEOFFRE 2 years ago
parent e78c40da01
commit fdcb0c0ad3

@ -0,0 +1,60 @@
{
"files.associations": {
"string": "cpp",
"string_view": "cpp",
"iostream": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"ratio": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
}
}

@ -1,5 +1,11 @@
#include <Adafruit_MLX90640.h>
#include <I2CManagement.hpp>
#include <sstream>
#include <math.h>
#include <memory>
#include <string>
#include <stdexcept>
#include <esp_task_wdt.h>
Adafruit_MLX90640 mlx;
float frame[32*24]; // buffer for full frame of temperatures
@ -8,88 +14,52 @@ float frame[32*24]; // buffer for full frame of temperatures
#define PRINT_TEMPERATURES
//#define PRINT_ASCIIART
template<typename ... Args>
std::string string_format( const std::string& format, Args ... args )
{
int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
if( size_s <= 0 ){ throw std::runtime_error( "Error during formatting." ); }
auto size = static_cast<size_t>( size_s );
std::unique_ptr<char[]> buf( new char[ size ] );
std::snprintf( buf.get(), size, format.c_str(), args ... );
return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}
void setup(){
esp_task_wdt_delete(NULL);
Serial.begin(460800);
Serial.setTimeout(0);
//Serial.println("Adafruit MLX90640 Simple Test");
I2CManager::Setup(6, 7);
I2CManager::scanAll();
Serial.println("Adafruit MLX90640 Simple Test");
//I2CManager::scanAll();
if (! mlx.begin(MLX90640_I2CADDR_DEFAULT, &Wire)) {
Serial.println("MLX90640 not found!");
while (1) delay(10);
}
Serial.println("Found Adafruit MLX90640");
Serial.print("Serial number: ");
Serial.print(mlx.serialNumber[0], HEX);
Serial.print(mlx.serialNumber[1], HEX);
Serial.println(mlx.serialNumber[2], HEX);
//mlx.setMode(MLX90640_INTERLEAVED);
mlx.setMode(MLX90640_CHESS);
Serial.print("Current mode: ");
if (mlx.getMode() == MLX90640_CHESS) {
Serial.println("Chess");
} else {
Serial.println("Interleave");
}
mlx.setResolution(MLX90640_ADC_16BIT);
Serial.print("Current resolution: ");
mlx90640_resolution_t res = mlx.getResolution();
switch (res) {
case MLX90640_ADC_16BIT: Serial.println("16 bit"); break;
case MLX90640_ADC_17BIT: Serial.println("17 bit"); break;
case MLX90640_ADC_18BIT: Serial.println("18 bit"); break;
case MLX90640_ADC_19BIT: Serial.println("19 bit"); break;
}
mlx.setRefreshRate(MLX90640_8_HZ);
Serial.print("Current frame rate: ");
mlx90640_refreshrate_t rate = mlx.getRefreshRate();
switch (rate) {
case MLX90640_0_5_HZ: Serial.println("0.5 Hz"); break;
case MLX90640_1_HZ: Serial.println("1 Hz"); break;
case MLX90640_2_HZ: Serial.println("2 Hz"); break;
case MLX90640_4_HZ: Serial.println("4 Hz"); break;
case MLX90640_8_HZ: Serial.println("8 Hz"); break;
case MLX90640_16_HZ: Serial.println("16 Hz"); break;
case MLX90640_32_HZ: Serial.println("32 Hz"); break;
case MLX90640_64_HZ: Serial.println("64 Hz"); break;
}
Wire.setClock(2000000);
}
void loop(){
char command = 0;
while (command != '@') { // Wait for the PC to send a '1' command
while(Serial.available() == 0){delay(1);}
command = Serial.read();
}
Serial.flush();
if (mlx.getFrame(frame) != 0) {
Serial.println("Failed");
return;
}
Serial.println('?');
std::stringstream k;
for (uint8_t h=0; h<24; h++) {
for (uint8_t w=0; w<32; w++) {
float t = frame[h*32 + w];
#ifdef PRINT_TEMPERATURES
Serial.print(t, 1);
Serial.print("|");
#endif
#ifdef PRINT_ASCIIART
char c = '&';
if (t < 20) c = ' ';
else if (t < 23) c = '.';
else if (t < 25) c = '-';
else if (t < 27) c = '*';
else if (t < 29) c = '+';
else if (t < 31) c = 'x';
else if (t < 33) c = '%';
else if (t < 35) c = '#';
else if (t < 37) c = 'X';
Serial.print(c);
#endif
k << std::to_string(t) << "|";
}
Serial.println();
}
Serial.println('!');
k << "@";
Serial.print(k.str().c_str());
}

Binary file not shown.

@ -2,12 +2,6 @@
#include <stdio.h>
#include <string.h>
// Linux headers
#include <fcntl.h> // Contains file controls like O_RDWR
#include <errno.h> // Error integer and strerror() function
#include <termios.h> // Contains POSIX terminal control definitions
#include <unistd.h> // write(), read(), close()
#include <thread>
#include <vector>
#include <iostream>
@ -15,106 +9,33 @@
#include <iterator>
#include <algorithm>
#include <CppLinuxSerial/SerialPort.hpp>
int main(){
struct termios tty;
std::cout << "opening serial port /dev/ttyUSB0" << std::endl;
int serialPort = open("/dev/ttyACM0", O_RDONLY); // open serial port
std::cout << "done" << std::endl;
if (serialPort < 0)
{
printf("Error %i from open: %s\n", errno, strerror(errno));
return -1;
}
if (tcgetattr(serialPort, &tty) != 0)
{ // read serial port configuration
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
return -1;
}
mn::CppLinuxSerial::SerialPort serialPort("/dev/ttyACM0",
mn::CppLinuxSerial::BaudRate::B_460800,
mn::CppLinuxSerial::NumDataBits::EIGHT,
mn::CppLinuxSerial::Parity::NONE,
mn::CppLinuxSerial::NumStopBits::ONE);
serialPort.SetTimeout(100); // Block for up to 100ms to receive data
serialPort.Open();
// local flags
tty.c_lflag &= ~ICANON; // enable canonical mode
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
// input modes
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // Disable any special handling of received bytes
// output modes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
// c_cc
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;
// baudrate
cfsetispeed(&tty, B460800); // set baudrate (input)
cfsetospeed(&tty, B460800); // (output)
// Save tty settings, also checking for error
if (tcsetattr(serialPort, TCSANOW, &tty) != 0)
{
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
return -1;
}
char read_buf[1024];
float out_buf[24][32];
std::string outs = std::string("");
int n = 0;
int i = 0;
int j = 0;
bool goodData = false;
system("clear");
std::string s;
while(true){
n = read(serialPort, &read_buf, sizeof(read_buf));
if (n > 0)
{
//std::cout << read_buf;
std::cout << n << std::endl;
j = i = 0;
outs.clear();
for(char c : read_buf){
if(goodData){
if(c >= '0' && c <= '9' || c == '.'){
outs.append({c});
}else if(c == '|'){
std::cout << outs;
try{
out_buf[i][j] = std::stof(outs);
outs.clear();
j++;
} catch(std::invalid_argument e){
std::cerr << "stof error" << std::endl;
}
}else if(c == '\n'){
i++;
}else if(c == '!'){
return 0;
break;
}
}else{
if(c == '?'){
i = -1;
goodData = true;
}
}
}
i = 0;
j = 0;
goodData = false;
for (uint8_t h=0; h<24; h++) {
for (uint8_t w=0; w<32; w++) {
//printf("%.2f|",out_buf[h][w]);
}
//std::cout << std::endl;
}
usleep(100000);
system("clear");
serialPort.Write("@");
while(s.find('@') == std::string::npos){
std::cout << s.find('@') << std::endl;
std::string readData;
serialPort.Read(readData);
s += readData;
std::cout << readData << std::endl;
}
std::cout << "found \\n at pos: " << s.find('\n') << std::endl;
std::cout << s << std::endl;
s = "";
}
return 0;
}
Loading…
Cancel
Save