I changed that motor control code, it works as expected!
Code: Select all
// mt-demo.c
// 0.61
// Multitasking-Demo:
// 3 Motoren an die Motorausgänge A,B,C anschließen !
// die Motoren werden automatisch angesteuert,
// die Encoderwerte werden simultan angezeigt!
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include "ev3_constants.h"
#include "ev3_command.h"
#include "ev3_button.h"
#include "ev3_timer.h"
#include "ev3_lcd.h"
#include "ev3_sound.h"
#include "ev3_output.h"
#define clreol DRAW_OPT_CLEAR_EOL
volatile int flag_threadrun = 0;
//------------------------------------------------------------------------------
void *DisplayValues(void *threadid) {
while(flag_threadrun) {
TextOutEx(0, 1, "Enc.A:", clreol); NumOut(42, 1, MotorRotationCount(OUT_A));
TextOutEx(0, 2, "Enc.B:", clreol); NumOut(42, 2, MotorRotationCount(OUT_B));
TextOutEx(0, 3, "Enc.C:", clreol); NumOut(42, 3, MotorRotationCount(OUT_C));
Wait(40);
}
pthread_exit(0);
}
//------------------------------------------------------------------------------
inline unsigned long Random(unsigned long x) {
return (rand() % x);
}
inline void Motor(int outputs, int pwr) {
SetPower(outputs, pwr);
On (outputs);
}
void *MotorControl(void *threadid) {
int speed;
while(flag_threadrun) {
speed=Random(201) - 100; // ergibt eine Zufallszahl für die Motorleistung zwischen -100 und +100
Motor(OUT_A, speed);
speed=Random(201) - 100;
Motor(OUT_B, speed);
speed=Random(201) - 100;
Motor(OUT_C, speed);
Wait( 200+ (Random(2801)) ); // ergibt eine Zufallszahl für die Aktionsdauer von 200 - 3000 ms
}
pthread_exit(0);
}
//------------------------------------------------------------------------------
main() {
unsigned short btn;
// initialize
OutputInit();
LcdInit();
ButtonLedInit();
ResetAllTachoCounts(OUT_ABCD);
flag_threadrun=1;
pthread_t f2_thread, f1_thread;
pthread_create(&f1_thread,NULL,MotorControl,NULL);
pthread_create(&f2_thread,NULL,DisplayValues,NULL);
while(1) {
if (ButtonPressed(btn)) break;
Wait(100);
}
flag_threadrun=0;
pthread_join(f1_thread,NULL);
pthread_join(f2_thread,NULL);
OutputClose();
OutputExit();
ButtonLedClose();
ButtonLedExit();
LcdExit();
}
Can you (or anyone else) provide me with a code snippet which mimics the keypressed() function and which I can use directly in my test code, just returning TRUE if any btn was hit and FALSE instead? In general I would appreciate some pattern like
if ( keypressed() ) {btn=getchar();} // but for btn instead of char or key, so actually more like
if ( btnpressed() ) {btn=getbtn();}
I'll try your LcdText functions as you suggested but help on this issue by completed source code would also be very much appreciated. Otherwise, I also may wait until the implementation of TextOut/NumOut or even printf for a LCD stdout console has been completed.
As TextOut has not been implemented right now: what about using real x,y pixel coordinates instead of x-pixel and y-line-numbers? It would be of more universal or general functionality later on.