Move most of core stuff
This commit is contained in:
parent
6bf46577f9
commit
6559f386d2
139
libs/microbit/core.cpp
Normal file
139
libs/microbit/core.cpp
Normal file
@ -0,0 +1,139 @@
|
||||
#include "ksbit.h"
|
||||
#include <limits.h>
|
||||
|
||||
namespace StringMethods {
|
||||
|
||||
/**
|
||||
* Returns the character at the specified index.
|
||||
* @param pos The zero-based index of the desired character.
|
||||
*/
|
||||
//%
|
||||
StringData *charAt(StringData *s, int pos) {
|
||||
return ManagedString((char)ManagedString(s).charAt(pos)).leakData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Unicode value of the character at the specified location.
|
||||
* @param index The zero-based index of the desired character. If there is no character at the specified index, NaN is returned.
|
||||
*/
|
||||
//%
|
||||
int charCodeAt(StringData *s, int index) {
|
||||
return ManagedString(s).charAt(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string that contains the concatenation of two or more strings.
|
||||
* @param other The string to append to the end of the string.
|
||||
*/
|
||||
//%
|
||||
StringData *concat(StringData *s, StringData *other) {
|
||||
ManagedString a(s), b(other);
|
||||
return (a + b).leakData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether relative order of two strings (in ASCII encoding).
|
||||
* @param that String to compare to target string
|
||||
*/
|
||||
//%
|
||||
int compare(StringData *s, StringData *that) {
|
||||
return strcmp(s->data, that->data);
|
||||
}
|
||||
|
||||
/** Returns the length of a String object. */
|
||||
//% property
|
||||
int length(StringData *s) {
|
||||
return s->len;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
namespace BooleanMethods {
|
||||
// Cache the string literals "true" and "false" when used.
|
||||
// Note that the representation of booleans stays the usual C-one.
|
||||
|
||||
static const char sTrue[] __attribute__ ((aligned (4))) = "\xff\xff\x04\x00" "true\0";
|
||||
static const char sFalse[] __attribute__ ((aligned (4))) = "\xff\xff\x05\x00" "false\0";
|
||||
|
||||
/**
|
||||
* Returns a string representation of an object.
|
||||
*/
|
||||
//%
|
||||
StringData* toString(bool v)
|
||||
{
|
||||
if (v) {
|
||||
return (StringData*)(void*)sTrue;
|
||||
} else {
|
||||
return (StringData*)(void*)sFalse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace String {
|
||||
/**
|
||||
* Make a string from the given ASCII character code.
|
||||
*/
|
||||
//%
|
||||
StringData *fromCharCode(int code)
|
||||
{
|
||||
return ManagedString((char)code).leakData();
|
||||
}
|
||||
}
|
||||
|
||||
namespace NumberMethods {
|
||||
/**
|
||||
* Returns a string representation of a number.
|
||||
*/
|
||||
//%
|
||||
StringData* toString(int n)
|
||||
{
|
||||
return ManagedString(n).leakData();
|
||||
}
|
||||
}
|
||||
|
||||
namespace Math {
|
||||
/**
|
||||
* Returns the value of a base expression taken to a specified power.
|
||||
* @param x The base value of the expression.
|
||||
* @param y The exponent value of the expression.
|
||||
*/
|
||||
//%
|
||||
int pow(int x, int y)
|
||||
{
|
||||
if (y < 0)
|
||||
return 0;
|
||||
int r = 1;
|
||||
while (y) {
|
||||
if (y & 1)
|
||||
r *= x;
|
||||
y >>= 1;
|
||||
x *= x;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom number between 0 and `max`.
|
||||
*/
|
||||
//%
|
||||
int random(int max) {
|
||||
if (max == INT_MIN)
|
||||
return -uBit.random(INT_MAX);
|
||||
else if (max < 0)
|
||||
return -uBit.random(-max);
|
||||
else if (max == 0)
|
||||
return 0;
|
||||
else
|
||||
return uBit.random(max);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the square root of a number.
|
||||
* @param x A numeric expression.
|
||||
*/
|
||||
//%
|
||||
int sqrt(int x)
|
||||
{
|
||||
return ::sqrt(x);
|
||||
}
|
||||
}
|
116
libs/microbit/core.d.ts
vendored
116
libs/microbit/core.d.ts
vendored
@ -78,75 +78,6 @@ interface Array<T> {
|
||||
|
||||
|
||||
interface String {
|
||||
|
||||
/**
|
||||
* Returns the character at the specified index.
|
||||
* @param pos The zero-based index of the desired character.
|
||||
*/
|
||||
//% shim=string::at
|
||||
charAt(pos: number): string;
|
||||
|
||||
/**
|
||||
* Returns the Unicode value of the character at the specified location.
|
||||
* @param index The zero-based index of the desired character. If there is no character at the specified index, NaN is returned.
|
||||
*/
|
||||
//% shim=string::code_at
|
||||
charCodeAt(index: number): number;
|
||||
|
||||
/**
|
||||
* Returns a string that contains the concatenation of two or more strings.
|
||||
* @param strings The strings to append to the end of the string.
|
||||
*/
|
||||
//% shim=string::concat
|
||||
concat(other: string): string;
|
||||
|
||||
/**
|
||||
* Returns the position of the first occurrence of a substring.
|
||||
* @param searchString The substring to search for in the string
|
||||
* @param position The index at which to begin searching the String object. If omitted, search starts at the beginning of the string.
|
||||
*/
|
||||
indexOf(searchString: string, position?: number): number;
|
||||
|
||||
/**
|
||||
* Returns the last occurrence of a substring in the string.
|
||||
* @param searchString The substring to search for.
|
||||
* @param position The index at which to begin searching. If omitted, the search begins at the end of the string.
|
||||
*/
|
||||
lastIndexOf(searchString: string, position?: number): number;
|
||||
|
||||
/**
|
||||
* Determines whether two strings are equivalent in the current locale.
|
||||
* @param that String to compare to target string
|
||||
*/
|
||||
localeCompare(that: string): number;
|
||||
|
||||
/**
|
||||
* Returns a section of a string.
|
||||
* @param start The index to the beginning of the specified portion of stringObj.
|
||||
* @param end The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end.
|
||||
* If this value is not specified, the substring continues to the end of stringObj.
|
||||
*/
|
||||
slice(start?: number, end?: number): string;
|
||||
|
||||
/**
|
||||
* Returns the substring at the specified location within a String object.
|
||||
* @param start The zero-based index number indicating the beginning of the substring.
|
||||
* @param end Zero-based index number indicating the end of the substring. The substring includes the characters up to, but not including, the character indicated by end.
|
||||
* If end is omitted, the characters from start through the end of the original string are returned.
|
||||
*/
|
||||
substring(start: number, end?: number): string;
|
||||
|
||||
/** Converts all the alphabetic characters in a string to lowercase. */
|
||||
toLowerCase(): string;
|
||||
|
||||
/** Converts all the alphabetic characters in a string to uppercase. */
|
||||
toUpperCase(): string;
|
||||
|
||||
/** Returns the length of a String object. */
|
||||
//% shim=string::count
|
||||
length: number;
|
||||
|
||||
//% shim=string::at
|
||||
[index: number]: string;
|
||||
}
|
||||
|
||||
@ -187,50 +118,3 @@ interface Number {
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
declare namespace Math {
|
||||
/**
|
||||
* Returns the absolute value of a number (the value without regard to whether it is positive or negative).
|
||||
* For example, the absolute value of -5 is the same as the absolute value of 5.
|
||||
* @param x A numeric expression for which the absolute value is needed.
|
||||
*/
|
||||
//% shim=math::abs
|
||||
export function abs(x: number): number;
|
||||
|
||||
/**
|
||||
* Returns the sign of the x, indicating whether x is positive, negative or zero.
|
||||
* @param x The numeric expression to test
|
||||
*/
|
||||
//% shim=math::sign
|
||||
export function sign(x: number): number;
|
||||
|
||||
/**
|
||||
* Returns the larger of two supplied numeric expressions.
|
||||
*/
|
||||
//% shim=math::max
|
||||
export function max(a:number, b:number): number;
|
||||
|
||||
/**
|
||||
* Returns the smaller of two supplied numeric expressions.
|
||||
*/
|
||||
//% shim=math::min
|
||||
export function min(a:number, b:number): number;
|
||||
|
||||
/**
|
||||
* Returns the value of a base expression taken to a specified power.
|
||||
* @param x The base value of the expression.
|
||||
* @param y The exponent value of the expression.
|
||||
*/
|
||||
//% shim=math::pow
|
||||
export function pow(x: number, y: number): number;
|
||||
|
||||
/** Returns a pseudorandom number between 0 and `max`. */
|
||||
//% shim=math::random
|
||||
export function random(max:number): number;
|
||||
|
||||
/**
|
||||
* Returns the square root of a number.
|
||||
* @param x A numeric expression.
|
||||
*/
|
||||
//% shim=math::sqrt
|
||||
export function sqrt(x: number): number;
|
||||
}
|
||||
|
@ -284,8 +284,8 @@ namespace game {
|
||||
private _blink: number;
|
||||
|
||||
constructor(x: number, y: number) {
|
||||
this._x = math.clamp(0, 4, x);
|
||||
this._y = math.clamp(0, 4, y);
|
||||
this._x = Math.clamp(0, 4, x);
|
||||
this._y = Math.clamp(0, 4, y);
|
||||
this._dir = 90;
|
||||
this._brightness = 255;
|
||||
init();
|
||||
@ -322,8 +322,8 @@ namespace game {
|
||||
this._x = this._x - leds;
|
||||
this._y = this._y + leds;
|
||||
}
|
||||
this._x = math.clamp(0, 4, this._x);
|
||||
this._y = math.clamp(0, 4, this._y);
|
||||
this._x = Math.clamp(0, 4, this._x);
|
||||
this._y = Math.clamp(0, 4, this._y);
|
||||
plot();
|
||||
}
|
||||
|
||||
@ -336,8 +336,8 @@ namespace game {
|
||||
public goTo(x: number, y: number): void {
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._x = math.clamp(0, 4, this._x);
|
||||
this._y = math.clamp(0, 4, this._y);
|
||||
this._x = Math.clamp(0, 4, this._x);
|
||||
this._y = Math.clamp(0, 4, this._y);
|
||||
plot();
|
||||
}
|
||||
|
||||
@ -594,7 +594,7 @@ namespace game {
|
||||
* @param brightness TODO
|
||||
*/
|
||||
public setBrightness(brightness: number): void {
|
||||
this._brightness = math.clamp(0, 255, brightness);
|
||||
this._brightness = Math.clamp(0, 255, brightness);
|
||||
plot();
|
||||
}
|
||||
|
||||
@ -639,7 +639,7 @@ namespace game {
|
||||
* @param ms TODO
|
||||
*/
|
||||
public setBlink(ms: number): void {
|
||||
this._blink = math.clamp(0, 10000, ms);
|
||||
this._blink = Math.clamp(0, 10000, ms);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -9,6 +9,7 @@
|
||||
"shims.d.ts",
|
||||
"core.d.ts",
|
||||
"ksbit.h",
|
||||
"core.cpp",
|
||||
"mbit.ts",
|
||||
"images.cpp",
|
||||
"basic.cpp",
|
||||
|
@ -13,16 +13,52 @@ namespace helpers {
|
||||
|
||||
namespace console {
|
||||
export function log(msg: string) {
|
||||
writeString(msg);
|
||||
writeString("\r\n");
|
||||
serial.writeString(msg);
|
||||
serial.writeString("\r\n");
|
||||
}
|
||||
|
||||
//% shim=micro_bit::serialSendString
|
||||
function writeString(text: string): void { }
|
||||
}
|
||||
|
||||
namespace math {
|
||||
namespace Math {
|
||||
export function clamp(min: number, max:number, value:number): number {
|
||||
return Math.min(max, Math.max(min, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the absolute value of a number (the value without regard to whether it is positive or negative).
|
||||
* For example, the absolute value of -5 is the same as the absolute value of 5.
|
||||
* @param x A numeric expression for which the absolute value is needed.
|
||||
*/
|
||||
export function abs(x: number): number
|
||||
{
|
||||
return x < 0 ? -x : x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sign of the x, indicating whether x is positive, negative or zero.
|
||||
* @param x The numeric expression to test
|
||||
*/
|
||||
export function sign(x: number): number
|
||||
{
|
||||
if (x == 0) return 0;
|
||||
if (x > 0) return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the larger of two supplied numeric expressions.
|
||||
*/
|
||||
export function max(a:number, b:number): number
|
||||
{
|
||||
if (a >= b) return a;
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the smaller of two supplied numeric expressions.
|
||||
*/
|
||||
export function min(a:number, b:number): number
|
||||
{
|
||||
if (a <= b) return a;
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
84
libs/microbit/shims.d.ts
vendored
84
libs/microbit/shims.d.ts
vendored
@ -1,6 +1,90 @@
|
||||
// Auto-generated. Do not edit.
|
||||
|
||||
|
||||
declare interface String {
|
||||
/**
|
||||
* Returns the character at the specified index.
|
||||
* @param pos The zero-based index of the desired character.
|
||||
*/
|
||||
//% shim=StringMethods::charAt
|
||||
charAt(pos: number): string;
|
||||
|
||||
/**
|
||||
* Returns the Unicode value of the character at the specified location.
|
||||
* @param index The zero-based index of the desired character. If there is no character at the specified index, NaN is returned.
|
||||
*/
|
||||
//% shim=StringMethods::charCodeAt
|
||||
charCodeAt(index: number): number;
|
||||
|
||||
/**
|
||||
* Returns a string that contains the concatenation of two or more strings.
|
||||
* @param other The string to append to the end of the string.
|
||||
*/
|
||||
//% shim=StringMethods::concat
|
||||
concat(other: string): string;
|
||||
|
||||
/**
|
||||
* Determines whether relative order of two strings (in ASCII encoding).
|
||||
* @param that String to compare to target string
|
||||
*/
|
||||
//% shim=StringMethods::compare
|
||||
compare(that: string): number;
|
||||
|
||||
/** Returns the length of a String object. */
|
||||
//% property shim=StringMethods::length
|
||||
length(): number;
|
||||
}
|
||||
|
||||
|
||||
declare interface Boolean {
|
||||
/**
|
||||
* Returns a string representation of an object.
|
||||
*/
|
||||
//% shim=BooleanMethods::toString
|
||||
toString(): string;
|
||||
}
|
||||
declare namespace String {
|
||||
|
||||
/**
|
||||
* Make a string from the given ASCII character code.
|
||||
*/
|
||||
//% shim=String::fromCharCode
|
||||
function fromCharCode(code: number): string;
|
||||
}
|
||||
|
||||
|
||||
declare interface Number {
|
||||
/**
|
||||
* Returns a string representation of a number.
|
||||
*/
|
||||
//% shim=NumberMethods::toString
|
||||
toString(): string;
|
||||
}
|
||||
declare namespace Math {
|
||||
|
||||
/**
|
||||
* Returns the value of a base expression taken to a specified power.
|
||||
* @param x The base value of the expression.
|
||||
* @param y The exponent value of the expression.
|
||||
*/
|
||||
//% shim=Math::pow
|
||||
function pow(x: number, y: number): number;
|
||||
|
||||
/**
|
||||
* Returns a pseudorandom number between 0 and `max`.
|
||||
*/
|
||||
//% shim=Math::random
|
||||
function random(max: number): number;
|
||||
|
||||
/**
|
||||
* Returns the square root of a number.
|
||||
* @param x A numeric expression.
|
||||
*/
|
||||
//% shim=Math::sqrt
|
||||
function sqrt(x: number): number;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//% color=45 weight=31
|
||||
declare namespace images {
|
||||
|
Loading…
Reference in New Issue
Block a user