为非 ARC 的 iOS 开发提供的依赖项,使用Sofmodem Arduino 库,并具有 FSK 通信。
这些库有一个假设与 Arduino 使用 Sofmodem 附加板* 通信,并通过 FSK 与 iOS 通信。目前,SoftModem 的源代码未作为框架创建。如果要在项目中使用 SoftModem,则必须从 SoftModemTerminal 的源代码中复制与 SoftModem 相关的源代码。以下是与 SoftModem 相关的源代码列表。请将这些复制到项目源代码中。
* AudioQueueObject.h
* AudioQueueObject.m
* AudioSignalAnalyzer.h
* AudioSignalAnalyzer.m
* AudioSignalGenerator.h
* AudioSignalGenerator.m
* CharReceiver.h
* FSKModemConfig.h
* FSKByteQueue.h
* FSKRecognizer.h
* FSKRecognizer.mm
* FSKSerialGenerator.h
* FSKSerialGenerator.m
* lockfree.h
* MultiDelegate.h
* MultiDelegate.m
* PatternRecognizer.h
SoftModem 使用以下两个框架进行音频输入和输出。请将它们添加到项目中。
* AudioToolbox.framework
* AVFoundation.framework
首先,使用 AVAudioSession 类设置应用程序的类别。要进行音频录制和播放,必须设置 AVAudioSessionCategoryPlayAndRecord。
AVAudioSession *session = [AVAudioSession sharedInstance];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruption:) name:
AVAudioSessionInterruptionNotification object:nil];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
[session setActive:YES error:nil];
中断选择器方法
- (void) interruption:(NSNotification*)notification
{
NSDictionary *interuptionDict = notification.userInfo;
NSUInteger interuptionType = (NSUInteger)[interuptionDict valueForKey:AVAudioSessionInterruptionTypeKey];
if (interuptionType == AVAudioSessionInterruptionTypeBegan)
[self beginInterruption];
#if __CC_PLATFORM_IOS >= 40000
else if (interuptionType == AVAudioSessionInterruptionTypeEnded)
[self endInterruptionWithFlags:(NSUInteger)[interuptionDict valueForKey:AVAudioSessionInterruptionOptionKey]];
#else
else if (interuptionType == AVAudioSessionInterruptionTypeEnded)
[self endInterruption];
#endif
}
接下来,对于音频分析,创建 AudioSignalAnalyzer,FSKRecognizer 的实例。AudioSignalAnalyzer 解析来自麦克风的输入波形,以检测波形的下降和上升沿。FSKRecognizer 根据 AudioSignalAnalyzer 的分析结果恢复数据位。
recognizer = [[FSKRecognizer alloc] init];
[recognizer addReceiver:self];
analyzer = [[AudioSignalAnalyzer alloc] init];
[analyzer addRecognizer:recognizer];
然后为声音输出创建 FSKSerialGenerator 类的实例。FSKSerialGenerator 将数据位转换为音频信号并进行输出。
generator = [[FSKSerialGenerator alloc] init];
[generator play];
将实现 CharReceiver 协议的类注册到 FSKRecognizer 类和 AVAudioSessionDelegate。
@interface YourClass : NSObject <AVAudioSessionDelegate, CharReceiver>
在初始化时注册 FSKRecognizer 类。
YourClass *yourClassInstance;
[recognizer addReceiver: yourClassInstance];
当接收到一个字节的数据时,会调用 receivedChar: 方法。
- (void) receivedChar: (char) input
{
// Receive handling
}
发送数据比接收数据简单得多。使用 FSKSerialGenerator 类的 writeByte: 方法发送单个字节。
[generator writeByte: 0xff];
arms22 - Softmodem 硬件、Arduino 库和 iOS 的 ARC 版本库的创建者。