How to add a string to this function in Arduino C? - arduino

I've been toying with this code, and been looking up solutions to this problem to no avail. I was told you were to add a string to a function like so in Arduino C++:
myFunction(String myString)
I've seen examples using this as well. This is the chunk of code that is having issues.
if (piezoV >= 0.25) {
Serial.println(piezoV);
// Serial.println(F("Playing track " + tracknum +""));
String file = String(trackid) + String(tracknum) + String(ext);
musicPlayer.playFullFile(String file);
int tracknum = tracknum + 1;
}
The code fetches this error:
musicPlayer.playFullFile(String file); - expected primary-expression before 'file'
I'm new to Arduino C++, and I am using experience from other languages I've learned to help, but I was wondering if SO could help me with this. Thanks in advance.

musicPlayer.playFullFile(String file);
is not a valid way to call a function in C++. You declare the function with types:
void playFullFile(String file) {
doWhateverIsNeededWith(file);
}
but you call it without the type:
String file = "./pax_singing_badly_in_shower.mp3"; // torture user :-)
musicPlayer.playFullFile(file);

Related

Cannot convert value of type 'UnsafeBufferPointer' to expected argument type 'rawPointer'

extension NSData {
func hexString() -> String {
return UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(bytes), count: length)
.reduce("") { $0 + String(format: "%02x", $1) }
}
}
Since Xcode 11 getting the Mac address from hex in swift is just making me loopy. I was told this was a good way to do it, but all I get are errors.
I get the MAC address incased in a string from a Ble device connecting to iOS , of course in 11, 12 and under I get the Mac address in the format I need.
I get the Mac address like this from the Ble device. <hexvalue>
I have code that removes the < and > that works, but ever since the updates Apple did, I know get this as an example : "{length = 6, bytes = 0x686578636172}\n
How do I just simply remove the extra crap now? all I need is the hex. I can convert that to the Mac by inserting :, that's easy. debugDescription doesn't work. .description doesn't work and I understand it was never intended to be used that way, but I am plopped into a code base that uses it like that in a lot of files.
try this
import Foundation
extension Data {
func hexString() -> String {
self.compactMap {
String(format: "%02x", $0)
}.joined()
}
}
var data = Data("Hello World".utf8)
data.append(contentsOf: [1,2,3,4,5])
print(data.hexString())
prints
48656c6c6f20576f726c640102030405
or even better
import Foundation
extension Data {
var hexString: String {
self.compactMap {
String(format: "%02x", $0)
}.joined()
}
}
var data = Data("Hello World".utf8)
data.append(contentsOf: [1,2,3,4,5])
print(data.hexString)

Cannot identify language in SALT

I just started using SALT for a project i am working on. It is said to work with Python but I find quite a bit of difference in thier syntax and overall format. I have pasted a code for a simple task which just opens and imports and loads some libraries onto the SALT console. I hope someone can check abnd see if he/she can instantly identify the language being used and what the code does. This is because i have a suspicion that the code is a mixture between C, Python as well as Java... if that is so doen't that mean it should be a totally different language on its own?
code:
var rtwxlib = import'rtwxlib';
var string = import'string';
var monitor = rtwxlib.Monitor
{
EvActivate = func() { print "Activate\n"; };
EvShutdown = func() { print "Shutdown\n"; };
EvProgress = func(self, fDone, msg = "") {
print("Progress: %d %s \r"::format(fDone*100, msg));
};
EvEventMsg = func(self, msg) {
print("\nEvent: %s\n"::format(iStat, msg));
};
};
var solver = rtwxlib.Solver(monitor);
solver::Open("test.wrx");
solver::DelGeometry();
solver::SaveAs('testresults.wrx');
solver::Close();
I was able to indentify the syntax of the language used as C. I beieve the problem I had which confused me was the libraries used for this code which I later found out was for a particular program. Thank you to all those who tried to help though :)

Is it Possible to Perform Addition in a Regex?

