static void update_time() { if (!curr_time) return; char buffer[BUFFER_SIZE]; uint8_t vbuffer[GRID_ROW_COUNT]; time_string(buffer, BUFFER_SIZE); for (uint8_t i = 0; i < GRID_ROW_COUNT; i++) { vbuffer[i] = char_to_uint(buffer[i]); } square_grid_set_values(grid, &vbuffer); }
unsigned int hex_to_uint(std::string s) { if (s.size() == 0) throw InvalidFormatException("Should at least one char, e.g. 0"); unsigned int sum = 0; unsigned int mul = 1; for (int i = s.size() - 1; i >= 0; --i) { sum += mul*char_to_uint(s[i]); mul *= 16; } return sum; }
uint32_t str_to_uint32(char *s) { int i; char c; uint32_t res = 0; for (i = 0; (i < 8) && (s[i] != '\0'); i++) { c = s[i]; res <<= 4; res += char_to_uint(c); } return(res); }
unsigned int binary_to_uint(std::string s) { if (s.size() == 0) throw InvalidFormatException("Should at least one char, e.g. 0"); unsigned int sum = 0; unsigned int mul = 1; for (int i = s.size() - 1; i >= 0; --i) { if (s[i] != '0' && s[i] != '1') throw InvalidFormatException("binary literal should be 0 or 1"); sum += mul*char_to_uint(s[i]); mul *= 2; } return sum; }
void parse(int scan, uint32_t *max, uint32_t *min) { int i, j; char line[LINE_LEN] = ""; uint32_t address; int rec_type, addr_bytes, byte_count; uint8_t c; uint8_t buf[32]; do { fgets(line, LINE_LEN, infile); if (line[0] == 'S') /* an S-record */ { rec_type = line[1] - '0'; if ((rec_type >= 1) && (rec_type <= 3)) /* data record */ { address = 0; addr_bytes = rec_type + 1; for (i = 4; i < (addr_bytes * 2) + 4; i++) { c = line[i]; address <<= 4; address += char_to_uint(c); } byte_count = (char_to_uint(line[2]) << 4) + char_to_uint(line[3]); byte_count -= (addr_bytes + 1); if (scan) { if (*min > address) *min = address; if (*max < (address + byte_count)) *max = address + byte_count; } else { address -= min_addr; if (verbose) fprintf(stderr, "Writing %d bytes at %08X\r", byte_count, address); j = 0; for (i = (addr_bytes * 2) + 4; i < (addr_bytes * 2) + (byte_count * 2) + 4; i += 2) { buf[j] = (char_to_uint(line[i]) << 4) + char_to_uint(line[i+1]); j++; } fseek(outfile, address, SEEK_SET); fwrite(buf, 1, byte_count, outfile); } } } } while(!feof(infile)); rewind(infile); *max -= 1; }