knowing the value of the variable tempStatusSelectorByte is 0, in the if stament it is giving true for tempStatusSelectorByte>4.
uint8_t tempStatusSelectorByte;
tempStatusSelectorByte = AT24C32_Device.readStatusSelectorByte();
printf("settingup -> statusSelectorByte: %d\n", statusSelectorByte); //It prints 0
if(tempStatusSelectorByte>4 ){ // it is true
Please let me know how to solve the problem.
Related
I tried to use GetDiskFreeSpaceExA function, but it doesn't work:
int drvNbr = PathGetDriveNumber(db7zfolderw);
if (drvNbr == -1) // fn returns -1 on error
{
const char * errmsg = "error occured during get drive number";
strcpy_s(retmsg, strlen(errmsg) + 1, errmsg);
return -3;
}
char driverletter = (char)(65 + drvNbr);
string driverstr(1, driverletter);
driverstr = driverstr + ":";
PULARGE_INTEGER freespace = 0;
PULARGE_INTEGER totalnumbtype = 0;
PULARGE_INTEGER totalnumberfreebyte = 0;
fileSize = SzArEx_GetFileSize(&db, i);
BOOL myresult=GetDiskFreeSpaceExA(
driverstr.c_str(),
freespace,
totalnumbtype,
totalnumberfreebyte
);
The value of variable freespace is 0. I have no idea why it didn't work if the value of variable which is driverstr.c_str() was D:?
Thanks for your help.
You need to supply pointers to variables that will hold the value returned. Right now you a re supplying null pointers so nothing is retured:
::ULARGE_INTEGER freespace{};
::ULARGE_INTEGER totalnumbtype{};
::ULARGE_INTEGER totalnumberfreebyte{};
::BOOL myresult
{
::GetDiskFreeSpaceExA
(
driverstr.c_str()
, &freespace
, &totalnumbtype
, &totalnumberfreebyte
)
};
It would also be a good idea to use wide char versions of these functions.
I'm trying to use rust-xcb to get a window's class given a window ID.
fn get_class(conn: &xcb::Connection, id: &i32) {
let window: xcb::xproto::Window = *id as u32;
let class_prop: xcb::xproto::Atom = 67; // XCB_ATOM_WM_CLASS from xproto.h
let cookie = xcb::xproto::get_property(&conn, false, window, class_prop, 0, 0, 2);
match cookie.get_reply() {
Ok(reply) => {
let x: &[std::os::raw::c_void] = reply.value();
println!("reply is {:?}", x[0]);
}
Err(err) => println!("err {:?}", err),
}
}
The documentation is kind of sparse and hasn't been incredibly helpful, though I did find this bit about the GetPropertyReply and of the xcb_get_property_reply_t it wraps.
I looked at this answer in JavaScript but I don't know what the ctypes equivalent in Rust is. I tried just casting the &[c_void] as a &str or String:
...
Ok(reply) => {
let len = reply.value_len() as usize;
let buf = reply.value() as &str;
println!("{}", buf.slice_unchecked(0, len)); // this seems redundant
}
...
but it returns
error: non-scalar cast: `&[_]` as `&str`
I tried casting the &[c_void] as a &[u8] and then collecting the Vec into a String, which sort of works:
...
Ok(reply) => {
let value : &[u8] = reply.value();
let buf : String = value.into_iter().map(|i| *i as char).collect();
println!("\t{:?}", buf);
}
...
but I'm now getting weird results. for example , when I use xprop on Chrome I see "google-chrome" but for me it is only showing "google-c", and "roxterm" is showing up as "roxterm\u{0}". I'm guessing "\u{0}" is something Unicode related but I'm not sure, and I don't know why stuff is being concatenated either. Maybe I have to check the reply again?
Here's my updated function:
fn get_class(conn: &Connection, id: &i32) -> String {
let window: xproto::Window = *id as u32;
let long_length: u32 = 8;
let mut long_offset: u32 = 0;
let mut buf = Vec::new();
loop {
let cookie = xproto::get_property(
&conn,
false,
window,
xproto::ATOM_WM_CLASS,
xproto::ATOM_STRING,
long_offset,
long_length,
);
match cookie.get_reply() {
Ok(reply) => {
let value: &[u8] = reply.value();
buf.extend_from_slice(value);
match reply.bytes_after() {
0 => break,
_ => {
let len = reply.value_len();
long_offset += len / 4;
}
}
}
Err(err) => {
println!("{:?}", err);
break;
}
}
}
let result = String::from_utf8(buf).unwrap();
let results: Vec<&str> = result.split('\0').collect();
results[0].to_string()
}
There were three main parts to this question:
I put xproto::get_property() in a loop so I could check reply.bytes_after() and accordingly adjust long_offset. I think with an appropriate long_length there will usually only be one read, but just being safe.
As #peter-hall said, converting &[u8] -> String should be done using String::from_utf8, which needs a Vec; so I let mut buf = Vec::new() and buf.extend_from_slice over the loop before creating the result string with String::from_utf8(buf).unwrap()
According to this random page WM_CLASS is actually two consecutive null-terminated strings, so I split the result by \0 and grab the first value.
I might've just been looking in the wrong place, but xcb has absolutely terrible documentation..
I am trying to get information about the window in focus. It seems that I get a correct window id from xcb_get_input_focus_reply_t->focus: it stays the same for my Eclipse IDE (56623164) and is another for any other window in focus. However, the value length is always 0 for XCB_ATOM_WM_NAME.
shortened code
cookie = xcb_get_property(c, 0, fr->focus, XCB_ATOM_WM_NAME,
XCB_ATOM_STRING, 0, 0);
if ((reply = xcb_get_property_reply(c, cookie, NULL))) {
int len = xcb_get_property_value_length(reply);
if (len == 0) {
printf("Zero Length\n");
free(reply);
return;
}
printf("WM_NAME is %.*s\n", len, (char*) xcb_get_property_value(reply));
}
Eclipse Debugger
reply xcb_get_property_reply_t * 0x60bd40
response_type uint8_t 1 '\001'
format uint8_t 0 '\0'
sequence uint16_t 2
length uint32_t 0
type xcb_atom_t 0
bytes_after uint32_t 0
value_len uint32_t 0
pad0 unsigned char [12] 0x60bd54
There is no error (I passed and inspected a xcb_generic_error_t). Do you have any idea what could go wrong? Maybe I should use Xlib instead...
This code works for me, it is in js-ctypes but you can ignore that part and see this for API use:
var win = aXcbWindowT;
// console.log('win:', win);
var req_title = ostypes.API('xcb_get_property')(ostypes.HELPER.cachedXCBConn(), 0, win, ostypes.CONST.XCB_ATOM_WM_NAME, ostypes.CONST.XCB_ATOM_STRING, 0, 100); // `100` means it will get 100*4 so 400 bytes, so that 400 char, so `rez_title.bytes_after` should be `0` but i can loop till it comes out to be 0
var rez_title = ostypes.API('xcb_get_property_reply')(ostypes.HELPER.cachedXCBConn(), req_title, null);
// console.log('rez_title:', rez_title);
var title_len = ostypes.API('xcb_get_property_value_length')(rez_title); // length is not null terminated so "Console - chrome://nativeshot/content/resources/scripts/MainWorker.js?0.01966718940939427" will be length of `88`, this matches `rez_title.length` but the docs recommend to use this call to get the value, i dont know why
console.log('title_len:', title_len, 'rez_title.contents.length:', rez_title.contents.length); // i think `rez_title.contents.length` is the actual length DIVIDED by 4, and rez_title_len is not dividied by 4
var title_buf = ostypes.API('xcb_get_property_value')(rez_title); // "title_len: 89 rez_title.contents.length: 23" for test case of "Console - chrome://nativeshot/content/resources/scripts/MainWorker.js?0.01966718940939427"
// console.log('title_buf:', title_buf);
var title = ctypes.cast(title_buf, ctypes.char.array(title_len).ptr).contents.readString();
console.log('title:', title);
ostypes.API('free')(rez_title);
return title;
Sometimes though what is returned by xcb_get_input_focus_reply_t->focus is not the window to act on. I have found that sometimes it doesn't have a title, but if you use xcb_query_tree you can find its parent window probaby has a title:
var req_query = ostypes.API('xcb_query_tree')(ostypes.HELPER.cachedXCBConn(), win);
var rez_query = ostypes.API('xcb_query_tree_reply')(ostypes.HELPER.cachedXCBConn(), req_query, null);
console.log('rez_query.contents:', rez_query.contents);
if (root === -1) {
root = rez_query.contents.root;
}
win = rez_query.contents.parent; // this win should have the title
What is done by (1L<<16u) in following code?
and What is the role of this pointer(&mode_absolute_pos_el.tracking) as an input to a function?
if (command_axis2.cif_mode_state == STATUS__DONE)
{
delta_position = absolute_position - turret_pos.elevation_16bit_pm180deg;
delta_position = MathRange_PlusMinusHalfRange32Bit(delta_position, 1L<<16u);
mode_absolute_pos_el.speed_setpoint = MathTracking_Main(&mode_absolute_pos_el.tracking, delta_position, 0L);
}
This is the complete function as one requested for the complete one:
static int16_t TwinX_AbsPos_Calc_El(int16_t absolute_position)
{
uint16_t position_reached = NO;
int32_t delta_position = 0L;
if (command_axis2.cif_mode_state == STATUS__DONE)
{
delta_position = absolute_position - turret_pos.elevation_16bit_pm180deg;
delta_position = MathRange_PlusMinusHalfRange32Bit(delta_position, 1L<<16u);
mode_absolute_pos_el.speed_setpoint = MathTracking_Main(&mode_absolute_pos_el.tracking, delta_position, 0L);
/* verify that absolute position is reached with tolerance: +/- CUSTOMER_ABSOLUTE_POS_ERROR
* for more than MODE_ABSOLUTE_POS_OK_DELAY_IN_MS:
* bai: That has to be here because otherwise position_reached is always "Yes" because delta_position == 0L */
if ((delta_position < TwinX_MODE_ABSOLUTE_POS_ERROR) && (delta_position > (-1 * TwinX_MODE_ABSOLUTE_POS_ERROR)))
{
position_reached = YES;
}
mode_absolute_pos_el.absolute_pos_reached = MathDebounce_Status(&mode_absolute_pos_el.debounce, position_reached);
}
else
{
mode_absolute_pos_el.speed_setpoint = 0;
MathTracking_SetStartCondition(&mode_absolute_pos_el.tracking, turret_speed.elev_speed_max16bit);
}
return mode_absolute_pos_el.speed_setpoint;
}
And below you can see the MATH_DEBOUNCE :
typedef struct
{
bool_t debounced_status; /* debounced status ZERO / ONE */
uint32_t debounce_counter; /* counter */
uint32_t threshold_for_zero; /* threshold to set debounced status to ZERO */
uint32_t threshold_for_one; /* threshold to set debounced status to ONE */
uint32_t step_down_size; /* step size to count down. used for underclock */
}MATH_DEBOUNCE_t;
void MathDebounce_Init(MATH_DEBOUNCE_t *const debounce_p,
bool_t initial_status,
uint16_t debounce_delay,
uint16_t underclock);
void MathDebounce_ResetStatus(MATH_DEBOUNCE_t *const debounce_p, bool_t reset_status);
bool_t MathDebounce_Status(MATH_DEBOUNCE_t *const debounce_p, bool_t status);
1 << 16 is 2^16. << is a left binary shift operator.
Lets look at how to get 2^3, imagine that you have 1 which in binary form looks like this 0b0001. One point about binary system is that powers of 2 have only one bit in their binary representation, so 2^3 = 8 looks like 0b1000. It's a very fast way to get a power of 2.
Passing pointer to function is usually a way to get more then one output from it. Like when return value is something like error code and actual output of the function is passed through the pointer you pass to it.
But it's hard to tell what exactly it does because you didn't provide the code of the function.
I have a situation in Visual C++ 2008 that I have not seen before. I have a class with 4 STL objects (list and vector to be precise) and integers.
It has a method:
inline int id() { return m_id; }
The return value from this method is corrupt, and I have no idea why.
debugger screenshot http://img687.imageshack.us/img687/6728/returnvalue.png
I'd like to believe its a stack smash, but as far as I know, I have no buffer over-runs or allocation issues.
Some more observations
Here's something that puts me off. The debugger prints right values in the place mentioned // wrong ID.
m_header = new DnsHeader();
assert(_CrtCheckMemory());
if (m_header->init(bytes, size))
{
eprintf("0The header ID is %d\n", m_header->id()); // wrong ID!!!
inside m_header->init()
m_qdcount = ntohs(h->qdcount);
m_ancount = ntohs(h->ancount);
m_nscount = ntohs(h->nscount);
m_arcount = ntohs(h->arcount);
eprintf("The details are %d,%d,%d,%d\n", m_qdcount, m_ancount, m_nscount, m_arcount);
// copy the flags
// this doesn't work with a bitfield struct :(
// memcpy(&m_flags, bytes + 2, sizeof(m_flags));
//unpack_flags(bytes + 2); //TODO
m_init = true;
}
eprintf("Assigning an id of %d\n", m_id); // Correct ID.
return
m_header->id() is an inline function in the header file
inline int id() { return m_id; }
I don't really know how best to post the code snippets I have , but here's my best shot at it. Please do let me know if they are insufficient:
Class DnsHeader has an object m_header inside DnsPacket.
Main body:
DnsPacket *p ;
p = new DnsPacket(r);
assert (_CrtCheckMemory());
p->add_bytes(buf, r); // add bytes to a vector m_bytes inside DnsPacket
if (p->parse())
{
read_packet(sin, *p);
}
p->parse:
size_t size = m_bytes.size(); // m_bytes is a vector
unsigned char *bytes = new u_char[m_bytes.size()];
copy(m_bytes.begin(), m_bytes.end(), bytes);
m_header = new DnsHeader();
eprintf("m_header allocated at %x\n", m_header);
assert(_CrtCheckMemory());
if (m_header->init(bytes, size)) // just set the ID and a bunch of other ints here.
{
size_t pos = DnsHeader::SIZE; // const int
if (pos != size)
; // XXX perhaps generate a warning about extraneous data?
if (ok)
m_parsed = true;
}
else
{
m_parsed = false;
}
if (!ok) {
m_parsed = false;
}
return m_parsed;
}
read_packet:
DnsHeader& h = p.header();
eprintf("The header ID is %d\n", h.id()); // ID is wrong here
...
DnsHeader constructor:
m_id = -1;
m_qdcount = m_ancount = m_nscount = m_arcount = 0;
memset(&m_flags, 0, sizeof(m_flags)); // m_flags is a struct
m_flags.rd = 1;
p.header():
return *m_header;
m_header->init: (u_char* bytes, int size)
header_fmt *h = (header_fmt *)bytes;
m_id = ntohs(h->id);
eprintf("Assigning an id of %d/%d\n", ntohs(h->id), m_id); // ID is correct here
m_qdcount = ntohs(h->qdcount);
m_ancount = ntohs(h->ancount);
m_nscount = ntohs(h->nscount);
m_arcount = ntohs(h->arcount);
You seem to be using a pointer to an invalid class somehow. The return value shown is the value that VS usually uses to initialize memory with:
2^32 - 842150451 = 0xCDCDCDCD
You probably have not initialized the class that this function is a member of.
Without seeing more of the code in context.. it might be that the m_id is out of the scope you expect it to be in.
Reinstalled VC++. That fixed everything.
Thank you for your time and support everybody! :) Appreciate it!