Skip to content

Commit

Permalink
Merge branch 'master' into crypto_aes_ccm
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams authored Dec 9, 2024
2 parents 5423dd2 + ff3ae01 commit dc31001
Show file tree
Hide file tree
Showing 46 changed files with 689 additions and 236 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
Pixl.js: Remove Wiznet W5100 support from default build (there's now a espruino_#v##_pixljs_wiznet.zip without JIT enabled) to ensure we have enough flash to continue builds
Enable nostartfiles optimisation for Pixl,MDBT42 and nRF52DK
STM32F4: Add SDIO support
STM32: Ensure we kick the WDT if auto kicking is enabled and in deep sleep (avoids having to to it manually and wait 30ms for USB to wake up/shut down)
Allow a 'file receive' packet which can request Espruino sends a file as binary packets (also fix files not being closed if transmission fails)
Fix parsing of semicolons in DO with a statement: `do print(a);while(a--);`
In SAVE_ON_FLASH builds (Microbit 1) remove getSerial, Math.LN*/LOG*SQRT* constants, passwords, Serial/I2C/SPI.find, Date.toUTCString
ESP32: add setIP and setAPIP
Graphics.wrapString fix issue with missing final char if immediately after a '.' or other char we can split after (#2572)
Graphics: g.dump/asBMP can now output 16 bit images
Crypto: Add support for AES CCM

2v24 : Bangle.js2: Add 'Bangle.touchRd()', 'Bangle.touchWr()'
Expand Down
1 change: 1 addition & 0 deletions README_BuildProcess.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ These are set automatically when `SAVE_ON_FLASH` is set (see `jsutils.h`)
* `ESPR_NO_LET_SCOPING` - don't create scopes for `let` (treat it like `var`, which was the 2v13 and earlier behaviour)
* `ESPR_NO_PROMISES` - Don't include promise-handling functions
* `ESPR_NO_PRETOKENISE` - Don't include code to pretokenise functions marked with `"ram"` - code pretokenised in the IDE can still be executed
* `ESPR_NO_PASSWORD` - Disable password protection on the console (E.setPassword/E.lockConsole)


### chip
Expand Down
2 changes: 0 additions & 2 deletions libs/bluetooth/bluetooth_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,12 @@ void jsble_queue_pending_buf(BLEPending blep, uint16_t data, char *ptr, size_t l
// Push the actual event
JsSysTime d = (JsSysTime)((data<<8)|blep);
jshPushIOEvent(EV_BLUETOOTH_PENDING, d);
jshHadEvent();
}

/// Add a new bluetooth event to the queue with 16 bits of data
void jsble_queue_pending(BLEPending blep, uint16_t data) {
JsSysTime d = (JsSysTime)((data<<8)|blep);
jshPushIOEvent(EV_BLUETOOTH_PENDING, d);
jshHadEvent();
}

/* Handler for common event types (between nRF52/ESP32). Called first
Expand Down
1 change: 1 addition & 0 deletions libs/bluetooth/jswrap_bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -2365,6 +2365,7 @@ void jswrap_ble_setTxPower(JsVarInt pwr) {
"type" : "staticmethod",
"class" : "NRF",
"name" : "setLowPowerConnection",
"deprecated" : true,
"generate" : "jswrap_ble_setLowPowerConnection",
"params" : [
["lowPower","bool","Whether the connection is low power or not"]
Expand Down
10 changes: 6 additions & 4 deletions libs/filesystem/fat_sd/sdio_diskio.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// USED FOR SDIO-BASED SD CARDS
/*
* @author: ickle
* @author: ickle
* @source: http://www.61ic.com/code/archiver/?tid-27986.html
*/

Expand Down Expand Up @@ -175,10 +175,12 @@ DRESULT disk_ioctl (
res = RES_OK;
break;

case GET_SECTOR_COUNT : // Get number of sectors on the disk (DWORD)
*(DWORD*)buff = 131072; // 4*1024*32 = 131072
case GET_SECTOR_COUNT : { // Get number of sectors on the disk (DWORD)
SD_CardInfo SDCardInfo;
SD_GetCardInfo(&SDCardInfo);
*(DWORD*)buff = SDCardInfo.CardCapacity>>9;
res = RES_OK;
break;
} break;

case GET_SECTOR_SIZE : // Get R/W sector size (WORD)
*(WORD*)buff = 512;
Expand Down
29 changes: 16 additions & 13 deletions libs/graphics/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@ JsGraphicsSetPixelFn graphicsGetSetPixelUnclippedFn(JsGraphics *gfx, int x1, int
return gfx->setPixel; // fast
}

/// Merge one color into another based RGB565(amt is 0..256)
uint16_t graphicsBlendColorRGB565(uint16_t f, uint16_t b, int amt) {
unsigned int br = (b>>11)&0x1F;
unsigned int bg = (b>>5)&0x3F;
unsigned int bb = b&0x1F;
unsigned int fr = (f>>11)&0x1F;
unsigned int fg = (f>>5)&0x3F;
unsigned int fb = f&0x1F;
unsigned int ri = (br*(256-amt) + fr*amt) >> 8;
unsigned int gi = (bg*(256-amt) + fg*amt) >> 8;
unsigned int bi = (bb*(256-amt) + fb*amt) >> 8;
return (bi | gi<<5 | ri<<11);
}

/// Merge one color into another based on current bit depth (amt is 0..256)
uint32_t graphicsBlendColor(JsGraphics *gfx, unsigned int fg, unsigned int bg, int iamt) {
unsigned int amt = (iamt>0) ? (unsigned)iamt : 0;
Expand All @@ -374,18 +388,7 @@ uint32_t graphicsBlendColor(JsGraphics *gfx, unsigned int fg, unsigned int bg, i
// TODO: if our graphics instance is paletted this isn't correct!
return (bg*(256-amt) + fg*amt + 127) >> 8;
} else if (gfx->data.bpp==16) { // Blend from bg to fg
unsigned int b = bg;
unsigned int br = (b>>11)&0x1F;
unsigned int bg = (b>>5)&0x3F;
unsigned int bb = b&0x1F;
unsigned int f = fg;
unsigned int fr = (f>>11)&0x1F;
unsigned int fg = (f>>5)&0x3F;
unsigned int fb = f&0x1F;
unsigned int ri = (br*(256-amt) + fr*amt) >> 8;
unsigned int gi = (bg*(256-amt) + fg*amt) >> 8;
unsigned int bi = (bb*(256-amt) + fb*amt) >> 8;
return (bi | gi<<5 | ri<<11);
return graphicsBlendColorRGB565(fg,bg,iamt);
#ifdef ESPR_GRAPHICS_12BIT
} else if (gfx->data.bpp==12) { // Blend from bg to fg
unsigned int b = bg;
Expand Down Expand Up @@ -911,7 +914,7 @@ void graphicsScroll(JsGraphics *gfx, int xdir, int ydir) {
static void graphicsDrawString(JsGraphics *gfx, int x1, int y1, const char *str) {
// no need to modify coordinates as setPixel does that
while (*str) {
#ifdef USE_FONT_6X8
#ifdef USE_FONT_6X8
graphicsDrawChar6x8(gfx,x1,y1,*(str++),1,1,false);
x1 = (int)(x1 + 6);
#else
Expand Down
2 changes: 2 additions & 0 deletions libs/graphics/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ void graphicsSetModified(JsGraphics *gfx, int x1, int y1, int x2, int y2);
JsGraphicsSetPixelFn graphicsGetSetPixelFn(JsGraphics *gfx);
/// Get a setPixel function and set modified area (assuming no clipping) (inclusive of x2,y2) - if all is ok it can choose a faster draw function
JsGraphicsSetPixelFn graphicsGetSetPixelUnclippedFn(JsGraphics *gfx, int x1, int y1, int x2, int y2, bool coordsRotatedAlready);
/// Merge one color into another based RGB565(amt is 0..256)
uint16_t graphicsBlendColorRGB565(uint16_t fg, uint16_t bg, int iamt);
/// Merge one color into another based on current bit depth (amt is 0..256)
uint32_t graphicsBlendColor(JsGraphics *gfx, unsigned int fg, unsigned int bg, int iamt);
/// Merge one color into another based on current bit depth (amt is 0..256)
Expand Down
135 changes: 83 additions & 52 deletions libs/graphics/jswrap_graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,7 @@ It is recommended that you use `Graphics.setFont("4x6")` for more flexibility.
"type" : "method",
"class" : "Graphics",
"name" : "setFontVector",
"ifndef" : "SAVE_ON_FLASH",
"#if" : "!defined(SAVE_ON_FLASH) && !defined(NO_VECTOR_FONT)",
"generate_full" : "jswrap_graphics_setFontSizeX(parent, size, true)",
"params" : [
["size","int32","The height of the font, as an integer"]
Expand Down Expand Up @@ -2542,7 +2542,10 @@ JsVar *jswrap_graphics_wrapString(JsVar *parent, JsVar *str, int maxWidth) {
wasNewLine = ch=='\n';
canSplitAfter = ch==0; // can split after if there is an image next
if (endOfText) break;
if (ch!=0) continue; // allow us to handle images next
if (ch!=0) {
if (!jsvStringIteratorHasChar(&it)) endOfText=true; // handle sometimes missed final char: #2572
continue; // allow us to handle images next
}
}
canSplitAfter = false;
#ifndef SAVE_ON_FLASH
Expand Down Expand Up @@ -2807,7 +2810,7 @@ void jswrap_graphics_drawCString(JsGraphics *gfx, int x, int y, char *str) {
"type" : "method",
"class" : "Graphics",
"name" : "getVectorFontPolys",
"#if" : "!defined(SAVE_ON_FLASH) || !defined(NO_VECTOR_FONT)",
"#if" : "!defined(SAVE_ON_FLASH) && !defined(NO_VECTOR_FONT)",
"generate" : "jswrap_graphics_getVectorFontPolys",
"params" : [
["str","JsVar","The string"],
Expand Down Expand Up @@ -4142,64 +4145,93 @@ JsVar *jswrap_graphics_asBMP_X(JsVar *parent, bool printBase64) {
int rowstride = (((width*bpp)+31) >> 5) << 2; // padded to 32 bits
// palette length (byte size is 3x this)
int paletteEntries = hasPalette?(1<<bpp):0;
int headerLen = 14 + 12 + paletteEntries*3;
int headerLen;
if (bpp==16) { // Chrome doesn't like 16 bit BMPs in the other format
headerLen = 14 + 56;
} else {
headerLen = 14 + 12 + paletteEntries*3;
}
int fileSize = headerLen + height*rowstride;
// if printing base64 we only need enough memory for header + one row
int imgDataLen = printBase64 ? (headerLen + rowstride) : fileSize;
JsVar *imgData = jsvNewFlatStringOfLength((unsigned)imgDataLen);
if (!imgData) return 0; // not enough memory
unsigned char *imgPtr = (unsigned char *)jsvGetFlatStringPointer(imgData);
if (!imgPtr) return 0; // just in case
imgPtr[0]=66; //B
imgPtr[1]=77; //M
imgPtr[2]=(unsigned char)fileSize;
imgPtr[3]=(unsigned char)(fileSize>>8); // plus 2 more bytes for size
imgPtr[10]=(unsigned char)headerLen;
// maybe we want the InfoHeader, not BITMAPCOREHEADER (http://www.ece.ualberta.ca/~elliott/ee552/studentAppNotes/2003_w/misc/bmp_file_format/bmp_file_format.htm)
// Chrome doesn't like 16 bit BMPs in this format
// BITMAPCOREHEADER
imgPtr[14]=12; // sizeof(BITMAPCOREHEADER)
imgPtr[3]=(unsigned char)(fileSize>>8);
imgPtr[4]=(unsigned char)(fileSize>>16);
imgPtr[5]=(unsigned char)(fileSize>>24);
imgPtr[10]=(unsigned char)headerLen; // data offset
// size in here
imgPtr[18]=(unsigned char)width;
imgPtr[19]=(unsigned char)(width>>8);
imgPtr[20]=(unsigned char)height;
imgPtr[21]=(unsigned char)(height>>8);
imgPtr[22]=1; // color planes, should be 1
imgPtr[24]=(unsigned char)bpp; // bpp
if (hasPalette) {
// palette starts at 26
if (bpp==1) {
// first is white(?)
imgPtr[26]=255;
imgPtr[27]=255;
imgPtr[28]=255;
} else {
if (realBPP==3) {
for (int i=0;i<paletteEntries;i++) {
imgPtr[26 + (i*3)] = (i&1) ? 255 : 0;
imgPtr[27 + (i*3)] = (i&2) ? 255 : 0;
imgPtr[28 + (i*3)] = (i&4) ? 255 : 0;
}
#if defined(GRAPHICS_PALETTED_IMAGES)
} else if (realBPP==4) {
for (int i=0;i<16;i++) {
int p = PALETTE_4BIT[i];
imgPtr[26 + (i*3)] = (unsigned char)((p<<3)&0xF8);
imgPtr[27 + (i*3)] = (unsigned char)((p>>3)&0xFC);
imgPtr[28 + (i*3)] = (unsigned char)((p>>8)&0xF8);
}
} else if (realBPP==8) {
for (int i=0;i<255;i++) {
int p = PALETTE_8BIT[i];
imgPtr[26 + (i*3)] = (unsigned char)((p<<3)&0xF8);
imgPtr[27 + (i*3)] = (unsigned char)((p>>3)&0xFC);
imgPtr[28 + (i*3)] = (unsigned char)((p>>8)&0xF8);
}
#endif
} else { // otherwise default to greyscale
for (int i=0;i<(1<<realBPP);i++) {
unsigned char c = (unsigned char)(255 * i / (1<<realBPP));
imgPtr[26 + (i*3)] = c;
imgPtr[27 + (i*3)] = c;
imgPtr[28 + (i*3)] = c;
if (bpp==16) { // Chrome doesn't like 16 bit BMPs in the other format
// BITMAPINFOHEADER
const int h = 14; // initial header len
imgPtr[h+0]=56; // sizeof(BITMAPV3INFOHEADER)
imgPtr[h+8]=(unsigned char)height;
imgPtr[h+9]=(unsigned char)(height>>8);
imgPtr[h+12]=1; // planes
imgPtr[h+14]=16; // bits
imgPtr[h+16]=3; // compression BI_BITFIELDS
uint32_t size = height*rowstride;
imgPtr[h+20]=(unsigned char)(size);
imgPtr[h+21]=(unsigned char)(size>>8);
imgPtr[h+22]=(unsigned char)(size>>16);
imgPtr[h+23]=(unsigned char)(size>>24);
//imgPtr[h+40]=0x00;//R
imgPtr[h+41]=0xF8;
imgPtr[h+44]=0xE0;//G
imgPtr[h+45]=0x07;
imgPtr[h+48]=0x1F;//B
//imgPtr[h+49]=0x00;
} else {
// BITMAPCOREHEADER
imgPtr[14]=12; // sizeof(BITMAPCOREHEADER)
imgPtr[20]=(unsigned char)height;
imgPtr[21]=(unsigned char)(height>>8);
imgPtr[22]=1; // color planes, should be 1
imgPtr[24]=(unsigned char)bpp; // bpp
if (hasPalette) {
// palette starts at 26
if (bpp==1) {
// first is white(?)
imgPtr[26]=255;
imgPtr[27]=255;
imgPtr[28]=255;
} else {
if (realBPP==3) {
for (int i=0;i<paletteEntries;i++) {
imgPtr[26 + (i*3)] = (i&1) ? 255 : 0;
imgPtr[27 + (i*3)] = (i&2) ? 255 : 0;
imgPtr[28 + (i*3)] = (i&4) ? 255 : 0;
}
#if defined(GRAPHICS_PALETTED_IMAGES)
} else if (realBPP==4) {
for (int i=0;i<16;i++) {
int p = PALETTE_4BIT[i];
imgPtr[26 + (i*3)] = (unsigned char)((p<<3)&0xF8);
imgPtr[27 + (i*3)] = (unsigned char)((p>>3)&0xFC);
imgPtr[28 + (i*3)] = (unsigned char)((p>>8)&0xF8);
}
} else if (realBPP==8) {
for (int i=0;i<255;i++) {
int p = PALETTE_8BIT[i];
imgPtr[26 + (i*3)] = (unsigned char)((p<<3)&0xF8);
imgPtr[27 + (i*3)] = (unsigned char)((p>>3)&0xFC);
imgPtr[28 + (i*3)] = (unsigned char)((p>>8)&0xF8);
}
#endif
} else { // otherwise default to greyscale
for (int i=0;i<(1<<realBPP);i++) {
unsigned char c = (unsigned char)(255 * i / (1<<realBPP));
imgPtr[26 + (i*3)] = c;
imgPtr[27 + (i*3)] = c;
imgPtr[28 + (i*3)] = c;
}
}
}
}
Expand Down Expand Up @@ -4228,8 +4260,6 @@ JsVar *jswrap_graphics_asBMP_X(JsVar *parent, bool printBase64) {
} else { // <= 1 pixel per byte
for (int x=0;x<width;x++) {
unsigned int c = graphicsGetPixel(&gfx, x, y);
if (bpp==16) // 16 bit BMP is RGB555, not RGB565
c = (c&31) | ((c>>1)&~31U);
for (int j=0;j<bpp;j+=8) {
imgPtr[idx++] = (unsigned char)(c);
bytesWritten++;
Expand All @@ -4242,6 +4272,7 @@ JsVar *jswrap_graphics_asBMP_X(JsVar *parent, bool printBase64) {
idx += rowstride-bytesWritten;
// if printing to console, we're going to print everything as long as we have a multiple of 3 (or we're at the end)
if (printBase64 && idx>2) {
jshKickWatchDog(); // uploading can take a while
bool isLastRow = y==0;
int count = isLastRow ? idx : (idx-(idx%3));
JsVar *view = jsvNewArrayBufferFromString(imgData, (unsigned int)count); // create an arraybuffer - this means we can pass to btoa with zero allocations
Expand Down
3 changes: 2 additions & 1 deletion libs/graphics/lcd_fsmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1772,7 +1772,8 @@ void lcdFillRect_FSMC(JsGraphics *gfx, int x1, int y1, int x2, int y2, unsigned

unsigned int lcdGetPixel_FSMC(JsGraphics *gfx, int x, int y) {
lcdSetCursor(gfx,x,y);
lcdSetWrite(); // ?
LCD_WR_REG(0x2E); // start read
LCD_RD_Data(); // dummy read
return LCD_RD_Data();
}

Expand Down
1 change: 1 addition & 0 deletions libs/hexbadge/jswrap_hexbadge.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ int jswrap_badge_capSense(int corner) {
"type" : "staticmethod",
"class" : "Badge",
"name" : "getBatteryPercentage",
"deprecated" : true,
"generate" : "jswrap_badge_getBatteryPercentage",
"return" : ["int", "A percentage between 0 and 100" ]
}
Expand Down
Loading

0 comments on commit dc31001

Please sign in to comment.