void noinline ow_write_byte(uint8_t busmask, uint8_t value) { OW_CONFIG_OUTPUT(busmask); for (uint8_t i = 0; i < 8; i++) { ow_write(busmask, (uint8_t)(value & _BV(i))); } }
/* * wbook_store_workbook(struct wbookctx *wbook) * * Assemble worksheets into a workbook and send the BIFF data to an OLE * storage. * */ void wbook_store_workbook(struct wbookctx *wbook) { struct owctx *ole = wbook->OLEwriter; int i; /* Call the finalization methods for each worksheet */ for (i = 0; i < wbook->sheetcount; i++) { wsheet_close(wbook->sheets[i]); } /* Add workbook globals */ bw_store_bof(wbook->biff, 0x0005); wbook_store_codepage(wbook); wbook_store_window1(wbook); wbook_store_1904(wbook); wbook_store_all_fonts(wbook); wbook_store_all_num_formats(wbook); wbook_store_all_xfs(wbook); wbook_store_all_styles(wbook); wbook_calc_sheet_offsets(wbook); /* Add BOUNDSHEET records */ for (i = 0; i < wbook->sheetcount; i++) { wbook_store_boundsheet(wbook, wbook->sheets[i]->name, wbook->sheets[i]->offset); } bw_store_eof(wbook->biff); /* Write Worksheet data if data <~ 7MB */ if (ow_set_size(ole, wbook->biffsize)) { ow_write_header(ole); ow_write(ole, wbook->biff->data, wbook->biff->datasize); for (i = 0; i < wbook->sheetcount; i++) { unsigned char *tmp; size_t size; while ((tmp = wsheet_get_data(wbook->sheets[i], &size))) { ow_write(ole, tmp, size); free(tmp); } } } }
int8_t noinline ow_match_rom(ow_rom_code_t *rom) { /* reset the bus */ if (!reset_onewire(ONEWIRE_BUSMASK)) return -1; /* transmit command byte */ ow_write_byte(ONEWIRE_BUSMASK, OW_ROM_MATCH_ROM); /* transmit rom code */ for (uint8_t i = 0; i < 8; i++) { for (uint8_t j = 0; j < 8; j++) { ow_write(ONEWIRE_BUSMASK, (uint8_t)(rom->bytewise[i] & _BV(j))); } } return 1; }
/* high-level functions */ int8_t noinline ow_search_rom(uint8_t busmask, uint8_t first) { /* reset discover state machine */ if (first) { ow_global.last_discrepancy = -1; /* reset rom code */ for (uint8_t i = 0; i < 8; i++) ow_global.current_rom.bytewise[i] = 0; } else { /* if last_discrepancy is below zero, discovery is done */ if (ow_global.last_discrepancy < 0) return 0; } int8_t discrepancy = -1; /* reset the bus */ if (!reset_onewire(busmask)) return -1; /* transmit command byte */ ow_write_byte(busmask, OW_ROM_SEARCH_ROM); for (uint8_t i = 0; i <64; i++) { /* read bits */ uint8_t bit1 = ow_read(busmask); uint8_t bits = (uint8_t)((ow_read(busmask) << 1) | bit1); if (bits == 3) { /* no devices, just return */ return 0; } else if (bits == 0) { if (i == ow_global.last_discrepancy) { /* set one */ ow_set_address_bit(&ow_global.current_rom, i, 1); /* transmit one next time */ bit1 = 1; } else if (i > ow_global.last_discrepancy) { /* set zero */ ow_set_address_bit(&ow_global.current_rom, i, 0); discrepancy = (int8_t)i; } else { uint8_t rom_bit = (uint8_t)(ow_global.current_rom.bytewise[i / 8] & _BV(i % 8)); if (rom_bit == 0) discrepancy = (int8_t)i; /* transmit last bit next time */ bit1 = rom_bit; } } else { /* normal case, no discrepancy */ ow_set_address_bit(&ow_global.current_rom, i, bit1); } OW_CONFIG_OUTPUT(busmask); /* select next bit */ ow_write(busmask, bit1); } ow_global.last_discrepancy = discrepancy; /* new device discovered */ return 1; }