I have tested my app on simulators 4.3, 5.0, 5.1 and 6.0 for iPod, iPad an iPhone. It's working as expected. But on all my devices it doesn't work.
I know that on devices the filenames are case-sensitive. Both the used constant DATA_FILE and the type are lowercase, and the file handle is not nil after opening.
As I can see, there is no actual reading after the proper file positioning. Why not?
This is the code, reduced to the minimum to show the problem.
NSLog(#"getData, index = %lu", index);
NSFileHandle *fhIndex = [NSFileHandle fileHandleForReadingAtPath: [[NSBundle mainBundle] pathForResource: DATA_FILE ofType:#"idx"]];
u_char indexData[4];
NSLog(#"index*4 = %lu", (index * 4));
[fhIndex seekToFileOffset: (index * 4)];
unsigned long long test = [fhIndex offsetInFile];
NSLog(#"FilePos = %llu",test);
[[fhIndex readDataOfLength: 4] getBytes: indexData length: 4];
test = [fhIndex offsetInFile];
NSLog(#"FilePos = %llu",test);
NSLog(#"indexData = %i %i %i %i", indexData[0], indexData[1], indexData[2], indexData[3]);
An index (unsigned long) is given to the routine, in this case index=62825. The program make a seek to position index*4 and then reads the next four bytes. On devices, these four bytes are not read!
Debugger output in the simulator:
2012-10-31 10:46:24.557 program[469:12003] getData, index = 62825
2012-10-31 10:46:24.559 program[469:12003] index*4 = 251300
2012-10-31 10:46:24.559 program[469:12003] FilePos = 251300
2012-10-31 10:46:24.580 program[469:12003] FilePos = 251304
2012-10-31 10:46:24.581 program[469:12003] indexData = 0 73 130 174
... and on the devices:
2012-10-31 10:41:57.776 program[2942:907] getData, index = 62825
2012-10-31 10:41:57.780 program[2942:907] index*4 = 251300
2012-10-31 10:41:57.782 program[2942:907] FilePos = 251300
2012-10-31 10:41:57.783 program[2942:907] FilePos = 251300
2012-10-31 10:41:57.784 program[2942:907] indexData = 248 10 18 0
As you can see in the output on devices, the file position is not changed after the read, that means, there was actually no read!
Wrong filesize has guided me in the right direction.
After deleting the Build directory the program is now working on simulators and devices.
Related
I am trying to generate a QR Code using CoreImage.
I want to be able to control the symbol version, the masking pattern, and the error correction level.
Using the simple "CIFilter filterWithName:" does not give you the ability to set the symbol version or the mask pattern.
The only way it seems possible is to use a CIQRCodeDescriptor - using "CIQRCodeDesciptor initWithPayload: symbolVersion: maskPattern: errorCorrectionLevel:"
Has anyone been able to use this method to successfully generate a QR Code image?
If so, can you please post a simple complete example?
To be able to use CIQRCodeDescriptor you need
codewords (mode + character count + data + terminator + padding)
correct symbol version (version for the character count; 1-40)
correct mask pattern (mask with minimum penalty; 0-7)
Follows example of "Think Different".
Notice the extra bits in codeword
Think Different: 54 68 69 6E 6B 20 44 69 66 66 65 72 65 6E 74
Codeword: 40 F5 46 86 96 E6 B2 04 46 96 66 66 57 26 56 E7 40 EC 11
The codeword construction is explained at nayuiki or at the bottom.
let codeword : [UInt8] = [0x40, 0xf5, 0x46, 0x86, 0x96, 0xe6, 0xb2, 0x04, 0x46, 0x96, 0x66, 0x66, 0x57, 0x26, 0x56, 0xe7, 0x40, 0xec, 0x11]
let data = Data(bytes: codeword, count: codeword.count)
if let descriptor = CIQRCodeDescriptor(payload: data, symbolVersion: 1, maskPattern: 4, errorCorrectionLevel: .levelL) {
if let image = imageFromBarcodeCodeDescriptor(descriptor)?.transformed(by: .init(scaleX: 10, y: 10)) {
let newImage = NSImage()
newImage.addRepresentation(NSCIImageRep(ciImage: image))
imageView1.image = newImage
}
}
func imageFromBarcodeCodeDescriptor(_ descriptor: CIBarcodeDescriptor) -> CIImage? {
let filter = CIFilter(name: "CIBarcodeGenerator", parameters: ["inputBarcodeDescriptor" : descriptor])
return filter?.outputImage
}
Concatenate segments, add padding, make codewords
Notes:
The segment mode is always a 4-bit field.
The character count’s field width depends on the mode and version.
The terminator is normally four “0” bits, but fewer if the data
codeword capacity is reached.
The bit padding is between zero to seven “0” bits, to fill all unused
bits in the last byte.
The byte padding consists of alternating (hexadecimal) EC and 11 until
the capacity is reached.
The entire sequence of data bits:
01000000111101010100011010000110100101101110011010110010000001000100011010010110011001100110011001010111001001100101011011100111010000001110110000010001
The entire sequence of data codeword bytes (by splitting the bit
string into groups of 8 bits), displayed in hexadecimal: 40 F5 46 86
96 E6 B2 04 46 96 66 66 57 26 56 E7 40 EC 11
It seems CIQRCodeGenerator doesn't support those parameters.
Maybe you can find what you are looking for in this library.
You need to use "CIBarcodeGenerator" CIFilter with the CIQRCodeDescriptor as input:
let data = ... // error corrected payload data
if let barcode = CIQRCodeDescriptor(payload: data,
symbolVersion: 1, // 1...40
maskPattern: 0, // 0..7
errorCorrectionLevel: .levelL) // Any of the available enum values
{
let filter = CIFilter(name: "CIBarcodeGenerator",
parameters: ["inputBarcodeDescriptor": barcode])
let image = filter?.outputImage
}
The caveat though is that you need to obtain somehow the errorCorrectedPayload data for the message you are trying to encode. One of the ways to do this would be to use "CIQRCodeGenerator" to encode the message, parse the resulting image with Vision to extract the barcode descriptor from it, and then get the errorCorrectedPayload data from that descriptor.
A simple working example is:
// Create the CIFilter (CIQRCodeGenerator)
CIFilter *ciFilter = [CIFilter filterWithName:#"CIQRCodeGenerator"];
[ciFilter setDefaults];
NSData *data = [#"123456" dataUsingEncoding:NSUTF8StringEncoding];// QR code value
[ciFilter setValue:data forKey:#"inputMessage"];
[ciFilter setValue:#"L" forKey:#"inputCorrectionLevel"];// L: low, M: Medium, Q: Quartile, H: High
// Create the image at the desired size
CGSize size = CGSizeMake(280, 280);// Desired QR code size
CGRect rect = CGRectIntegral(ciFilter.outputImage.extent);
CIImage *ciImage = [ciFilter.outputImage imageByApplyingTransform:CGAffineTransformMakeScale(size.width/CGRectGetWidth(rect), size.height/CGRectGetHeight(rect))];
// Create a UIImage (if needed)
UIImage *image = [UIImage imageWithCIImage:ciImage];
_imageView.image = image;
i want my arduino to tell me what time running it is E.g. "this is the 22nd time this loop has run." what command/s should i use?
i am currently using this code:
Serial.print("This loop has run ");
Serial.print(loopsRun);
Serial.println(" times.");
loopsRun++;
yes i have declared all variables, i just want to know if there is a way to check the last digit of any int.
22 % 10 = 2 you'd say 22'nd'
1022 % 10 = 2 you'd say 1022 'nd'
27 % 10 = 7 you'd say 7 'th'
457 % 10 = 7 you'd say 457 'th'
Am I getting the pattern right? If so then you need a switch statement and a % operator
unsigned int remainder = loopsRun % 10;
switch (remainder)
{
case 0: suffix = "th"; break;
case 1: suffix = "st"; break;
case 2: suffix = "nd"; break;
<etc>
}
I took over the project from someone who had gone a long time ago.
I am now looking at ADC modules, but I don't get what the codes mean by.
MCU: LM3S9B96
ADC: AD7609 ( 18bit/8 channel)
Instrumentation Amp : INA114
Process: Reading volts(0 ~ +10v) --> Amplifier(INA114) --> AD7609.
Here is codes for that:
After complete conversion of 8 channels which stored in data[9]
Convert data to micro volts??
//convert to microvolts and store the readings
// unsigned long temp[], data[]
temp[0] = ((data[0]<<2)& 0x3FFFC) + ((data[1]>>14)& 0x0003);
temp[1] = ((data[1]<<4)& 0x3FFF0) + ((data[2]>>12)& 0x000F);
temp[2] = ((data[2]<<6)& 0x3FFC0) + ((data[3]>>10)& 0x003F);
temp[3] = ((data[3]<<8)& 0x3FF00) + ((data[4]>>8)& 0x00FF);
temp[4] = ((data[4]<<10)& 0x3FC00) + ((data[5]>>6)& 0x03FF);
temp[5] = ((data[5]<<12) & 0x3F000) + ((data[6]>>4)& 0x0FFF);
temp[6] = ((data[6]<<14)& 0x3FFF0) + ((data[7]>>2)& 0x3FFF);
temp[7] = ((data[7]<<16)& 0x3FFFC) + (data[8]& 0xFFFF);
I don't get what these codes are doing...? I know it shifts but how they become micro data format?
transfer function
//store the final value in the raw data array adstor[]
adstor[i] = (signed long)(((temp[i]*2000)/131072)*10000);
131072 = 2^(18-1) but I don't know where other values come from
AD7609 datasheet says The FSR for the AD7609 is 40 V for the ±10 V range and 20 V for the ±5 V range, so I guessed he chose 20vdescribed in the above and it somehow turned to be 2000???
Does anyone have any clues??
Thanks
-------------------Updated question from here ---------------------
I don't get how 18bit concatenated value of data[0] + 16bit concatenated value of data[1] turn to be microvolt after ADC transfer function.
data[9]
+---+---+--- +---+---+---+---+---+---++---+---+---++---+---+---++
analog volts | 1.902v | 1.921v | 1.887v | 1.934v |
+-----------++-----------+------------+------------+------------+
digital value| 12,464 | 12,589 | 12,366 | 12,674 |
+---+---+---++---+---+---++---+---+---++---+---+---++---+---+---+
I just make an example from data[3:0]
1 resolution = 20v/2^17-1 = 152.59 uV/bit and 1.902v/152.59uv = 12,464
Now get thru concatenation:
temp[0] = ((data[0]<<2)& 0x3FFFC) + ((data[1]>>14)& 0x0003) = C2C0
temp[1] = ((data[1]<<4)& 0x3FFF0) + ((data[2]>>12)& 0x000F) = 312D3
temp[2] = ((data[1]<<6)& 0x3FFC0) + ((data[3]>>10)& 0x003F) = 138C
Then put those into transfer function and get microvolts
adstor[i] = (signed long)(((temp[i]*2000)/131072)*10000);
adstor[0]= 7,607,421 with temp[0] !=1.902*e6
adstor[1]= 30,735,321 with temp[1] != 1.921*e6
adstor[2]= 763,549 with temp[2]
As you notice, they are quite different from the analog value in table.
I don't understand why data need to bit-shifting and <<,>> and added up with two data[]??
Thanks,
Please note that the maximum 18-bit value is 2^18-1 = $3FFFF = 262143
For [2] it appears that s/he splits 18-bit word concatenated values into longs for easier manipulation by step [3].
[3]: Regarding adstor[i] = (signed long)(((temp[i]*2000)/131072)*10000);
To convert from raw A/D reading to volts s/he multiplies with the expected volts and divides by the maximum possible A/D value (in this case, $3FFFF) so there seems to be an error in the code as s/he divides by 2^17-1 and not 2^18-1. Another possibility is s/he uses half the range of the A/D and compensates for that this way.
If you want 20V to become microvolts you need to multiply it by 1e6. But to avoid overflow of the long s/he splits the multiplication into two parts (*2000 and *10000). Because of the intermediate division the number gets small enough to be multiplied at the end by 10000 without overflowing at the expense of possibly losing some least significant bit(s) of the result.
P.S. (I use $ as equivalent to 0x due to many years of habit in certain assembly languages)
Im trying to convert little endian to big endian and having trouble.
I have an array that has 2 bytes of unsigned char which is hex values.
and i would like to make it big endian by the zeros are gone and i cant get the values i want.
For example,
My goal is to get
array[0] = 0b
array[1] = 40
output = 0b40 so i can see this value as 2880 when %d
so i used
output = (array[1]>>4) | (array[0]<<4);
but in reality, i see
printf("output = %x04\n", output);
output = 00b4
I want that output to be 0b40 so i can get 2880 when i do
printf("output = %x\n", output);
output = 0b40
printf("output = %d\n", output);
output = 2880
thanks guys any help will be appreciated
You need to shift the bottom up by eight bits, not four, and not shift the top:
output = (array[0]<<8) | array[1];
See convert big endian to little endian in C for more discussion and code.
This question already has answers here:
Crash on presenting UIImagePickerController under iOS 6.0
(5 answers)
Closed 8 years ago.
I just upgraded from iOS 5.1.1 to 6.0 and the following code path is crashing with SIGTRAP when it attempts to display a popover with an imagePickerViewController as its contentVC to allow the user to select an image or video from the photoLibrary.
It was working great with 5.1.1. I've been troubleshooting this for a couple of days with no headway.
Does anyone have a handle on this? Is there an alternative method to use for iOS6.0? I can add more code if needed...
I can capture images and video with the line:
[self presentViewController:self.imagePickerViewController.imagePickerController animated:YES completion:NULL];
Here is the method in full called after the user touches a UIButton.
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType mediaType:(NSString *)mediaType
{
if (self.capturedMovies.count > 0)
[self.capturedMovies removeAllObjects];
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self.imagePickerViewController setupImagePicker:sourceType mediaType:mediaType];
if (sourceType == UIImagePickerControllerSourceTypeCamera) { // WORKS
[self presentViewController:self.imagePickerViewController.imagePickerController animated:YES completion:NULL];
}
else if (sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
//else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie] || [mediaType isEqualToString:(NSString *)kUTTypeImage]) {
if (!self.moviePickerPopoverController) {
self.moviePickerPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.imagePickerViewController.imagePickerController];
}
UIView *uiViewObject = self.mediaTitleTextField;
CGFloat xLocation = uiViewObject.frame.origin.x;
CGFloat yLocation = uiViewObject.frame.origin.y;
CGFloat width = uiViewObject.bounds.size.width;
CGFloat height = uiViewObject.bounds.size.height;
// CRASHES HERE!!!
[self.moviePickerPopoverController presentPopoverFromRect:CGRectMake(xLocation, yLocation, width, height)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
}
}
Here is what pops up in the code window:
Thread 1: signal SIGTRAP on line 3:
libsystemkernel.dylib`_kill:
0x394e7830: mov r12, #37
0x394e7834: svc #128
0x394e7838: blo 0x394e7850 ; __kill + 32 <<<<<<<<< Thread 1: signal SIGTRAP
0x394e783c: ldr r12, [pc, #4] ; __kill + 24
0x394e7840: ldr r12, [pc, r12]
0x394e7844: b 0x394e784c ; __kill + 28
0x394e7848: ldrdeq r6, r7, [r0], -r12
0x394e784c: bx r12
0x394e7850: bx lr
Here is the backtrace from the debugger:
(lldb) bt
* thread #1: tid = 0x2503, 0x394e7838 libsystem_kernel.dylib`__kill + 8, stop reason = signal SIGTRAP
frame #0: 0x394e7838 libsystem_kernel.dylib`__kill + 8
frame #1: 0x001275d4 MyAppName`TFHandleExceptions + 992
frame #2: 0x36bd357e CoreFoundation`__handleUncaughtException + 614
frame #3: 0x39313a64 libobjc.A.dylib`_objc_terminate() + 128
frame #4: 0x3363c07a libc++abi.dylib`safe_handler_caller(void (*)()) + 78
frame #5: 0x3363c114 libc++abi.dylib`std::terminate() + 20
frame #6: 0x3363d598 libc++abi.dylib`__cxa_rethrow + 88
frame #7: 0x393139d0 libobjc.A.dylib`objc_exception_rethrow + 12
frame #8: 0x36b19f20 CoreFoundation`CFRunLoopRunSpecific + 456
frame #9: 0x36b19d48 CoreFoundation`CFRunLoopRunInMode + 104
frame #10: 0x38ca82ea GraphicsServices`GSEventRunModal + 74
frame #11: 0x3701d300 UIKit`UIApplicationMain + 1120
frame #12: 0x000629b0 MyAppName`main + 96 at main.m:16
This is a duplicate of https://stackoverflow.com/a/12575058/1074338
Also, I should have touched the go button on the debugger...
which revealed the offending code:
*** Terminating app due to uncaught exception
'UIApplicationInvalidInterfaceOrientation',
reason: 'Supported orientations has no common orientation
with the application, and shouldAutorotate is returning YES'