简体   繁体   English

实时回放定时事件(毫秒级精度,或几乎)

[英]Play back timed events in real time (millisecond accuracy, or almost)

I'm writing an OS X app that records real time events that come from a serial port and saves them to disk. 我正在编写一个OS X应用程序,该应用程序记录来自串行端口的实时事件并将其保存到磁盘。 So each millisecond I get 7 integers that I need to save and later "play back" by drawing them in a sort of live line chart. 因此,每毫秒,我都会得到7个需要保存的整数,然后通过在一种实时折线图中绘制它们来“播放”。 The saving stuff to disk is already done; 将内容保存到磁盘已经完成; playing them back is where I'm stuck. 播放它们是我遇到的困难。

I've done this sort of thing before with other technologies (Arduino, Corona, Processing) where I get a main game loop in which I can do whatever I want time-wise. 在使用其他技术(Arduino,Corona,Processing)之前,我已经做过这类事情,在该技术中,我得到了一个主游戏循环,在该循环中,我可以按时进行任何想做的事情。 What I need to know is how set up some sort of 'onEnterFrame' loop to be able to compare the elapsed time to the first event in my stack and fire it when needed. 我需要知道的是如何设置某种“ onEnterFrame”循环,以便将经过的时间与我的堆栈中的第一个事件进行比较,并在需要时将其触发。 In this loop I would need to tell my views to update themselves and I know Apple guides state that UI updates must happen in the main thread. 在此循环中,我需要告诉自己的视图进行更新,并且我知道Apple指南指出UI更新必须在主线程中进行。

I'm a little lost on how to conceptually achieve this kind of onEnterFrame loop in Cocoa as the paradigm is quite different to what I already know. 我对如何在概念上在Cocoa中实现这种onEnterFrame循环有些困惑,因为范例与我已经知道的完全不同。

Of the many things I encountered, this gave me good timing accuracy in my initial tests (1/10th of a millisecond error): 在我遇到的许多事情中,这为我的初始测试提供了良好的定时精度(1/10毫秒的错误):

func test() {
  print("test")
}

// Run test() every 1 millisecond
let timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: #selector(test), userInfo: nil, repeats: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM