defer(); function fails to compile on CBC v2

3 replies [Last post]
Title: NooBot
Joined: 05/06/2012
BotPoints: 6
User offline. Last seen 11 years 1 week ago.

On CBC when trying to compile, error below. This program compiles fine on OS X.

Compilling /mnt/user/co/motor02.c
/tmp/cc5F4Tof.o: In function 'black_pressed';
motor02.c:(.text+0x2b4): undefined reference to 'defer'
collect2: Id returned 1 exit status
Compile failed.

Related. Doing any background process, for example to test a button or sensor, seems to significantly slow down main routine code. For example two motor off() calls in a row, the second will occur significantly later than first. So was expecting defer() to improve this, however above compile error. The defer() call is in this subprocess function.

Thx for input!

Beta's picture
Title: The Magnificent
Joined: 02/24/2012
BotPoints: 266
User offline. Last seen 9 years 15 weeks ago.

Defer is actually not implemented on the CBCv2. It is a relic from the XBC. It is pretty simple to implement it, however:

  1. #include <pthread.h>
  3. void defer()
  4. {
  5. pthread_yield();
  6. }

It may be necessary to wrap it in the following preprocessor macro:

  1. #ifdef __linux__
  2. #include <pthread.h>
  3. void defer()
  4. {
  5. pthread_yield();
  6. }
  7. #else
  8. void defer() {}
  9. #endif

I haven't tested this yet, but that should do the trick. Let me know if you have any success.


Braden McDorman

Developer of the KIPR Link, KISS IDE, KIPR's 2D Simulator, and CBCJVM.

Reach me at bmcdorman(cat)kipr(dog)org where (cat)=@ and (dog)=. if you need assistance of any kind.

Jeremy Rand
Jeremy Rand's picture
Title: Botball Youth Advisory Council
Joined: 04/03/2009
BotPoints: 1168
User offline. Last seen 8 years 1 week ago.

I've used msleep(5) to simulate a defer(). Probably not as good as Braden's suggestion, but seems to work.

-Jeremy Rand
Senior Programmer, Team SNARC (2012-2013), Norman Advanced (2010-2011), Norman HS (2008-2009), Norman North (2005-2007), Whittier MS (2003-2004)
2012-2013 VP of Tech, 2011 President, Botball YAC (2009-2013)
Mentor, Alcott and Whittier MS

Title: NooBot
Joined: 04/27/2015
BotPoints: 2
User offline. Last seen 8 years 5 weeks ago.

Unfortunately, due to the way the "console" is implemented in cbcui, it isn't possible to clear. The "clearing" done right now is kind of a hack, as you've noticed. I don't think there is a function that gives you more control than cbc_printf. There are ways to start a program over ssh, which would give you correct output on a remote terminal, since ssh is a full blown terminal emulator, but that is beyond the scope of what KIPR supports.