long rfsearch(FILE *fp, const char *pattern, const size_t size, int N) { long pos = -2L, tempos; char *sbuf, *p; size_t i, skip; int ch = 0; /* Allocate a search buffer */ if (NULL == (sbuf = (char *)malloc(size - 1))) goto RDONE; /* Buffer the file and position us within it */ if (0 == fsetup(fp, size)) goto RDONE; pos = -1L; fseek(fp, -1L, SEEK_END); tempos = ftell(fp) - strlen(pattern); /* Set up for smart searching */ if (1 < strlen(pattern) && NULL != (p = strrchr(pattern + 1, *pattern))) skip = strlen(pattern) - (p - (char *)pattern); else skip = strlen(pattern); /* Look for the pattern */ while (0L <= tempos) { fseek(fp, tempos, SEEK_SET); if (EOF == (ch = fgetc(fp))) break; if ((int)*pattern == ch) { if (size - 1 <= fread(sbuf, sizeof(char), size - 1, fp)) { if (SUCCESS == memcmp(sbuf, &pattern[1], size - 1)) { if (0 == --N) { pos = tempos; goto RDONE; } } } tempos -= skip; } else --tempos; } /* Clean up and leave */ RDONE: free(sbuf); return pos; }
void setup() { fsetup(); /* 8 Extra Buttons can be connected from pin 2 to pin 9 */ for (int pin = 2; pin <= 9; ++pin) { pinMode(pin, INPUT_PULLUP); } /* WT12 */ #ifndef IS_USB pinMode (CTS, INPUT); WT12.begin(115200, SERIAL_8N1); WT12.attachRts(19); // callback iwrap_output = iwrap_out; iwrap_evt_hid_output = hid_output; iwrap_evt_ring = my_iwrap_evt_ring; iwrap_evt_hid_suspend = my_iwrap_evt_hid_suspend; iwrap_rsp_list_result = my_iwrap_rsp_list_result; iwrap_evt_no_carrier = my_iwrap_evt_no_carrier; // prebuild HID packet hid_data[0] = 0x9f; hid_data[1] = 0x21; hid_data[2] = 0xa1; in_changed = false; bt_retry = false; // useless #else Joystick.useManualSend(true); #endif // IS_USB // prebuild output packet memset(wheel_out.raw, 0, sizeof(csw_out_t)); wheel_out.header = 0xa5; wheel_out.id = 0x00; //wheel_out.leds = 0xffff; got_hid = false; // debug #ifdef HAS_DEBUG //iwrap_debug = my_iwrap_debug; Serial.begin(115200); Serial.println("MCU Ready"); delay(5000); Serial.println("check for active connection..."); #endif #ifndef IS_USB bt_connected = false; iwrap_send_command("LIST", iwrap_mode); #else bt_connected = true; #endif //iwrap_send_command("SET BT PAIR", iwrap_mode); timing = micros(); timing_bt = millis(); }