char* multiply(char *num1, int num2) { int temp=0,currNum,index=0,digitCount=0; while(num1[index]!='\0') { currNum=((num1[index]-48)*num2)+temp; num1[index]=(currNum%10)+48; temp=currNum/10; index++; } if(num1[index]=='\0') { digitCount=getDigitCount(temp); num1 = (char *) realloc(num1, (strlen(num1)+digitCount+1)*sizeof(char)); while(digitCount--) { num1[index]=(temp%10)+48; temp=temp/10; index++; } } num1[index]='\0'; return num1; }
void MAX7219::set7Segment(const char *number, byte topo) { byte *buf, chr; word digits; if(_topology[topo].elementType != MAX7219_MODE_7SEGMENT) return; digits = getDigitCount(topo); buf = (byte *)calloc(digits, sizeof(byte)); for(word i = 0; i < digits; i++) { chr = number[i]; //Set DP if so instructed if((byte)chr & MAX7219_FLG_SEGDP) { buf[i] |= MAX7219_FLG_SEGDP; chr &= ~MAX7219_FLG_SEGDP; } //Cheaper than using atoi() or a lookup table switch(chr) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buf[i] |= chr - (byte)'0'; break; case '-': buf[i] |= 0x0A; break; case 'E': case 'e': buf[i] |= 0x0B; break; case 'H': case 'h': buf[i] |= 0x0C; break; case 'L': case 'l': buf[i] |= 0x0D; break; case 'P': case 'p': buf[i] |= 0x0E; break; case ' ': buf[i] |= 0x0F; break; } } setDigits(buf, topo); free(buf); }
void moveFile(char* from, char* destination) // TODO: try to reduce the amount of dynamic memory allocation { char* filename = strrfind(from, '/') + 1; size_t name_len = strlen(destination) + strlen(filename) + 1; char* end_name; if (!strend(destination, "/")) { ++name_len; end_name = malloc(name_len); snprintf(end_name, name_len, "%s/%s", destination, filename); } else { end_name = malloc(name_len); snprintf(end_name, name_len, "%s%s", destination, filename); } // add (some number) to the end of the filename to avoid overwriting data char* safe_name = malloc(name_len); strcpy(safe_name, end_name); size_t safe_len; for (int m = 1; access(safe_name, F_OK) != -1; ++m) { free(safe_name); safe_len = name_len + getDigitCount(m) + 3; safe_name = malloc(safe_len); snprintf(safe_name, safe_len, "%s (%d)", end_name, m); } free(end_name); end_name = safe_name; name_len = safe_len; // shouldn't be necessary, but it keeps a safe state // write to status log writeDebug("Rename \"%s\" to \"%s\"", from, end_name); if (rename(from, end_name) < 0) { writeWarning("Unable to move file to destination"); } else { // send notification char notify_message[FILE_NAME_MAX]; snprintf(notify_message, FILE_NAME_MAX, "Rename \"%s\" to \"%s\"", from , end_name); if (isPaused()) { sendPausedNotification(from, end_name); } else { sendMovingNotification(from, end_name); } } free(end_name); }
void MAX7219::clearDisplay(byte topo) { byte *buf, digits; if(_topology[topo].elementType == MAX7219_MODE_OFF || _topology[topo].elementType == MAX7219_MODE_NC) return; digits = getDigitCount(topo); buf = (byte *)calloc(digits, sizeof(byte)); if(_topology[topo].elementType == MAX7219_MODE_7SEGMENT) memset((void *)buf, 0x0F, digits * sizeof(byte)); setDigits(buf, topo); free(buf); }
void MAX7219::setBarGraph(const byte *values, boolean dot, byte topo){ byte *buf; word digits; if(_topology[topo].elementType != MAX7219_MODE_BARGRAPH) return; digits = getDigitCount(topo); buf = (byte *)malloc(digits * sizeof(byte)); for(byte i = 0; i < digits; i++) { if(!values[i]) buf[i] = values[i]; else if(dot) buf[i] = 1 << (values[i] - 1); else { buf[i] = 0x00; for(int j = 0; j < values[i]; j++) buf[i] |= 1 << j; } } setDigits(buf, topo); free(buf); }
void MAX7219::setDigits(const byte *values, byte topo) { word *buf; word transfers; byte chips; chips = _topology[topo].chipTo - _topology[topo].chipFrom + 1; buf = (word *)malloc(chips * sizeof(word)); transfers = (_topology[topo].chipFrom == _topology[topo].chipTo ? getDigitCount(topo) : (_topology[topo].chipFrom == _topology[topo].chipTo - 1 ? max(7 - _topology[topo].digitFrom, _topology[topo].digitTo) + 1 : 8)); #if defined(MAX7219_DEBUG) Serial.print("Chips: "); Serial.print(chips); Serial.print(", transfers: "); Serial.print(transfers); Serial.print(", element: "); Serial.println(topo); #endif for (word i = 0; i < transfers; i++) { for(byte j = 0; j < chips; j++) if(i < (j == _topology[topo].chipFrom ? 7 - _topology[topo].digitFrom + 1 : (j == _topology[topo].chipTo ? _topology[topo].digitTo + 1 : 8))) buf[j] = word(MAX7219_REG_DIGIT0 + (j ? i : _topology[topo].digitFrom + i), values[(transfers - 1) * j + i]); else buf[j] = word(MAX7219_REG_NOOP, 0x00); writeRegisters(buf, chips, _topology[topo].chipFrom); } free(buf); }
void MAX7219::zeroDisplay(byte topo) { byte *buf; word digits; if(_topology[topo].elementType == MAX7219_MODE_OFF || _topology[topo].elementType == MAX7219_MODE_NC) return; digits = getDigitCount(topo); buf = (byte *)malloc(digits * sizeof(byte)); switch(_topology[topo].elementType) { case MAX7219_MODE_7SEGMENT: memset((void *)buf, 0x00, digits * sizeof(byte)); break; case MAX7219_MODE_MATRIX: buf[0] = 0x01; memset((void *)&buf[1], 0x00, (digits - 1) * sizeof(byte)); break; case MAX7219_MODE_BARGRAPH: memset((void *)buf, 0x01, digits * sizeof(byte)); break; } setDigits(buf, topo); free(buf); }