Assuming the placeholder $2 is populated with an integer, is it possible to increment it by 1?:
var strReplace = #"$2";
Regex.Replace(strInput, #"((.)*?)", strReplace);
You can use a callback version of Regex.Replace with a MatchEvaluator, see examples at:
http://msdn.microsoft.com/en-us/library/cft8645c.aspx
http://www.dotnetperls.com/regex-replace
Here's an example (ideone):
using System;
using System.Text.RegularExpressions;
class Program
{
static string AddOne(string s)
{
return Regex.Replace(s, #"\d+", (match) =>
{
long num = 0;
long.TryParse(match.ToString(), out num);
return (num + 1).ToString();
});
}
static void Main()
{
Console.WriteLine(AddOne("hello 123!"));
Console.WriteLine(AddOne("bai bai 11"));
}
}
Output:
hello 124!
bai bai 12
In standard (CS theoretic) regular expressions, it is impossible with a regular expression.
However, Perl and such have extensions to Regular Expressions, which has implications for their behaviour, and I am not familiar enough with them to definitely say that the extended regexes will not do it, but I'm fairly sure that this behaviour is not possible with a regex.
It's not possible with a standard regular expression but you can if you write a custom MatchEvaluator.
string str = "Today I have answered 0 questions on StackOverlow.";
string pattern = #"\w*?(\d)";
var result = Regex.Replace(str, pattern,
m => (int.Parse(m.Groups[0].Value)+1).ToString() );
Console.WriteLine(result);
Today I have answered 1 questions on StackOverlow.

Using Closure compiler with Underscore.js _.template

Is there any way to compile Underscore.js templates on the server and get the Closure compiler to work with the generated code?
The main problem is that _.template:
_.template = function(str, data) {
var c = _.templateSettings;
var tmpl = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};' +
'with(obj||{}){__p.push(\'' +
str.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(c.interpolate, function(match, code) {
return "'," + code.replace(/\\'/g, "'") + ",'";
})
.replace(c.evaluate || null, function(match, code) {
return "');" + code.replace(/\\'/g, "'")
.replace(/[\r\n\t]/g, ' ') + "__p.push('";
})
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n')
.replace(/\t/g, '\\t')
+ "');}return __p.join('');";
var func = new Function('obj', tmpl);
return data ? func(data) : func;
};
generates JavaScript with a with-statement in it. The two obvious routes are:
modify Underscore.js's _.template not to generate withs
coerce Closure into playing nice
Is the second option possible?
Generally, the JS engines perform better without "with", so if generating it without "with" is an option that is likely the best solution.
Otherwise your options depend on whether you are hoping to using Closure Compilers ADVANCED mode. In SIMPLE mode, the compiler won't rename your properties on the template, and will assume that any undeclared variable are globals. So as long are your template object isn't causing any local variables to be shadowed it might "just work".

Decode S-JIS string to UTF-8

I am working on a Japanese File and I have no knowledge of the language. The file is encoded in S-JIS. Now, I am supposed to convert the contents into UTF-8 so that the content looks like Japanese. And here I am completely blank. I tried the following code that I found somewhere on Internet but no luck:
byte[] arrByte = Encoding.UTF8.GetBytes(arrActualData[x]);
string str = ASCIIEncoding.ASCII.GetString(arrByte);
Can anyone help me with this?
Thanks in advance
Kunal
In C#, the following code works for me.
I wanted to try this out so evidence of my results below:
public void Convert()
{
using (TextReader input = new StreamReader(
new FileStream("shift-jis.txt", FileMode.Open),
Encoding.GetEncoding("shift-jis")))
{
using (TextWriter output = new StreamWriter(
new FileStream("utf8.txt", FileMode.Create), Encoding.UTF8))
{
var buffer = new char[512];
int len;
while ((len = input.Read(buffer, 0, 512)) > 0)
{
output.Write(buffer, 0, len);
}
}
}
}
Shown here is the file encoded in shift-jis (or SJIS/Shift_JIS they are the same), using JEdit to verify the encoding (the word in the file is the japanese text ใƒ†ใ‚นใƒˆ meaning test):
After running the code & opening the file written to (utf8.txt) :
But it should be said that such a file conversion does not strictly require knowledge of any language.

Resources