mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-06 02:35:23 +03:00
OK; next stage: a pair of NTC, BME280, sensors' power management
This commit is contained in:
parent
b082f4ea46
commit
83b9fd552b
Binary file not shown.
@ -20,8 +20,6 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "i2c.h"
|
||||
#include "strfunc.h" // hexdump
|
||||
#include "usb_dev.h"
|
||||
|
||||
i2c_speed_t i2c_curspeed = I2C_SPEED_AMOUNT;
|
||||
extern volatile uint32_t Tms;
|
||||
@ -45,7 +43,8 @@ static uint8_t dmaaddr = 0; // address to continuous read by DMA
|
||||
static inline int isI2Cbusy(){
|
||||
cntr = Tms;
|
||||
do{
|
||||
if(Tms - cntr > I2C_TIMEOUT){ USND("Timeout, DMA transfer in progress?"); return 1;}
|
||||
IWDG->KR = IWDG_REFRESH;
|
||||
if(Tms - cntr > I2C_TIMEOUT) return 1;
|
||||
}while(I2Cbusy);
|
||||
return 0;
|
||||
}
|
||||
@ -90,7 +89,6 @@ void i2c_setup(i2c_speed_t speed){
|
||||
SCLL = 0x2;
|
||||
break;
|
||||
default:
|
||||
USND("Wrong I2C speed!");
|
||||
return; // wrong speed
|
||||
}
|
||||
RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
|
||||
@ -318,21 +316,8 @@ int i2c_scan_next_addr(uint8_t *addr){
|
||||
return 1;
|
||||
}
|
||||
|
||||
// dump I2Cbuf
|
||||
void i2c_bufdudump(){
|
||||
if(goterr){
|
||||
USND("DMARDERR");
|
||||
goterr = 0;
|
||||
}
|
||||
if(i2cbuflen < 1) return;
|
||||
USND("DMARD=");
|
||||
hexdump16(USB_sendstr, (uint16_t*)I2Cbuf, i2cbuflen);
|
||||
}
|
||||
|
||||
// get DMA buffer with conversion to little-endian (if transfer was for 16-bit)
|
||||
uint16_t *i2c_dma_getbuf(uint16_t *len){
|
||||
//if(i2c_got_DMA) USND("DMA GOT!");
|
||||
//U("T="); U(u2str(Tms)); U("; cndtr: "); USND(u2str(DMA1_Channel7->CNDTR));
|
||||
if(!i2c_got_DMA || i2cbuflen < 1) return NULL;
|
||||
i2c_got_DMA = 0;
|
||||
i2cbuflen >>= 1; // for hexdump16 - now buffer have uint16_t!
|
||||
|
||||
@ -48,7 +48,6 @@ uint16_t *i2c_read_reg16(uint8_t addr, uint16_t reg16, uint16_t nwords, uint8_t
|
||||
uint8_t i2c_write(uint8_t addr, uint16_t *data, uint8_t nwords);
|
||||
uint8_t i2c_write_dma16(uint8_t addr, uint16_t *data, uint8_t nwords);
|
||||
|
||||
void i2c_bufdudump();
|
||||
int i2c_dma_haderr();
|
||||
uint16_t *i2c_dma_getbuf(uint16_t *len);
|
||||
int i2c_getwords(uint16_t *buf, int bufsz);
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE QtCreatorProject>
|
||||
<!-- Written by QtCreator 17.0.1, 2025-09-28T20:36:21. -->
|
||||
<!-- Written by QtCreator 17.0.1, 2025-09-29T22:10:22. -->
|
||||
<qtcreator>
|
||||
<data>
|
||||
<variable>EnvironmentId</variable>
|
||||
|
||||
@ -32,6 +32,8 @@ void sys_tick_handler(void){
|
||||
++Tms;
|
||||
}
|
||||
|
||||
const char *scanend = "SCANEND\n", *foundid = "FOUNDID=";
|
||||
|
||||
int main(void){
|
||||
char inbuff[MAXSTRLEN+1];
|
||||
if(StartHSE()){
|
||||
@ -58,33 +60,40 @@ int main(void){
|
||||
int l = USB_receivestr(inbuff, MAXSTRLEN);
|
||||
if(l < 0) USB_sendstr("USBOVERFLOW\n");
|
||||
else if(l){
|
||||
const char *ans = parse_cmd(inbuff);
|
||||
const char *ans = parse_cmd(inbuff, SEND_USB);
|
||||
if(ans) USB_sendstr(ans);
|
||||
}
|
||||
if(i2c_scanmode){
|
||||
if(i2c_scanmode){ // send this to both
|
||||
uint8_t addr;
|
||||
int ok = i2c_scan_next_addr(&addr);
|
||||
if(addr == I2C_ADDREND) USND("SCANEND");
|
||||
else if(ok){
|
||||
U("FOUNDID=");
|
||||
USND(uhex2str(addr));
|
||||
if(addr == I2C_ADDREND){
|
||||
USB_sendstr(scanend); USB_putbyte('\n');
|
||||
usart_sendstr(scanend); usart_putbyte('\n');
|
||||
}else if(ok){
|
||||
const char *straddr = uhex2str(addr);
|
||||
USB_sendstr(foundid); USB_sendstr(straddr); USB_putbyte('\n');
|
||||
usart_sendstr(foundid); usart_sendstr(straddr); usart_putbyte('\n');
|
||||
}
|
||||
}
|
||||
mlx_process();
|
||||
if(cartoon) for(int i = 0; i < N_SESORS; ++i){
|
||||
if(cartoon) for(int i = 0; i < N_SESORS; ++i){ // USB-only
|
||||
uint32_t Tnow = mlx_lastimT(i);
|
||||
if(Tnow != Tlastima[i]){
|
||||
fp_t *im = mlx_getimage(i);
|
||||
if(im){
|
||||
U(Sensno); USND(i2str(i));
|
||||
U(Timage); USND(u2str(Tnow)); drawIma(im);
|
||||
chsendfun(SEND_USB);
|
||||
U(Sensno); UN(i2str(i));
|
||||
U(Timage); UN(u2str(Tnow)); drawIma(im);
|
||||
Tlastima[i] = Tnow;
|
||||
}
|
||||
}
|
||||
}
|
||||
usart_process();
|
||||
if(usart_ovr()) USND("USART_OVERFLOW\n");
|
||||
if(usart_ovr()) usart_sendstr("USART_OVERFLOW\n");
|
||||
char *got = usart_getline(NULL);
|
||||
if(got){ U("USART='"); U(got); USND("'"); }
|
||||
if(got){
|
||||
const char *ans = parse_cmd(got, SEND_USART);
|
||||
if(ans) usart_sendstr(ans);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,52 +34,6 @@ static fp_t mlx_image[MLX_PIXNO] = {0}; // ready image
|
||||
// 10100 bytes:
|
||||
static MLX90640_params params; // calculated parameters (in heap, not stack!) for other functions
|
||||
|
||||
void dumpIma(const fp_t im[MLX_PIXNO]){
|
||||
for(int row = 0; row < MLX_H; ++row){
|
||||
for(int col = 0; col < MLX_W; ++col){
|
||||
printfl(*im++, 1);
|
||||
USB_putbyte(' ');
|
||||
}
|
||||
newline();
|
||||
}
|
||||
}
|
||||
|
||||
#define GRAY_LEVELS (16)
|
||||
// 16-level character set ordered by fill percentage (provided by user)
|
||||
static const char* CHARS_16 = " .':;+*oxX#&%B$@";
|
||||
void drawIma(const fp_t im[MLX_PIXNO]){
|
||||
// Find min and max values
|
||||
fp_t min_val = im[0], max_val = im[0];
|
||||
const fp_t *iptr = im;
|
||||
for(int row = 0; row < MLX_H; ++row){
|
||||
for(int col = 0; col < MLX_W; ++col){
|
||||
fp_t cur = *iptr++;
|
||||
if(cur < min_val) min_val = cur;
|
||||
else if(cur > max_val) max_val = cur;
|
||||
}
|
||||
}
|
||||
fp_t range = max_val - min_val;
|
||||
U("RANGE="); USND(float2str(range, 3));
|
||||
U("MIN="); USND(float2str(min_val, 3));
|
||||
U("MAX="); USND(float2str(max_val, 3));
|
||||
if(fabsf(range) < 0.001) range = 1.; // solid fill -> blank
|
||||
// Generate and print ASCII art
|
||||
iptr = im;
|
||||
for(int row = 0; row < MLX_H; ++row){
|
||||
for(int col = 0; col < MLX_W; ++col){
|
||||
fp_t normalized = ((*iptr++) - min_val) / range;
|
||||
// Map to character index (0 to 15)
|
||||
int index = (int)(normalized * GRAY_LEVELS);
|
||||
// Ensure we stay within bounds
|
||||
if(index < 0) index = 0;
|
||||
else if(index > (GRAY_LEVELS-1)) index = (GRAY_LEVELS-1);
|
||||
USB_putbyte(CHARS_16[index]);
|
||||
}
|
||||
newline();
|
||||
}
|
||||
newline();
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
Calculate parameters & values
|
||||
*****************************************************************************/
|
||||
@ -109,7 +63,7 @@ MLX90640_params *get_parameters(const uint16_t dataarray[MLX_DMA_MAXLEN]){
|
||||
uint16_t val = CREG_VAL(REG_VDD);
|
||||
i8 = (int8_t) (val >> 8);
|
||||
params.kVdd = i8 * 32; // keep sign
|
||||
if(params.kVdd == 0){USND("kvdd=0"); return NULL;}
|
||||
if(params.kVdd == 0){UN("kvdd=0"); return NULL;}
|
||||
i16 = val & 0xFF;
|
||||
params.vdd25 = ((i16 - 0x100) * 32) - (1<<13);
|
||||
val = CREG_VAL(REG_KVTPTAT);
|
||||
@ -123,7 +77,7 @@ MLX90640_params *get_parameters(const uint16_t dataarray[MLX_DMA_MAXLEN]){
|
||||
val = CREG_VAL(REG_APTATOCCS) >> 12;
|
||||
params.alphaPTAT = val / 4. + 8.;
|
||||
params.gainEE = (int16_t)CREG_VAL(REG_GAIN);
|
||||
if(params.gainEE == 0){USND("gainee=0"); return NULL;}
|
||||
if(params.gainEE == 0){UN("gainee=0"); return NULL;}
|
||||
int8_t occRow[MLX_H];
|
||||
int8_t occColumn[MLX_W];
|
||||
occacc(occRow, MLX_H, &CREG_VAL(REG_OCCROW14));
|
||||
@ -151,7 +105,7 @@ MLX90640_params *get_parameters(const uint16_t dataarray[MLX_DMA_MAXLEN]){
|
||||
// so index of ktaavg is 2*(row&1)+(col&1)
|
||||
val = CREG_VAL(REG_KTAVSCALE);
|
||||
uint8_t scale1 = ((val & 0xFF)>>4) + 8, scale2 = (val&0xF);
|
||||
if(scale1 == 0 || scale2 == 0){USND("scale1/2=0"); return NULL;}
|
||||
if(scale1 == 0 || scale2 == 0){UN("scale1/2=0"); return NULL;}
|
||||
fp_t mul = (fp_t)(1<<scale2), div = (fp_t)(1<<scale1); // kta_scales
|
||||
uint16_t a_r = CREG_VAL(REG_SENSIVITY); // alpha_ref
|
||||
val = CREG_VAL(REG_SCALEACC);
|
||||
@ -271,10 +225,10 @@ fp_t *process_image(const int16_t subpage1[REG_IMAGEDATA_LEN]){
|
||||
fp_t dvdd, dTa, Kgain, pixOS[2]; // values for both subpages
|
||||
// 11.2.2.2. Supply voltage value calculation
|
||||
i16a = (int16_t)IMD_VAL(REG_IVDDPIX);
|
||||
//U("rval="); USND(i2str(i16a));
|
||||
//U("rval="); UN(i2str(i16a));
|
||||
dvdd = resol_corr*i16a - params.vdd25;
|
||||
dvdd /= params.kVdd;
|
||||
//U("dvdd="); USND(float2str(dvdd, 2));
|
||||
//U("dvdd="); UN(float2str(dvdd, 2));
|
||||
fp_t dV = i16a - params.vdd25; // for next step
|
||||
dV /= params.kVdd;
|
||||
// 11.2.2.3. Ambient temperature calculation
|
||||
|
||||
@ -60,5 +60,3 @@ typedef struct{
|
||||
int ch_resolution(uint8_t newresol);
|
||||
MLX90640_params *get_parameters(const uint16_t dataarray[MLX_DMA_MAXLEN]);
|
||||
fp_t *process_image(const int16_t subpage1[REG_IMAGEDATA_LEN]);
|
||||
void dumpIma(const fp_t im[MLX_PIXNO]);
|
||||
void drawIma(const fp_t im[MLX_PIXNO]);
|
||||
|
||||
@ -28,7 +28,7 @@
|
||||
#include "usb_dev.h"
|
||||
#include "strfunc.h"
|
||||
#define D(x) U(x)
|
||||
#define DN(x) USND(x)
|
||||
#define DN(x) UN(x)
|
||||
#define DB(x) USB_putbute(x)
|
||||
#else
|
||||
#define D(x)
|
||||
|
||||
@ -16,11 +16,13 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <stm32f3.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "i2c.h"
|
||||
#include "mlxproc.h"
|
||||
#include "proto.h"
|
||||
#include "strfunc.h"
|
||||
#include "usart.h"
|
||||
#include "usb_dev.h"
|
||||
@ -33,6 +35,34 @@ static uint8_t I2Caddress = 0x33 << 1;
|
||||
extern volatile uint32_t Tms;
|
||||
uint8_t cartoon = 0; // "cartoon" mode: refresh image each time we get new
|
||||
|
||||
// functions to send data over USB or USART: to change them use flag in `parse_cmd`
|
||||
typedef struct{
|
||||
int (*S)(const char*); // send string
|
||||
int (*P)(uint8_t); // put byte
|
||||
int (*B)(const uint8_t*, int); // send raw bytes
|
||||
} sendfun_t;
|
||||
|
||||
static sendfun_t usbsend = {
|
||||
.S = USB_sendstr, .P = USB_putbyte, .B = USB_send
|
||||
};
|
||||
static sendfun_t usartsend = {
|
||||
.S = usart_sendstr, .P = usart_putbyte, .B = usart_send
|
||||
};
|
||||
|
||||
static sendfun_t *sendfun = &usbsend;
|
||||
|
||||
void chsendfun(int sendto){
|
||||
if(sendto == SEND_USB) sendfun = &usbsend;
|
||||
else sendfun = &usartsend;
|
||||
}
|
||||
|
||||
// newline
|
||||
#define N() sendfun->P('\n')
|
||||
#define printu(x) do{sendfun->S(u2str(x));}while(0)
|
||||
#define printi(x) do{sendfun->S(i2str(x));}while(0)
|
||||
#define printuhex(x) do{sendfun->S(uhex2str(x));}while(0)
|
||||
#define printfl(x,n) do{sendfun->S(float2str(x, n));}while(0)
|
||||
|
||||
// common names for frequent keys
|
||||
const char *Timage = "TIMAGE=";
|
||||
const char *Sensno = "SENSNO=";
|
||||
@ -47,11 +77,11 @@ const char *helpstring =
|
||||
"gn - get nth image 'as is' - float array of 768x4 bytes\n"
|
||||
"i0..4 - setup I2C with speed 10k, 100k, 400k, 1M or 2M (experimental!)\n"
|
||||
"l - list active sensors IDs\n"
|
||||
"tn - show temperature map of nth image\n"
|
||||
"mn - show temperature map of nth image\n"
|
||||
"p - pause MLX\n"
|
||||
"s - stop MLX (and start from zero @ 'c'\n"
|
||||
"s - stop MLX (and start from zero @ 'c')\n"
|
||||
"tn - show nth image aquisition time\n"
|
||||
"C - \"cartoon\" mode on/off (show each new image)\n"
|
||||
"C - \"cartoon\" mode on/off (show each new image) - USB only!!!\n"
|
||||
"Dn - dump MLX parameters for sensor number n\n"
|
||||
"G - get MLX state\n"
|
||||
"Ia addr [n] - set device address for interactive work or (with n) change address of n'th sensor\n"
|
||||
@ -59,7 +89,7 @@ const char *helpstring =
|
||||
"Iw words - send words (hex/dec/oct/bin) to I2C\n"
|
||||
"Is - scan I2C bus\n"
|
||||
"T - print current Tms\n"
|
||||
"Us - send string 's' to USART\n"
|
||||
"Us - send string 's' to other interface\n"
|
||||
;
|
||||
|
||||
TRUE_INLINE const char *setupI2C(char *buf){
|
||||
@ -78,7 +108,7 @@ TRUE_INLINE const char *setupI2C(char *buf){
|
||||
}
|
||||
i2c_setup((i2c_speed_t)speed);
|
||||
}
|
||||
U("I2CSPEED="); USND(speeds[i2c_curspeed]);
|
||||
sendfun->S("I2CSPEED="); sendfun->S(speeds[i2c_curspeed]); N();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -89,11 +119,11 @@ TRUE_INLINE const char *chhwaddr(const char *buf){
|
||||
if(nxt && nxt != buf){
|
||||
if(!mlx_sethwaddr(I2Caddress, a)) return ERR;
|
||||
}else{
|
||||
USND("Wrong number");
|
||||
sendfun->S("Wrong number"); N();
|
||||
return ERR;
|
||||
}
|
||||
}else{
|
||||
USND("Need address");
|
||||
sendfun->S("Need address"); N();
|
||||
return ERR;
|
||||
}
|
||||
return OK;
|
||||
@ -117,7 +147,7 @@ TRUE_INLINE const char *chaddr(const char *buf){
|
||||
int n = getsensnum(nxt);
|
||||
if(n > -1) mlx_setaddr(n, addr);
|
||||
}else addr = I2Caddress >> 1;
|
||||
U("I2CADDR="); USND(uhex2str(addr));
|
||||
sendfun->S("I2CADDR="); sendfun->S(uhex2str(addr)); N();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -135,8 +165,8 @@ static const char *rdI2C(const char *buf){
|
||||
char b[5];
|
||||
u16s(*b16, b);
|
||||
b[4] = 0;
|
||||
USND(b);
|
||||
}else hexdump16(USB_sendstr, b16, N);
|
||||
sendfun->S(b); N();
|
||||
}else hexdump16(sendfun->S, b16, N);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -156,7 +186,8 @@ static const char *wrI2C(const char *buf){
|
||||
uint16_t N = readNnumbers(buf);
|
||||
if(N == 0) return ERR;
|
||||
for(int i = 0; i < N; ++i){
|
||||
U("byte "); U(u2str(i)); U(" :"); USND(uhex2str(locBuffer[i]));
|
||||
sendfun->S("byte "); sendfun->S(u2str(i));
|
||||
sendfun->S(" :"); sendfun->S(uhex2str(locBuffer[i])); N();
|
||||
}
|
||||
if(!i2c_write(I2Caddress, locBuffer, N)) return ERR;
|
||||
return OK;
|
||||
@ -165,58 +196,58 @@ static const char *wrI2C(const char *buf){
|
||||
static void dumpfarr(float *arr){
|
||||
for(int row = 0; row < 24; ++row){
|
||||
for(int col = 0; col < 32; ++col){
|
||||
printfl(*arr++, 2); USB_putbyte(' ');
|
||||
printfl(*arr++, 2); sendfun->P(' ');
|
||||
}
|
||||
newline();
|
||||
N();
|
||||
}
|
||||
}
|
||||
// dump MLX parameters
|
||||
TRUE_INLINE void dumpparams(const char *buf){
|
||||
int N = getsensnum(buf);
|
||||
if(N < 0){ U(ERR); return; }
|
||||
if(N < 0){ sendfun->S(ERR); return; }
|
||||
MLX90640_params *params = mlx_getparams(N);
|
||||
if(!params){ U(ERR); return; }
|
||||
U(Sensno); USND(i2str(N));
|
||||
U("\nkVdd="); printi(params->kVdd);
|
||||
U("\nvdd25="); printi(params->vdd25);
|
||||
U("\nKvPTAT="); printfl(params->KvPTAT, 4);
|
||||
U("\nKtPTAT="); printfl(params->KtPTAT, 4);
|
||||
U("\nvPTAT25="); printi(params->vPTAT25);
|
||||
U("\nalphaPTAT="); printfl(params->alphaPTAT, 2);
|
||||
U("\ngainEE="); printi(params->gainEE);
|
||||
U("\nPixel offset parameters:\n");
|
||||
if(!params){ sendfun->S(ERR); return; }
|
||||
sendfun->S(Sensno); sendfun->S(i2str(N)); N();
|
||||
sendfun->S("\nkVdd="); printi(params->kVdd);
|
||||
sendfun->S("\nvdd25="); printi(params->vdd25);
|
||||
sendfun->S("\nKvPTAT="); printfl(params->KvPTAT, 4);
|
||||
sendfun->S("\nKtPTAT="); printfl(params->KtPTAT, 4);
|
||||
sendfun->S("\nvPTAT25="); printi(params->vPTAT25);
|
||||
sendfun->S("\nalphaPTAT="); printfl(params->alphaPTAT, 2);
|
||||
sendfun->S("\ngainEE="); printi(params->gainEE);
|
||||
sendfun->S("\nPixel offset parameters:\n");
|
||||
float *offset = params->offset;
|
||||
for(int row = 0; row < 24; ++row){
|
||||
for(int col = 0; col < 32; ++col){
|
||||
printfl(*offset++, 2); USB_putbyte(' ');
|
||||
printfl(*offset++, 2); sendfun->P(' ');
|
||||
}
|
||||
newline();
|
||||
N();
|
||||
}
|
||||
U("K_talpha:\n");
|
||||
sendfun->S("K_talpha:\n");
|
||||
dumpfarr(params->kta);
|
||||
U("Kv: ");
|
||||
sendfun->S("Kv: ");
|
||||
for(int i = 0; i < 4; ++i){
|
||||
printfl(params->kv[i], 2); USB_putbyte(' ');
|
||||
printfl(params->kv[i], 2); sendfun->P(' ');
|
||||
}
|
||||
U("\ncpOffset=");
|
||||
printi(params->cpOffset[0]); U(", "); printi(params->cpOffset[1]);
|
||||
U("\ncpKta="); printfl(params->cpKta, 2);
|
||||
U("\ncpKv="); printfl(params->cpKv, 2);
|
||||
U("\ntgc="); printfl(params->tgc, 2);
|
||||
U("\ncpALpha="); printfl(params->cpAlpha[0], 2);
|
||||
U(", "); printfl(params->cpAlpha[1], 2);
|
||||
U("\nKsTa="); printfl(params->KsTa, 2);
|
||||
U("\nAlpha:\n");
|
||||
sendfun->S("\ncpOffset=");
|
||||
printi(params->cpOffset[0]); sendfun->S(", "); printi(params->cpOffset[1]);
|
||||
sendfun->S("\ncpKta="); printfl(params->cpKta, 2);
|
||||
sendfun->S("\ncpKv="); printfl(params->cpKv, 2);
|
||||
sendfun->S("\ntgc="); printfl(params->tgc, 2);
|
||||
sendfun->S("\ncpALpha="); printfl(params->cpAlpha[0], 2);
|
||||
sendfun->S(", "); printfl(params->cpAlpha[1], 2);
|
||||
sendfun->S("\nKsTa="); printfl(params->KsTa, 2);
|
||||
sendfun->S("\nAlpha:\n");
|
||||
dumpfarr(params->alpha);
|
||||
U("\nCT3="); printfl(params->CT[1], 2);
|
||||
U("\nCT4="); printfl(params->CT[2], 2);
|
||||
sendfun->S("\nCT3="); printfl(params->CT[1], 2);
|
||||
sendfun->S("\nCT4="); printfl(params->CT[2], 2);
|
||||
for(int i = 0; i < 4; ++i){
|
||||
U("\nKsTo"); USB_putbyte('0'+i); USB_putbyte('=');
|
||||
sendfun->S("\nKsTo"); sendfun->P('0'+i); sendfun->P('=');
|
||||
printfl(params->KsTo[i], 2);
|
||||
U("\nalphacorr"); USB_putbyte('0'+i); USB_putbyte('=');
|
||||
sendfun->S("\nalphacorr"); sendfun->P('0'+i); sendfun->P('=');
|
||||
printfl(params->alphacorr[i], 2);
|
||||
}
|
||||
newline();
|
||||
N();
|
||||
}
|
||||
// get MLX state
|
||||
TRUE_INLINE void getst(){
|
||||
@ -228,8 +259,8 @@ TRUE_INLINE void getst(){
|
||||
[MLX_RELAX] = "do nothing"
|
||||
};
|
||||
mlx_state_t s = mlx_state();
|
||||
U("MLXSTATE=");
|
||||
USND(states[s]);
|
||||
sendfun->S("MLXSTATE=");
|
||||
sendfun->S(states[s]); N();
|
||||
}
|
||||
|
||||
// `draw`==1 - draw, ==0 - show T map, 2 - send raw float array with prefix 'SENSNO=x\nTimage=y\n' and postfix "ENDIMAGE\n"
|
||||
@ -239,8 +270,8 @@ static const char *drawimg(const char *buf, int draw){
|
||||
uint32_t T = mlx_lastimT(sensno);
|
||||
fp_t *img = mlx_getimage(sensno);
|
||||
if(img){
|
||||
U(Sensno); USND(u2str(sensno));
|
||||
U(Timage); USND(u2str(T));
|
||||
sendfun->S(Sensno); sendfun->S(u2str(sensno)); N();
|
||||
sendfun->S(Timage); sendfun->S(u2str(T)); N();
|
||||
switch(draw){
|
||||
case 0:
|
||||
dumpIma(img);
|
||||
@ -255,12 +286,12 @@ static const char *drawimg(const char *buf, int draw){
|
||||
// send by portions of 256 bytes (as image is larger than ringbuffer)
|
||||
while(_2send){
|
||||
uint32_t portion = (_2send > 256) ? 256 : _2send;
|
||||
USB_send(d, portion);
|
||||
sendfun->B(d, portion);
|
||||
_2send -= portion;
|
||||
d += portion;
|
||||
}
|
||||
}
|
||||
USND("ENDIMAGE");
|
||||
sendfun->S("ENDIMAGE"); N();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@ -270,26 +301,35 @@ static const char *drawimg(const char *buf, int draw){
|
||||
|
||||
TRUE_INLINE void listactive(){
|
||||
int N = mlx_nactive();
|
||||
if(!N){ USND("No active sensors found!"); return; }
|
||||
if(!N){ sendfun->S("No active sensors found!\n"); return; }
|
||||
uint8_t *ids = mlx_activeids();
|
||||
U("Found "); USB_putbyte('0'+N); USND(" active sensors:");
|
||||
sendfun->S("Found "); sendfun->P('0'+N);
|
||||
sendfun->S(" active sensors:"); N();
|
||||
for(int i = 0; i < N_SESORS; ++i)
|
||||
if(ids[i]){
|
||||
U("SENSID"); U(u2str(i)); USB_putbyte('=');
|
||||
U(uhex2str(ids[i] >> 1));
|
||||
newline();
|
||||
sendfun->S("SENSID");
|
||||
sendfun->S(u2str(i)); sendfun->P('=');
|
||||
sendfun->S(uhex2str(ids[i] >> 1));
|
||||
N();
|
||||
}
|
||||
}
|
||||
|
||||
static void getimt(const char *buf){
|
||||
int sensno = getsensnum(buf);
|
||||
if(sensno > -1){
|
||||
U(Timage); USND(u2str(mlx_lastimT(sensno)));
|
||||
}else U(ERR);
|
||||
sendfun->S(Timage); sendfun->S(u2str(mlx_lastimT(sensno))); N();
|
||||
}else sendfun->S(ERR);
|
||||
}
|
||||
|
||||
const char *parse_cmd(char *buf){
|
||||
/**
|
||||
* @brief parse_cmd - user string parser
|
||||
* @param buf - user data
|
||||
* @param isusb - ==1 to send answer over usb, else send over USART1
|
||||
* @return answer OK/ERR or NULL
|
||||
*/
|
||||
const char *parse_cmd(char *buf, int sendto){
|
||||
if(!buf || !*buf) return NULL;
|
||||
chsendfun(sendto);
|
||||
if(buf[1]){
|
||||
switch(*buf){ // "long" commands
|
||||
case 'a':
|
||||
@ -325,7 +365,9 @@ const char *parse_cmd(char *buf){
|
||||
}
|
||||
break;
|
||||
case 'U':
|
||||
if(usart_sendstr(buf + 1) && usart_putchar('\n')) return OK;
|
||||
if(sendto == SEND_USB) chsendfun(SEND_USART);
|
||||
else chsendfun(SEND_USB);
|
||||
if(sendfun->S(buf + 1) && N()) return OK;
|
||||
return ERR;
|
||||
default:
|
||||
return ERR;
|
||||
@ -345,18 +387,18 @@ const char *parse_cmd(char *buf){
|
||||
case 's':
|
||||
mlx_stop(); return OK;
|
||||
case 'C':
|
||||
if(sendto != SEND_USB) return ERR;
|
||||
cartoon = !cartoon; return OK;
|
||||
case 'G':
|
||||
getst();
|
||||
break;
|
||||
case 'T':
|
||||
U("T=");
|
||||
USND(u2str(Tms));
|
||||
sendfun->S("T="); sendfun->S(u2str(Tms)); N();
|
||||
break;
|
||||
case '?': // help
|
||||
case 'h':
|
||||
case 'H':
|
||||
U(helpstring);
|
||||
sendfun->S(helpstring);
|
||||
break;
|
||||
default:
|
||||
return ERR;
|
||||
@ -364,3 +406,52 @@ const char *parse_cmd(char *buf){
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// dump image as temperature matrix
|
||||
void dumpIma(const fp_t im[MLX_PIXNO]){
|
||||
for(int row = 0; row < MLX_H; ++row){
|
||||
for(int col = 0; col < MLX_W; ++col){
|
||||
printfl(*im++, 1);
|
||||
sendfun->P(' ');
|
||||
}
|
||||
N();
|
||||
}
|
||||
}
|
||||
|
||||
#define GRAY_LEVELS (16)
|
||||
// 16-level character set ordered by fill percentage (provided by user)
|
||||
static const char* CHARS_16 = " .':;+*oxX#&%B$@";
|
||||
// draw image in ASCII-art
|
||||
void drawIma(const fp_t im[MLX_PIXNO]){
|
||||
// Find min and max values
|
||||
fp_t min_val = im[0], max_val = im[0];
|
||||
const fp_t *iptr = im;
|
||||
for(int row = 0; row < MLX_H; ++row){
|
||||
for(int col = 0; col < MLX_W; ++col){
|
||||
fp_t cur = *iptr++;
|
||||
if(cur < min_val) min_val = cur;
|
||||
else if(cur > max_val) max_val = cur;
|
||||
}
|
||||
}
|
||||
fp_t range = max_val - min_val;
|
||||
sendfun->S("RANGE="); sendfun->S(float2str(range, 3));
|
||||
sendfun->S("\nMIN="); sendfun->S(float2str(min_val, 3));
|
||||
sendfun->S("\nMAX="); sendfun->S(float2str(max_val, 3)); N();
|
||||
if(fabsf(range) < 0.001) range = 1.; // solid fill -> blank
|
||||
// Generate and print ASCII art
|
||||
iptr = im;
|
||||
for(int row = 0; row < MLX_H; ++row){
|
||||
for(int col = 0; col < MLX_W; ++col){
|
||||
fp_t normalized = ((*iptr++) - min_val) / range;
|
||||
// Map to character index (0 to 15)
|
||||
int index = (int)(normalized * GRAY_LEVELS);
|
||||
// Ensure we stay within bounds
|
||||
if(index < 0) index = 0;
|
||||
else if(index > (GRAY_LEVELS-1)) index = (GRAY_LEVELS-1);
|
||||
sendfun->P(CHARS_16[index]);
|
||||
}
|
||||
N();
|
||||
}
|
||||
N();
|
||||
}
|
||||
|
||||
|
||||
@ -20,5 +20,11 @@
|
||||
|
||||
extern const char *Timage, *Sensno;
|
||||
|
||||
#define SEND_USB (1)
|
||||
#define SEND_USART (0)
|
||||
|
||||
extern uint8_t cartoon;
|
||||
char *parse_cmd(char *buf);
|
||||
void chsendfun(int sendto);
|
||||
const char *parse_cmd(char *buf, int sendto);
|
||||
void dumpIma(const fp_t im[MLX_PIXNO]);
|
||||
void drawIma(const fp_t im[MLX_PIXNO]);
|
||||
|
||||
@ -23,11 +23,6 @@
|
||||
|
||||
#include "usb_dev.h"
|
||||
|
||||
#define printu(x) do{USB_sendstr(u2str(x));}while(0)
|
||||
#define printi(x) do{USB_sendstr(i2str(x));}while(0)
|
||||
#define printuhex(x) do{USB_sendstr(uhex2str(x));}while(0)
|
||||
#define printfl(x,n) do{USB_sendstr(float2str(x, n));}while(0)
|
||||
|
||||
void u16s(uint16_t n, char *buf);
|
||||
void hexdump16(int (*sendfun)(const char *s), uint16_t *arr, uint16_t len);
|
||||
void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len);
|
||||
|
||||
@ -85,11 +85,12 @@ static int transmit_tbuf(){
|
||||
}
|
||||
|
||||
// return 0 if can't write to ringbuffer
|
||||
int usart_putchar(const char ch){
|
||||
int r = RB_write(&dmarb, (uint8_t*)&ch, 1);
|
||||
int usart_putbyte(uint8_t ch){
|
||||
int r = RB_write(&dmarb, &ch, 1);
|
||||
if(r != 1){
|
||||
if(transmit_tbuf()) r = RB_write(&dmarb, (uint8_t*)&ch, 1);
|
||||
if(transmit_tbuf()) r = RB_write(&dmarb, &ch, 1);
|
||||
}
|
||||
if(r < 0) r = 0;
|
||||
return r;
|
||||
}
|
||||
|
||||
@ -163,7 +164,7 @@ void usart1_exti25_isr(){
|
||||
USART1->ICR = USART_ICR_CMCF; // clear character match flag
|
||||
register int l = UARTBUFSZI - DMA1_Channel5->CNDTR - 1; // substitute '\n' with '\0', omit empty strings!
|
||||
if(l > 0){
|
||||
if(recvdata){ // user didn't read old data - mark as buffer overflow
|
||||
if(recvdatalen){ // user didn't read old data - mark as buffer overflow
|
||||
bufovr = 1;
|
||||
}
|
||||
recvdata = rbuf[rbufno];
|
||||
|
||||
@ -32,6 +32,7 @@ int usart_ovr(); // RX overfull occured
|
||||
void usart_process(); // send next data portion
|
||||
int usart_setup(uint32_t speed); // set USART1 with given speed
|
||||
char *usart_getline(int *len); // read from rbin to buf
|
||||
int usart_send(const uint8_t *data, int len);
|
||||
int usart_sendstr(const char *str);
|
||||
int usart_putchar(const char ch);
|
||||
int usart_putbyte(uint8_t ch);
|
||||
void usart_stop();
|
||||
|
||||
@ -47,7 +47,7 @@ void linecoding_handler(usb_LineCoding *lc);
|
||||
#define RBINSZ (128)
|
||||
|
||||
#define newline() USB_putbyte('\n')
|
||||
#define USND(s) do{USB_sendstr(s); USB_putbyte('\n');}while(0)
|
||||
#define UN(s) do{USB_sendstr(s); USB_putbyte('\n');}while(0)
|
||||
#define U(s) USB_sendstr(s)
|
||||
|
||||
int USB_sendall();
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
#define BUILD_NUMBER "1"
|
||||
#define BUILD_DATE "2025-09-28"
|
||||
#define BUILD_NUMBER "15"
|
||||
#define BUILD_DATE "2025-09-29"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user