Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
justinknight93 committed Aug 21, 2024
1 parent 9df9901 commit 46a107d
Show file tree
Hide file tree
Showing 4 changed files with 809 additions and 0 deletions.
196 changes: 196 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,198 @@
# Doolittle
An application loader for the M5 Cardputer that runs Javascript.

## Required Libraries
- M5Cardputer by M5Stack
- JavaScript by Joseph Read

## Installation Guide
1. Flash the firmware.
2. Copy the file structure from the SD Card Root folder to the root of your Cardputer's SD Card.
3. Anything in boot.js should run automatically.

## API Reference for Native Functions Accessible from JS

### `load(script: string)`

**Description**: Sets the script to be executed in the Duktape context.

**Parameters**:
- `script` (string): The JavaScript code as a string to be loaded.

**Returns**: `void`

---

### `print(message: string)`

**Description**: Prints the given message to the Serial monitor.

**Parameters**:
- `message` (string): The message to print.

**Returns**: `void`

---

### `now()`

**Description**: Returns the current time in milliseconds since the epoch.

**Returns**: `number`: The current timestamp in milliseconds.

---

### `delay(ms: number)`

**Description**: Pauses execution for the specified number of milliseconds.

**Parameters**:
- `ms` (number): The number of milliseconds to delay.

**Returns**: `void`

---

### `digitalWrite(pin: number, value: boolean)`

**Description**: Sets the digital value (HIGH or LOW) for a specified pin.

**Parameters**:
- `pin` (number): The pin number.
- `value` (boolean): The value to write (true for HIGH, false for LOW).

**Returns**: `void`

---

### `pinMode(pin: number, mode: number)`

**Description**: Configures the specified pin to behave as an input or an output.

**Parameters**:
- `pin` (number): The pin number.
- `mode` (number): The mode to set (INPUT, OUTPUT, etc.).

**Returns**: `void`

---

### `httpGet(url: string, headers: string[])`

**Description**: Performs an HTTP GET request to the specified URL. Optionally includes headers.

**Parameters**:
- `url` (string): The URL to send the GET request to.
- `headers` (string[]): An array of headers to include in the request. Headers should be provided as key-value pairs in the array. Example: `[
"Content-Type", "application/json",
"Authorization", "Bearer your_token_here",
"Accept", "application/json"
]`

**Returns**: `object`: An object with two properties:
- `response` (number): The HTTP response code.
- `body` (string): The response body.

---

### `color(r: number, g: number, b: number)`

**Description**: Creates a color from the given RGB values.

**Parameters**:
- `r` (number): The red component (0-255).
- `g` (number): The green component (0-255).
- `b` (number): The blue component (0-255).

**Returns**: `number`: The color value in 16-bit RGB format.

---

### `setTextColor(color: number)`

**Description**: Sets the text color for drawing operations.

**Parameters**:
- `color` (number): The color value in 16-bit RGB format.

**Returns**: `void`

---

### `setTextSize(size: number)`

**Description**: Sets the text size for drawing operations.

**Parameters**:
- `size` (number): The text size multiplier.

**Returns**: `void`

---

### `drawRect(x: number, y: number, width: number, height: number, color: number)`

**Description**: Draws a rectangle with the specified parameters.

**Parameters**:
- `x` (number): The x-coordinate of the top-left corner.
- `y` (number): The y-coordinate of the top-left corner.
- `width` (number): The width of the rectangle.
- `height` (number): The height of the rectangle.
- `color` (number): The color value in 16-bit RGB format.

**Returns**: `void`

---

### `drawFillRect(x: number, y: number, width: number, height: number, color: number)`

**Description**: Draws a filled rectangle with the specified parameters.

**Parameters**:
- `x` (number): The x-coordinate of the top-left corner.
- `y` (number): The y-coordinate of the top-left corner.
- `width` (number): The width of the rectangle.
- `height` (number): The height of the rectangle.
- `color` (number): The color value in 16-bit RGB format.

**Returns**: `void`

---

### `drawString(text: string, x: number, y: number)`

**Description**: Draws the specified text at the given coordinates.

**Parameters**:
- `text` (string): The text to draw.
- `x` (number): The x-coordinate where the text starts.
- `y` (number): The y-coordinate where the text starts.

**Returns**: `void`

---

### `width()`

**Description**: Returns the width of the display.

**Returns**: `number`: The width of the display in pixels.

---

### `height()`

