The compilation warnings below are not so clear to me, appart from the
deprecation warhing, but the signature of the method in the valadoc :
http://valadoc.org/#!api=gstreamer-1.0/Gst
shows no other method signature.
the other warning are more obscure.
max#max-ubuntu:~/mdev/cr valac --pkg gstreamer-0.10 gstpipe.vala
/home/max/dev/main-sandbox/cr/gstpipe.vala.c: In function ‘application_message’:
/home/max/dev/main-sandbox/cr/gstpipe.vala.c:64:2: warning: passing argument 1 of ‘_gst_structure_copy0’ discards ‘const’ qualifier from pointer target type [enabled by default]
/home/max/dev/main-sandbox/cr/gstpipe.vala.c:26:17: note: expected ‘gpointer’ but argument is of type ‘const struct GstStructure *’
/home/max/dev/main-sandbox/cr/gstpipe.vala.c:82:9: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
/home/max/dev/main-sandbox/cr/gstpipe.vala.c: In function ‘main’:
/home/max/dev/main-sandbox/cr/gstpipe.vala.c:173:2: warning: ‘g_type_init’ is deprecated (declared at /usr/include/glib-2.0/gobject/gtype.h:669) [-Wdeprecated-declarations]
using Gst;
void application_message(Gst.Bus bus, Gst.Message msg) {
var s = msg.get_structure();
if(s == null)
return;
string msgtype = s.get_name();
if(msgtype != "level")
return;
GLib.Value rms = s.get_value("rms");
//GLib.Value st = s.get_value("stream-time");
GLib.DateTime now = new GLib.DateTime.now_local();
var sec = now.to_unix();
var msec = (sec * 1000) + now.get_microsecond();
var z = rms.strdup_contents();
//z = z.replace("{", "[").replace("}", "]");
stdout.printf("%ld, %s \n", (long) msec, z);
}
void main (string[] args) {
Gst.init (ref args);
try {
var pipeline = Gst.parse_launch(
"pulsesrc device=\"alsa_input.usb-046d_08c9_674634A4-02-U0x46d0x8c9.analog-mono\" ! " +
"level name=wavelevel interval=10000000 ! " +
"wavenc ! filesink location=audioz.wav"
);
var bus = pipeline.get_bus();
bus.add_signal_watch();
bus.message.connect(application_message);
// Set pipeline state to PLAYING
pipeline.set_state (State.PLAYING);
// Creating and starting a GLib main loop
new MainLoop ().run ();
}
catch(Error e) {
print("%s\n", e.message);
}
}
You can generally ignore warnings from the C compiler when using Vala. Vala has better information than the C compiler, so it knows certain things are valid when the C compiler has no way of knowing that. Unfortunately we can't just add casts everywhere since there are situations where we can't generate a valid cast (and, what's more, no way to know what those situations are).
The final warning, about g_type_init being deprecated, is because g_type_init is no longer necessary as of glib 2.36. You can get rid of that warning by passing --target-glib=2.36 (or any later version of glib) to valac, but be warned that the generated code may no longer work with older versions of glib.
TBH, I often just pass -X -w to valac to get the C compiler to be quiet. Occasionally I miss a useful warning, but it gets rid of a lot of useless warnings.
Related
I am trying to write a function in CAPL that takes a signal and calculates the physical value with the signal value, the signal factor and the signal offset.
This is how a simple gateway normally works:
message CAN1.myMessage1 myMessage1 = {DIR = RX};//message from the database
message CAN2.myMessage2 myMessage2 = {DIR = TX};//another message from the database
on message CAN1.*
{
if(this.id == myMessage1.id)
{
myMessage1 = this;
myMessage2.mySignalB = myMessage1.mySignalA * myMessage1.mySignalA.factor + myMessage1.mySignalA.offset;
}
}
And this is what I am trying to do:
...
on message CAN1.*
{
if(this.id ==myMessage1.id)
{
myMessage1 = this;
myMessage2.mySignalB = PhysicalValue(myMessage1.mySignalA);
}
}
double PhysicalValue(signal * s)
{
return s*s.factor+s.offset;
}
There are two problems with this code:
Firstly when I pass the signal as the parameter the compiler says that the types don't match. The second problem is that inside the function the attributes (factor and offset) are no longer recognized.
These problems might have something to do with the weird object-oriented-but-not-really nature of CAPL. The value of the signals can be accessed directly but it also has attributes?
int rawValue = myMessage1.mySignalA;
If you are familiar with C you might say that the problem is that I am specifying a pointer in the function but that I am not passing a pointer into it. But in CAPL there are no pointers and the * simply means anything.
Without the * I would have needed to use a specific signal which would have defeated the purpose of the function.
EDIT:
I have found the attribute .phys by now which does exactly what my demo function would have done.
double physValue = myMessage1.mySignalA.phys;
This has already made my code much shorter but there are other operations that I need to perform for multiple signals so being able to use signals as a function parameter would still be useful.
What you can do is this:
double PhysicalValue(signal * s)
{
// access signal by prepending a $
return $s.phys;
}
Call like this
on message CAN1.*
{
if(this.id ==myMessage1.id)
{
myMessage1 = this;
myMessage2.mySignalB = PhysicalValue(CAN1::myMessage1::mySignalA);
}
}
I.e. when you call your function, you have to provide the qualified name of the signal (with colons rather than dots). To my knowledge it is not possible to use myMessage1.mySignalA, since signals itself are not a CAPL datatype.
Apart from this, you might re-think whether you really should be using on message, but rather switch to on signal. Handling the signal values no matter with which message they are sent is done by CANoe's signal server.
Note that CANoe already has a function which does exactly what you're trying to do (multiplying by factor and adding offset). It's called getSignal:
on message CAN1.*
{
if(this.id == myMessage1.id)
{
myMessage2.mySignalB = getSignal(myMessage1::mySignalA);
}
}
Offsets and factors are defined in e.g. the DBC files.
I want to have a Scilab function which is able to alter its input variables, For example in C I could have
void double(int* x){
*x *= 2;
return;
}
There are intppty, funptr, addinter, istk, sadr and stk in Scilab which seem to be relevant, however I can't find any working example. Scilab does have a pointer type (i.e. 128). I would appreciate if you could help me figure this out.
P.S.1. I have also mirrored this question here on Reddit.
P.S.2. Scilab also have intersci, SWIG, fort, external, call, API_Scilab/gateway which can interface C/C++ functions or Fortran subroutines. Unfortunately intersci has been deprecated and SWIG seems to be only for Linux with limited C++ compatibility.
P.S.3. scilab has function overloading which can do stuff with the functions defined by deff and a combination of %,<...>,_... syntax.
P.S.4. The way API_Scilab/gateway works, is basically you dvelop the code using functionalities provided bu the header file api_scilab.h, compile it with ilib_build, write a loader*.sce script and then load it with exec.
P.S.5. supposedly one should be able to install mingw compiler with
atomsInstall('mingw'); atomsLoad('mingw');
However I am not able to get it to work as I have explained here.
This is possible by using, e.g. a C++ Scilab 6 gateway (example needs a compiler on the machine, it should not be a problem for Linux and OSX users):
gw=[
"#include ""double.hxx"""
"#include ""function.hxx"""
"types::Function::ReturnValue sci_incr(types::typed_list &in, int _iRetCount,"
" types::typed_list &out)"
"{"
" if (in.size() != 1 || in[0]->isDouble() == false) {"
" throw ast::InternalError(""Wrong type/number of input argument(s)"");"
" }"
" types::Double *pDbl = in[0]->getAs<types::Double>();"
" double *pdbl = pDbl->get();"
""
" for (int i=0; i < pDbl->getSize(); i++) (*pdbl) += 1.0;"
""
" return types::Function::OK;"
"}"];
cd TMPDIR;
mputl(gw,TMPDIR+"/sci_incr.cpp");
ulink
ilib_build("incr", ["incr" "sci_incr" "cppsci"],"sci_incr.cpp", [])
exec loader.sce
After compilation/link of the interface, you can have the following behavior:
--> x=1
x =
1.
--> incr(x)
--> x
x =
2.
However, don't consider this as a feature, because the Scilab language has not been designed to use it !
For my understanding this is not possible, in scilab input arguments are in the right side of the function and outputs are on the left side. See https://help.scilab.org/docs/6.0.2/en_US/function.html
[output,...] = function(input,...)
So, if you would like an input/output argument you have to assign the input argument to the output one inside your function.
[c] = f1(a, b)
c = a + b
endfunction
And you call it using same variable as input and output argument:
d = 10;
d = f1(d, 1);
I have used a fifo pipe to read in some data (weather data) into a char variable. The console will display this variable correctly. However, when I try to display it through HTML on the CGI page, it simply does not display. Code below -
int main(void) {
int fd;
char *myfifo = "pressure.txt";
char buff[BUFFER];
long fTemp;
//open and read message
fd = open(myfifo, O_RDONLY);
read(fd, buff, BUFFER);
printf("Received: %s\n", buff);
close(fd);
printf("Content-type: text/html\n\n");
puts("<HTML>");
puts("<BODY>");
printf("Data is: %s", buff);
puts("</BODY>");
puts("</HTML>");
return EXIT_SUCCESS;
}
As you can see in the console is displays correctly -
Received: 2014-08-13 16:54:57
25.0 DegC, 1018.7 mBar
Content-type: text/html
<HTML>
<BODY>
Data is 2014-08-13 16:54:57
25.0 DegC, 1018.7 mBar
</BODY>
</HTML>
logout
But on the CGI webpage it does not display the weather data, but it does display "data is".
Two important things when writing a CGI program:
the program will be run by the webserver, which is normally
started as a different user (the 'www' user for example).
it's possible that the program is started from within another
directory, which can cause different behaviour if you don't
specify the full path of a file you want to open.
Since both these things can cause problems, it can be helpful
to add some debug information. Of course, it's always a good idea
to check return values of functions you use.
To make it easier to display debug or error messages, I'd first
move the following code up, so that all output that comes after
it will be rendered by the browser:
printf("Content-type: text/html\r\n\r\n");
puts("<HTML>");
puts("<BODY>");
It may be useful to know what the webserver uses as the directory
from which the program is started. The getcwd
call can help here. Let's use a buffer of size BUFFER to store
the result in, and check if it worked:
char curpath[BUFFER];
if (getcwd(curpath, BUFFER) == NULL)
printf("Can't get current path: %s<BR>\n", strerror(errno));
else
printf("Current path is: %s<BR>\n", curpath);
The getcwd function returns NULL in case of an error, and sets the value
of errno to a number which indicates what went wrong. To convert this
value to something readable, the strerror
function is used. For example, if BUFFER was not large enough to be
able to store the path, you'll see something like
Can't get current path: Numerical result out of range
The open call returns a negative number
if it didn't work, and sets errno again. So, to check if this worked:
fd = open(myfifo, O_RDONLY);
if (fd < 0)
printf("Can't open file: %s<BR>\n", strerror(errno));
In case the file can be found, but the webserver does not have permission
to open it, you'll see
Can't open file: Permission denied
If the program is started from another directory than you think, and
it's unable to locate the file, you would get:
Can't open file: No such file or directory
Adding such debug info should make it more clear what's going on, and more
importantly, what's going wrong.
To make sure the actual data is read without problems as well, the return
value of the read function should be
checked and appropriate actions should be taken. If read fails,
a negative number is returned. To handle this:
numread = read(fd, buff, BUFFER);
if (numread < 0)
printf("Error reading from file: %s<BR>\n", strerror(errno));
Another value indicates success, and returns the number of bytes that were
read. If really BUFFER bytes were read, it's not at all certain that the
last byte in buff is a 0, which is needed for printf to know when the
string ended. To make sure it is in fact null-terminated, the last byte in
buff is set to 0:
if (numread == BUFFER)
buff[BUFFER-1] = 0;
Note that this actually overwrites one of the bytes that were read in this
case.
If fewer bytes were read, it's still not certain that the last byte that was
read was a 0, but now we can place our own 0 after the bytes that were read
so none of them are overwritten:
else
buff[numread] = 0;
To make everything work, you may need the following additional include files:
#include <unistd.h>
#include <string.h>
#include <errno.h>
The complete code of what I described is shown below:
int main(void)
{
int fd, numread;
char *myfifo = "pressure.txt";
char buff[BUFFER];
char curpath[BUFFER];
long fTemp;
// Let's make sure all text output (even error/debug messages)
// will be visible in the web page
printf("Content-type: text/html\r\n\r\n");
puts("<HTML>");
puts("<BODY>");
// Some debug info: print the current path
if (getcwd(curpath, BUFFER) == NULL)
printf("Can't get current path: %s<BR>\n", strerror(errno));
else
printf("Current path is: %s<BR>\n", curpath);
// Open the file
fd = open(myfifo, O_RDONLY);
if (fd < 0)
{
// An error occurs, let's see what it is
printf("Can't open file: %s<BR>\n", strerror(errno));
}
else
{
// Try to read 'BUFFER' bytes from the file
numread = read(fd, buff, BUFFER);
if (numread < 0)
{
printf("Error reading from file: %s<BR>\n", strerror(errno));
}
else
{
if (numread == BUFFER)
{
// Make sure the last byte in 'buff' is 0, so that the
// string is null-terminated
buff[BUFFER-1] = 0;
}
else
{
// Fewer bytes were read, make sure a 0 is placed after
// them
buff[numread] = 0;
}
printf("Data is: %s<BR>\n", buff);
}
close(fd);
}
puts("</BODY>");
puts("</HTML>");
return EXIT_SUCCESS;
}
I use the following code for Text-To-Speech application controls for blind persons in C++ Builder (most likely similar example can be used in Delphi). Main form has KeyPreview property checked to enable key F11 preview to start speaking active (focused) control. The code as it is works but there are some problems. This example is in C++ Builder code but from what I've found, Delphi suffers from same problem and the solution I found is the same. If you have Delphi solution, feel free to post it, it is similar anyway.
#include <sapi.h>
#include <WTypes.h>
//---------------------------------------------------------------------------
// Speak text string (synchronous function)
//---------------------------------------------------------------------------
bool SpeakText(UnicodeString Text)
{
ISpVoice* pVoice = NULL;
if (FAILED(::CoInitialize(NULL))) return false;
Word Saved8087CW = Default8087CW; // Disable floating point division by zero exception caused by Speak
Set8087CW(0x133f);
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if (SUCCEEDED(hr))
{
//pVoice->SpeakCompleteEvent()
//pVoice->SetSyncSpeakTimeout(1000);
hr = pVoice->Speak(WideString(Text).c_bstr(), SPF_DEFAULT, NULL);
pVoice->Release();
pVoice = NULL;
}
Set8087CW(Saved8087CW);
::CoUninitialize();
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
UnicodeString Speaker;
if (Key == VK_F11)
{
if (Screen->ActiveControl->InheritsFrom(__classid(TButton))) { Speaker += "Button, " + static_cast<TButton*>(Screen->ActiveControl)->Caption + "."; }
else if (Screen->ActiveControl->InheritsFrom(__classid(TEdit))) { Speaker += "Edit box, " + static_cast<TEdit*>(Screen->ActiveControl)->Text + "."; }
}
if (Speaker != "") SpeakText(Speaker);
}
//---------------------------------------------------------------------------
Problems:
pVoice->Speak causes Floating point division by zero if I don't override the exception using the Set8087CW function. This happens only on Windows 7 (possibly Vista and Windows 8 too) but not on Windows XP in the same program (compiled exe). Is there a solution without using Set8087CW? Removing these lines will cause the problem and exception. I have BCB2010.
Function is synchronous and won't shut up or return control to program until it finishes reading text. This is a problem for longer text. It also blocks program events. Is there a way to make it asynchronous or introduce an event to periodically check for F11 key status and if F11 is pressed again it stops reading and uninitializes object? For example poll every 300 ms (or after each word etc.) for key-press F11 and if pressed, stop speaking? Or run it threaded?
Does SAPI has memory leaks as some write on various sites?
Can above code use OleCheck instead of CoCreateInstance and CoUninitialize?
UPDATE for those looking for solution as suggested by Remy Lebeau:
SavedCW = Get8087CW();
Set8087CW(SavedCW | 0x4);
hr = pVoice->Speak(WideString(Text).c_bstr(), SPF_DEFAULT | SPF_ASYNC, NULL);
pVoice->WaitUntilDone(-1); // Waits until text is done... if F11 is pressed simply go out of scope and speech will stop
Set8087CW(SavedCW);
Also found detailed example in CodeRage 4 session: http://cc.embarcadero.com/item/27264
The error does occur in Vista as well. Masking floating point exceptions is the only solution.
To make Speak() run asynchronously, you need to include the SPF_ASYNC flag when calling it. If you need to detect when asynchronous speaking is finished, you can use ISpVoice::WaitUntilDone(), or call ISpVoice::SpeakCompleteEvent() and pass the returned HANDLE to one of the WaitFor...() family of functions, like WaitForSingleObject().
What kind of leaks do other sites talk about?
Not instead of, no. OleCheck() merely checks the value of an HRESULT value and throws an exception if it is an error value. You still have to call COM functions that return the actual HRESULT values in the first place. If anything, OleCheck() would be a replacement for SUCCEEDED() instead.
For what you are attempting, I would suggest the following approach instead:
struct s8087CW
{
Word Saved8087CW;
s8087CW(Word NewCW)
{
Saved8087CW = Default8087CW;
Set8087CW(NewCW);
// alternatively, the VCL documentation says to use SetExceptionMask() instead of Set8087CW() directly...
}
~s8087CW()
{
Set8087CW(Saved8087CW);
}
};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
::CoInitialize(NULL);
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
if (pVoice) pVoice->Release();
::CoUninitialize();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
if (Key == VK_F11)
{
TWinControl *Ctrl = Screen->ActiveControl;
if (Ctrl)
{
TButton *btn;
TEdit *edit;
if ((btn = dynamic_cast<TButton*>(Ctrl)) != NULL)
SpeakText("Button, " + btn->Caption);
else if ((edit = dynamic_cast<TEdit*>(Ctrl)) != NULL)
SpeakText("Edit box, " + edit->Text);
}
}
}
//---------------------------------------------------------------------------
ISpVoice* pVoice = NULL;
bool __fastcall TForm1::SpeakText(const String &Text)
{
s8087CW cw(0x133f);
if (!pVoice)
{
if (FAILED(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice)))
return false;
}
SPVOICESTATUS stat;
pVoice->GetStatus(&stat, NULL);
while (stat.dwRunningState == SPRS_IS_SPEAKING)
{
ULONG skipped;
pVoice->Skip(L"SENTENCE", 1000, &skipped);
pVoice->GetStatus(&stat, NULL);
}
return SUCCEEDED(pVoice->Speak(WideString(Text).c_bstr(), SPF_ASYNC, NULL));
}
I'm implementing fluid simulator using PhysiX. Unfortunately sth is wrong with cuda context manager and I have a problem with recognizing what it is. I have an init method which looks like this:
void InitializePhysX() {
bool recordMemoryAllocations = true;
const bool useCustomTrackingAllocator = true;
PxAllocatorCallback* allocator = &gDefaultAllocatorCallback;
PxErrorCallback* error = &gDefaultErrorCallback;
PxFoundation* mFoundation = PxCreateFoundation(PX_PHYSICS_VERSION, *allocator, *error);
if(!mFoundation)
printf("PxCreateFoundation failed!\n");
PxProfileZoneManager* mProfileZoneManager = &PxProfileZoneManager::createProfileZoneManager(mFoundation);
if(!mProfileZoneManager)
printf("PxProfileZoneManager::createProfileZoneManager failed!\n");
#ifdef PX_WINDOWS
pxtask::CudaContextManagerDesc cudaContextManagerDesc;
pxtask::CudaContextManager* mCudaContextManager = pxtask::createCudaContextManager(*mFoundation, cudaContextManagerDesc, mProfileZoneManager);
if( mCudaContextManager ){
if( !mCudaContextManager->contextIsValid() ){
mCudaContextManager->release();
mCudaContextManager = NULL;
printf("invalid context\n");
}
} else {
printf("create cuda context manager failed\n");
}
#endif
mPhysX = PxCreatePhysics(PX_PHYSICS_VERSION, *mFoundation, PxTolerancesScale(), recordMemoryAllocations, mProfileZoneManager);
if(!mPhysX)
printf("PxCreatePhysics failed!\n");
...
}
When I try to run my application it occures that mCudaContextManger is never created properly. "create cuda context manager failed" is being wrote on the console and:
"....\LowLevel\software\src\PxsContext.cpp (1122) : warning : GPU operation faied. No px::CudaContextManager available.
....\SimulationController\src\particles\ScParticleSystemSim.cpp (73) : warning : GPU particle system creation failed. Falling back to CPU implementation."
I have GeForce560Ti with newest driver (error also shows up on GeForce460 on my friend's laptop). Physix is set to use GPU in NVidia Control Panel.
Does anybody know what we made wrong and how to make GPU work? Thanks in advance!
File PhysX3Gpu_x86.dll was missing. I added it and now everything is fine.