Skip to content

Commit

Permalink
Hello Mandelbrot!
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarlettSpell committed Sep 22, 2022
0 parents commit ddddc49
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Build
on: [pull_request, push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install C++ toolchain
uses: numworks/setup-arm-toolchain@2020-q2
- name: Install nwlink
run: npm install -g nwlink
- name: Run make
run: make
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Copyright (c) 2022 ScarlettSpell. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of NumWorks nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 changes: 69 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Q ?= @
CC = arm-none-eabi-gcc
CXX = arm-none-eabi-g++
BUILD_DIR = target
NWLINK = nwlink
LINK_GC = 1
LTO = 1

define object_for
$(addprefix $(BUILD_DIR)/,$(addsuffix .o,$(basename $(1))))
endef

src = $(addprefix src/,\
main.cpp \
)

CPPFLAGS = -std=c++14 -fno-exceptions
CPPFLAGS += -Os -Wall
CPPFLAGS += $(shell $(NWLINK) eadk-cflags)
LDFLAGS = -Wl,--relocatable
LDFLAGS += -nostartfiles
LDFLAGS += --specs=nano.specs
LDFLAGS += -lm

ifeq ($(LINK_GC),1)
CPPFLAGS += -fdata-sections -ffunction-sections
LDFLAGS += -Wl,-e,main -Wl,-u,eadk_app_name -Wl,-u,eadk_app_icon -Wl,-u,eadk_api_level
LDFLAGS += -Wl,--gc-sections
endif

ifeq ($(LTO),1)
CPPFLAGS += -flto -fno-fat-lto-objects
CPPFLAGS += -fwhole-program
CPPFLAGS += -fvisibility=internal
LDFLAGS += -flinker-output=nolto-rel
endif

.PHONY: build
build: $(BUILD_DIR)/mandelbrot.bin

.PHONY: run
run: $(BUILD_DIR)/mandelbrot.nwa
@echo "INSTALL $<"
$(Q) $(NWLINK) install-nwa $<

$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.nwa
@echo "BIN $@"
$(Q) $(NWLINK) nwa-bin $< $@

$(BUILD_DIR)/mandelbrot.nwa: $(call object_for,$(src)) $(BUILD_DIR)/icon.o
@echo "LD $@"
$(Q) $(CC) $(CPPFLAGS) $(LDFLAGS) $^ -lm -o $@

$(addprefix $(BUILD_DIR)/,%.o): %.cpp | $(BUILD_DIR)
@echo "CXX $^"
$(Q) $(CXX) $(CPPFLAGS) $(SFLAGS) -c $^ -o $@

$(BUILD_DIR)/icon.o: src/icon.png
@echo "ICON $<"
$(Q) $(NWLINK) png-icon-o $< $@

.PRECIOUS: $(BUILD_DIR)
$(BUILD_DIR):
$(Q) mkdir -p $@/src

.PHONY: clean
clean:
@echo "CLEAN"
$(Q) rm -rf $(BUILD_DIR)
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Mandelbrot

[![Build](https://github.com/nwagyu/mandelbrot/actions/workflows/build.yml/badge.svg)](https://github.com/nwagyu/mandelbrot/actions/workflows/build.yml)

This is an app that will draw the [Mandelbrot set](https://en.wikipedia.org/wiki/Mandelbrot_set) on your NumWorks calculator.

## About this app

This app uses the same algorithm as the sample `mandelbrot.py` Python script that ships with the NumWorks calculator. But it is roughly 100x faster!

## Install the app

Installing is rather easy:
1. Download the latest `mandelbrot.nwa` file from the [Releases](https://github.com/nwagyu/mandelbrot/releases) page
2. Head to [my.numworks.com/apps](https://my.numworks.com/apps) to send the `nwa` file on your calculator

## How to use the app

Well, there isn't much to do. Just launch it and watch. You can press the "Home" button to exit.

## Build and run the app

To build this sample app, you will need to install the [embedded ARM toolchain](https://developer.arm.com/Tools%20and%20Software/GNU%20Toolchain) and [nwlink](https://www.npmjs.com/package/nwlink).

```shell
brew install numworks/tap/arm-none-eabi-gcc node # Or equivalent on your OS
npm install -g nwlink
make run
```
153 changes: 153 additions & 0 deletions src/eadkpp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#ifndef EADKPP_H
#define EADKPP_H

extern "C" {
#include <eadk.h>
}

namespace EADK {

namespace Display {

class Color {
public:
constexpr Color() : m_value(0) {}
constexpr Color(uint32_t rgb) : m_value(((rgb&0xF80000)>>8)|((rgb&0x00FC00)>>5)|((rgb&0x0000F8)>>3)) {}
constexpr operator eadk_color_t() const { return (eadk_color_t)m_value; }
private:
uint16_t m_value;
};
static_assert(sizeof(EADK::Display::Color) == sizeof(eadk_color_t), "EADK::Display::Color should match eadk_color_t");

class Point {
public:
constexpr Point(int x, int y) :
m_x(x), m_y(y) {}
uint16_t x() const { return m_x; }
uint16_t y() const { return m_y; }
constexpr operator eadk_point_t() const { return *reinterpret_cast<const eadk_point_t *>(this); }
private:
uint16_t m_x;
uint16_t m_y;
};
static_assert(sizeof(EADK::Display::Point) == sizeof(eadk_point_t), "EADK::Display::Point should match eadk_point_t");

class Rect {
public:
constexpr Rect(int x, int y, int width, int height) :
m_x(x), m_y(y), m_width(width), m_height(height) {}
uint16_t x() const { return m_x; }
uint16_t y() const { return m_y; }
uint16_t width() const { return m_width; }
uint16_t height() const { return m_height; }
constexpr operator eadk_rect_t() const { return *reinterpret_cast<const eadk_rect_t *>(this); }
private:
uint16_t m_x;
uint16_t m_y;
uint16_t m_width;
uint16_t m_height;
};
static_assert(sizeof(EADK::Display::Rect) == sizeof(eadk_rect_t), "EADK::Display::Rect should match eadk_rect_t");

static inline void pushRect(Rect rect, const Color * pixels) {
eadk_display_push_rect(rect, reinterpret_cast<const eadk_color_t *>(pixels));
}

static inline void pullRect(Rect rect, Color * pixels) {
eadk_display_pull_rect(rect, reinterpret_cast<eadk_color_t *>(pixels));
}

static inline void pushRectUniform(Rect rect, Color color) {
eadk_display_push_rect_uniform(rect, color);
}
static inline void drawString(const char * text, Point point, bool largeFont, Color textColor, Color backgroundColor) {
eadk_display_draw_string(text, point, largeFont, textColor, backgroundColor);
}

}

namespace Keyboard {

enum class Key : uint8_t {
Left = 0,
Up = 1,
Down = 2,
Right = 3,
OK = 4,
Back = 5,
Home = 6,
Shift = 12,
Alpha = 13,
XNT = 14,
Var = 15,
Toolbox = 16,
Backspace = 17,
Exp = 18,
Ln = 19,
Log = 20,
Imaginary = 21,
Comma = 22,
Power = 23,
Sine = 24,
Cosine = 25,
Tangent = 26,
Pi = 27,
Sqrt = 28,
Square = 29,
Seven = 30,
Eight = 31,
Nine = 32,
LeftParenthesis = 33,
RightParenthesis = 34,
Four = 36,
Five = 37,
Six = 38,
Multiplication = 39,
Division = 40,
One = 42,
Two = 43,
Three = 44,
Plus = 45,
Minus = 46,
Zero = 48,
Dot = 49,
EE = 50,
Ans = 51,
EXE = 52,
};

class State {
public:
constexpr State(uint64_t s = 0) : m_bitField(s) { }
inline bool keyDown(Key k) const {
return eadk_keyboard_key_down(*this, (eadk_key_t)k);
//return (m_bitField>>(uint8_t)k) & 1;
}
constexpr operator eadk_keyboard_state_t() const { return *reinterpret_cast<const eadk_keyboard_state_t *>(this); }
private:
uint64_t m_bitField;
};
static_assert(sizeof(EADK::Keyboard::State) == sizeof(eadk_keyboard_state_t), "EADK::Keyboard::State should match eadk_keyboard_state_t");


static inline State scan() {
return State(eadk_keyboard_scan());
}

}

namespace Timing {

static inline void msleep(uint32_t ms) {
return eadk_timing_msleep(ms);
}

}

static inline uint32_t random() {
return eadk_random();
}

}

#endif
Binary file added src/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "eadkpp.h"
#include <complex>
#include <cmath>

extern const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name"))) = "Mandelbrot";
extern const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level"))) = 0;

using namespace std::complex_literals;

constexpr int DisplayWidth = 320;
constexpr int DisplayHeight = 240;
constexpr int TileSize = 10;

int main(int argc, const char * argv[]) {
int iteration = 10;

EADK::Display::Color tile[TileSize*TileSize];

for (int tileX=0; tileX<320/TileSize; tileX++) {
for (int tileY=0; tileY<240/TileSize; tileY++) {
for (int offsetX=0; offsetX<TileSize; offsetX++) {
for (int offsetY=0; offsetY<TileSize; offsetY++) {
int x = tileX*TileSize + offsetX;
int y = tileY*TileSize + offsetY;

std::complex<float> z = 0;
std::complex<float> c = (3.5f*x/319.f-2.5f) + 1if*(-2.5f*y/221.f+1.25f);
int i=0;
while (i < iteration && abs(z) < 2) {
i++;
z = z*z+c;
float rgb = 255.0f*i/iteration;
tile[offsetY*TileSize+offsetX] = EADK::Display::Color(rgb);
}
}
}
EADK::Display::pushRect(EADK::Display::Rect(tileX*TileSize,tileY*TileSize,TileSize,TileSize), tile);
}
}
eadk_timing_msleep(1000);
}

0 comments on commit ddddc49

Please sign in to comment.