**Description**: Returns the height of the display.

**Returns**: `number`: The height of the display in pixels.

---

### `getKeysPressed()`

**Description**: Returns the current state of the keys pressed on the M5Cardputer.

**Returns**: `string[]`: An array of strings representing the pressed keys. Possible values include "Delete", "Enter", "Alt", "Tab", "Function", "Option", or the actual key character.

---
154 changes: 154 additions & 0 deletions SD Card Root/doolittle/boot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Snake Example

var screenWidth = width();
var screenHeight = height();
var cBG = color(0, 0, 0);
var cSnake = color(150, 150, 255);
var cApple = color(120, 255, 120);
var gridSize = 8;
var headX = 4;
var headY = 4;
var appleX = 8;
var appleY = 4;
var headDir = 3; // 0 = up, 1 = down, 2 = left, 3 = right
var totalDelay = 400;
var delayTime = totalDelay;
var tails = [];
var canMove = true;
var time = now();
var prevTime = now();
tails.push([headX, headY]);

setTextSize(2);
drawFillRect(headX * gridSize, headY * gridSize, gridSize, gridSize, cSnake);
drawFillRect(appleX * gridSize, appleY * gridSize, gridSize, gridSize, cApple);

function gameOver() {
drawFillRect(0, 0, screenWidth, screenHeight, color(255, 0, 0));
setTextColor(cBG);
drawString("Game Over", screenWidth / 2 - 60, screenHeight / 2 - 10);
delay(3000);
throw new Error("Game Over");
};

function updateTails() {
if (tails.length > 0) {
var lastTail = tails.pop();
drawFillRect(lastTail[0] * gridSize, lastTail[1] * gridSize, gridSize, gridSize, cBG);
tails.unshift([headX, headY]);

for (var i = 0; i < tails.length; i++) {
drawRect(tails[i][0] * gridSize + 1, tails[i][1] * gridSize + 1, gridSize - 2, gridSize - 2, cSnake);
}
}
};

function updateDelayTime() {
var timePassed = time - prevTime;
if (timePassed == 0) {
time = now();
return;
}
prevTime = time;
time = now();
delayTime -= timePassed;
};

function isOnSnake(x, y) {
if (headX === x && headY === y) {
return true;
}
for (var i = 0; i < tails.length; i++) {
if (tails[i][0] === x && tails[i][1] === y) {
return true;
}
}
return false;
}

function updateSnake() {
if (canMove) {
switch (getKeysPressed()[0]) {
case ";":
if (headDir !== 1) {
headDir = 0;
canMove = false;
}
break;
case ".":
if (headDir !== 0) {
headDir = 1;
canMove = false;
}
break;
case ",":
if (headDir !== 3) {
headDir = 2;
canMove = false;
}
break;
case "/":
if (headDir !== 2) {
headDir = 3;
canMove = false;
}
break;
}
}

if (delayTime < 0) {
canMove = true;
var prevX = headX;
var prevY = headY;

switch (headDir) {
case 0:
headY -= 1;
break;
case 1:
headY += 1;
break;
case 2:
headX -= 1;
break;
case 3:
headX += 1;
break
}

if (headX !== prevX || headY !== prevY) {
drawFillRect(prevX * gridSize, prevY * gridSize, gridSize, gridSize, cBG);
drawFillRect(headX * gridSize, headY * gridSize, gridSize, gridSize, cSnake);
}

if (headX === appleX && headY === appleY) {
do {
appleX = Math.floor(Math.random() * (screenWidth / gridSize));
appleY = Math.floor(Math.random() * (screenHeight / gridSize));
} while (isOnSnake(appleX, appleY));
drawFillRect(appleX * gridSize, appleY * gridSize, gridSize, gridSize, cApple);
tails.push([prevX, prevY]);
totalDelay -= 10;
}

updateTails();

if (headX < 0 || headX >= screenWidth / gridSize || headY < 0 || headY >= screenHeight / gridSize) {
gameOver();
}

for (var i = 1; i < tails.length; i++) {
if (headX === tails[i][0] && headY === tails[i][1]) {
gameOver();
}
}

delayTime = totalDelay;
}
}


while (true) {
updateDelayTime();
updateSnake();
}
2 changes: 2 additions & 0 deletions SD Card Root/doolittle/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SSID: wifiname
PASSWORD: wifipassword
Loading

0 comments on commit 46a107d

Please sign in to comment.