F746NG Test All Functions
Dependencies: TS_DISCO_F746NG LCD_DISCO_F746NG BSP_DISCO_F746NG BUTTON_GROUP Arduino
Diff: Functions.h
- Revision:
- 7:f654d2b1f08b
- Parent:
- 6:ba00a5cd9466
--- a/Functions.h Mon Dec 21 20:31:09 2020 +0000 +++ b/Functions.h Tue Dec 29 17:49:08 2020 +0000 @@ -1,160 +1,240 @@ #ifndef FUNCTIONS_H #define FUNCTIONS_H -using namespace ButGrp; -void SetLayer(int numlayer, int tipo=0) +string i2s(unsigned long number) { - return; - // vedere come passare BSPLCD_ - for (int idx = 0; idx < NUMLAYER; idx++) { - // sprintf(str, "Touches: %d",idx); - BSP_LCD_DisplayStringAt(0, LINE(12+idx), (uint8_t *)&str, LEFT_MODE); - if (idx==numlayer) { - BSP_LCD_SetLayerVisible(idx, ENABLE); - BSP_LCD_SelectLayer(idx); - // BSP_LCD_DisplayStringAt(50, LINE(12+idx), (uint8_t *)"ON", RIGHT_MODE); - } else { - if (tipo==0) { - BSP_LCD_SetLayerVisible(idx, DISABLE); - // BSP_LCD_DisplayStringAt(50, LINE(12+idx), (uint8_t *)"OFF", RIGHT_MODE); - } - } + // integer to string + if (number == 0) + return "0"; + string temp=""; + string returnvalue=""; + while (number>0) { + temp+=number%10+48; + number/=10; } + for (int i=0; i<temp.length(); i++) + returnvalue+=temp[temp.length()-i-1]; + return returnvalue; +} + + +int min(int a, int b) +{ + return (a<b?a:b); +} + +int max(int a, int b) +{ + return (a>b?a:b); } -void PressButton() + +void FattorizzazioneFunction(unsigned long risultato, unsigned long *Divisori) { - static int ccont=0; - led1= !led1; - ccont++; - if (nummenu==0) { - BSP_LCD_.SetFont(&Font16); - BSP_LCD_SetTextColor(LCD_COLOR_WHITE); - sprintf(str, "Tasto premuto %d volte", ccont); - BSP_LCD_DisplayStringAt(0, LINE(10), (uint8_t *) str, CENTER_MODE); + unsigned long long int idxPrimi=2; + int idxFattorizzati=0; + // Divisori[0]=0; + while (risultato>1) { + if (risultato%idxPrimi==0) { + Divisori[idxFattorizzati++]=idxPrimi; ; + risultato=risultato/idxPrimi; + } else { + idxPrimi++; + } } + Divisori[idxFattorizzati]=0; } -void GestioneButton() -{ - // vedi buttoGroup_Demo per esempio group - static bool toccato=false; - bool modifica=false; - BSP_TS_GetState(&TS_State); - if ((avanti.Touched())&& (toccato==false)) { - nummenu=(++nummenu)%MAXMENU; - // reset.Draw(INACTIVE, LCD_COLOR_GRAY); - modifica=true; +void FattorizzazioneCiclica() +{ + static unsigned long numdatrovare=2; + static long long int conteggio=0; + unsigned long Divisori[49]; + int i=0; + int deltaconteggio; + static int nowval=0, maxval=0, minval=9999; + string elencostringa=""; + char mmm[128]; + + FattorizzazioneFunction(numdatrovare, Divisori ); + + if (nummenu==1) { + while ((Divisori[i]>0) && (i<49)) { + // sprintf(substr, "%d%s", Divisori[i]),(Divisori[i+1]>0?" * ":"") ; + elencostringa+=i2s(Divisori[i])+(Divisori[i+1]>0?" * ":""); + i++; + } + elencostringa=elencostringa.substr(0,45); + sprintf(mmm, "%u= %s", numdatrovare,elencostringa); + BSP_LCD_.SetFont(&Font12); + BSP_LCD_SetTextColor(LCD_COLOR_BLACK); + BSP_LCD_FillRect(42, 100, DimX-84, 15); //cancella riga + BSP_LCD_SetTextColor(LCD_COLOR_WHITE); + BSP_LCD_DisplayStringAt(50, 100, (uint8_t *) mmm, LEFT_MODE); } - if ((indietro.Touched()) && (toccato==false)) { - nummenu=((--nummenu)+MAXMENU)%MAXMENU; - // reset.Draw(INACTIVE, LCD_COLOR_GRAY); - modifica=true; - } - wait(0.01f); - - if((TS_State.touchDetected) && (toccato==false)) { - toccato=true; - } else if((!TS_State.touchDetected) && (toccato==true)) { - toccato=false; + + if ((conteggio+1000<millis()) ) { + deltaconteggio=numdatrovare-nowval; + maxval=max(maxval,deltaconteggio); + if (conteggio>0) + minval=min(minval, deltaconteggio); + if (nummenu==1) { + sprintf(mmm, "Fattorizzati %d interi-sec. Min val= %d Max val=%d", deltaconteggio,minval,maxval); + BSP_LCD_DisplayStringAt(50, 120, (uint8_t *) mmm, LEFT_MODE); + } + nowval=numdatrovare; + conteggio=millis(); } - if (modifica==true) { - if (nummenu==0) { - BSP_LCD_.SetFont(&Font12); - BSP_LCD_SetTextColor(LCD_COLOR_WHITE); - sprintf(str, "menu: %d", nummenu); - } - BSP_LCD_DisplayStringAt(50, LINE(7), (uint8_t *)str, LEFT_MODE); - BSP_LCD_SetTextColor(LCD_COLOR_BLACK); - BSP_LCD_FillRect(40+1, 45+1, DimX-80-1, DimY-45-45-1); - BSP_LCD_FillRect(DimX/3+6, 1, ((DimX*2)/3)-7, 38); - BSP_LCD_.SetFont(&Font24); - BSP_LCD_SetTextColor(LCD_COLOR_YELLOW); - switch (nummenu) { - case 0: - BSP_LCD_DisplayStringAt(DimX/3+10, 10, (uint8_t *) "Dati Generali", LEFT_MODE); - break; - case 1: - BSP_LCD_DisplayStringAt(DimX/3+10, 10, (uint8_t *) "Da assegnare", LEFT_MODE); - break; - case 2: - BSP_LCD_DisplayStringAt(DimX/3+10, 10, (uint8_t *) "Dati TCP", LEFT_MODE); - break; - } - - } -} + numdatrovare++; -void GestioneTouch() -{ - static uint16_t x=0, y=0; - uint8_t cleared = 0; - uint8_t prev_nb_touches = 0; - BSP_TS_GetState(&TS_State); - BSP_LCD_SetBackColor(LCD_COLOR_BLACK); - BSP_LCD_SetTextColor(LCD_COLOR_WHITE); - BSP_LCD_.SetFont(&Font16); - if (TS_State.touchDetected) { - // Clear lines corresponding to old touches coordinates - if (TS_State.touchDetected < prev_nb_touches) { - for (int idx = (TS_State.touchDetected + 1); idx <= 5; idx++) { - BSP_LCD_ClearStringLine(idx); - } - } - prev_nb_touches = TS_State.touchDetected; - cleared = 0; - sprintf(str, "Touches: %d", TS_State.touchDetected); - BSP_LCD_DisplayStringAt(50, LINE(3), (uint8_t *)str, LEFT_MODE); - SetLayer( 1); - BSP_LCD_DrawPixel(x, y, LCD_COLOR_BLACK); - SetLayer( 0); - for (int idx = 0; idx < TS_State.touchDetected; idx++) { - x = TS_State.touchX[idx]; - y = TS_State.touchY[idx]; - sprintf(str, "Touch %d: x=%d y=%d ", idx+1, x, y); - BSP_LCD_DisplayStringAt(50, LINE(idx+4), (uint8_t *)str, LEFT_MODE); - } - SetLayer( 1); - BSP_LCD_DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_WHITE); - SetLayer( 0); - } else { - if (!cleared) { - sprintf(str, "Touches: 0"); - BSP_LCD_DisplayStringAt(50, LINE(3), (uint8_t *)&str, LEFT_MODE); - cleared = 1; - } - } -} - -void DatiTCP() -{ - static bool giavisualizzato=false; - if ((nummenu==2)&&(giavisualizzato==false)) { - BSP_LCD_SetBackColor(LCD_COLOR_BLACK); - BSP_LCD_SetTextColor(LCD_COLOR_WHITE); - BSP_LCD_.SetFont(&Font16); - BSP_LCD_DisplayStringAt(0, LINE(3), (uint8_t *)"Ethernet socket example", CENTER_MODE); - // Show the network address - const char *ip = net.get_ip_address(); - const char *netmask = net.get_netmask(); - const char *gateway = net.get_gateway(); - sprintf(str,"IP address: %s", ip ? ip : "None"); - BSP_LCD_DisplayStringAt(0, LINE(5), (uint8_t *)str, CENTER_MODE); - sprintf(str,"Netmask: %s", netmask ? netmask : "None"); - BSP_LCD_DisplayStringAt(0, LINE(6), (uint8_t *)str, CENTER_MODE); - sprintf(str,"Gateway: %s", gateway ? gateway : "None"); - BSP_LCD_DisplayStringAt(0, LINE(7), (uint8_t *)str, CENTER_MODE); - - giavisualizzato=true; - } - - if (nummenu!=2) - giavisualizzato=false; } +string addEOS(string sttde) +{ + string ret=sttde; + if (sttde.substr(sttde.size()-1,1) != "\n") ret=sttde+"\n"; + return ret; +} + +string padstr(string sttde, int maxlen, char fillchar) +{ + bool flagEOS=false; + string ret=sttde; + if (ret.size()>0) { + //se ha EOS lo tolgo + if (ret.substr(ret.size()-1,1) == "\n") { + ret=ret.substr(0,ret.size()-1); + flagEOS=true; + } + //pad + if (maxlen> ret.size()) ret.insert(ret.size(), maxlen - ret.size(), fillchar); + //se aveva EOS, lo rimetto + if (flagEOS==true) ret=addEOS(ret); + } + return ret; +} + + + +string subEOS(string sttde) +{ + string ret=sttde; + if (sttde.substr(sttde.size()-1,1) == "\n") ret=sttde.substr(0,sttde.size()-2); + return ret; +} + +char* string2char(string sttde) +{ + //ora aggiunge comunque l'EOS. Decidere se parametrizzare + sttde=addEOS(sttde); + char *cstr = new char[sttde.length() + 1]; + strcpy(cstr, sttde.c_str()); + // delete [] cstr; + return cstr; +} + +int c2i(char sttde) +{ + // single char to integer + return sttde-'0';; +} + + + + + +/* atof: convert string s to double */ +double atof(char s[]) +{ + double val, power; + int i, sign; + + + for (i = 0; isspace(s[i]); i++); /* skip white space */ + + sign = (s[i] == '-') ? -1 : 1; + if (s[i] == '+' || s[i] == '-') i++; + for (val = 0.0; isdigit(s[i]); i++) { + val = 10.0 * val + (s[i] - '0'); + } + if (s[i] == '.') i++; + for (power = 1.0; isdigit(s[i]); i++) { + val = 10.0 * val + (s[i] - '0'); + power *= 10.0; + } + return sign * val / power; +} + + + +/* reverse: reverse string s in place */ +void reverse(char s[]) +{ + int c, i, j; + for (i = 0, j = strlen(s)-1; i < j; i++, j-- ) { + c = s[i]; + s[i] = s[j]; + s[j] = c; + } +} + + +/* itoa: convert n to characters in s */ +void itoa(int n, char s[]) +{ + int i, sign; + if ((sign = n) < 0) /* record sign */ + n = -n; /* make n positive */ + i = 0; + do { /* generate digits in reverse order */ + s[i++] = n % 10 + '0'; /* get next digit */ + } while ((n /= 10) > 0); /* delete it */ + if (sign < 0) s[i++] = '-'; + s[i] = '\0'; + reverse(s) ; +} + + +/* atoi: convert s to integer; version 2 */ +int atoi(char s[]) +{ + int i, n, sign; + for (i = 0; isspace(s[i]) ; i++) { + ; /* skip white space */ + } + sign= (s[i] == '-') ? -1 : 1; + if (s[i] == '+' || s[i] == '-') i++; /* skip sign */ + for (n = 0; isdigit(s[i]); i++ ) { + ; + } + n = 10 * n + (s[i] - '0'); + return sign * n; +} + +int constrain(int amt, int low, int high) +{ + int ret=amt; + if (amt>high) ret=high; + else if (amt<low) ret= low; + return ret; + //return ((amt)<(low)?(low):((amt)>(high)?(high):(amt))); +} + + +/* trim: remove trailing blanks, tabs, newlines */ +int trim(char s[]) +{ + int n; + for (n = strlen(s)-1; n >= 0; n--) { + if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n') break; + } + s[n+1] = '\0'; + return n; +} + #endif \ No newline at end of file