Still a little new to C/C++ but I have a question about Boolean variables and calling a subroutine with them as a parameter. If I have the function:
void DAC_Write(BYTE number, WORD level, bool reset, bool dir, bool all, bool change, bool step, bool small_step, bool kill);
and in my calling function I have say:
bool reset = true;
instead of calling the function like this:
DAC_Write(0, 0, true, false, false, false, false, false, false);
can I call this instead:
DAC_Write(0, 0, reset, false, false, false, false, false, false);
or is that not proper / or just completely wrong? Just trying to minimize my code a bit...
Related
I'm making a todo app and don't know what is going bad on this. The user can push the button as many time as he want and I want only to function once. How can I do?
onTap: () {
isComplet = !isComplet;
if (Todo().isComplet = true) {
DatabaseService().completTask(todos[index].uid);
}
if (Todo().isComplet = false) {
DatabaseService().uncompletTask(todos[index].uid);
}
},
I posted before the entire code. If it can help, here's the link to the other question. Thanks!
How can I make that the on ttap button only be pressed one time? - Flutter
There are two things.
1.) Denounce on Tap to prevent multiple tap:
bool _isExecuting = false;
void debounce(Function action) async {
if (!_isExecuting) {
_isExecuting = true;
action();
await Future.delayed(Duration(milliseconds: 500), () {
_isExecuting = false;
});
}
}
onTap: (){debounce((){
// Your code
})}
2.) Your Logic with tap:
onTap: flag ? () { True case function } : () { False case function },
This type of functionality isn't the best user experience in my point of view, you can use a loading spinner until the process finishes.
But if you want to do that as you said you can do something like below:
(You should use async programming if operations are asynchronous)
First of all, you should define a boolean called tapped or whatever you wanna call with a default value of false,
onTap: (){
if(tapped) return;
tapped = true;
isComplet = !isComplet;
if (Todo().isComplet = true)
{DatabaseService().completTask(todos[index].uid);}
if (Todo().isComplet = false)
{DatabaseService().uncompletTask(todos[index].uid);}
tapped = !tapped;
},
Welcome to Stack Overflow, to execute the function only once, just have a bool variable and set it to false at the start of the widget, possibly in initState() or the builder function of StreamBuilder in case you are building multiple buttons in the same widget
int _buttonPressedOnce = false;
and modify your onTap() function a bit
onTap: () {
if (!_buttonPressedOnce) {
_buttonPressedOnce = true;
isComplet = !isComplet;
if (Todo().isComplet = true) {
DatabaseService().completTask(todos[index].uid);
}
if (Todo().isComplet = false) {
DatabaseService().uncompletTask(todos[index].uid);
}
}
},
I need to execute an ajax call after the user moves a node in a jsTree using the dnd plugin. If that ajax call returns an error, I need to either prevent the move from happening, or reverse the action altogether. From the move_node event listener, is there a way to tell the dnd plugin to reverse the drop or somehow cancel the action? I've tried returning false and setting e.preventDefault() and e.stopImmediatePropagation() but nothing seems to work.
$($tree).jstree({
core: {
data: $treeData,
check_callback: function (op, node, parent, position, more) {
switch (op) {
case 'move_node':
// moveNode() validates the move based on
// some data stored in each node, but doesn't
// (and shouldn't) make an ajax call to determine
// if the node *actually can* be moved
return moveNode(node, parent, position, more);
}
}
},
plugins: ['dnd']
}).on('move_node.jstree', function(e, data) {
// moveNode() returned true and the user moved the node
if (!doMoveNode(data)) {
// none of this works!!
e.preventDefault();
e.stopImmediatePropagation();
return false;
}
});
I figured this out... the key is to intercept the move before it happens, which can be done in the check_callback as so:
function doMoveNode (node, parent) {
// tell the server to move the node
var nodeID = node.original.appData.id,
parentID = parent.original.appData.id,
success = false;
// make a non-async call to move the node
$.ajax({
url: move-product.php',
type: 'post',
async: false,
data: {
nodeID: nodeID,
parentID: parentID
},
dataType: 'json',
success: function (json) {
if (json.errorCode === 0) {
// this happens when the server was happy
success = true;
}
},
error: function (xhr) {
alert('an error occurred');
}
});
return success;
}
function moveNode (node, parent, position, more) {
// perform client-side validation to see if we can move node into parent
// just a demo, so always return true
return true;
}
$($tree).jstree({
core: {
data: $treeData,
check_callback: function (op, node, parent, position, more) {
switch (op) {
case 'move_node':
if (more && more.core) {
// this is the condition when the user dropped
// make a synchronous call to the server and
// return false if the server rejects the action
return doMoveNode(node, parent);
}
// return true if we can move, false if not
return moveNode(node, parent, position, more);
}
}
},
plugins: ['dnd']
}).on('move_node.jstree', function(e, data) {
// the move already happened
});
I'm adding two instances of Leaflet Draw (https://github.com/Leaflet/Leaflet.draw) like this (only using lines):
var drawControl = new L.Control.Draw({
draw: {
polygon: false,
rectangle: false,
circle: false,
marker: false
}
});
map.addControl(drawControl);
var drawControl2 = new L.Control.Draw({
draw: {
polygon: false,
rectangle: false,
circle: false,
marker: false
}
});
map.addControl(drawControl2);
Now I want to listen to the draw:drawvertex event and do different things depending on if I had activated the drawControl or drawControl2:
map.on('draw:drawvertex', function (e) {
console.log("Vertex drawn", e);
});
How can I differentiate which drawControl is currently active?
Here is a dirty way to know which drawControl is active.
The trick is to put them in different map corners. It helps to check which ul.leaflet-draw-actions is visible when the user draws. The one in div.leaflet-top or the one in div.leaflet-bottom for example :
var drawControl = new L.Control.Draw({
position: 'topleft',
draw: {
polygon: false,
rectangle: false,
circle: false,
marker: false
}
});
map.addControl(drawControl);
var drawControl2 = new L.Control.Draw({
position: 'bottomleft',
draw: {
polygon: false,
rectangle: false,
circle: false,
marker: false
}
});
map.addControl(drawControl2);
map.on('draw:drawvertex', function (e) {
console.log("Vertex drawn", e);
if ($('div.leaflet-top ul.leaflet-draw-actions').is(':visible')){
console.log('it was drawn with drawControl');
}
else {
console.log('it was drawn with drawControl2 !');
}
});
It's dirty but it works.
With Protractor, and using Mocha framework, I am comparing two arrays of values, one from a bar chart, one from text fields.
The code looks like this:
it("Each bar should have a value equal to its percentage", () => {
var identicalValue: boolean = false;
helper.getFirstValues(textLocator).then((textValue) => {
helper.getBarValues(barLocator).then((barValue) => {
identicalValue = helper.compareArray(textValue, barValue);
//compareArray returns a boolean, true if the arrays have the same values
expect(identicalValue).to.equal(true);
});
});
});
the functions are coded this way:
public getFirstValues(factsheetTab: protractor.ElementFinder): webdriver.promise.Promise<{}> {
var deferred = protractor.promise.defer();
factsheetTab.all(by.tagName("tr")).map((ele, index) => {
return {
index: index,
text: ele.all(by.tagName("td")).first().getText()
}
}).then((topValue) => {
deferred.fulfill(topValue);
},
(reason) => { deferred.reject(reason) });
return deferred.promise;
};
public getBarValues(factsheetTab: protractor.ElementFinder): webdriver.promise.Promise<{}> {
var deferred = protractor.promise.defer();
factsheetTab.all(by.tagName("tr")).map((ele, index) => {
return {
index: index,
text: ele.all(by.tagName("td")).last().element(by.tagName("div")).getAttribute("data-value")
}
}).then((barValue) => {
deferred.fulfill(barValue);
},
(reason) => { deferred.reject(reason) });
return deferred.promise;
};
My problem is that when the comparison returns false, so when the two arrays have differences, the test is blocked. It doesn't fail, the browser remains opened on that step, and the process stops, ignoring the remaining tasks.
Note: the function helper.compareArray returns a correct value. I could also write "expect(false).to.equal(true)" and get blocked too.
Am I doing something wrong in this test? Do you see a reason why this test is not finished?
edit: I found a workaround, to not get stuck in case of the test failing:
it("Each bar should have a value equal to its percentage", () => {
var identicalValue: boolean = false;
var textValue = null;
helper.getFirstValues(textLocator).then((value) => {
textValue = value;
});
helper.getBarValues(barLocator).then((barValue) => {
chai.assert.deepEqual(barValue, textValue);
});
});
(using #Brine's suggestion, for the deepEqual)
This seems to work, the other tests are ran if this one fails.
I'm still curious to know what was wrong with the first version though.
Not sure if this is a bug in your helper.compareArray or Mocha... but why not compare the arrays directly? I don't use Mocha but it seems something like this would work:
expect(textValue).deepEqual(barValue);
I am writing a custom validation rule to check if the "category_id" passed to my create function is valid or not.
types: {
isValidCategoryId: function(id){
return Category.findOne({id: id}).exec(function(err, user){
if(err || !user)
return false;
else{
return true;
}
});
}
},
attributes: {
category_id : { isValidCategoryId: true, required: true, type: 'string' },
}
I understand that my custom validation function should return true, but in an asynchronous context, this may not work, like checking the value in DB.
How should I write my custom validation function to make it behave correctly?
I tried particlebanana's solution. It didn't work but at least it pointed me in the right direction.
According to the docs:
Validation rules may be defined as simple values or functions (both sync and async) that return the value to test against.
So, one easy way to do this would be:
attributes: {
category_id : {
required: true,
type: 'string',
'true': function(cb) {
Category.findOne({id: this.category_id}).exec(function(err, category){
return cb(!err && category);
});
}
}
}
As you can see, I'm just using the "true" validator here, but you could of course write your own validator to work out some more advanced logic. The key here is that your custom validators aren't async, but your validation rules can be. Yes, the terminology is very confusing.
Here's another example with a custom validator:
attributes: {
author: {
required: true,
type: 'string',
canPost: function(cb) {
Author.findOne(this.author).exec(function(err, author) {
return cb(author);
});
}
}
},
types: {
canPost: function(id, author) {
return author && author.canPost();
}
}
Hopefully that makes sense. If not, See the docs.
You can pass in a callback and return the result. It's a bit weird because it doesn't look like it follows the (err, result) standard but instead just uses (result). Give this a try:
types: {
isValidCategoryId: function(id, cb){
return Category.findOne({id: id}).exec(function(err, user){
if(err || !user)
return cb(false);
else{
return cb(true);
}
});
}
},